色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)模擬串口C語言程序代碼

          51單片機(jī)模擬串口C語言程序代碼

          作者: 時(shí)間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
          /
          #include "string.h"
          #include"stdio.h"
          #include
          sfr16 DPTR = 0x82;
          typedef unsigned char INT8U;
          typedef unsigned int INT16U;
          typedef unsigned char uint8_t;
          typedef unsigned int uint16_t;
          typedef unsigned int uchar;
          typedef unsigned char byte;
          typedef unsigned int word;
          #define YES 1
          #define NO 0
          //定義使用哪個(gè)定時(shí)器, 只可定義一個(gè)
          #define TIMER_0
          //#define TIMER_1
          //定義串口收、發(fā)送管腳。
          sbit rs_TXD = P2^1;
          sbit rs_RXD = P2^0;
          //根據(jù)定時(shí)器確定參數(shù)
          #ifdef TIMER_0
          #define TMOD_AND_WORD 0xF0;
          #define TMOD_TIME_MODE 0x01;
          #define TMOD_COUNT_MODE 0x05; //設(shè)置計(jì)數(shù)模式位
          sbit TCON_ENABLE_TIMER = TCON^4;
          sbit TCON_TFx = TCON^5; //中斷標(biāo)志位
          sbit IE_ETx = IE^1; //中斷允許位為 ET0
          sbit IP_PTx = IP^1; //中斷優(yōu)先級(jí)
          sfr rs_timerL = 0x8A; //TL0
          sfr rs_timerH = 0x8C; //TH0
          #endif
          #ifdef TIMER_1
          #define TMOD_AND_WORD 0x0F;
          #define TMOD_TIME_MODE 0x10;
          #define TMOD_COUNT_MODE 0x50; //設(shè)置計(jì)數(shù)模式位
          sbit TCON_ENABLE_TIMER = TCON^6; //
          sbit TCON_TFx = TCON^7; //中斷標(biāo)志位
          sbit IE_ETx = IE^3; //中斷允許位為 ET1
          sbit IP_PTx = IP^4; //中斷優(yōu)先級(jí)
          sfr rs_timerL = 0x8B; //TL1
          sfr rs_timerH = 0x8D; //TH1
          #endif
          INT8U bdata rs_BUF; //串行收、發(fā)時(shí)用的移位暫存器。
          sbit rs_BUF_bit7 = rs_BUF^7; //移位暫存器的最高位。
          INT8U rs_shift_count; //移位計(jì)數(shù)器。
          INT8U bdata rsFlags;
          sbit rs_f_TI = rsFlags^0; //0:正在發(fā)送; 1: 一個(gè)字符完畢
          sbit rs_f_RI_enable = rsFlags^1; //0:禁止接收; 1:允許接收
          sbit rs_f_TI_enable = rsFlags^2; //0:禁止發(fā)送; 1:允許發(fā)送
          //選擇以下一個(gè)晶體頻率
          //#define Fosc 6000000 //6MHz
          #define Fosc 11059200 //11.059MHz
          //#define Fosc 12000000
          //#define Fosc 18432000
          //#define Fosc 20000000
          //#define Fosc 24000000
          //#define Fosc 30000000
          //#define Fosc 40000000
          //選擇以下一個(gè)波特率:
          //#efine Baud 300 //11.059MHz時(shí),baud 最低為 300
          //#define Baud 1200
          //#define Baud 2400
          //#define Baud 4800
          #define Baud 9600
          //#define Baud 14400
          //#define Baud 19200
          //#define Baud 28800
          //#define Baud 38400
          //#define Baud 57600
          //收、發(fā)一位所需定時(shí)器計(jì)數(shù)
          #define rs_FULL_BIT0 ((Fosc/12) / Baud)
          #define rs_FULL_BIT (65536 - rs_FULL_BIT0)
          #define rs_FULL_BIT_H rs_FULL_BIT >> 8 //收、發(fā)一位所需定時(shí)器計(jì)數(shù)高位
          #define rs_FULL_BIT_L (rs_FULL_BIT & 0x00FF) //收、發(fā)一位所需定時(shí)器計(jì)數(shù)低位
          //檢測起始位的時(shí)間間隔所需定時(shí)器計(jì)數(shù)
          #define rs_TEST0 rs_FULL_BIT0 / 4 //波特率較低時(shí)可以除以 3 或除以 2
          #define rs_TEST ((~rs_TEST0))
          #define rs_TEST_H rs_TEST >> 8 //高位
          #define rs_TEST_L rs_TEST & 0x00FF //低位
          //發(fā)送起始位所需定時(shí)器總計(jì)數(shù)
          #define rs_START_BIT 0xFFFF - (Fosc/12/Baud) + 0x28
          #define rs_START_BIT_H rs_START_BIT >> 8 //發(fā)送起始位所需定時(shí)器計(jì)數(shù)高位
          #define rs_START_BIT_L rs_START_BIT & 0x00FF //發(fā)送起始位所需定時(shí)器計(jì)數(shù)低位
          #define rs_RECEIVE_MAX 32 //最大接收長度
          INT8U idata rs232buffer[rs_RECEIVE_MAX]; //收、發(fā)緩沖區(qū)
          INT16U ReceivePoint; //接收數(shù)據(jù)存儲(chǔ)指針
          void soft_rs232_interrupt( void );
          #ifdef TIMER_0
          void timer0 (void) interrupt 1 using 3
          {
          if (rs_RXD == 0 | rs_shift_count > 0)
          { soft_rs232_interrupt(); }
          else
          {
          rs_timerH = rs_TEST_H;
          rs_timerL = rs_TEST_L;
          }
          }
          #endif
          #ifdef TIMER_1
          void timer1 (void) interrupt 3 using 3
          {
          if (rs_RXD == 0 | rs_shift_count > 0)
          { soft_rs232_interrupt(); }
          else
          {
          rs_timerH = rs_TEST_H;
          rs_timerL = rs_TEST_L;
          }
          }
          #endif
          void soft_rs232_init (void) //串口初始化
          {
          TCON_ENABLE_TIMER = 0; //停止定時(shí)器
          TMOD &= TMOD_AND_WORD;
          TMOD |= TMOD_TIME_MODE;
          rs_RXD = 1; //接收腳置成高電平
          rs_TXD = 1; //發(fā)射腳置成高電平
          IP_PTx = 1; //置中斷優(yōu)先級(jí)為高
          IE_ETx = 1; //允許定時(shí)器中斷
          }
          void soft_receive_init() //監(jiān)測起始位
          {
          TCON_ENABLE_TIMER = 0; //停止定時(shí)器
          rs_timerH = rs_TEST_H;
          rs_timerL = rs_TEST_L;
          rs_shift_count = 0;
          TCON_ENABLE_TIMER = 1; //啟動(dòng)定時(shí)器
          }
          void soft_receive_enable() //允許接收
          {
          rs_f_RI_enable = 1; //允許接收
          rs_f_TI_enable = 0; //禁止發(fā)送
          soft_receive_init(); //監(jiān)測起始位, RXD 下降沿觸發(fā)接收字節(jié)過程.
          }
          void soft_send_enable (void) //允許發(fā)送
          {
          TCON_ENABLE_TIMER = 0; //停止定時(shí)器
          rs_f_TI_enable = 1; //允許發(fā)送
          rs_f_RI_enable = 0; //禁止接收
          rs_shift_count = 0; //清移位計(jì)數(shù)器
          rs_f_TI = 1; //發(fā)送一個(gè)字符完畢標(biāo)志
          TCON_ENABLE_TIMER = 1; //啟動(dòng)定時(shí)器
          }

          上一頁 1 2 下一頁

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉