標(biāo)題: 51單片機(jī)A/D轉(zhuǎn)換及串行通信程序 [打印本頁(yè)]

作者: 1130718845    時(shí)間: 2022-6-9 15:13
標(biāo)題: 51單片機(jī)A/D轉(zhuǎn)換及串行通信程序
要求:
1.硬件選擇通道,轉(zhuǎn)換結(jié)果顯示在LED上
2.利用串口助手與單片機(jī)就行串行通信,收到大寫字母轉(zhuǎn)換成小寫字母發(fā)回,反之亦然,但數(shù)字不變。

單片機(jī)源程序如下:
  1. #include <reg51.h>
  2. #include <stdio.h>
  3. #include <intrins.h>
  4. #include <absacc.h>
  5. #define uchar unsigned char
  6. #define uint unsigned int
  7.         
  8. uchar D,VAR,H4,L4;

  9. uchar cod[] = {0xC0, 0xF9, 0xA4, 0xB0,
  10.                                  0x99, 0x92, 0x82, 0xF8,
  11.                                  0x80, 0x90, 0x88, 0x83,
  12.                                  0xC6, 0xA1, 0x86, 0x8E
  13.                                  }; //16進(jìn)制
  14.                                                                                                                                                   
  15. void delay(int n)
  16. {
  17.         int i,j;
  18.         for(i=n;i>0;i--)
  19.         {
  20.                 for(j=28;j>0;j--);
  21.         }
  22. }

  23. void display()  
  24. {
  25.          H4=D/16;
  26.          L4=D%16;
  27.         
  28.          P2=cod[L4];
  29.          P1=0xfe;
  30.          delay(1);
  31.          P2=0xff;
  32.          delay(10);
  33.         
  34.          P2=cod[H4];
  35.          P1=0xfd;
  36.          delay(1);
  37.          P2=0xff;
  38.          delay(10);
  39. }

  40. void main()
  41. {
  42.         while(1)
  43.         {
  44.                 XBYTE[0x8000]=0x00;
  45.                 delay(50);
  46.                 D=XBYTE[0x8000];
  47.                 display();
  48.         }
  49.                
  50. }
復(fù)制代碼
  1. #include <reg51.h>
  2. #include <stdio.h>
  3. #include <intrins.h>
  4. #include <absacc.h>
  5. #define uchar unsigned char
  6. #define uint unsigned int
  7.         
  8. uchar D,VAR,H4,L4;

  9. uchar cod[] = {0xC0, 0xF9, 0xA4, 0xB0,
  10.                          0x99, 0x92, 0x82, 0xF8,
  11.                          0x80, 0x90, 0x88, 0x83,
  12.                          0xC6, 0xA1, 0x86, 0x8E
  13.                           }; //16進(jìn)制
  14.                                                                                                                                                   
  15. void delay(int n)
  16. {
  17.         int i,j;
  18.         for(i=n;i>0;i--)
  19.         {
  20.                 for(j=28;j>0;j--);
  21.         }
  22. }

  23. void display()  
  24. {
  25.          H4=D/16;
  26.          L4=D%16;
  27.          P2=cod[L4];
  28.          P1=0xfe;
  29.          delay(1);
  30.          P2=0xff;
  31.          delay(10);
  32.          P2=cod[H4];
  33.          P1=0xfd;
  34.          delay(1);
  35.          P2=0xff;
  36.          delay(10);
  37. }


  38. void main()
  39. {
  40.         PCON=0x80;//SMOD置1,波特率加倍
  41.         SCON=0x50;//設(shè)置為工作方式1:SCON:SMO SM1 SM2 REN TB8 RB8 TI(0101 0000)
  42.         TMOD=0x20;//設(shè)置計(jì)數(shù)器工作方式2
  43.         TH1=0xfa;//計(jì)數(shù)器賦初值
  44.         TL1=0xfa;
  45.         TR1=1;//打開(kāi)計(jì)數(shù)器
  46.         
  47.         while(1)
  48.         {
  49.                 while(!RI){display();}
  50.                 D=SBUF;
  51.                 VAR=D;
  52.                 RI=0;//清除接收中斷標(biāo)志位
  53.                 display();
  54.                 if(0x40<D&&D<0x5b){VAR=D+0x20;}//大寫變小寫
  55.                 else if(0x60<D&&D<0x7b){VAR=D-0x20;}//小寫變大寫
  56.                 SBUF=VAR;
  57.                 while(!TI){display();}
  58.                 TI=0;//清除發(fā)送完成標(biāo)志位
  59.         }
  60.                
  61. }
復(fù)制代碼





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