相应马老师的号召,我把cvavr中usart中断子程序注释了一下,里面也有几句不懂的地方,希望高手帮我注释一下,谢谢
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.4a Standard
Automatic Program Generator
?Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mailffice@hpinfotech.com
Project :
Version :
Date : 2005-10-19
Author : df
Company : dadf
Comments:
Chip type : ATmega8L
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE]; //定义接受缓冲区8个char型变量组成的数组
#if RX_BUFFER_SIZE<256 //若缓冲区大于256个字节,定义为 int型数组
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow; //接受缓冲区溢出标志
// USART Receiver interrupt service routine //usart接受中断服务程序
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR; //读取数据寄存器
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) //判断是否有错误标志
{
rx_buffer[rx_wr_index]=data; // 填充接受缓冲区
if (++rx_wr_index == RX_BUFFER_SIZE) //判断接受缓冲区是否满
rx_wr_index=0; //如果满则下一个接受数据覆盖缓冲区的首字节
if (++rx_counter == RX_BUFFER_SIZE) //记录缓冲区还有多少个字节没有读取,如果超出了缓冲区容量,置位缓冲区溢出标志
{
rx_counter=0;
rx_buffer_overflow=1; //缓冲区溢出
};
};
}
#ifndef _DEBUG_TERMINAL_IO_ //?????????
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_ //?????????
#pragma used+ //?????????
char getchar(void)
{
char data;
while (rx_counter==0); //没有数据可以读取,等待。。。。。
data=rx_buffer[rx_rd_index]; //读取缓冲区数据
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0; //标记出读到哪里,如果读到缓冲区末尾,则从头开始读
#asm("cli")
--rx_counter; //未读数据个数减少1
#asm("sei")
return data;
}
#pragma used- //?????????
#endif
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=1 State4=1 State3=T State2=T State1=T State0=T
PORTB=0x30;
DDRB=0x30;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
};
}
|