max485的RO接PD2,DI接PD3,RE和DE接PD1,代码如下:
#define CRYSTAL2 14745600
#define BAUD3 9600 //485波特率采用9600bps
#define BAUD_SETTING3 (unsigned char)((unsigned long)CRYSTAL2/(16*(unsigned long)BAUD3)-1)
#define BAUD_H3 (unsigned char)(BAUD_SETTING3>>8)
#define BAUD_L3 (unsigned char)BAUD_SETTING3
unsigned char inbox[80]; //收件
unsigned char outbox[80]; //发件
void uart1_init(void)
{
UCSR1B=0x00;
UCSR1A=0x00;
UCSR1C=0x06; //无校验,8位数据位,1位停止位
UBRR1L= BAUD_L3;
UBRR1H= BAUD_H3;
UCSR1B=0x98; //发送接收使能,接收中断使能
}
void usart1_out(unsigned char n)
{
unsigned char i=0;
DDRD&=0XFB; //PD2 置为输入,PD3置为输出
DDRD|=0X08;
DDRD|=(1<<1); //PD1输出
PORTD|=(1<<1); //PD1输出高电平
while(i<n )//一共发送amount个数据
{
while(!(UCSR1A&0x20));
UDR1=outbox[i++];
delay_nus(1000);
}
PORTD&=0xfd;//PD1输出低电平
}
#pragma interrupt_handler uart1_rx_isr: 31
void uart1_rx_isr(void) //接收中断
{
while(!(UCSR1A & 0x80));
inbox[cnt++]=UDR1;
}
void main(void){
uart1_init();
outbox[0]=0xf0;}
//atmega单片机接了个485的角度传感器,命令时f0f0,地址是a1b2,这段代码运行后收不到传感器的数据,因为没有测试调试工具,不知道错在哪里,请高手们指点下。