📄 uart2._c
字号:
#include<iom128v.h>
#include<macros.h>
//#define com0
#define fosc 4000000 //晶振4MHZ
#define baud 2400 //波特率
#define RXC_BUFF_SIZE 64
#define TXC_BUFF_SIZE 64
unsigned char RXC_BUFF[RXC_BUFF_SIZE];
unsigned char TXC_BUFF[TXC_BUFF_SIZE];
unsigned char RXC_RD;//接受缓冲区读指针
unsigned char RXC_WR;//接受缓冲区写指针
unsigned char TXC_RD;//发送缓冲区读指针
unsigned char TXC_WR;//发送缓冲区写指针
#ifdef com0
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C =(1<<UCSZ01)|(1<<UCSZ00);//8bit+1bit stop
UBRR0L=(fosc/16/(baud+1))%256;
UBRR0H=(fosc/16/(baud+1))/256;
UCSR0B =(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);//RXCEN TXCEN RXCIE
}
#else
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = (1<<UCSZ11)|(1<<UCSZ10);//8bit+1bit stop
UBRR1L=(fosc/16/(baud+1))%256;
UBRR1H=(fosc/16/(baud+1))/256;
UCSR1B =(1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1);//RXCEN TXCEN RXCIE
}
#endif
void putchar(unsigned char c)
{
TXC_BUFF[TXC_WR]=c;
if(TXC_WR<(TXC_BUFF_SIZE-1))
TXC_WR++;
else
TXC_WR=0;
#ifdef com0
UCSR0B|=(1<<UDRIE0);//开启UDRE中断
#else
UCSR1B|=(1<<UDRIE1);
#endif
}
unsigned char getchar(void)
{
unsigned temp;
while(RXC_RD==RXC_WR)
;
temp=RXC_BUFF[RXC_RD];
if(RXC_RD<(RXC_BUFF_SIZE-1))
RXC_RD++;
else
RXC_RD=0;
return temp;
}
void puts(char *s)
{
while (*s)
{
putchar(*s);
s++;
}
putchar(0x0a);
putchar(0x0d);
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
//all peripherals are now initialised
}
void main(void)
{
unsigned char i;
TXC_RD=0;
TXC_WR=0;
RXC_RD=0;
RXC_WR=0;
init_devices();
#ifdef com0
uart0_init();
#else
uart1_init();
#endif
SEI();
puts("HELLO!");
while(1)
{
if (getchar()=='t')//按键盘t键开始测试
{
puts("test ok!");
for (i=0;i<10;i++)
{
putchar(0x30+i);
}
putchar(0x0a);
putchar(0x0d);
}
}
}
//中断例程
#ifdef com0
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
RXC_BUFF[RXC_WR]=UDR0;
if(RXC_WR<(RXC_BUFF_SIZE-1))
RXC_WR++;
else
RXC_WR=0; //uart has received a character in UDR
}
#pragma interrupt_handler uart0_udre_isr:iv_USART0_UDRE
void uart0_udre_isr(void)
{
UDR0=TXC_BUFF[TXC_RD];//character transferred to shift register so UDR is now empty
if(TXC_RD<(TXC_BUFF_SIZE-1))
TXC_RD++;
else
TXC_RD=0;
if(TXC_RD==TXC_WR)
UCSR0B&=~(1<<UDRIE0);
}
#else
#pragma interrupt_handler uart1_rx_isr:iv_USART1_RXC
void uart1_rx_isr(void)
{
RXC_BUFF[RXC_WR]=UDR1;
if(RXC_WR<(RXC_BUFF_SIZE-1))
RXC_WR++;
else
RXC_WR=0; //uart has received a character in UDR
}
#pragma interrupt_handler uart1_udre_isr:iv_USART1_UDRE
void uart1_udre_isr(void)
{
UDR1=TXC_BUFF[TXC_RD];//character transferred to shift register so UDR is now empty
if(TXC_RD<(TXC_BUFF_SIZE-1))
TXC_RD++;
else
TXC_RD=0;
if(TXC_RD==TXC_WR)
UCSR1B&=~(1<<UDRIE1);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -