日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 技術(shù)學(xué)院 > 熱搜器件
[導(dǎo)讀]STC12C5A60S2單片機(jī)的串口擴(kuò)展

STC12C5A60S2單片機(jī)的串口從傳統(tǒng)的一個(gè)擴(kuò)展到了兩個(gè),
而且還增加了一個(gè)獨(dú)立波特率發(fā)生器,把定時(shí)器1解放了出來(lái),真的不是一般的方便,
還而且能用1T模式,速度大大滴提高了。。。
 
UART.C
 
 
#include <STC12C5A.H>     //STC12C5A系列單片機(jī) 
#include <intrins.h> 
#include "UART.H" 
 
#define uchar   unsigned char 
#define uint    unsigned int 
 
//緩存串口1和串口2接收到的字符 
uchar UART1_Recv_Val = 0; 
uchar UART2_Recv_Val = 0; 
 
 
 
void UART1_Init(uchar RELOAD, bit doubleBaud, bit timeMod) 

    SCON |= 0x50;       //串口1方式1,接收充許 
 
    BRT = RELOAD;       //波特率2400 
 
    if (timeMod == 1)       //1T 
    { 
        //T0x12   T1x12   UM0x6   BRTR    S2SMOD  BRTx12  EXTRAM  S1BRS 
        AUXR |= 0x15;       //串口1使用獨(dú)立波特率發(fā)生器,獨(dú)立波特率發(fā)生器1T 
    } 
    else                    //12T 
    { 
        AUXR |= 0x11; 
    } 
 
    if (doubleBaud == 1) 
    { 
        PCON |= 0x80;     //波特率加倍 
    } 
    else 
    { 
        PCON &= 0x7F;     //波特率不加倍 
    } 
 
    EA = 1; 
    ES = 1;             //充許串口1中斷 

 
 
 
void UART2_Init(uchar RELOAD, bit doubleBaud, bit timeMod) 

    //S2SM0  S2SM1   S2SM2   S2REN   S2TB8   S2RB8   S2TI     S2RI 
    S2CON |= 0x50;      //串口2,方式1,接收充許 
 
    BRT = RELOAD; 
 
    if (timeMod == 1)       //1T 
    { 
        //T0x12   T1x12   UM0x6   BRTR    S2SMOD  BRTx12  EXTRAM  S1BRS 
        AUXR |= 0x14;       //串口1使用獨(dú)立波特率發(fā)生器,獨(dú)立波特率發(fā)生器1T 
    } 
    else                    //12T 
    { 
        AUXR = (AUXR | 0x10) & 0xFB; 
    } 
 
    if (doubleBaud == 1) 
    { 
        AUXR |= 0x08;       //波特率加倍 
    } 
    else 
    { 
        AUXR &= 0xF7;       //波特率不加倍 
    } 
 
    EA = 1;  
    //-       -       -       -       -       -       ESPI    ES2 
    IE2 |= 0x01;            //充許串口2中斷            

 
 
 
void UART1_SendOneChar(uchar val) 

    //ES = 0;                   //關(guān)閉串口1中斷 
 
    SBUF = val; 
    while(TI == 0); 
    TI = 0; 
 
    //ES = 1;                  //恢復(fù)串口1中斷 
}                           
 
 
 
void UART2_SendOneChar(uchar val) 

    //IE2 = 0x00;                 //關(guān)閉串口2中斷 
 
    S2BUF = val;     
    while ((S2CON & 0x02) == 0); 
    S2CON &= 0xFD; 
 
    //IE2 = 0x01;                //恢復(fù)串口2中斷 

 
 
 
void UART1_SendStr(uchar *str) 

    while( (*str)!='/0' ) 
    { 
        UART1_SendOneChar(*str); 
        str++; 
    } 

 
 
 
void UART2_SendStr(uchar *str) 

    while( (*str)!='/0' ) 
    { 
        UART2_SendOneChar(*str); 
        str++; 
    } 

 
 
 
void UART1_Int(void) interrupt 4 

    if (RI == 1) 
    { 
        RI = 0; 
        UART1_Recv_Val = SBUF; 
    }    

 
 
 
void UART2_Int(void) interrupt 8 

    if ((S2CON & 0x01) == 1) 
    { 
        S2CON &= 0xFE; 
        UART2_Recv_Val = S2BUF; 
    }    

 
 
 
UART.H
 
 
#ifndef _UART_H_ 
#define _UART_H_ 
 
#define uchar   unsigned char 
#define uint    unsigned int 
 
//定義串口1口開(kāi)關(guān),關(guān)閉則不能接收數(shù)據(jù) 
#define OpenUART1()     ES=1 
#define CloseUART1()    ES=0 
#define OpenUART2()     IE2|=0x01 
#define CloseUART2()    IE2&=0xFE 
 
//緩存串口1和串口2接收到的字符 
extern uchar UART1_Recv_Val; 
extern uchar UART2_Recv_Val; 
 
 
 
void UART1_Init(uchar RELOAD, bit doubleBaud, bit timeMod); 
 
 
 
void UART2_Init(uchar RELOAD, bit doubleBaud, bit timeMod); 
 
 
 
void UART1_SendOneChar(uchar val); 
 
 
 
void UART2_SendOneChar(uchar val); 
 
 
 
void UART1_SendStr(uchar *str); 
 
 
 
void UART2_SendStr(uchar *str); 
 
 
#endif 
 
 
 
main.c
 
 
#include <STC12C5A.H>     //STC12C5A系列單片機(jī) 
#include <intrins.h> 
#include "UART.H" 
 
#define uchar   unsigned char 
#define uint    unsigned int 
 
 
//獨(dú)立波特率發(fā)生器初值,1T模式 
//Fosc = 晶振頻率, Baud0 = 標(biāo)準(zhǔn)波特率 
//RELOAD = 256 - INT(Fosc/Baud0/32 + 0.5)        
//Baud = Fosc/(256 - RELOAD)/32 
//error = (Baud - Baud0)/Baud0 * 100% 
uchar RELOAD = 0xD9;                    //Fosc = 12MHz, Baud0 = 9600 
 
//波特率是否加倍,0不倍,1加倍 
bit doubleBaud = 0; 
 
//獨(dú)立波特率發(fā)生器,0為12T模式,1為1T模式 
bit timeMod = 1; 
 
 
 
sbit LED1 = P1^0; 
sbit LED2 = P1^1; 
 
 
 
void main(void) 

    //串口標(biāo)志位,0使用串口1,1使用串口2 
    bit UART_flag = 1; 
 
    LED1 = 1; 
    LED1 = 1; 
 
    //串口1和串口2初始化 
    UART1_Init(RELOAD, doubleBaud, timeMod); 
    UART2_Init(RELOAD, doubleBaud, timeMod); 
 
    //先用串口1接收字符 
    OpenUART1(); 
    CloseUART2(); 
 
    UART1_SendOneChar(0x0C);            //超級(jí)終端清屏 
    UART1_SendStr("/r/n"); 
    UART1_SendStr("/r/n"); 
    UART1_SendStr("1.串口1通訊/r/n"); 
    UART2_SendStr("2.串口2通訊/r/n"); 
 
    while (UART1_Recv_Val == 0); 
 
    UART1_SendStr("/r/n");          //換行 
 
    if (UART1_Recv_Val == '1') 
    { 
        OpenUART1(); 
        CloseUART2(); 
        UART1_SendStr("Light LED(UART1):/r/n"); 
 
        UART_flag = 0; 
    } 
    else 
    { 
        CloseUART1(); 
        OpenUART2(); 
        UART2_SendStr("Light LED(UART2):/r/n"); 
 
        UART_flag = 1; 
    } 
    UART1_Recv_Val = 0;         //緩存清零 
    UART2_Recv_Val = 0;         //緩存清零 
     
    while (1) 
    { 
        if (UART_flag == 0)             //串口1接收字符 
        { 
            if (UART1_Recv_Val != 0) 
            { 
                switch (UART1_Recv_Val) 
                { 
                    case '1': 
                        LED1 = ~LED1; 
                        break; 
                    case '2': 
                        LED2 = ~LED2; 
                        break; 
                    default: 
                        LED1 = 1; 
                        LED1 = 1; 
                        break; 
                } 
                UART1_Recv_Val = 0;            //緩存清零 
            } 
        } 
        else                               //串口2接收字符 
        { 
            if (UART2_Recv_Val != 0) 
            { 
                switch (UART2_Recv_Val) 
                { 
                    case '1': 
                        LED1 = ~LED1; 
                        break; 
                    case '2': 
                        LED2 = ~LED2; 
                        break; 
                    default: 
                        LED1 = 1; 
                        LED1 = 1; 
                        break; 
                } 
                UART2_Recv_Val = 0;            //緩存清零 
            } 
        } 
    } 

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉