Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483287
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: 嵌入式

2013-03-03 12:24:28

复习下之前学的

=============================================
    LED+继电器+蜂鸣器
=============================================


点击(此处)折叠或打开

  1. #include <REG52.H>
  2. #include <intrins.h>
  3. #include <personal.h> //个人定义头文件
  4. #define TIME 500

  5. sbit relay = P3^7;
  6. sbit bp = P3^6;

  7. void beep(u16 t)
  8. {
  9.     u16 i;
  10.     for(i=0; i<t; i++)
  11.     {
  12.         bp = 1;
  13.         delay500us();
  14.         bp = 0;
  15.         delay500us();
  16.     }
  17. }

  18. void main()
  19. {
  20.     u8 i = 1, aa, bb, cnt;
  21.     while(1)
  22.     {
  23.         relay = 0;
  24.         aa = 0x7F;
  25.         cnt = 0;
  26.         while(cnt < 8)
  27.         {
  28.             P1 = aa;
  29.             delay(TIME);
  30.             aa = _cror_(aa,1);
  31.             cnt++;
  32.         }
  33.         relay = 1;
  34.         P1 = 0xff;
  35.         bb = 0xfe;
  36.         cnt = 0;
  37.         beep(100);
  38.         while(cnt < 8)
  39.         {
  40.             P0 = bb;
  41.             delay(TIME);
  42.             bb = _crol_(bb,1);
  43.             cnt++;
  44.         }
  45.         P0 = 0xff;
  46.     }
  47. }

=============================================
    矩阵键盘
=============================================

点击(此处)折叠或打开

  1. #include <reg52.h>
  2. #include <personal.h>
  3. #include <intrins.h>

  4. u8 code a[4] = {0xe,0xd,0xb,0x7};
  5. u8 code CODE[16] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
  6.                     0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
  7.                    };


  8. void main()
  9. {
  10.     u8 f4, l4, i;
  11.     while(1)
  12.     {
  13.         P1 = 0xf0;
  14.         P2 = 0x7f;
  15.         P0 = 0xbf;
  16.         f4 = P1;
  17.         if((f4&0xf0)!=0xf0)
  18.         {
  19.             delay(30);
  20.             f4 = P1;
  21.             if((f4&0xf0)!=0xf0)
  22.             {
  23.                 P1 = 0x0f;
  24.                 l4 = P1;
  25.                 f4 = _cror_(f4, 4);
  26.                 for(i=0; i<4; i++)
  27.                 {
  28.                     if(f4 == a[i])
  29.                     {
  30.                         f4 = i;
  31.                     }
  32.                     if(l4 == a[i])
  33.                     {
  34.                         l4 = i;
  35.                     }
  36.                 }
  37.                 P0 = CODE[l4*4+f4];
  38.             }
  39.         }
  40.         else
  41.         {
  42.             P1 = 0xff;
  43.         }
  44.         delay(200);
  45.     }
  46. }

=============================================
    数码管
=============================================

点击(此处)折叠或打开

  1. #include <reg52.h>
  2. #include <personal.h>
  3. #include <intrins.h>

  4.         u8 code CODE[16] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
  5.                             0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
  6.                            };

  7. void main()
  8. {
  9.     u8 i = 0, minis_pins = 0xfe;
  10.     while(1)
  11.     {
  12.         P2 = minis_pins;
  13.         P0 = CODE[i];
  14.         delay(1000);
  15.         minis_pins = _crol_(minis_pins, 1);
  16.         i = (++i) % 16;
  17.     }
  18. }

=============================================
    外部中断+数码管计数
=============================================

点击(此处)折叠或打开

  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include <personal.h>

  4.         u8 code CODE[16] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
  5.                             0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
  6.                            };

  7. u8 a[8]= {0};
  8. u8 cntl = 0x7f, cnt=0;

  9. sbit key = P3^2;
  10. sbit test = P1^7;

  11. void disp()
  12. {
  13.     u8 i, j;
  14.     for(i=0; i<=cnt; i++)
  15.     {
  16.         j=cntl; //Attention
  17.         j = _cror_(j, i); //j changed
  18.         P0 = CODE[a[7-i]]; //Attention
  19.         P2 = j;
  20.         P2 = 0xff;

  21.     }
  22. }

  23. void add(u8 cur_index) reentrant
  24. {
  25.     if(a[cur_index] > 8)
  26.     {
  27.         a[cur_index] = 0;
  28.         if(cur_index == 7-cnt)
  29.         {
  30.             //Attention
  31.             cnt++;
  32.         }
  33.         add(cur_index-1);
  34.     }
  35.     else
  36.     {
  37.         a[cur_index]++;
  38.     }
  39. }

  40. void main()
  41. {
  42.     EA = 1;
  43.     EX0 = 1;
  44.     IT0 = 1;
  45.     P2 = cntl;
  46.     P0 = CODE[0];
  47.     while(1)
  48.     {
  49.         disp();
  50.     }
  51. }

  52. void intkey(void) interrupt 0
  53. {
  54.     if(!key)
  55.     {
  56.         delay(300);
  57.         while(!key)
  58.         {
  59.             ;
  60.         }
  61.         add(7);
  62.     }
  63. }

=======================================================
 串口通信
=======================================================

点击(此处)折叠或打开

  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include <personal.h>

  4. u8 code sdata[] = {'Y','A', 'N', 'G', 0x0d, 0x0a, 0x00};
  5. sbit K1 = P3^2;

  6. void send_char(u8 txd)
  7. {
  8.     SBUF = txd;
  9.     while(!TI) //等待数据传送完
  10.     {
  11.         ;
  12.     }
  13.     TI = 0;
  14. }

  15. void send_string()
  16. {
  17.     u8 i = 0;
  18.     while(sdata[i])
  19.     {
  20.         SBUF = sdata[i++];
  21.         while(!TI)
  22.         {
  23.             ;
  24.         }
  25.         TI = 0;

  26.     }
  27. }

  28. void query_ctrl_led_tr()
  29. {
  30.     u8 tmp;
  31.     TMOD = 0x20; //定时器1工作于8位自动重载模式,用于产生波特率
  32.     TH1 = TL1 = 0xfd; //波特率9600
  33.     SCON = 0x50; //串行工作方式为1,允许接收
  34.     PCON = 0x00; //波特率不倍增
  35.     TR1 = 1; //启动定时器

  36.     while(1)
  37.     {
  38.         if(RI == 1) //数据到达标志
  39.         {
  40.             RI = 0;
  41.             tmp = SBUF;
  42.             P0 = tmp;
  43.             send_char(tmp);
  44.         }
  45.     }
  46. }

  47. void query_ascii_t()
  48. {
  49.     u8 i;
  50.     SCON = 0x40;
  51.     PCON = 0x00;
  52.     TMOD = 0x20;
  53.     TH1 = TL1 = 0xfd;
  54.     TR1 = 1;

  55.     while(1)
  56.     {
  57.         i = 0;
  58.         while(sdata[i])
  59.         {
  60.             SBUF = sdata[i++];
  61.             while(!TI)
  62.             {
  63.                 ;
  64.             }
  65.             TI = 0;
  66.         }
  67.         delay(2000);
  68.     }
  69. }

  70. void itrpt_key_tr()
  71. {
  72.     SCON = 0x50;
  73.     TMOD = 0x20;
  74.     PCON = 0x00;
  75.     TH1 = TL1 = 0xfd;
  76.     EA = 1;
  77.     ES = 1;
  78.     TR1 = 1; //定时器1

  79.     while(1)
  80.     {
  81.         if(K1 == 0)
  82.         {
  83.             delay(20);
  84.             if(K1 == 0)
  85.             {
  86.                 send_string();
  87.             }
  88.             while(!K1);
  89.         }
  90.     }
  91. }

  92. void Int_Uart(void) interrupt 4
  93. {
  94.     u8 r = 0;
  95.     if(RI)
  96.     {
  97.         RI = 0;
  98.         r = SBUF;
  99.         P0 = SBUF;
  100.         send_char(r);
  101.     }
  102. }

  103. void main()
  104. {
  105.     //ctrl_led_tr();
  106.     //ascii_t();
  107.     itrpt_key_tr();
  108. }

2011-09-29 14:28 发表于百度空间,今搬至CU。 

阅读(2106) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~