打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c语言程序和串口调试助手的奇偶校验

UCSR0C以前为0x06,执行UCSR0C = UCSR0C | (0x03<<4);后0x36为奇校验
以下程序中现象:
1、用串口调试助手上电第一次发送:53 53 02 55 aa,led2(对应mos_led_on(1)),和led3闪一次,再发送一次:53 53 02 52 52 能正确将数组RedRightdirection中的数发出,以后再发n次这2个灯始终不闪并发不出RedRightdirection中的数;
2、如一直发53 53 02 55 aa,led2(对应mos_led_on(1)),和led3每发一次数,闪一次;
3、每发送一次53 53 02 55 aa,串口调试助手80%情况下返回53 53 02 55 aa,还返回不确定的数字;
调试过程:
以为程序跑飞,ram不够用,收发出错。。。。把精力耗在了串口接收或发送的问题上,不断查验中断和收发程序,也没高通,后来准备给姜老师演示问题时在while前加了UCSR0C = UCSR0C | (0x03<<4)却发现第一次led2,led3也不能正常闪烁,而且黑体字部分无论何时都能正常收发,最后发现问题:串口调试助手中收发设为none校验!而不是程序中的奇校验!第一次led2,led3能闪是因为此时是无校验方式,经过LedDisplay后变为奇校验所有发不出数据!能折腾了3,4天!

程序:void LedDisplay(char *temp){

unsigned char str1[]={0x01,0x01};

comBuf temp_st;

unsigned int i=0;

//UCSR0C = UCSR0C & (~(0x01<<4)); //偶校验

//UCSR0C = UCSR0C | (0x01<<5); //偶校验

UCSR0C = UCSR0C | (0x03<<4); //设为奇校验或偶校验都可以,但不校验就不行!//UCSR0C:0x06->0x36

memcpy(&temp_st.data[0],&str1[0],2);

temp_st.size = 2;

Send_UART(&temp_st);

mos_thread_sleep(300); // sleep 300ms



for(i=0;i<InstructionLen;i++){

temp_st.data[i] = *(temp++);

temp_st.size = 1;

Send_UART(&temp_st);

}



}

while(1)

{

mos_led_on(0);

mos_thread_sleep(100);

mos_led_off(0);

mos_thread_sleep(900);

com_mode(IFACE_SERIAL, IF_LISTEN);

recv_packet = com_recv (IFACE_SERIAL);

//if(recv_packet->size) {Send_UART(recv_packet);} //for only this line,len can flash
if(recv_packet->data[0]==0x55 && recv_packet->data[1]==0xaa) {

Send_UART(recv_packet);
mos_led_on(1);

mos_led_on(2);

mos_thread_sleep(2000);

mos_led_off(2);

mos_led_off(1);

}
if(recv_packet->size) { //B:0x42 L:4C R:52 G:47 R:52 

Derection = recv_packet->data[0];

Color = recv_packet->data[1];

if(Derection==0x42 && Color==0x52) LedDisplay(RedBidirection);

if(Derection==0x4c && Color==0x52) LedDisplay(RedLeftdirection); //RBGL:52 42 47 4C 

if(Derection==0x52 && Color==0x52) LedDisplay(RedRightdirection);

if(Derection==0x42 && Color==0x47) LedDisplay(GreenBidirection);

if(Derection==0x4c && Color==0x47) LedDisplay(GreenBidirection);

if(Derection==0x52 && Color==0x47) LedDisplay(GreenBidirection);

}
com_free_buf(recv_packet); // free the recv'd packet to the pool

com_mode(IFACE_SERIAL, IF_LISTEN); // set it to listening mode}





}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
关于串口的时序
UART串行通信详解
Atmega128串口详解
深入浅出VC++串口编程之基于Win32 API
Linux串口设置参数
第四章 Teletext原理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服