main.c #include "uart.h" #include "lcd1602.h" #include "stc.h" //extern unsigned char receive; unsigned char hello[]={"ello,boy\n"}; void main() { Lcdstart(); UartInit(); Uart_send_char('h'); Uart_send_str(hello); while(1) { if(uartflag) { ES=0; LCDclear(); LCD_write_num(2,2,receive); ES=1; } uartflag=0; } } uart.c #include "uart.h" #include "stc.h" //#include "lcd1602.h" unsigned long int receive; bit uartflag=0; unsigned char recdata[6]; unsigned char cnt=0; void Uartnit(void); void Uart_send_char(unsigned char ch); void Uart_send_str(unsigned char *str); void interrupt_uart() interrupt 4 //中断接收 receive { if(TI) { TI = 0; REN = 1; } if(RI) { RI = 0; recdata[cnt]=SBUF; cnt++; if(recdata[0] == 0xaa)//0xaa为起始字节 { if(cnt>=6) { //这里处理你的数据,以更新显示。 receive=recdata[1]+recdata[2]*10+recdata[3]*100+recdata[4]*1000+recdata[5]*10000; uartflag=1; cnt=0; } }else { cnt=0;//没有接收到字头,设置下一个可能为字头, uartflag=0; } //receive= SBUF; //uartflag=1; } } /***********************************************************************/ /***************************通用函数***********************************/ void Uartinit(void) //9600bps@11.0592MHz { PCON &= 0x7F; //波特率不倍速 SCON = 0x50; //8位数据,可变波特率 AUXR &= 0xFB; //独立波特率发生器时钟为Fosc/12,即12T BRT = 0xFD; //设定独立波特率发生器重装值 AUXR |= 0x01; //串口1选择独立波特率发生器为波特率发生器 AUXR |= 0x10; //启动独立波特率发生器 EA=1; ES=1; } void Uart_send_char(unsigned char ch) { ES = 0; TI = 0; SBUF = ch; while(!TI); TI = 0; ES = 1; } void Uart_send_str(unsigned char *str) { while(*str != 0) { Uart_send_char(*str); str++; } }
收藏0 举报
pangencao 发表于 2013-6-12 14:54 高级发码与普通方式分别怎么操作的?
dqyubsh 发表于 2013-6-11 21:44 直观感觉,首先要把receive=那行从中断里拿出来,移到主程序里处理。或者把这行去掉,看看运行情况。至少不 ...
16777216 发表于 2013-6-13 15:33 能提供下具体的思路么 或者一些伪代码
dqyubsh 发表于 2013-6-13 20:56 http://www.freemodbus.org/
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
人才类勋章
时间类勋章
发帖类勋章
等级类勋章
61
399
1
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号