📄 uart01.c
字号:
//ICC-AVR application builder : 2009-3-5 20:22:42
// Target : M128
// Crystal: 11.059Mhz
#include <iom128v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0x00;
POTC = 0xFF; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
PORTE = 0xFF;
DDRE = 0x00;
PORTF = 0xFF;
DDRF = 0x00;
PORTG = 0x1F;
DDRG = 0x00;
}
//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x0E;
UBRR0L = 0x47; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
//UART1 initialisation
// desired baud rate:2400
// actual baud rate:2400 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = 0x1F; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0x18;
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
uart0_init();
uart1_init();
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
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
/*数据发送,查询方式*/
void Uart0_Transmit(unsigned char data)
{
while (!(UCR0A & (1<<UDRE0))); /* 等待发送缓冲器为空*/
UDR0 = data; /* 发送数据*/
}
void Uart1_Transmit(unsigned char data)
{
while (!(UCSR1A & (1<<UDRE1))); /* 等待发送缓冲器为空*/
UDR1 = data; /* 发送数据*/
}
/*数据接收,查询方式*/
unsigned char Uart1_Receive( void )
{
while (!(USR1A & (1<<RXC1))); /*等待接收数据*/
return UDR1; /*获取并返回数据*/
}
unsigned char Uart0_Receive( void )
{
while (!(UCS0A & (1<<RXC0))); /*等待接收数据*/
return UDR0; /*获取并返回数据*/
}
void DelayMs(unsigned int t)
{
do
{
_NOP();
}while(--t);
}
/*主函数*/
void main(void)
{
char i;
init_devices();
while(1)
{
i=Uart0_Receive();
DelayMs(1000);
Uart0_Transmit(i);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -