📄 usart.c
字号:
// Compiler : IAR EWAVR
// Target : M8
// Crystal: 8Mhz
//USART section
uchar uart_ping_data[4] = {0x00,0x00,0x00,0x00}; //the ping data buffer
__flash uchar uart_pong_data[4] = {0x00,0xFF,0x00,0xFF}; //the pong data buffer
uchar *P_uart_data;
// UART0 initialize
// MIDI communication, the desired Baud Rate is 31.25k
// desired baud rate: 38.4k
// actual: baud rate: 28.8k (2.1%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00; //U2X is 0, normal speed.
UCSRC = 0xA6; //UCSRC anc UBRRH shares the same I/O location ,select with BIT.8(URSEL),even parity
UBRRL = 0x0C; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98; //receive interrupt enable ,transmit interrupt not enable
}
#pragma vector = USART_RXC_vect //USART receive interrupt
__interrupt void USART_RXD_isr(void)
{
uchar temp_ucsra;
temp_ucsra = UCSRA; //read the flag of UCSRA
if((temp_ucsra & 0x04) == 0x00) //even parity pass
{
*P_uart_data = UDR; //read the receive data
P_uart_data++;
}
if(P_uart_data == uart_ping_data + 4)
P_uart_data = uart_ping_data;
}
#pragma vector = USART_TXC_vect //USART transmit interrupt
__interrupt void USART_TXD_isr(void)
{
//UDR = 0x66;
}
void usart_transmit(uchar *data)
{
uchar i;
for(i = 0; i < 33; i++)
{
UDR = *(data + i);
while(!( UCSRA & (1 << UDRE)))
;
UCSRA |= 0x20;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -