https://static.assets-stash.eet-china.com/album/old-resources/2009/11/12/5092fbea-d5ad-4311-83db-0276b79dd6d7.rar
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <math.h>
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/pgmspace.h >
#include <avr/eeprom.h>
#include <avr/wdt.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#define clrwdt() wdt_reset();PORTB ^= (1<<PB2)//喂狗
#define receEnable() PORTB |= (1<<PB1) //RS485接收使能,输出低电平
#define sendEnable() PORTB &= ~(1<<PB1) //RS485发送使能,输出高电平
#define uchar unsigned char
#define uint unsigned int
uchar s[]="freemark!/n";
//********************************************************************
//MCU初始化程序
//********************************************************************
void port_init(void)
{
DDRA = 0xFF; //
PORTA = 0x00; //
DDRB = 0xF6; //
PORTB = 0xF9; //
DDRC = 0xFF; //
PORTC = 0xFF; //
DDRD = 0x00; //
PORTD = 0xFF; //
DDRE = 0x7C; //
PORTE = 0x83; //
DDRF = 0x00; //
PORTF = 0x00; //
DDRG = 0x07; //
PORTG = 0x00; //
}
//********************************************************************
void usart_init(void)
{
UCSR0A=0X00;
UCSR0B=0x00; //先禁止接收发送使能
UBRR0H=0X00; //设置波特率9600 晶振11.0592MHz
UBRR0L=71;//0X71;
UCSR0C=0x06; //设置帧格式: 8 个数据位, 1 个停止位
UCSR0B=0x08; //允许发送接收
}
void usart_transmit(uchar x) //查询发送一个字节
{
UDR0=x;
while(!(UCSR0A&(1<<UDRE0)));
UCSR0A|=0X40;
}
uchar usart_receive(void) //查询接收一个字节
{
while(!(UCSR0A&0x80));
return UDR0;
}
void delay_1S(void)
{
clrwdt();
_delay_ms(200);
clrwdt();
_delay_ms(200);
clrwdt();
_delay_ms(200);
clrwdt();
_delay_ms(200);
clrwdt();
_delay_ms(200);
}
void delay_2S(void)
{
delay_1S();
delay_1S();
}
void main()
{
uchar i=0;
usart_init();
port_init();
while(1)
{
clrwdt();
_delay_ms(200);
sendEnable();
for(i=0;i<10;i++)
usart_transmit(s
);
receEnable();
delay_1S();
}
}
文章评论(0条评论)
登录后参与讨论