完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
单片机通过串口接收数组,对数组判断后返回一串数组。现在出现接受数组需要发送好几次才能收到,返回数组也会出现数据丢失现象,需要发送多次才能出现返回数据,并且返回数据也有丢失。程序如下,求大神指点哪里有问题:
#include #include "jidianqi.h" #include "n79e81x.h" #include "typedef.h" #include "define.h" #include "version.h" #include "Common.h" #include "Delay.h" #include #define FOSC_221184 #define S_ADDR 0X01 bit recive_com=0; unsigned char *ptr; bit send_com=0; bit k_diver=1; unsigned char cntxd; unsigned char recive_dat[6]; unsigned char send_dat[6]={0xaa,0x01,0x40,0x0c,0x00,0xbb}; void InitialUART0_Timer1(UINT32 u32Baudrate) { SCON = 0x52; TMOD |= 0x21; TH0=0X0D; TL0=0X67; ES=1; EA=1; TR1=1; TR0=1; ET0=0; ET1=0 ; #ifdef FOSC_110592 TH1 = 256 - (28800/u32Baudrate); /* 11.059M/384=28800 */ #endif #ifdef FOSC_184320 TH1 = 256 - (48000/u32Baudrate); /* 18.4320M/384=48000 */ #endif #ifdef FOSC_221184 TH1 = 256 - (57600/u32Baudrate); /* 22.1184M/384=57600 */ #endif #ifdef FOSC_331776 TH1 = 256 - (86400/u32Baudrate); /* 33.1776M/384=86400 */ #endif #ifdef FOSC_368640 TH1 = 256 - (96000/u32Baudrate); /* 36.8640M/384=96000 */ #endif TR1 = 1; } void delay(int z) { int x,y; for(x=z;x>0;x--) for(y=125;y>0;y--); } void main() { InitialUART0_Timer1(9600); while(1) { if(recive_com==1) { recive_com=0; cntxd=sizeof(send_dat);ptr=send_dat;TI=1; } } } void UART1_int() interrupt 4 { static unsigned char count; if(RI) { RI=0; recive_dat[count]=SBUF; if(count==0&&recive_dat[count]==0xaa) { count=1; } else if(count==1&&recive_dat[count]==0x01 ) { count=2; } else if(count==2 &&recive_dat[count]==0x40) { count=3; } else if(count==3 ) { count=4; } else if(count==4) { count=5 ; } else if(count==5 ) { count=0; recive_com=1; } } if(TI) { TI=0; if(cntxd>0) { SBUF=*ptr; cntxd--; ptr++ ; send_com=1; } } } |
|
相关推荐
3个回答
|
|
|
|
|
|
|
|
|
解决了吗,这个不实用啊 |
|
|
|
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
470 浏览 0 评论
532 浏览 0 评论
660 浏览 0 评论
822 浏览 0 评论
RT-Thread与英飞凌(infineon)合作得板子PSOC 6 板子学习
758 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
17027 浏览 31 评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-13 05:28 , Processed in 0.706202 second(s), Total 76, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
1329