標(biāo)題: <更新>PC232.h <Pc232_MAX要定義> [打印本頁(yè)]

作者: liuqq    時(shí)間: 2015-5-19 01:47
標(biāo)題: <更新>PC232.h <Pc232_MAX要定義>
#ifndef _PC232_H_
#define _PC232_H_

#include <reg51.h>
#include "intrins.h"


#define uint8   unsigned char
#define uint16  unsigned int


#define PC232_MAX  16             //最大字符串長(zhǎng)度

uint8 PC232_data;               //當(dāng)前字符
uint8 PC232_stringSBUF[PC232_MAX]={" "};  //字符串
uint8 PC232_string[PC232_MAX]={" "};  //字符串
uint8 PC232_Loc=0;               //字符地址


void PC232delay( )   //10ms@22.0784MHz
{
    unsigned char i, j, k;

    i = 1;
    j = 216;
    k = 35;
    do
    {
        do
        {
            while (--k);
        } while (--j);
    } while (--i);
}


void UartInit()     //9600bps@22.0784MHz
{
    EA=1;
    ES=1;
    PCON &= 0x7F;       //波特率不倍速
    SCON = 0x50;        //8位數(shù)據(jù),可變波特率
    TMOD &= 0x0F;       //清除定時(shí)器1模式位
    TMOD |= 0x20;       //設(shè)定定時(shí)器1為8位自動(dòng)重裝方式
    TL1 = 0xFA;     //設(shè)定定時(shí)初值
    TH1 = 0xFA;     //設(shè)定定時(shí)器重裝值
    ET1 = 0;        //禁止定時(shí)器1中斷
    TR1 = 1;        //啟動(dòng)定時(shí)器1


}

/*------------------------------- 發(fā)送一個(gè)字節(jié)---------------------------------------*/
void SendByte(uint8 dat)
{
    SBUF = dat;
    while(!TI);
    TI = 0;
}

/*------------------- 發(fā)送一個(gè)字符串------------------------------------------*/
void SendStr(uint8 *s)
{
    while(*s != '\0')  //字符串是否發(fā)送完,
    {
        SendByte(*s);
        s++;
    }
}

/*------------------- 串口中斷------------------------------------------*/
void PC232serial()interrupt 4
{
    if(RI && PC232_Loc<PC232_MAX){

        switch(PC232_MAX){
            case 1 : PC232_data=SBUF;
                     SendByte(PC232_data);
                     PC232_string[0]=SBUF;
                     PC232_string[1]=0x00;
                     break;
            default: PC232_data=SBUF;
                     SendByte(PC232_data);
                 PC232_stringSBUF[PC232_Loc]=SBUF;
                     PC232_string[PC232_Loc]=PC232_stringSBUF[PC232_Loc];
                     PC232_string[++PC232_Loc]=0x00;
                     break;
         }

    }
     if(PC232_Loc==PC232_MAX){
                       PC232_Loc=0;
                         PC232_stringSBUF[0]=0x00;
     }


     RI=0;

}


#endif







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1