标题: AVR单片机pwm程序+仿真_数显 [打印本页]

作者: jinsheng7533967    时间: 2018-12-16 16:09
标题: AVR单片机pwm程序+仿真_数显


  1. #include <iom16v.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int

  4. uchar Table[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x79};
  5. uchar Data1[4]={10,0,5,0};
  6. uchar Data2[4]={3,9,1,0};
  7. uchar Key1,Key2;

  8. void DelayMs(uchar i)
  9. {
  10.         uchar j;
  11.         for(;i!=0;i--)
  12.         {
  13.                 for(j=150;j!=0;j--);
  14.         }
  15. }

  16. void Display(uchar *p)
  17. {
  18.         uchar i,sel=0x01;
  19.         for(i=0;i<4;i++)
  20.         {
  21.                 PORTC=sel;
  22.                 PORTA=0xff-Table[p[i]];
  23.                 DelayMs(2);
  24.                 sel=sel<<1;
  25.         }
  26. }

  27. void Sub_Process(uchar *p)
  28. {
  29.         uchar temp;
  30.         temp=p[1]*10+p[2];
  31.         if(temp!=0)
  32.         {
  33.                 temp=temp-1;
  34.                 p[1]=temp/10;
  35.                 p[2]=temp%10;
  36.         }
  37. }
  38. void Key_Process()
  39. {

  40.         if(Key1==1)
  41.         {
  42.                 Add_Process(Data1);
  43.                 Key1=0;
  44.         }
  45.         if(Key2==1)
  46.         {
  47.                 Sub_Process(Data1);
  48.                 Key2=0;
  49.         }
  50. }

  51. void Init_IO(void)
  52. {
  53.         DDRA=0xff;
  54.         PORTA=0xff;
  55.         DDRC=0xff;
  56.         PORTC=0xff;
  57.         DDRB=0x00;
  58.         PORTB=0x00;
  59.         DDRD=0xff;
  60.         PORTD=0xff;
  61. }
  62. void main(void)
  63. {
  64.         Init_IO();
  65.         TCCR1A=0xc3;
  66.         TCCR1B=0x02;
  67.         DelayMs(50);
  68.         while(1)
  69.         {
  70.                 Key_Process();
  71.                 Set_Process(Data1);
  72.                 Display(Data1);
  73.         }
  74. }
复制代码

完整代码见附件

pwm_数显.zip (15.36 KB, 下载次数: 52)


作者: zrb5688    时间: 2018-12-26 17:52
不错,学习了,谢谢楼主分享。

作者: dhutcb    时间: 2019-5-30 10:20
赞一个,不错
作者: yaolong986    时间: 2020-11-24 15:36
不错,收藏了有用这芯片




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1