f53x_linsnooper_uart.c

来自「8051试验程序 基础教材」· C语言 代码 · 共 266 行

C
266
字号
//-----------------------------------------------------------------------------
// F53x_UART.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This module contains a functions for UART interface. These functions are
// using for setting, sending etc.
//
// FID:
// Target:          C8051F53x
// Tool chain:      KEIL C51 7.20
// Command Line:    None
// Project Name:    LIN Snooper
//
// Release 1.0
//    -Initial Revision
//    -01 NOV 2006
//
//


//-----------------------------------------------------------------------------
// Header File Preprocessor Directive
//-----------------------------------------------------------------------------
#ifndef __UART_C__
#define __UART_C__


//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include  <ioc8051f530.h>                // SFR declarations
#include "F53x_LINSnooper_Main.h"      // Include common specific definitions
#include "F53x_LINSnooper_UART.h"      // Include UARTs specific definitions
#include "F53x_LINSnooper_Timer.h"     // Include Timers specific definitions


//-----------------------------------------------------------------------------
// Constant Definitions
//-----------------------------------------------------------------------------
#define  F53x_UARTS_SOURCE


//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
uchar UART_BUFFER[UART_BUFFER_SIZE];
uchar UART_BUFFER_PUSH;                // Position to push byte in buffer
uchar UART_BUFFER_GET;                 //  Position to get byte from buffer
uchar UART_EVENT = 0x00;

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// UART0_Initialize
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  :   This function initializes the UART0 interface.
// Note:
//-----------------------------------------------------------------------------
void UART0_Initialize(void)
{
    SCON0 = 0x10;                      // UART Receiver Enable
                                       // 8-bit UART,
                                       // Multiprocessor Communications Disable
    IE |= 0x10;                        // Enable UART interrupt
    Timer1_Initialize();               // UART timer initialization
}

//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// UART0_ISR
//----------------------------------------------------------------------------
//
// Return Value :   None
// Parameters   :   None
// Description  :   This is interrupt service.
// Note:
//----------------------------------------------------------------------------
#pragma vector = TI0_int
__interrupt void UART0_ISR(void)
{
   if ( SCON0_bit.RI0 )                          // Receive byte
    {
       SCON0_bit.RI0=0;                          // Clear Receive Interrupt Flag
        UART_BUFFER[UART_BUFFER_PUSH] = SBUF0;
        Inc_UART_BUFFER_PUSH();
    }

   if ( SCON0_bit.TI0 )                          // Transmit byte
    {
       SCON0_bit.TI0 = 0;                        // Clear Transmit Interrupt Flag
        UART_EVENT &= ~UART_TRANSMIT;  // Byte sent
    }
}


//----------------------------------------------------------------------------
// UART_Send_Byte
//----------------------------------------------------------------------------
//
// Return Value :   None
// Parameters   :   byte - byte to send
// Description  :   This function sends byte via UART interface
// Note:
//----------------------------------------------------------------------------
void UART_Send_Byte(uchar byte)
{
    while (UART_EVENT & UART_TRANSMIT) ; // Wait for free TX buffer
    UART_EVENT |= UART_TRANSMIT;         // UART sending flag
    SBUF0 = byte;
}


//----------------------------------------------------------------------------
// UART_Send_Word
//----------------------------------------------------------------------------
//
// Return Value :   None
// Parameters   :   buf - buffer to send
//                      numb - number of bytes
// Description  :   This function sends bytes via UART interface
// Note:
//----------------------------------------------------------------------------
void UART_Send_Word(uchar __code *buf, uchar numb)
{
    int i;

    for( i=0; i<numb; i++ )
    {
        UART_Send_Byte(buf[i]);
    }
}


//----------------------------------------------------------------------------
// UART_Send_HEX
//----------------------------------------------------------------------------
//
// Return Value :   None
// Parameters   :   byte - the byte to changed
//                      type - "0x" string flag on/off
// Description  :   This function converts byte to HEX format and sends it.
// Note:                type = 0 - the "0x" string is switch off
//                      type = 1 - the "0x" string is switch on
//----------------------------------------------------------------------------
void UART_Send_HEX(uchar byte, uchar type)
{
    char HEXchars[] = "0123456789ABCDEF";
    uchar a = byte % 16;
    uchar b = (byte - a) / 16;

    if ( type == 1)
        UART_Send_Word( "0x", 2 );
    UART_Send_Byte( HEXchars[b] );
    UART_Send_Byte( HEXchars[a] );
}


//----------------------------------------------------------------------------
// UART_Send_DEC
//----------------------------------------------------------------------------
//
// Return Value :   None
// Parameters   :   byte - the byte to changed
//                      zeros - count of zeros displayed
// Description  :   This function converts byte to DEC format and sends it
// Note:                zeros = 0 - zeros are not displayed
//                      zeros = 1 - one zero displayed
//                      zeros   = 2 - two zeros displayed
//----------------------------------------------------------------------------
void UART_Send_DEC(uchar byte, uchar zeros)
{
    uchar a,b;

    a = byte / 100;
    if ( !(( a == 0 ) & (zeros < 2)) )
        UART_Send_Byte( a + '0' );

    b = byte - (a * 100);
    a = b / 10;
    if ( !(( a == 0 ) & (zeros < 1)) )
        UART_Send_Byte( a + '0' );

    a = b - (a * 10);
    UART_Send_Byte( a + '0' );
}


//-----------------------------------------------------------------------------
// Inc_UART_BUFFER_PUSH
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  : Increnents position to push byte in buffer
// Note:
//-----------------------------------------------------------------------------
void Inc_UART_BUFFER_PUSH(void)
{
    if (++UART_BUFFER_PUSH == UART_BUFFER_SIZE)
    {
        UART_BUFFER_PUSH = 0;
    }
    if (UART_BUFFER_PUSH == UART_BUFFER_GET)
    {
        UART_BUFFER_PUSH--;
        if (UART_BUFFER_PUSH > UART_BUFFER_SIZE)
        {
            UART_BUFFER_PUSH = UART_BUFFER_SIZE;
        }
    }
}


//-----------------------------------------------------------------------------
// Dec_UART_BUFFER_GET
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  : Decrements position to get byte from buffer
// Note:
//-----------------------------------------------------------------------------
void Dec_UART_BUFFER_GET(void)
{
    if (--UART_BUFFER_GET > UART_BUFFER_SIZE)
    {
        UART_BUFFER_GET = UART_BUFFER_SIZE-1;
    }
}


//-----------------------------------------------------------------------------
// Inc_UART_BUFFER_GET
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  : Increnents position to get byte from buffer
// Note:
//-----------------------------------------------------------------------------
void Inc_UART_BUFFER_GET(void)
{
    if (++UART_BUFFER_GET == UART_BUFFER_SIZE)
    {
        UART_BUFFER_GET = 0;
    }
}

#endif                                 // endif definition of __UART_C__

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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