1. 51-microcontroler

[临时施工]单片机2018-2019

1.

/* Main.c file generated by New Project wizard
 *
 * Created:   周四 7月 4 2019
 * Processor: AT89C51
 * Compiler:  Keil for 8051
 */

#include <reg51.h>
#include <stdio.h>
int tab[4] = {0x7e,0xbd,0xdb,0xe7};//编码表
void sleep(int i){
    int j = 110;
    for(;i>0;i--)
        for(;j>0;j--);
}

void main(void)
{ 
    // Write your code here
    int i;
    while (1){
        i = 0;
        for(;i<=3;i++){//从两端往里走
            sleep(10000);
            P0 = tab[i];
            
        }
        for(;i>0;i--){//从中间往外走
            sleep(10000);
            P0 = tab[i];
            
        }
    }
}

2.

/* Main.c file generated by New Project wizard
 *
 * Created:   周四 7月 4 2019
 * Processor: AT89C51
 * Compiler:  Keil for 8051
 */

#include <reg51.h>
#include <stdio.h>
sbit key0 = P1^0;
sbit key1 = P1^1;
sbit key2 = P1^2;

sbit led0 = P0^0;
sbit led1 = P0^1;
sbit led2 = P0^2;

void sleep(int i){
    int j = 110;
    for(;i>0;i--)
        for(;j>0;j--);
}
void main(void)
{ 
    // Write your code here
    P0 = 0xff;
    P1 = 0xff;
    while (1){
        switch(P1){//按键判断
            case 0xfe:{//按键1
                led0 = 0;
                while(key0 == 0); 
                led0 = 1;
            };break;
            case 0xfd:{//按键2
                led1 = 0;
            };break;
            case 0xfb:{//按键3
                sleep(20);
                while(key2 == 0);
                led2 = 0;
            };break;
        }
        
    }
}

3.

//1号机
/* Main.c file generated by New Project wizard
 *
 * Created:   周四 7月 4 2019
 * Processor: AT89C51
 * Compiler:  Keil for 8051
 */

#include <reg51.h>
#include <stdio.h>
#define SEG P0
typedef unsigned char uint8_t ;
// 0~9 数字字模
const uint8_t n[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x2, 0x78, 0x0, 0x10};

void sleep(int i){
    int j = 110;
    for(;i>0;i--)
        for(;j>0;j--);
}
void rx() interrupt 4{//串口接收中断子程序
    P0 = SBUF;
}

void main(void)
{ 
    // Write your code here
    TMOD = 0x20;//定时器工作在模式二
    SCON = 0x50;//串口工作方式1,允许接受
    TH1 = 0xfd;//设置9600波特率
    TL1 = 0xfd;
    TR1 = 1;
    EA = 1;
    ES = 1;
    SEG = n[1];//显示自己的编号
    sleep(30000);
    //发送自己的编号
    SBUF = n[1];
    while(TI == 0);//等待发送完成
    while(1);
}
//2号机
/* Main.c file generated by New Project wizard
 *
 * Created:   周四 7月 4 2019
 * Processor: AT89C51
 * Compiler:  Keil for 8051
 */

#include <reg51.h>
#include <stdio.h>
#define SEG P0
typedef unsigned char uint8_t ;
// 0~9 数字字模
const uint8_t n[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x2, 0x78, 0x0, 0x10};

void sleep(int i){
    int j = 110;
    for(;i>0;i--)
        for(;j>0;j--);
}
void rx() interrupt 4{//串口接收中断子程序
    P0 = SBUF;
}

void main(void)
{ 
    // Write your code here
    TMOD = 0x20;//定时器工作在模式二
    SCON = 0x50;//串口工作方式1,允许接受
    TH1 = 0xfd;//设置9600波特率
    TL1 = 0xfd;
    TR1 = 1;
    EA = 1;
    ES = 1;
    SEG = n[2];//显示自己的编号
    sleep(30000);
    //发送自己的编号
    SBUF = n[2];
    while(TI == 0);//等待发送完成
    while(1);
}

4.

/* Main.c file generated by New Project wizard
 *
 * Created:   周四 7月 4 2019
 * Processor: AT89C51
 * Compiler:  Keil for 8051
 */

#include <reg51.h>
#include <stdio.h>
typedef unsigned char uint8_t;
sbit motor = P2^0;//DC motor
sbit relay = P2^1;//realy

// 0~9 数字字模
const uint8_t n[10] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x2, 0x78, 0x0, 0x10};

void sleep(int i){
    int j = 110;
    for(;i>0;i--)
        for(;j>0;j--);
}

void main(void)
 { 
    // Write your code here
    int i = 9;
    for(;i>=0;i--){
        P0 = n[i];
        sleep(30000);
    }
    P0 = 0xff;
    motor = 0;
    relay = 0;
    while (1);
 }