📄 atmega8atmega16communication.c
字号:
#include <iom16v.h>
#include <macros.h>
#include <eeprom.h>
/* PIN define */
#define MAX485_TXD() (PORTD |= (1<<2))
#define MAX485_RXD() (PORTD &= ~(1<<2))
void package_send_data(unsigned char data_1,unsigned char data_2);/*,unsigned char data_3,unsigned char data_4);*/
void DELAY_S(unsigned char time);
void DELAY_S(unsigned char time)//X*1MS
{
unsigned char x1,x2;
for(x2=0;x2<time;x2++)
{WDR();
for(x1=0;x1<255;x1++)
{
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
}
}
void package_send_data(unsigned char data_1,unsigned char data_2)/*unsigned
char data_3,unsigned char data_4)*/
{
UCSRB&=~(1<<RXCIE);//禁止接受中断
MAX485_TXD();
DELAY_S(1);
while (!(UCSRA&(1<<UDRE)));//top--------1
UDR=0xaa;
while (!(UCSRA&(1<<UDRE)));//top--------2
UDR=0xaa;
while (!(UCSRA&(1<<UDRE)));//address-----3
UDR=data_1;
while (!(UCSRA&(1<<UDRE)));//address_back-----4
UDR=data_2;
while (!(UCSRA&(1<<UDRE)));//answer=0xff-----5
UDR=0xff;
/*while (!(UCSRA&(1<<UDRE)));//data_1-----6
UDR=data_3;
while (!(UCSRA&(1<<UDRE)));//data_2-----7
UDR=data_4;
while (!(UCSRA&(1<<UDRE)));//data_2-----8*/
/* UDR=(data_1+data_2+0xff); /*+data_3+data_4);*/
DELAY_S(20);//19200DELY 7
MAX485_RXD();
UCSRB|=(1<<RXCIE);//开接受中断
}
void main(void)
{
CLI();
DDRB = 0B11100011;
PORTB = 0B00111100;
DDRD = 0B11110100;
PORTD = 0B10011111;
//串口初始化
UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE);//允许发送和接收
UBRRL = 47;
UBRRH = 0;
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP位
while(1)
{
if(!(PINB&0B00000100))
{
DELAY_S(200);
if(!(PINB&0B00000100))
{
unsigned char address = 0x01;
package_send_data(address,0x34); /*,CAMREA_NO,POSITION);}*/
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -