ATmega8自适应波特率的实现(软件包)
扫描二维码
随时随地手机看文章
ProjectName:Methodofauto-baudrateforATmega8
Date:2006-June-01
Author:EighthArmy
Systemcrystal:Internal8MHzbyRC
CompilerReversion:IARforAVR412A
Additionalinformation:
1Embeddedsystemstartupbaudrateis2400bps.
2HigherbaudrateofPCisnotrecommend.(Ref.BAUD_RATE_ARRAYspecifICation)
3Afterslaverreturns"AA55",itmeansthebaudratesetcompleted.
4Filename:Main.c
Debuggingdata:AA55
*******************************************************************/
#i nclude
#i nclude"INCLUDES.H"
#i nclude"CONSTANTS.H"
#i nclude"FUNCTIONS.H"
#i nclude"VARIABLES.H"
constucharBAUD_RATE_ARRAY[12]={
/*Embeddedstartupvalue->PCRepeatTimes*/
0xA0,0X01,/*2400*/
0xCF,0x00,/*4800->48005Times*/
0x67,0x00,/*9600->96007Times*/
0x33,0x00,/*19200->1920010Times*/
0x19,0x00,/*38400->3840030Times*/
0x10,0x00/*57600->5760017Times*/
};
/******SuperLoopbody*******************************************/
voidmain(void){
InitialSystem();
while(1)
{
if(!(ucStatusProgram&bSET_BAUD))
{
if(uiTenor&bRECEIVED)
{
uiTenor&=~bRECEIVED;
if(!Judge(&uCCommunicatorBuffer[0]))
{
ExchangeBaudRate((uchar*)&BAUD_RATE_ARRAY[ucSearch]);
ucSearch+=2;
}
else
{
PORTB|=(1<
TransmitData(ucCommunicatorBuffer,2);
}
}
}
else
{
if(uiTenor&bRECEIVED)
{
uiTenor&=~bRECEIVED;
PORTB^=(1<
}
}
}
}
/*******************************************************************
@Fn:InitialSystem()
@Br:Initializationofsystem
@Pa:None
@Rt:None
*******************************************************************/
voidInitialSystem(void){
_DI();
ConfigTimerSystem();
ConfigParallelPort();
ConfigSerialPort();
ucSearch=0;
_EI();
}
/*******************************************************************
@Fn:Delay_mS()
@Br:Non-InterruptonemilliSeconddelay
@Pa:uiMS
@Rt:None
*******************************************************************/
voidDelay_mS(uintuiMS){
ulongulMultiplex;
ulMultiplex=(ulong)uiMS*(ulong)8;
while(ulMultiplex--);
}
/*******************************************************************
@Fn:ConfigSerialPort()
@Br:ConfigurationofMCU
@Pa:None
@Rt:None
*******************************************************************/
voidConfigSerialPort(void){
UCSRA=(1<
UBRRL=0xA0;/*2400bps@8MHz*/
UCSRB=(1<
/*******************************************************************
@Fn:ConfigTimerSystem()
@Br:ConfigurationofTIMERonsystem
@Pa:None
@Rt:None
*******************************************************************/
voidConfigTimerSystem(void){
TCCR0=(1<
TIMSK=(1<
/*******************************************************************
@Fn:ConfigTimerSystem()
@Br:Configurationofoutputport
@Pa:None
@Rt:None
*******************************************************************/
voidConfigParallelPort(void){
PORTB=0xFE;/*Setofall*/
DDRB=0xFF;/*Settooutputmodeofall*/
}
/*******************************************************************
@Fn:TransmitData()
@Br:Sendaframontoserial-bus
@Pa:ucpDataAddress:Pointofdatabuffer
ucLength:Lengthofdata
@Rt:None
*******************************************************************/
voidTransmitData(uchar*ucpDataAddress,ucharucLength){
while(ucLength)
{
while(!(UCSRA&(1<
ucLength--;
}
}
/*******************************************************************
@Fn:ExchangeBaudRate()
@Br:Exchangevaluesformodifybaudrate
@Pa:ucpChar:Charactervalue
@Rt:None
*******************************************************************/
voidExchangeBaudRate(uchar*ucpChar){
UCSRB&=~(1<
UBRRH=*ucpChar++;
UCSRB=(1<
/*******************************************************************
@Fn:Judge()
@Br:Judgebaudrateaccordingtothereceiveddata
@Pa:ucpCharacter:Pointerofreceiveddatabuffer
@Rt:1:Right0:Error
*******************************************************************/
ucharJudge(uchar*ucpCharacter){
if((*ucpCharacter++!=CHAR0)||(*ucpCharacter++!=CHAR1))returnFAILURE;
else
returnSUCCESS;
}
/*Filename:INTERRUPTS.H*/
#i nclude
#i nclude"includes.h"
#i nclude"CONSTANTS.H"
#i nclude"FUNCTIONS.H"
#i nclude"GLOBALS.H"
/*******************************************************************
@Fn:T0OVERFLOW_Handler
@Br:ServiceofTimer_0interrupt
@Pa:None
@Rt:None
*******************************************************************/
#pragmavector=TIMER0_OVF_vect
__interruptvoidT0OVERFLOW_Handler(void){
TCNT0=0;
}
/*******************************************************************
@Fn:USART_RX
@Br:ServiceofUSARTinterrupt
@Pa:None
@Rt:None
*******************************************************************/
#pragmavector=USART_RXC_vect
__interruptvoidUSART_RX(void){
ucCommunicatorBuffer[ucReceiveCounter++]=UDR;
if(!(ucStatusProgram&bSET_BAUD))
{
if(ucReceiveCounter>1)
{
ucReceiveCounter=0;
uiTenor|=bRECEIVED;
}
}
else
{
if(ucReceiveCounter>2)
{
if(ucReceiveCounter>=ucCommunicatorBuffer[1])
{
ucReceiveCounter=0;
uiTenor|=bRECEIVED;
}
}
}
}
#ifndefCONSTANTS_H
#defineCONSTANTS_H
/******SystemMacro**********************************************************/
#define_EI()SREG|=0X80
#define_DI()SREG&=~0X80
#defineSLEEP()MCUCR|=(1<
/******StatusMacro**********************************************************/
#definebSET_BAUD0x01
/******TenorMacro***********************************************************/
#definebRECEIVED0x01
/******ApplicationMacro*****************************************************/
#defineINDICATEPB0
#defineCHAR00xAA
#defineCHAR10x55
#endif
#ifndefFUNCTIONS_H
#defineFUNCTIONS_H
/******Main.c****************************************************************/
voidInitialSystem(void);
voidDelay_mS(uintuiMS);
voidConfigSerialPort(void);
voidConfigTimerSystem(void);
voidConfigParallelPort(void);
voidTransmitData(uchar*ucpDataAddress,ucharucLength);
voidExchangeBaudRate(uchar*ucpChar);
ucharJudge(uchar*ucpCharacter);
#endif
#ifndefVERIABLES_H
#defineVERIABLES_H
ucharucReceiveCounter;
ucharucCommunicatorBuffer[64];
uintuiTenor;
ucharucStatusProgram;
ucharucSearch;
#endif
#ifndefGLOBALS_H
#defineGLOBALS_H
externucharucReceiveCounter;
externucharucCommunicatorBuffer[64];
externuintuiTenor;
externucharucStatusProgram;
externucharucSearch;
#endif
#ifndefINCLUDES_H
#defineINCLUDES_H
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#defineMax(A,B)(A>B:A?B)
#defineMin(A,B)(A
#ifndefuchar
#defineucharunsignedchar
#endif
#ifndefuint
#defineuintunsignedint
#endif
#ifndefulong
#defineulongunsignedlong
#endif
#ifndefSUCCESS
#defineSUCCESS1
#endif
#ifndefFAILURE
#defineFAILURE0
#endif
#endif