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

📄 test._c

📁 avr单片机串口先进先出实例程序
💻 _C
字号:
//ICC-AVR application builder : 2009-4-2 9:23:34
// Target : M16
// Crystal: 7.3728Mhz

#include <iom16v.h>
#include <macros.h>

unsigned char count,uart_inter_flag=0,uart_buf[25];

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

//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 10.000mSec (0.0%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0xB8; //set count
 OCR0  = 0x48;  //set compare
 TCCR0 = 0x05; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
 TCNT0 = 0xB8; //reload counter value
 count++;
}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = BIT(URSEL) | 0x06;
 UBRRL = 0x2F; //set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x98;
}

#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
 //uart has received a character in UDR
 unsigned char i,temp; 
 while(!(UCSRA&0x80)); 
 temp = UDR; 
 for(i=0;i<24;i++) 
  uart_buf[i]=uart_buf[i+1]; 
 uart_buf[24]=temp; 
 uart_inter_flag=0x01;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer0_init();
 uart0_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x01; //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)
 {
  if(uart_inter_flag==1)
  {
   uart_inter_flag=0;
   for(i=0;i<25;i++)
    putchar(uart_buf[i]);
  }
  if(count>30)
  {
   count=0;
   PORTB^=(1<<3);
  }
 }
}

⌨️ 快捷键说明

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