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

📄 validatebaudrate._c

📁 Validate Baud rate s accuracy this program will show from FF to 00.
💻 _C
字号:
//ICC-AVR application builder : 2009-4-7 20:13:28
// Target : M16
// Crystal: 16.000Mhz

// Title: 		ValidateBaudRate.c
// Operation:
// 			   1> This program make echo to PC sending in Baudrate 38400
//			   2> AVR Study Board sends Data"from FF to 00" to PC in the SComAssistant.
//             3> The display will be showed when the key k7 is pressed.
// Attention:  
//             1> HEX must be setuped to show in the SComAssistant.
//             

/* UCSRA: Control & Status Register A
   b7-RXC: 	USART receive complete
   b6-TXC: 	USART transmit complete
   b5-UDRE:	USART Data register empty
   b4-FE:	Frame Error
   b3-DOR:  Data overtun
   b2-PE: 	Parity error
   b1-U2X: 	double the USART transmission speed
   b0-MPCM: multi-processor communication mode
   
   USARB
*  b7-RXCIE: RX complete interrupt enable
   b6-TXCIE: TX complete interrupt enable
   b5-UDRIE: USART data register empty interrupt enable
*  b4-RXEN:	 Receiver enable
*  b3-TXEN:  Transmitter enable
   b2-UCSZ2: chracter size     	 ------->0
   b1-RXB8	 receive data bit 8
   b0-TXB8:	 Transmit data 8
   
   UCSRC
*  b7-URSEL: register select   	 0/UBRRH,	  1/UCSRC
   b6-UMSEL: USART mode select 	 0/Asyn		  1/Synchronous
   b5-UPM1:  -------- Parity mode 00/disable, 01 reserved
   b4-UPM0:	 -------- 		 	  10 even,	  11 odd 
   b3-USBS:	stop bit selection 0/1-bit, 1/2-bit
*  b2-UCSZ1	 		  	   	   --------> 1
*  b1-UCSZ0  				   --------> 1
   b0-UCPOL: clock polarity	  0 rising XCK edge, 1 falling XCK edge  
*/

#define  uchar unsigned char
#define  uint  unsigned int
#define K1 0x01        //按键K0与PC0相连 启动键(start)
#define K0 0x02        //按键K1与PC1相连 调节键(adjust)
#define K6 BIT(6)      //按键K6与PC6相连 休眠键(sleep)
#define K7 BIT(7)      //按键K7与PC7相连 暂停键(pause)
#define dig0 (1<<6)    //推荐用这种形式,简单形象
#define dig1 (1<<7)
#define LED PORTD
#define LED1 PORTA
#define PressKey (PINC&K1)==0

#include <iom16v.h>
#include <macros.h>

#pragma interrupt_handler uart_rx_isr: 12//the interrupt of RX finish

unsigned char RecBuf[40],KeyMark=0;
int rec_head=0, rec_tail=0;
unsigned char rec_data;

void uart_rx_isr(void)
{
 RecBuf[rec_head]=UDR;
 rec_head++;
 if(rec_head>=40)
 	rec_head=0;			  
}

void port_init(void)
{
 DDRA  = 0xFF;		//set PortA output
 DDRB  = 0xff;		//set PORTB output
 DDRC  = 0x00;		//set PORTC output
 PORTC = 0xff;
 DDRD  = 0x7f;		//set PD.7 input for RX ?
                    //PD.0 is RX.
}

void USART_init(void)
{
 UCSRB=0x00;
 UCSRA=0x00;
 UCSRB=0b10011000;	 //b7: RXCIE enabeled, b4: RXEN enabled, B3: TXEN enabled
 UBRRH=0x00;		 //
 					 //Crystal=16MHx
 //UBRRL=103;		 //Bausdrate=9600 tested work fine
 //UBRRL=51;		 //Baudrate=19200 tested work fine
 UBRRL=25;		 	 //Baudrate=38400 tested work fine //v7.14版本在这里代码生成器犯低级错误;
 //eg:in the application bulider,UBRR=25,however,it can bulid UBRR=19.
 //UBRRL=8;	 		 //Baurate=115200 tested work fine
 UCSRC=0b10000110;	 //Asyn, No parity, 1-stop, 8-bit, rising edge
// memset(RecBuf, 0, sizeof(RecBuf));
 rec_head=0;
 rec_tail=0;
} 

void delay_ms(int count)
{
 int i, j;
 for(i=count; i>0; i--)
    for(j=500; j>0; j--)
	   ;
}

void transmit(unsigned char abyte)
{
 UDR=abyte;
 while(!(UCSRA&0b01000000))	//b6=1 TXE	,it quit when transmission finish.
	  ;
}

//*****************************************************************
void main(void)
{
// unsigned char outa=0b01010101, outb=0b10101010, outc=0x00, outd=0x00;
 int dswin;
 unsigned char SendData=0;
 port_init();
 USART_init();
 SEI();
 
 while(1)
   {
    WDR();				  	//Watchdog reset
	if(rec_head!=rec_tail)
	   {
	    rec_data=RecBuf[rec_tail];	//read data from head and write data at tail
		rec_tail++;
		//rec_data=RecBuf[rec_tail++]; //you can amend the sentences above two like that
		//i++; show that first use it,then add it.
		if(rec_tail>=40)	//make up circular queue
		   rec_tail=0;
		   
		dswin=rec_head<<2;	//the lowest 2 bit of PORTD are RXD and TXD.
	    PORTD=dswin;        //The two ports are occupied,so it need "<<2".
		PORTA=rec_data;     //rec_head++,so it can stand for the number of received data.
		transmit(rec_data);
		if(rec_data==13)	//★??
			transmit(10);
	   } 
    if(PressKey&&!KeyMark)    //clamping using state bit
    {
      delay_ms(20); 
      if(PressKey)
      {
        KeyMark=1;
        SendData=0xFF;        
      }  
    }
    else if(!PressKey)
    KeyMark=0;
//    while(PressKey);        //clamping using endless loop on conditionZ
     if(SendData)
     {
         while(!(UCSRA&(1<<UDRE)));
         UDR=SendData;
         SendData--;
     }

 }
}

⌨️ 快捷键说明

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