1、编译环境:CodeVisionAVR 2、功能:实现以 5AH 开头的 6 个字节数据帧的接收及发送 3、接收及发送方式:中断接收,查询发送 4、校验方式:所有字节相加模除 256 等于 0 则接收正确,否则不予接收 程序如下所示: #include ”mega16
h" #define uchar unsigned char #define uint unsigned int #define BAUD 9600 #define F_CLK 4000000 #define MATCH_OK 1; #define MATCH_ERROR 0; #define UDRE 5 volatile uchar i=0; volatile uchar recc_flag=0; //命令字节接收标志(recieve command 简写成 recc) volatile uchar comm_flag=0; //命令帧接收完毕标志(command 简写成 comm) volatile uchar comm; //定义变量,用于传递 UDR0 中接收到的命令字 volatile uchar command[6]={0x00,0x00,0x00,0x00,0x00,0x00}; //定义长度为 6 的数组用于接收长度为 6 字节的命令帧,并将所有元素初始化为 0x00 /*延时*/ void delay(uint t) { while(t——); } /*****/ /*******帧头校验******/ uchar check_comm(void) { uchar i; uint result=0; for(i=0;i〈6;i++) { result+=command[i]; } if((result%256)==0) { return MATCH_OK; } else { return