完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
各位大神帮忙看看用的多大的晶振,。不胜感激!
#include //1,到计时管脚定义 //地址IO口定义 #define adr5 P0_0 #define adr4 P0_1 #define adr3 P0_2 #define adr2 P0_3 #define adr1 P0_4 unsigned char adr_self = 1; void fun_adr(void); //红灯检测 #define red_check P3_5 bit red_flag = 0; void red_fun(void); void red_show(void); //绿灯检测 #define gree_check P3_4 bit gree_flag = 0; void gree_fun(void); void gree_show(void); //黄灯检测 #define yellow_check P3_3 bit yellow_flag = 0; void yellow_fun(void); void yellow_show(void); unsigned char copy_time_r = 0; unsigned char copy_time_g = 0; unsigned char copy_time_y = 0; //485收发控制 #define CON_485 P0_7 //喂看门狗 #define watch_dog P3_7 //显示个位 #define one_a P1_3 // P1_3 #define one_b P1_1 //P1_1 #define one_c P1_5 // P1_5 #define one_d P1_4//P1_4 #define one_e P1_6 //P1_6 #define one_f P1_7 // P1_7 #define one_g P1_2 //P1_2 //显示十位 J3 #define two_a P2_6 #define two_b P2_5 #define two_c P2_4 #define two_d P2_3 #define two_e P2_2 #define two_f P2_1 #define two_g P2_0 //绿灯共阳极电源 #define gree_power P2_7 #define red_power P2_6 #define yelow_power P2_5 //波特率9600 #define Relaod_count 0xfa //239 int time1_counter = 0; //共阳七段数码管0123456789 const unsigned char led_data[10] = {0x00,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//{0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; unsigned char address = 1; //显示标志 unsigned char show_flag = 0; unsigned char show_copy = 0; unsigned char show_color = 0; unsigned char adr_copy = 1; #define true 1 #define faulse 0 #define begin_time 1 #define cycle 2 unsigned char cycle_counter = 0; bit rx_ok = 0; unsigned char rx_counter = 0; unsigned char rx_buffer[50] = {0x5a,0x5a,0,0,0,0,0,0}; void system_initial(void); void send_uart(unsigned char order); void uart_interrupt_receive(void); void receive_process(void); //3,子程序 void disp_one(unsigned char order) { one_a = (led_data[order] & 0x01); one_b = ((led_data[order] & 0x02)>>1); one_c = ((led_data[order] & 0x04)>>2); one_d = ((led_data[order] & 0x08)>>3); one_e = ((led_data[order] & 0x10)>>4); one_f = ((led_data[order] & 0x20)>>5); one_g = ((led_data[order] & 0x40)>>6); } void disp_two(unsigned char order) { two_a = ((led_data[order] & 0x01)); two_b = ((led_data[order] & 0x02)>>1); two_c = ((led_data[order] & 0x04)>>2); two_d = ((led_data[order] & 0x08)>>3); two_e = ((led_data[order] & 0x10)>>4); two_f = ((led_data[order] & 0x20)>>5); two_g = ((led_data[order] & 0x40)>>6); } //串口工作模式3,波特率9600,奇校验(enev)数据位8,停止为1 //scon SMO SM1 SM2 REN TB8 RB8 TI RI //TMOD GATE C/T M1 M0 GATE C/T M1 M0 //PCON SMOD - - - GFI GFO FD IDL //fb = // void system_initial(void) { SCON = 0Xd0;//1101 0000位可变拨特拨特率,方式3 11位异步收发 TMOD = 0X21;//设置定时器1位8位重装,定时器0为16位定时器 PCON &= 0x7f;// TH0 = 0; TL0 = 0; ET0 = 1; TR0 = 1; TH1 = Relaod_count; TL1 = Relaod_count; TR1 = 1;//开定时器1 ES = 0;//BU允许串口中断 EA = 1;//开总中断 CON_485 = 0; TXD = 1; RXD = 1; //关闭LED电源 red_power = 1; gree_power = 1; P1 =0X00; P2 = 0Xff; //disp_one(0x00); //disp_two(0x00); } unsigned char show_time = 0; unsigned char red_time = 0; unsigned char red_counter = 38; unsigned char gree_time = 0; unsigned char gree_counter = 30; unsigned char yellow_time = 0; unsigned char yellow_counter = 3; unsigned char gewei = 0; unsigned char shiwei = 0; unsigned char show_counter = 0; unsigned char delay_begin = 0; void time0(void) interrupt 1 { if(++time1_counter > 20)//t = 8/12*0xffff/1000=98.3ms { time1_counter = 0; watch_dog = ~watch_dog; TXD = ~TXD; } show_time++; if(show_time > 200) { delay_begin++; show_time = 0; if(red_flag == 1) { //red_time 红色信号灯亮灯时间,每1秒加1 //red_counter 红色信号灯亮灯时间备份。 red_time++; if(red_counter > begin_time) red_counter--; } if(gree_flag == 1) { gree_time++; if(gree_counter > begin_time) gree_counter--; } if(yellow_flag == 1) { yellow_time++; if(yellow_counter > begin_time) yellow_counter--; } } //1秒没有收到显示数据,关闭显示 if(show_flag == 1) { show_counter++; if(show_counter > 30) { show_flag = 0; show_counter = 0; //red_power = 1; //gree_power = 1; } } TH0 = 0Xdb;//0x24;//24原来0XDB 0XD4. STC 软件算//定时器时钟12T模式 0x00 0xdc //定时器时钟12T模式 TL0 = 0XDc;//0x2b; ET0 = 1; TR0 = 1; } void send_uart(unsigned char order) { ES = 0; TI = 0; P = 0; ACC = order; TB8 = P; SBUF = order; while(TI == 0) ; TI = 0; ES = 1; TXD = 1; RXD = 1; } void uart_interrupt_receive(void) interrupt 4 { unsigned char k = 0; unsigned char jiaoyan = 0; unsigned char tem = 0; if(RI == 1) { RI = 0; ACC = SBUF; if(P == RB8) //奇校验 { rx_buffer[rx_counter] = SBUF; ++rx_counter; } else { rx_counter=0; rx_ok = 0; } if(rx_buffer[0] != 0x55) { rx_counter=0; rx_ok = 0; } if((rx_buffer[1] != 0xff) && (rx_buffer[0] != 0x55)) { rx_counter=0; rx_ok = 0; } //if((rx_buffer[2] != 0x1) && (rx_buffer[0] != 0x55)) // { // rx_counter=0; // rx_ok = 0; // } if(rx_counter == (5+3*rx_buffer[3])) { rx_ok = 1; rx_counter=0; } } } void delay_ms(unsigned int x) { unsigned int tem = 0; unsigned char m = 0; unsigned char n = 0; for(tem = 0;tem < x;tem++) { for(m = 0;m < 10;m++) { for(n = 0;n < 100;n++) ; } } } void show_one_09(void) { unsigned char tem =0; for (tem = 0;tem <10;tem++) { disp_one(tem); delay_ms(1000); } } void show_two_09(void) { unsigned char tem = 0; for (tem = 0;tem < 10;tem++) { disp_two(tem); delay_ms(1000); } } void receive_process(void) { unsigned char tem = 0; unsigned char copy_rx_buffer[]={0,0}; unsigned char tem_show = 0; unsigned char tem_color = 0; rx_ok = 0; CON_485 = 1; for (tem = 0;tem < (5+3*rx_buffer[3]);tem++) { send_uart(rx_buffer[tem]); copy_rx_buffer[tem] = rx_buffer[tem]; } delay_ms(2); CON_485 = 0; for(tem = 0;tem { tem_color = rx_buffer[5+3*tem];//显示颜色 switch(tem_color) { case 1: gree_power = 0; red_power = 1; break; case 2: red_power = 1; break; case 3: gree_power = 0; red_power = 0; break; default: gree_power = 1; red_power = 1; } tem_show = rx_buffer[6+3*tem]; //防止1秒钟内收到同一数据,闪烁(只同一颜色和时间) if((show_copy != tem_show) || (show_color != tem_color)) {disp_two(tem_show);} show_copy = tem_show; show_color = tem_color; //red_power = 0; show_flag = 1; show_counter = 0; } } void fun_adr(void) { unsigned char tem = 0; unsigned char iii = 0; unsigned char nnn = 0; for(iii = 0;iii < 20;iii++) { P2|= 0X1F; tem = P2; tem &= 0x1f; if(iii == 0) { adr_self = tem; } else { if(tem == adr_self) nnn = nnn+1; } } if(nnn > 10) adr_self = adr_self+1; else adr_self = 1; } //////////////////////////////// /////////////////////////////// void red_fun(void) { unsigned char tem = 0; unsigned char v0 = 0; red_check = 1; for(tem = 0;tem < 100;tem++) { if(red_check == 0) { v0++; } red_check = 1; //delay_ms(10); } if(v0 > 95)//亮红灯 { red_flag = 1; gree_power = faulse; //黄灯结束,红灯开始 yellow_flag = 0; if(yellow_time > begin_time) { yellow_counter = yellow_time; show_time = 200; TH0 = 0XFB; TL0 = 0X00; ET0 = 1; TR0 = 1; } yellow_time = begin_time; copy_time_y = begin_time; /////////////////////////////// } else {/* red_flag = 0; if(red_time > 0) red_counter = red_time;//红灯亮开始计时,红灯灭停止计时,计时完毕备份。 red_time = 0; copy_time_r = 0; */ } } void gree_fun(void) { unsigned char tem = 0; unsigned char v0 = 0; gree_check = 1; for(tem = 0;tem < 100;tem++) { if(gree_check == 0) { v0++; } gree_check = 1; //delay_ms(10); } if(v0 > 95) { gree_flag = 1; //红灯结束,绿灯开始 //关红灯 red_flag = 0; if(red_time > begin_time) { red_counter = red_time;//红灯亮开始计时,红灯灭停止计时,计时完毕备份。 show_time = 200; show_time = 200; TH0 = 0XFB; TL0 = 0X00; ET0 = 1; TR0 = 1; } red_time = begin_time;//-1,20150621改,倒计时从2直接到0 (原来in time -1 copy_time_r = begin_time-1; } else { /* gree_flag = 0; if(gree_time > 0)//绿灯计时最后3秒关断两次,闪烁提示 gree_counter = gree_time; gree_time = 0; copy_time_g = 0; */ } } void yellow_fun(void) { unsigned char tem = 0; unsigned char v0 = 0; yellow_check = 1; for(tem = 0;tem < 100;tem++) { if(yellow_check == 0) { v0++; } yellow_check = 1; //delay_ms(10); } if(v0 > 95) { yellow_flag = 1; gree_power = true; {//绿灯结束,黄灯开始 gree_flag = 0; if(gree_time > begin_time)//绿灯计时最后3秒关断两次,闪烁提示 {//定时器对齐 gree_counter = gree_time; show_time = 200; show_time = 200; TH0 = 0XFB; TL0 = 0X00; ET0 = 1; TR0 = 1; } gree_time = begin_time;////-1,20150621改,倒计时从2直接到0 copy_time_g = begin_time-1; /////////////////////////// } } else { /* yellow_flag = 0; if(yellow_time > 0) yellow_counter = yellow_time; yellow_time = 0; */ } } void red_show(void) { unsigned char tem = 0; if(red_flag == 1) { if(red_counter > 99) { gewei = 9; shiwei = 9; } else { gewei = (red_counter % 10); shiwei = (red_counter / 10); } if(cycle_counter == 18) cycle_counter = 19; if(cycle_counter == 8) cycle_counter = 9; cycle_counter = 1; if(cycle_counter == 3) cycle_counter = 4; if(cycle_counter == 5 ||cycle_counter == 10 ||cycle_counter == 21) { if(red_counter<=8) {if(red_counter==1) {disp_one(gewei); delay_ms(2000); red_counter=0; P1=0x00; } disp_one(gewei); } else{P1=0x00;} //disp_two(shiwei); } } } void gree_show(void) { unsigned char tem = 0; if(gree_flag == 1) { if(gree_counter > 99) { gewei = 9; shiwei = 9; } else { gewei = (gree_counter % 10); shiwei = (gree_counter / 10); } if(cycle_counter == 19) cycle_counter = 20; if(cycle_counter == 0) cycle_counter = 7; if(cycle_counter == 9) cycle_counter = 10; if(cycle_counter == 1) cycle_counter = 2; if(cycle_counter == 4) cycle_counter = 5; if(cycle_counter == 5 ||cycle_counter == 10 ||cycle_counter == 21) { if(gree_counter<=8) { if(gree_counter==1) { disp_one(gewei); delay_ms(2000); gree_counter=0; P1=0x00; } disp_one(gewei); } else{P1=0x00;} 、 } void yellow_show(void) { unsigned char tem = 0; if(yellow_flag == 1) { if(yellow_counter > 99) { gewei = 9; shiwei = 9; } else 、 /////////////////////////////////// if(yellow_counter > 3) yellow_counter = 4; /////////////////////////////////// /////////////////////////////////// gewei = (yellow_counter % 10); shiwei = (yellow_counter / 10); } if(cycle_counter == 0) 、 if(cycle_counter == 7) cycle_counter = 8; if(cycle_counter == 2) cycle_counter = 3; if(cycle_counter == 5 ||cycle_counter == 10 ||cycle_counter == 21) { disp_one(gewei); disp_two(shiwei); } } } |
|
相关推荐
4个回答
|
|
|
|
|
|
这个程序关键是定时器的时间要准,波特率那不知道原程序弄的对不对。定时器怎么反推? |
|
|
|
|
|
|
|
确定波特率之后,通过晶振频率和定时器的设置计算出定时器的初始值,所以反算就是通过波特率,定时器初始值和定时器的设置来判断晶振。查看如何计算根据波特率来计算定时器初始值部分的内容。
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
820 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第十一章 KEY实验
388 浏览 0 评论
783 浏览 0 评论
801 浏览 2 评论
飞凌嵌入式-ELFBOARD-ELF 2硬件知识分享-最小系统原理
466 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11932 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-12 19:39 , Processed in 0.626750 second(s), Total 80, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号