找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3611|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

[求助][原創(chuàng)]程序?qū)崿F(xiàn)PWM的占空比并且實(shí)現(xiàn)一小時(shí)內(nèi)的可控定時(shí)工功能

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:44588 發(fā)表于 2012-9-11 20:34 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

 程序大致可以實(shí)現(xiàn)定時(shí)刻PWM輸出功能,但有一個(gè)缺點(diǎn):輸出的波形不穩(wěn)定,在數(shù)碼管上顯示是不是出現(xiàn)跳碼。跪求大神指點(diǎn)。

#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P2^1;
sbit alarm=P1^7; //高電平時(shí)鬧鐘響
sbit selecta=P2^4;
sbit selectb=P2^5;
sbit selectc=P2^6;
sbit select=P2^7; //只需要第一段,第二段和第六段亮,即
uchar Keystate;
uchar buffer[]={0,0,0,0,0};
uint a,b;
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共陰數(shù)碼管代碼
uchar Overflow=0;//定時(shí)溢出次數(shù)
uchar minute,second;//定義倒計(jì)時(shí)變量
//ms延時(shí)函數(shù)
void delayms(uint x)
{
 uint i;
 while(x--) for(i=0;i<1000;i++);
}
void delay(uint x)
{
uchar j;
 while(x--)for(j=0;j<5;j++);
}
//數(shù)碼管顯示函數(shù)
   void display()
 {
  uchar s,k;
  buffer[0]=minute/10;
  buffer[1]=minute%10;
  buffer[2]=second/10;
  buffer[3]=second%10;
  buffer[4]=b%10;
  for(k=0;k<5;k++)
  {
   s=k;
    switch(k)
  {
   case 0:selecta=0;selectb=1;selectc=0;
           break;
   case 1: selecta=1;selectb=1;selectc=0;
         break;
   case 2: selecta=0;selectb=0;selectc=1;
         break;
   case 3: selecta=1;selectb=0;selectc=1;
         break;
   case 4:selecta=1;selectb=1;selectc=1;
         break;
   }
  P0=table[buffer];
  delay(3);
  }
 }//要實(shí)現(xiàn)動(dòng)態(tài)顯示
//本程序需要用到兩個(gè)定時(shí)器,兩個(gè)中斷
void KeyScan();
void main()
{
 P2=0x00;
 a=0;
 b=5;
 minute=30;
 second=0;
 RD=0;
 select=1;
 TMOD=0x11;
 TH0=(65536-10)/256;
 TL0=(65536-10)%256;
 EA=1;
 IT1=1; IT0=1; 
 EX1=1; EX0=1;
 PX1=1;
 KeyScan();
 TR1=1;
 ET1=1;
 while(1)
 {
  display();
  delay(10);
 }
 }
 void X1_INT()interrupt 2
{
if(b>0) b--;
else b=9;
}
void X0_INT()interrupt 0
{
b++;
if(b>9) b=0;
}
 void time1() interrupt 3
 {
   TL1=(65536-10)%256;
   TH1=(65536-10)/256;
   a++;
   if(a==10) a=0;
   if(a<b) PWM=1;
   else PWM=0;
  }
void timer0start()//定時(shí)器初始化函數(shù)
{
 TH0=(65536-50000)/256;  //定時(shí)器定時(shí)為50ms,晶振的周期為12MHz
 TL0=(65536-50000)%256;
 ET0=1;  //定時(shí)器1中斷開
 TR0=1; //定時(shí)器1開
}
 //鬧鈴函數(shù) 需要輸出聲音悅耳
  void beef()
  {
  uchar x=100;
   while(x--)
  {
   alarm=!alarm;
   delayms(10);
   }
   alarm=0;
   }
  //定時(shí)器0中斷函數(shù)程序
void time0()interrupt 1
{
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 if(Overflow==20)
  {
  Overflow=0;
  if(second==0)
   {
   if(minute==0)
   {
   beef();
   minute=0;
   second=0;
   while(1);
     }
   else minute--;
   second=59; 
   }
  else second--;
  }
 else Overflow++;
  } 
  //按鍵掃描函數(shù)
 void KeyScan() 
{
 uint x=1000; 
  while(x--)

  P1=0xff;
 Keystate=P1&0x03;
 display();
 delay(50);
 if(Keystate!=0x03)
 { 
    delayms(2);  //去抖
   if(Keystate!=0x03)//去抖后再確認(rèn)一下
 {
    delayms(1);
     switch(Keystate)
  {
     case 0x01:minute++;
               if(minute==60)minute=0;
               break;
     case 0x02:minute--;
              if(minute==0)minute=59;
              break;
     default:break;
  }
  while(P1!=0xff)display();    //松手檢測(cè)并保證可以從循環(huán)中出來(lái)
  }  
     }
 }
 timer0start();
 } 

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表