test.c

来自「Avr Serial Port Communication」· C语言 代码 · 共 48 行

C
48
字号
#include <mega16.h> 
#include <delay.h>

void Setup(void);
void Communication(void); 

void main()
{
       
        Setup();
        Communication();
        while (1)
        {
                
                delay_ms(1000);
                PORTA.0 = 0;
                delay_ms(1000);
        }                       
}
              

void Communication(void)
{    
         char x;   
         while(1)
         {
                while(UCSRA.7 == 0);  // when register starts to get chars
                x = UDR;   // get recieved charater x
                while(UCSRA.5 == 0);  //when register becomes empty
                UDR = x;   //transmiit x
                // UDR:  common I/O address of USART for recieving and transmitting  Page 151 
         }

}  

                                             
void Setup(void)
{
        // serial output
        UCSRA = 0b00000000;   // not double speed
        UCSRB = 0b00011000;   // enable Rx, Tx
        UCSRC = 0b10000011;   // no parity, 1 stop, 8 data bit     
        UBRRH = 0;  
        UBRRL = 12;// 38400 bps
}
        

⌨️ 快捷键说明

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