头像-173627

c116学员0

  • 其他

个人成就

获得 0 次赞

帮助过0人

请问三路的抢答器如何加上串口以实现通信

#include <reg52.h>#define uint unsigned int #define uchar unsigned char static unsigned int state=0;static unsigned int action=0;  sbit  lcden=P2^7;sbit  lcdrs=P2^6;sbit  lcdrw=P2^5;/*-----------------------------------------------------------变量定义------------------------------------------------------------*/sbit key1 = P3^1;sbit key2 = P3^0;sbit key3 = P3^2;sbit key_start=P3^3;sbit key_reset = P3^5;bit key_start_flag = 0;bit key_flag = 0;uchar second = 20;uchar timer0_count = 0;uchar number = 0;uchar number_display = 0;void  delay(uchar z){ uchar x,y; for(x=z;x>0;x--)  for(y=110;y>0;y--);}void   write_com(uchar com) //根据1602写数据的时序图写程序{ lcdrs=0; //re低电平写命令  lcdrw=0; P0=com; delay(5); lcden=1; delay(5); lcden=0;}void   write_data(uchar   shuju){  lcdrs=1;//rs高电平写数据  lcdrw=0;  P0=shuju;  delay(5);  lcden=1;  delay(5);  lcden=0;}void  LCD_init() //根据1602的指令集写{ lcden=0;   write_com(0x80);   write_com (0x0e);   write_com(0x38);   write_com(0x01);   write_com(0x06);}/*-----------------------------------------------------------“开始抢答”键扫描函数------------------------------------------------------------*/void start_keyscan(){ if(key_start == 0) //如果“开始答题”按键被按下 { delay(8);  //按键防抖动,延时8ms后,如果检测到按键依然是按下的状态,说明按键是真的被按下了 if((key_start == 0)&&(!key_start_flag))//如果按下“开始答题按键”,并且当前不是正在答题的状态 { key_start_flag = 1; //“key_start_flag” 变量置1,表示已经按下了开始答题按键,进入了开始答题的状态 action  = 1; TR0 = 1; //定时器0开始运行,开始倒计时 state = 0; } }}/*-----------------------------------------------------------“复位键”扫描函数------------------------------------------------------------*/void reset_keyscan(){ if(key_reset == 0)//如果“复位”按键被按下 { delay(8); //按键防抖动,延时8ms后,如果检测到按键依然是按下的状态,说明按键是真的被按下了 if(key_reset == 0)//如果按下“复位按键” { TR0=0; second=20; //设置要显示的秒数为12 key_start_flag = 0; //“key_start_flag”标志位置0,表示跳出答题的状态 number_display = 0;//设置显示的抢答者的号码为0    state = 1; } } }/*-----------------------------------------------------------4位抢答键扫描函数------------------------------------------------------------*/uchar key_scan3(){if(key_flag==0){     if(key1 == 0)  { delay(8); //按键防抖动 if(key1 == 0) { key_flag=1; number  = 1;  //要显示的抢答者的号码为1 number_display = number; TR0=~TR0; while(!key1); } } if(key2 == 0) { delay(8); if(key2 == 0) {   key_flag=1;  number  = 2; number_display = number; TR0=~TR0; while(!key2); } } if(key3 == 0) { delay(8); if(key3 == 0) {   key_flag=1;  number  = 3; number_display = number; TR0=~TR0; while(!key3); } } }  }void display(uchar add,uchar x){ uchar ge,shi; ge=x%10; shi=x%100/10; write_com(0x80+add); write_data(0x30+shi); write_data(0x30+ge); } void main(){ TMOD=0x01; TH0=0x3c; TL0=0xb0; EA=1; ET0=1; TR0=0; LCD_init(); while(1) { start_keyscan(); reset_keyscan(); while(key_start_flag) { reset_keyscan(); while(!key_scan3()) { display(0x06,second); display(0x09,number_display); if(second == 0) break;//跳出当前循环 } display(0x06,second); display(0x09,number_display); } display(0x06,second); display(0x09,number_display); }}/*-----------------------------------------------------------中断服务函数------------------------------------------------------------*/void timer0() interrupt 1{ TH0=0x3c; TL0=0xb0; timer0_count ++; if(timer0_count == 20) { timer0_count = 0; second --; if(second == 0) { TR0 = 0; number_display = 0; } } }

请问三路的抢答器如何加上串口以实现通信

#include <reg52.h>#define uint unsigned int #define uchar unsigned char static unsigned int state=0;static unsigned int action=0;  sbit  lcden=P2^7;sbit  lcdrs=P2^6;sbit  lcdrw=P2^5;/*-----------------------------------------------------------变量定义------------------------------------------------------------*/sbit key1 = P3^1;sbit key2 = P3^0;sbit key3 = P3^2;sbit key_start=P3^3;sbit key_reset = P3^5;bit key_start_flag = 0;bit key_flag = 0;uchar second = 20;uchar timer0_count = 0;uchar number = 0;uchar number_display = 0;void  delay(uchar z){ uchar x,y; for(x=z;x>0;x--)  for(y=110;y>0;y--);}void   write_com(uchar com) //根据1602写数据的时序图写程序{ lcdrs=0; //re低电平写命令  lcdrw=0; P0=com; delay(5); lcden=1; delay(5); lcden=0;}void   write_data(uchar   shuju){  lcdrs=1;//rs高电平写数据  lcdrw=0;  P0=shuju;  delay(5);  lcden=1;  delay(5);  lcden=0;}void  LCD_init() //根据1602的指令集写{ lcden=0;   write_com(0x80);   write_com (0x0e);   write_com(0x38);   write_com(0x01);   write_com(0x06);}/*-----------------------------------------------------------“开始抢答”键扫描函数------------------------------------------------------------*/void start_keyscan(){ if(key_start == 0) //如果“开始答题”按键被按下 { delay(8);  //按键防抖动,延时8ms后,如果检测到按键依然是按下的状态,说明按键是真的被按下了 if((key_start == 0)&&(!key_start_flag))//如果按下“开始答题按键”,并且当前不是正在答题的状态 { key_start_flag = 1; //“key_start_flag” 变量置1,表示已经按下了开始答题按键,进入了开始答题的状态 action  = 1; TR0 = 1; //定时器0开始运行,开始倒计时 state = 0; } }}/*-----------------------------------------------------------“复位键”扫描函数------------------------------------------------------------*/void reset_keyscan(){ if(key_reset == 0)//如果“复位”按键被按下 { delay(8); //按键防抖动,延时8ms后,如果检测到按键依然是按下的状态,说明按键是真的被按下了 if(key_reset == 0)//如果按下“复位按键” { TR0=0; second=20; //设置要显示的秒数为12 key_start_flag = 0; //“key_start_flag”标志位置0,表示跳出答题的状态 number_display = 0;//设置显示的抢答者的号码为0    state = 1; } } }/*-----------------------------------------------------------4位抢答键扫描函数------------------------------------------------------------*/uchar key_scan3(){if(key_flag==0){     if(key1 == 0)  { delay(8); //按键防抖动 if(key1 == 0) { key_flag=1; number  = 1;  //要显示的抢答者的号码为1 number_display = number; TR0=~TR0; while(!key1); } } if(key2 == 0) { delay(8); if(key2 == 0) {   key_flag=1;  number  = 2; number_display = number; TR0=~TR0; while(!key2); } } if(key3 == 0) { delay(8); if(key3 == 0) {   key_flag=1;  number  = 3; number_display = number; TR0=~TR0; while(!key3); } } }  }void display(uchar add,uchar x){ uchar ge,shi; ge=x%10; shi=x%100/10; write_com(0x80+add); write_data(0x30+shi); write_data(0x30+ge); } void main(){ TMOD=0x01; TH0=0x3c; TL0=0xb0; EA=1; ET0=1; TR0=0; LCD_init(); while(1) { start_keyscan(); reset_keyscan(); while(key_start_flag) { reset_keyscan(); while(!key_scan3()) { display(0x06,second); display(0x09,number_display); if(second == 0) break;//跳出当前循环 } display(0x06,second); display(0x09,number_display); } display(0x06,second); display(0x09,number_display); }}/*-----------------------------------------------------------中断服务函数------------------------------------------------------------*/void timer0() interrupt 1{ TH0=0x3c; TL0=0xb0; timer0_count ++; if(timer0_count == 20) { timer0_count = 0; second --; if(second == 0) { TR0 = 0; number_display = 0; } } }