找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索

51單片機(jī)串口程序C語(yǔ)言版

查看數(shù): 4242 | 評(píng)論數(shù): 1 | 收藏 5
關(guān)燈 | 提示:支持鍵盤翻頁(yè)<-左 右->
    組圖打開中,請(qǐng)稍候......
發(fā)布時(shí)間: 2016-10-11 23:05

正文摘要:

這篇文章主要來(lái)介紹一下 51單片機(jī)的串口,下面先來(lái)看一下與串口相關(guān)的寄存器。         (因?yàn)槌绦蛘迟N過(guò)來(lái)的時(shí)候注釋會(huì)顯示亂碼,沒辦法只好使用截圖,請(qǐng)大家將就著看一下,還有需要提醒的是程序的 ...

回復(fù)

ID:140725 發(fā)表于 2016-10-11 23:10
  1. #include <reg52.h>
  2. #include<intrins.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #define uchar unsigned char
  6. #define uint unsigned int
  7. sbit Key1 = P2^3;
  8. sbit Key2 = P2^2;
  9. sbit Key3 = P2^1;
  10. sbit Key4 = P2^0;
  11. sbit BELL = P3^6;
  12. sbit CONNECT = P3^7;
  13. unsigned int  Key1_flag = 0;
  14. unsigned int  Key2_flag = 0;
  15. unsigned int  Key3_flag = 0;
  16. unsigned int  Key4_flag = 0;
  17. unsigned char b;
  18. unsigned char code Num[21]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
  19.                             0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,
  20.                             0x10,0x89};
  21. unsigned char code Disdigit[4] = {0x7F,0xBF,0xDF,0xEF};
  22. unsigned char Disbuf[4];
  23. void delayms(uint t)
  24. {
  25. uint i;
  26. while(t--)
  27. {
  28.   /* 對(duì)于11.0592M時(shí)鐘,約延時(shí)1ms */
  29.   for (i=0;i<125;i++)
  30.   {}
  31. }
  32. }

  33. //-----------------------------------------------------
  34. void SendData(uchar Dat)
  35. {
  36. uchar i=0;
  37. SBUF = Dat;
  38. while (1)
  39.   {
  40.     if(TI)
  41.       {
  42.         TI=0;
  43.         break;
  44.    }
  45.   }
  46. }
  47. void ScanKey()
  48. {
  49.     if(Key1 == 0)
  50.     {
  51.        delayms(100);
  52.     if(Key1 == 0)
  53.     {
  54.        Key1_flag = 1;
  55.        Key2_flag = 0;
  56.     Key3_flag = 0;
  57.     Key4_flag = 0;
  58.        Key1 = 1;
  59.     }
  60.     else;
  61.     }
  62.     if(Key2 == 0)
  63.     {
  64.     delayms(100);
  65.     if(Key2 == 0)
  66.     {
  67.        Key2_flag = 1;
  68.        Key1_flag = 0;
  69.     Key3_flag = 0;
  70.     Key4_flag = 0;
  71.     Key2 = 1;
  72.     }
  73.     else;
  74.     }
  75.    if(Key3 == 0)
  76.     {
  77.     delayms(50);
  78.     if(Key3 == 0)
  79.     {
  80.        Key3_flag = 1;
  81.        Key1_flag = 0;
  82.     Key2_flag = 0;
  83.     Key4_flag = 0;
  84.     Key3 = 1;
  85.     }
  86.     else;
  87.     }
  88. if(Key4 == 0)
  89.     {
  90.     delayms(50);
  91.     if(Key4 == 0)
  92.     {
  93.        Key4_flag = 1;
  94.        Key1_flag = 0;
  95.     Key2_flag = 0;
  96.     Key3_flag = 0;
  97.     Key4 = 1;
  98.     }
  99.     else;
  100.     }
  101. else;  
  102. }
  103. void KeyProc()
  104. {
  105.     if(Key1_flag)
  106.     {
  107.      TR1 = 1;
  108.   SendData(0x55);
  109.      Key1_flag = 0;     
  110. }   
  111. else if(Key2_flag)
  112.     {
  113.      TR1 = 1;
  114.      SendData(0x11);   
  115.      Key2_flag = 0;  
  116.     }
  117.     else if(Key3_flag)
  118.     {
  119.     P1=0xff;
  120.     BELL = 0;
  121. CONNECT = 1;
  122.     Key3_flag = 0;
  123.     }
  124. else if(Key4_flag)
  125. {
  126. CONNECT = 0;
  127. BELL = 1;
  128.     Key4_flag = 0;
  129. }
  130.     else;      
  131. }
  132. void Initdisplay(void)
  133. {
  134. Disbuf[0] = 1;
  135. Disbuf[1] = 2;
  136. Disbuf[2] = 3;
  137. Disbuf[3] = 4;
  138. }
  139. void Display()     //顯示
  140. {
  141. unsigned int     i = 0;
  142. unsigned int temp,count;
  143.         temp = Disdigit[count];   
  144.   P2 =temp;
  145.   temp = Disbuf[count];
  146.   temp = Num[temp];
  147.         P0 =temp;
  148.         count++;      
  149.     if  (count==4)
  150.         count=0;        
  151. }   

  152. void time0() interrupt 1  using 2
  153. {  
  154.    Display();   
  155.   TH0 = (65535 - 2000)/256;
  156.   TL0 = (65535 - 2000)%256;
  157. }
  158.   
  159. void main()
  160. {
  161. Initdisplay();
  162. TMOD = 0x21;
  163. TH0 = (65535 - 2000)/256;
  164. TL0 = (65535 - 2000)%256;
  165. TR0 = 1;
  166. ET0 = 1;  
  167. TH1 = 0xFD; //11.0592M
  168. TL1 = 0xFD;
  169. PCON&=0x80;
  170. TR1 = 1;
  171. ET1 = 1;
  172. SCON = 0x40; //串口方式
  173. REN = 1;
  174. PT1 = 0;
  175. PT0 = 1;
  176. EA = 1;
  177.   while(1)
  178.   {
  179.    ScanKey();
  180.    KeyProc();     
  181.    
  182.    if(RI)
  183.    {
  184.    Disbuf[0] = 0;
  185.    Disbuf[1] = 20;
  186.    Disbuf[2] = SBUF>>4;
  187.    Disbuf[3] = SBUF&0x0f;
  188.    RI = 0;
  189.    }
  190.    else;   
  191.   }
  192. }
復(fù)制代碼

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表