微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > pwm控制的流水灯!很不错

pwm控制的流水灯!很不错

时间:10-02 整理:3721RD 点击:

  1. #include
  2. #include
  3. #define LED_PORT P1
  4. #define TIMER_BEGIN  0xe333;  //4ms
  5. const int inc_timer_count=0x001d;
  6. unsigned char code game[16]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x18,0x24,0x42,0x81,0x03,0x0c,0x30,0xc0};
  7. volatile unsigned int pre_time_set=0;
  8. void init_timer0();
  9. void modi_timer0();
  10. void main()
  11. {
  12.   init_timer0();
  13.   while (1);
  14. }

  15. void init_timer0()
  16. {
  17.    TR0=0;
  18.    EA=0;
  19.    TMOD=0x01;
  20.    TL0=0x33;
  21.    TH0=0XE3;
  22.       
  23.    ET0=1;
  24.    EA=1;
  25.    TR0=1;
  26. }
  27. void modi_timer0()   
  28. { static bit c_state=0;//当前状态
  29.   static unsigned int proc_count=0;  //处理次数
  30.   static unsigned int timer0_high=0;
  31.   static unsigned int timer0_low=0;
  32.   static unsigned char cur_proc_led=0;
  33.   TR0=0;
  34.   if (!proc_count)
  35.   {
  36.     timer0_high=TIMER_BEGIN;
  37.   timer0_low=0x0ffff;
  38.   }
  39.   if (c_state)  //高电平
  40.   {
  41.     timer0_high=timer0_high+inc_timer_count;
  42.     TH0=(timer0_high )>>8;
  43.   TL0=(timer0_high)&0x00ff;
  44.   LED_PORT=0xff;
  45.   }
  46.   else
  47.   {
  48.     timer0_low=timer0_low-inc_timer_count;
  49.     TH0=(timer0_low )>>8;
  50.   TL0=(timer0_low)&0x00ff;
  51.   LED_PORT=game[cur_proc_led];
  52.   }
  53.   TR0=1;
  54.   c_state=~c_state;
  55.   if (++proc_count==500)
  56.     {  proc_count=0;
  57.        if (++cur_proc_led==16)
  58.         cur_proc_led=0;
  59.   }
  60. }

  61. void time0(void ) interrupt 1 using 1//定时器T0中断。
  62. {
  63.    modi_timer0();
  64. }

复制代码

具有亮度控制功能!
PWM亮度控制!


很好,很强大

很好,牛x呀

pwm 也能这样玩,学习

我也想学习一下昂。多谢了。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top