串口通信USART-AVR程序代碼
#include
#include
#defineRXB81
#defineTXB80
#defineUPE2
#defineOVR3
#defineFE4
#defineUDRE5
#defineRXC7
#defineFRAMING_ERROR(1< #definePARITY_ERROR(1< #defineDATA_OVERRUN(1< #defineDATA_REGISTER_EMPTY(1< #defineRX_COMPLETE(1< //USARTReceiverbuffer #defineRX_BUFFER_SIZE8 charrx_buffer[RX_BUFFER_SIZE]; unsignedcharnCount=0;//定義串口接收計(jì)數(shù)變量 #ifRX_BUFFER_SIZE<256 unsignedcharrx_wr_index,rx_rd_index,rx_counter; #else unsignedintrx_wr_index,rx_rd_index,rx_counter; #endif //ThisflagissetonUSARTReceiverbufferoverflow bitrx_buffer_overflow; //USARTReceiverinterruptserviceroutine interrupt[USART_RXC]voidusart_rx_isr(void) { charstatus,data; status=UCSRA; data=UDR; if((status&(FRAMING_ERROR|PARITY_ERROR|DATA_OVERRUN))==0) { //printf("interruptiontriged!rn");//插樁測(cè)試,驗(yàn)證中斷服務(wù)程序處于有效工作狀態(tài) nCount++;//接收到數(shù)據(jù)時(shí),每接收一個(gè),接收計(jì)數(shù)加1 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_ //GetacharacterfromtheUSARTReceiverbuffer #define_ALTERNATE_GETCHAR_ #pragmaused+ chargetchar(void) { chardata; 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; #asm("sei") returndata; } #pragmaused- #endif //StandardInput/Outputfunctions //Declareyourglobalvariableshere voidmain(void) { //Declareyourlocalvariableshere unsignedchartemp_data=0; //Input/OutputPortsinitialization //PortAinitialization //Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In //State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T PORTA=0x00; DDRA=0x00; //PortBinitialization //Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In //State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T PORTB=0x00; DDRB=0x00; //PortCinitialization //Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In //State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T PORTC=0x00; DDRC=0x00; //PortDinitialization //Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In //State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T PORTD=0x00; DDRD=0x00; //Timer/Counter0initialization //Clocksource:SystemClock //Clockvalue:Timer0Stopped //Mode:Normaltop=FFh //OC0output:Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; //Timer/Counter1initialization //Clocksource:SystemClock //Clockvalue:Timer1Stopped //Mode:Normaltop=FFFFh //OC1Aoutput:Discon. //OC1Boutput:Discon. //NoiseCanceler:Off //InputCaptureonFallingEdge //Timer1OverflowInterrupt:Off //InputCaptureInterrupt:Off //CompareAMatchInterrupt:Off //CompareBMatchInterrupt:Off TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; //Timer/Counter2initialization //Clocksource:SystemClock //Clockvalue:Timer2Stopped //Mode:Normaltop=FFh //OC2output:Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; //ExternalInterrupt(s)initialization //INT0:Off //INT1:Off //INT2:Off MCUCR=0x00; MCUCSR=0x00; //Timer(s)/Counter(s)Interrupt(s)initialization TIMSK=0x00; //USARTinitialization //CommunicationParameters:8Data,1Stop,NoParity //USARTReceiver:On //USARTTransmitter:On //USARTMode:Asynchronous //USARTBaudRate:9600 UCSRA=0x00; UCSRB=0x98; UCSRC=0x86; UBRRH=0x00; UBRRL=0x33; //AnalogComparatorinitialization //AnalogComparator:Off //AnalogComparatorInputCapturebyTimer/Counter1:Off ACSR=0x80; SFIOR=0x00; //Globalenableinterrupts #asm("sei") SREG|=0x80;//開(kāi)全局中斷 while(1) { //Placeyourcodehere if(nCount>0)//判斷是否有接收數(shù)據(jù) { nCount--;//處理過(guò)的數(shù)據(jù),減少數(shù)據(jù)計(jì)數(shù) temp_data=getchar();//獲取接收字符 if(temp_data<='9'&&temp_data>='0') { temp_data=temp_data-38; printf("%d",temp_data); } elseif(temp_data<='Z'&&temp_data>='A') {temp_data=temp_data+32; printf("%c",temp_data); } elseif(temp_data<='z'&&temp_data>='a') {temp_data=temp_data-32; printf("%c",temp_data);} else {printf("errorn");} } }; }