以下是我自己写的程序,但是进不去中断。
/******ATMEGA128
单片机串口中断程序******/
#include "avr/io.h"
#include "avr/interrupt.h"
void UART_Init(void);
void UART_Send_Byte(unsigned char mydata);
void UART_Send_Str(char *s);
unsigned char rec=0;
#define fosc 16000000 //晶振16MHZ
#define baud 9600 //波特率
void main(void)
{
UART_Init();
UART_Send_Str("串口设置完毕
");
while(1)
{
PORTA=0x10;
PORTB=0x10;
PORTC=0x10;
}
}
void UART_Init(void)
{
UCSR0B = 0x00;
UCSR0A = 0x00;
UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);
UBRR0L=(fosc/16/(baud+1))%256;
UBRR0H=(fosc/16/(baud+1))/256;
UCSR0B =(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);//RXCEN TXCEN
}
void UART_Send_Byte(unsigned char mydata)
{
while (!(UCSR0A&(1<<UDRE0)));
UDR0=mydata;
}
/*******发送字符串*******/
void UART_Send_Str(char *s)
{
while(*s)
{
UART_Send_Byte(*s);
s++;
}
}
/****************************
******串口0接收中断程序*******
*******************************/
ISR(USART0_RX_vect)
{
rec=UDR0;
UART_Send_Byte(rec);
}
一周热门 更多>