Project Name: Method of auto-baudrate for ATmega8
Date: 2006-June-01
Author: Eighth Army
System Crystal: Internal 8MHz by RC
Compiler Reversion: IAR for AVR 412A
Additional information:
1 Embedded system startup baudrate is 2400bps.
2 Higher baud rate of PC is not recommend.(Ref. BAUD_RATE_ARRAY specifICation)
3 After slaver returns "AA 55",it means the baud rate set completed.
4 File name:Main.c
Debugging data: AA 55
*******************************************************************/
#i nclude <iom8.h>
#i nclude "INCLUDES.H"
#i nclude "CONSTANTS.H"
#i nclude "FUNCTIONS.H"
#i nclude "VARIABLES.H"
const uchar BAUD_RATE_ARRAY[12] = {
/* Embedded startup value -> PC Repeat Times */
0xA0,0X01, /* 2400 */
0xCF,0x00, /* 4800 -> 4800 5 Times */
0x67,0x00, /* 9600 -> 9600 7 Times */
0x33,0x00, /* 19200 -> 19200 10 Times */
0x19,0x00, /* 38400 -> 38400 30 Times */
0x10,0x00 /* 57600 -> 57600 17 Times */
};
/****** Super Loop body *******************************************/
void main( 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 << INDICATE );
ucStatusProgram |= bSET_BAUD;
TransmitData( ucCommunicatorBuffer, 2 );
}
}
}
else
{
if( uiTenor & bRECEIVED )
{
uiTenor &= ~bRECEIVED;
PORTB ^= ( 1 << INDICATE );
TransmitData( ucCommunicatorBuffer, ucCommunicatorBuffer[1] );
}
}
}
}
/*******************************************************************
@Fn: InitialSystem()
@Br: Initialization of system
@Pa: None
@Rt: None
*******************************************************************/
void InitialSystem( void ){
_DI();
ConfigTimerSystem();
ConfigParallelPort();
ConfigSerialPort();
ucSearch = 0;
_EI();
}
/*******************************************************************
@Fn: Delay_mS()
@Br: Non-Interrupt one milliSecond delay
@Pa: uiMS
@Rt: None
*******************************************************************/
void Delay_mS( uint uiMS ){
ulong ulMultiplex;
ulMultiplex = ( ulong )uiMS * ( ulong )8;
while( ulMultiplex-- );
}
/*******************************************************************
@Fn: ConfigSerialPort()
@Br: Configuration of MCU
@Pa: None
@Rt: None
*******************************************************************/
void ConfigSerialPort( void ){
UCSRA = ( 1 << U2X ); /* Double baud rate enabLED */
UBRRH = 0x01;
UBRRL = 0xA0; /* 2400bps @ 8MHz */
UCSRB = ( 1 << RXCIE ) | ( 1 << TXEN ) | ( 1 << RXEN ); /*Enable the Rxc & RX & TX interrupt*/
UCSRC = ( 1 << URSEL ) | ( 1 << UCSZ1 ) | ( 1 << UCSZ0 ); /*8 bit Data*/
}
/*******************************************************************
@Fn: ConfigTimerSystem()
@Br: Configuration of TIMER on system
@Pa: None
@Rt: None
*******************************************************************/
void ConfigTimerSystem( void ){
TCCR0 = ( 1 << CS02 ) + ( 1 << CS00 ); /* clk/1024 */
TCNT0 = 0X00;
上一篇:12864(ICCAVR程序)