平台 atmega32 avr studio +winavr
代码:
//包含所需头文件
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
#define BIT(x) (1<<(x))
#define NOP() asm("nop")
#define WDR() asm("wdr")
char flag=0;
char data;
//端口初始化
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//串口通信初始化
void usart_init(void)
{
UCSRB = 0x00;//禁止中断
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x33;
UBRRH = 0x00;
UCSRB = 0x98;
}
//串行接收结束中断服务程序
//#pragma interrupt_handler usart_rx_isr:14
//void usart_rx_isr(void)
SIGNAL(_VECTOR(13))
{
data = UDR;
flag = 1;
}
void uart_put_c(char data_c)
{
while ( !( UCSRA & (1<<UDRE)) );
UDR = data_c;
}
void uart_put_s(char *data_s)
{
while (*data_s)
{
uart_put_c(*data_s++);
}
uart_put_c(0x0D);//回车
uart_put_c(0x0A);//换行
}
void init_devices(void)
{
cli(); //禁止所有中断
MCUCR = 0x00;
//MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
port_init();
usart_init();
sei();//开全局中断
}
//主函数
int main(void)
{
init_devices();
//在这继续添加你的代码]
uart_put_s("Start uart!");
uart_put_s("1,2,3,4!");
PORTA = 0x55;
while(1)
{
if(flag)
{
PORTA = 0xaa;
uart_put_c('a');
uart_put_c(0x0D);//回车
uart_put_c(0x0A);//换行
flag = 0;
}
NOP();
}
return 0;
}
|