📄 m16-11.c~
字号:
//从机11
#include <mega16.h>
#include <usart.h>
#include <crc8.h>
#define amount 10 //设定通讯数据量
#define address 11 //请在这里设定本机地址
#define max485_out PORTD.2=1
#define max485_in PORTD.2=0
#define max485_RW_ok DDRD.2=1
unsigned char send[amount]; //发件箱
unsigned char inbox[amount]; //收件箱
unsigned char n=0; //记忆中断次数
//**************************************************************************************
void usart_out(unsigned char *datas,unsigned char n)
{
unsigned char i=0;
max485_out; //使MAX485处于发送状态
while(i<n) //一共发送n个数据
{
if(i==0) UCSRB|=1; else UCSRB&=254;
UDR=*(datas+i); //装载数据开始发送
while((UCSRA&64)==0); //等待发送结束
UCSRA|=64; //清除发送结束标志
i++; //发送次数统计
}
max485_in; //使MAX485处于接收状态
}
//**************************************************************************************
interrupt[12] Rxd_isr(void) //接收中断
{
if( UCSRA&28 ){ n=UDR; n=0; UCSRA|=0x01; } else //接收出错就重新打开地址帧筛选功能
{
if( UCSRB&2 ) n=0; //检测到地址信息时计数清零
inbox[n]=UDR; n++; //把接收到的数据保存到收件箱
if( inbox[0]==address ) UCSRA&=254; else UCSRA|=0x01; //地址筛选
if( n==amount ) //如果接收到完整的数据包
{
if( inbox[amount-1]==crc8(inbox,amount-1) ) //如果crc8校验正确就...
{
send[0]=1; //发件箱地址指向主机
//send[1]=? //请更新发件箱的数据
//send[n]=?
send[amount-1]=crc8(send,amount-1); //产生发件箱的crc8校验码
usart_out(send,amount); //发送发件箱的数据包/查询方式比较耗时
OCR1A=inbox[3]; //收件箱测试(控制T/C1的PWM驱动LED)
}
}
}
}
//**************************************************************************************
void main(void)
{
usart_init();
max485_in;
max485_RW_ok;
TCCR1A =0B10000001; //OCR1A/PD5/8位快速PWM
TCCR1B =0B00001001; //时钟1分频
DDRD |=0b00100000; //输出使能
OCR1A =255; //初始化PWM输出100%占空比
#asm("sei")
while (1)
{
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -