⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usart.c

📁 AVR 单片机M8的 USART 实现发送和接收数据的基本 应用,对刚学AVR来说是一个不错的入门例程
💻 C
字号:
//ICC-AVR application builder : 2007-6-4 15:31:38
// Target : M8
// Crystal: 1.0000Mhz
//usart test 2007-06-04

#include <iom8v.h>
#include <macros.h>

/*void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}   */

//UART0 initialize
// 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 = BIT(URSEL) | 0x06;
 UBRRL = 0x0C; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x18;
}

//call this routine to initialize 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 initialized
}

//
void main(void)
{
 unsigned char i;
 init_devices();
 //insert your functional code here...
 while (1)
 {
  while ( !(UCSRA & (1<<RXC)) ); //接收数据
  i=UDR;
 
  
  while ( !( UCSRA & (1<<UDRE)) );//发送数据
  UDR=i;
  
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -