微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 这个程序输出的PWM波形周期为什么不是2ms啊

这个程序输出的PWM波形周期为什么不是2ms啊

时间:10-02 整理:3721RD 点击:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar time,temp,num=1;
sbit PWM=P2^7;//PWM波形输出端口
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};        //共阳极数码管0~9编码
void delayms(uint xms)//短暂延时
{
  uint i,j;
  for(i=xms;i>0;i--)
      for(j=110;j>0;j--);
}
void display_pwm()//占空比显示
{
  uchar shi,ge;
  shi=num/10;
  ge=num%10;
  P3=0xbf;
  P0=table[shi];
  delayms(1);
  P3=0x7f;
  P0=table[ge];
  delayms(1);
}
void keyScan()
{
        P2=0xfe;
        temp=P2;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P2;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        temp=P2;
                        switch(temp)
                        {
                                case 0xee:
                                         TR0=1;
                                         break;
                                case 0xde:
                                     num=num+10;
                                      if(num>=100)
                                         num=0+num%10;
                                         break;
                        }
                        while(temp!=0xf0)
                        {
                                temp=P2;
                                temp=temp&0xf0;
                        }
                }
        }
        P2=0xfd;
        temp=P2;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P2;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        temp=P2;
                        switch(temp)
                        {
                                case 0xed:
                                    num++;        
                                     if(num==100)
                                    num=0;
                                        break;
                                case 0xdd:
                                    TR0=0;
                                        break;
                                       
                        }
                        while(temp!=0xf0)
                        {
                                temp=P2;
                                temp=temp&0xf0;
                        }
                }
        }
}
void main()
{
  TMOD=0x01;  
  TH0=(65536-20)/256;
  TL0=(65536-20)%256;
  EA=1;
  ET0=1;
  TR0=0;
  while(1)
  {        
         keyScan();
        
            display_pwm();
  }
}
void T0_time()interrupt 1
{
        TH0=(65536-20)/256;
        TL0=(65536-20)%256;
         time++;
        if(time==100)
                time=0;
         if((num!=0)&&(num!=100))
         {   if(time<=num)
                       PWM=1;
             else
                       PWM=0;
          }
     else if(num==0)
                   PWM=1;
         else if(num==100)
                   PWM=0;
}


程序没错就是晶振问题啦  这程序是12MHz来写的

程序只是近似,他在比较时还要占用很多句语句时间,所以c语言没有偏绘这么准就是这个原因,他私下会转为很多语句,这里占用了一些时间,

可以改的准一点吗?要怎么改呢?

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

网站地图

Top