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;
TIMSK = ( 1 << TOIE0 ); /* Overflow enabled for TIMER_0*/
}
/*******************************************************************
@Fn: ConfigTimerSystem()
@Br: Configuration of output port
@Pa: None
@Rt: None
*******************************************************************/
void ConfigParallelPort( void ){
PORTB = 0xFE; /*Set of all*/
DDRB = 0xFF; /*Set to output mode of all*/
}
/*******************************************************************
@Fn: TransmitData()
@Br: Send a fram onto serial-bus
@Pa: ucpDataAddress: Point of data buffer
ucLength: Length of data
@Rt: None
*******************************************************************/
void TransmitData( uchar *ucpDataAddress, uchar ucLength ){
while( ucLength )
{
while(!( UCSRA & ( 1 << UDRE )));
UDR = *ucpDataAddress++;
ucLength--;
}
}
/*******************************************************************
@Fn: ExchangeBaudRate()
@Br: Exchange values for modify baud rate
@Pa: ucpChar: Character value
@Rt: None
*******************************************************************/
void ExchangeBaudRate( uchar *ucpChar ){
UCSRB &= ~( 1 << RXCIE ) | ( 1 << TXEN ) | ( 1 << RXEN );
UBRRL = *ucpChar++;
UBRRH = *ucpChar++;
UCSRB = ( 1 << RXCIE ) | ( 1 << TXEN ) | ( 1 << RXEN );
}
/*******************************************************************
@Fn: Judge()
@Br: Judge baud rate according to the received data
@Pa: ucpCharacter: Pointer of received data buffer
@Rt: 1:Right 0:Error
*******************************************************************/
uchar Judge( uchar *ucpCharacter ){
if(( *ucpCharacter++ != CHAR0 ) || ( *ucpCharacter++ != CHAR1 )) return FAILURE;
else
return SUCCESS;
}
/* File name: INTERRUPTS.H */
#i nclude <iom8.h>
#i nclude "includes.h"
#i nclude "CONSTANTS.H"
#i nclude "FUNCTIONS.H"
#i nclude "GLOBALS.H"
/*******************************************************************
@Fn: T0OVERFLOW_Handler
@Br: Service of Timer_0 interrupt
@Pa: None
@Rt: None
*******************************************************************/
#pragma vector = TIMER0_OVF_vect
__interrupt void T0OVERFLOW_Handler( void ){
TCNT0 = 0;
}
/*******************************************************************
@Fn: USART_RX
@Br: Service of USART interrupt
@Pa: None
@Rt: None
*******************************************************************/
#pragma vector = USART_RXC_vect
__interrupt void USART_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;
}
}
}
}
#ifndef CONSTANTS_H
#define CONSTANTS_H
/****** System Macro **********************************************************/
#define _EI() SREG|=0X80
#define _DI() SREG&=~0X80
#define SLEEP() MCUCR|=(1<<SE);asm("sleep")
/****** Status Macro **********************************************************/
#define bSET_BAUD 0x01
/****** Tenor Macro ***********************************************************/
#define bRECEIVED 0x01
/****** Application Macro *****************************************************/
#define INDICATE PB0
#define CHAR0 0xAA
#define CHAR1 0x55
#endif
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
/****** Main.c ****************************************************************/
void InitialSystem( void );
void Delay_mS( uint uiMS );
void ConfigSerialPort( void );
void ConfigTimerSystem( void );
void ConfigParallelPort( void );
void TransmitData( uchar *ucpDataAddress, uchar ucLength );
void ExchangeBaudRate( uchar *ucpChar );
uchar Judge( uchar *ucpCharacter );
#endif
#ifndef VERIABLES_H
#define VERIABLES_H
uchar ucReceiveCounter;
uchar ucCommunicatorBuffer[64];
uint uiTenor;
uchar ucStatusProgram;
uchar ucSearch;
#endif
#ifndef GLOBALS_H
#define GLOBALS_H
extern uchar ucReceiveCounter;
extern uchar ucCommunicatorBuffer[64];
extern uint uiTenor;
extern uchar ucStatusProgram;
extern uchar ucSearch;
#endif
#ifndef INCLUDES_H
#define INCLUDES_H
#i nclude <assert.h>
#i nclude <ctype.h>
#i nclude <float.h>
#i nclude <math.h>
#i nclude <stddef.h>
#i nclude <stdio.h>
#i nclude <stdlib.h>
#i nclude <string.h>
#i nclude <sysmac.h>
#define Max(A,B) (A>B:A?B)
#define Min(A,B) (A<B:A?B)
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#ifndef ulong
#define ulong unsigned long
#endif
#ifndef SUCCESS
#define SUCCESS 1
#endif
#ifndef FAILURE
#define FAILURE 0
#endif
#endif