📄 usart使用.c
字号:
//ICC-AVR application builder : 1980-1-4 4:49:58
// Target : M8
// Crystal: 1.0000Mhz
#include <iom8v.h>
#include <macros.h>
unsigned char f[6]={0x01,0x02,0x03,0x04,0x05,0x00};
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0x7F; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x02;
}
//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x02;//倍速
UCSRC = 0x86;//写UCSRAC,XCK下降沿采样,异步
UBRRL = 0x0C; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;//接收结束中断使能
}
/*字符输入函数*/
/*unsigned char getchar(void)
{
while(!(UCSRA& (1<<RXC)));
return UDR;
}
*///UART0 发送一个字节
void put_char(unsigned char d)
{/* 等待发送缓冲器为空 */
while ( !( UCSRA & (1<<UDRE)));
/* 将数据放入缓冲器,发送数据 */
UDR=d;
//while(!(UCSRA&0x40)); //等待TXC置位
//UCSRA|=0x40; //写1清零
}
//字符输入函数
void put_s(unsigned char *s)
{
while (*s)
{
put_char(*s);
s++;
}
}
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)//接收中断RXC置1时中断
{
put_char(UDR);//接收完发送短信
//uart has received a character in UDR
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
void delay_us(int time)
{
do
{
time--;
}
while (time>1);
}
void delay_ms(unsigned int time)
{
while(time!=0)
{
delay_us(30);
time--;
}
}
void main()
{int i;
init_devices();
/*put_s("hello donkey!");
put_char(0x0a);
put_char(0x0d);
put_s("i love you");*/
while(1)
{
for(i=0;i<6;i++)
{
put_char(f[i]);
}//显示数字时,调proteus里面黑屏右键hex display mode
//while(1);
delay_ms(100);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -