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

📄 serial.c

📁 COP8 CPU的一个解释型BASIC源码
💻 C
字号:
////////////////////////////////////////////////////////////////
//
//                S C R I P T H E R M
//             A SCRIPTABLE THERMOMETER
// 
// entry of the National Semiconductor COP8FLASH Design Contest
//        submitted by Alberto Ricci Bitti (C) 2001
//              a.riccibitti@ra.nettuno.it
//
//--------------------------------------------------------------
//       FOR A BETTER VIEW SET TAB SIZE=4, INDENT SIZE=4
//--------------------------------------------------------------
// FILE   : serial.c
// PURPOSE: serial port driver.
//          Implements reception with a parametrizable queue 
//          (interrupt driven), transmission waiting for 
//          the previous character to be sent.
//
////////////////////////////////////////////////////////////////

#include <ioc8cdr.h>
#include "serial.h"

#define QUEUE_SIZE 16 /*circular queue size*/
static volatile unsigned char rx_buffer[QUEUE_SIZE]  ;
static volatile unsigned char rx_in =  0; /* empty buffer */
static volatile unsigned char rx_out = 0; /* empty buffer */



void com_initialize(void)
{
    rx_in = rx_out = 0;         //flush queue

    __PORTLC_bit.bit2 = 1;      //set TX pin as output
    __PORTLC_bit.bit3 = 0;      //set RX pin as input
    
//prescaler registers: xtal =10.00, baud = 9600
//using the formula from the data sheets gives P=1, N=130  
//divisor is 11 bit wide, shared across BAUD and PSR registers

#define P 1
#define N 130

//note: for the 3,27 crystal (standard on the null target board) set N=17 and P=2.5

    __BAUD = (N-1) & 0xff;         
    
    __PSR =  (P << 3) | ((N-1) >> 8);

    __ENU = 1;
    __ENUR = 0;
    __ENUI = 0x22;              //enable UART on TX pin, async mode, interrupt on RX
}


#pragma vector=__UART_RECEIVE
__interrupt void com_RX_interrupt(void)
{                      
    rx_buffer[rx_in] = __RBUF;     //this clears the interrupt flag as well
    if (++rx_in >= QUEUE_SIZE)
        rx_in = 0;
}


unsigned char com_pending(void)
{
        return rx_in != rx_out;
}


unsigned char com_getchar(void)
{   unsigned char retval;     
    while( ! com_pending() )
    { /*while loop intentionally void*/ };

    retval =  rx_buffer[rx_out];
    if (++rx_out >= QUEUE_SIZE)
        rx_out = 0;
    return retval;
}


void com_putchar(unsigned char c)
{
    while( ! __ENU_bit.tbmt )
    { /*while loop intentionally void*/ };
    __TBUF = c;
}

⌨️ 快捷键说明

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