⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 啊沙发沙发沙发撒范德按时打发似的发射 萨发生的发生法定
💻 C
字号:
/*******************************************************************
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 
Debuging data:  AA 55  
*******************************************************************/

#include <iom8.h>
#include "INCLUDES.H"
#include "CONSTANTS.H"
#include "FUNCTIONS.H"
#include "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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -