serial.c

来自「avr MP3 的源程序,包含文件系统,适合初学者」· C语言 代码 · 共 86 行

C
86
字号
//-------------------------------------------------------------------------
//FileName     : serial.c
//Created by   : ZhengYanbo
//Version      : v1.0
//Last Modified: 2006.3.3
//Fuction      : routines of reading and writing AT45DB041B dataflash 
//Comments     : This is used for FlyBird mp3 player.
//------------------------------------------------------------------------

#include "type.h"
#include "serial.h"

//******************************************************************
// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud rate: 9600
// UBRR0H=0x00;
// UBRR0L=0x67;
// TEST OK!
void USART_init(word baud)
//******************************************************************
{  
    //Set baud rate
    UBRR0H = (unsigned char)(baud>>8);
    UBRR0L = (unsigned char)baud;
    
    UCSR0A=0x00;
    //Enable receiver and transmitter
    //RXEN=1, TXEN=1;
    UCSR0B = UCSR0B & 0x18;
    //Set frame format:8 Data, 1 Stop, No Parity
    UCSR0C = UCSR0C & 0x86;
}

//******************************************************************
//USART0 get a byte
//TEST OK!
byte USART_get_char(void)
//******************************************************************
{  
    //Wait data to be received
    while(!(UCSR0A & 0x80));
    //get and return data
    return(UDR0);
}

//******************************************************************
//send a byte by USART0
//TEST OK!
void USART_put_char(byte data)
//******************************************************************
{
    //Wait for empty transmit buffer
    while(!(UCSR0A & 0x20));
    //put data into buffer
    UDR0 = data;    
}

//****************************************************************** 
//return when no data
byte USART_get_char_1(void)
//******************************************************************
{      
    byte waitCount=0;
    byte res_data, dummy;       
    
    //Wait data to be received
    
    while(1)
    {
    if(!(UCSR0A & 0x80))
        {waitCount++; if(waitCount > 250) {res_data = 0; break;}}
    else        
        {res_data = UDR0; break;};
    } //check RXC bit
    
    while(UCSR0A & 0x80) dummy = UDR0;
    return(res_data); //return data
}


//end of serial.c

⌨️ 快捷键说明

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