本文主要是介绍51单片机使用中断输出不同频率的方波https://bbs.csdn.net/topics/392659369,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include "reg52.h"
#include<intrins.h>
//#include "math.h"
sbit QiTin = P3^0;//启停按键
sbit duan = P2^6;//数码段
sbit wei = P2^7;//数码位
sbit NanBeiHong = P1^0;
sbit NanBeiHuan = P1^1;
sbit NanBeiLu = P1^2;
//sbit DongXiHong = P1^5;
//sbit DongXiHuan = P1^6;
//sbit DongXiLu = P1^7;
bit k=0, d=0;//中断标记//红绿灯倒换标记
unsigned char code ShuMaGuan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0X80};//0~9隐.小数点
unsigned char code WeiXuanBiao[] = { 0x00,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f };
//unsigned char code ShaoMiaoZhi[]={238,237,235,231,222,221,219,215,190,189,187,183,126,125,123,119};
unsigned char Js=0;//中断计时
unsigned char Hm=0;//毫秒
unsigned char Mo=0;//秒
//unsigned char Dj=0;//倒计时
void ShuMaXianShi(unsigned char s,unsigned char w,unsigned char d)
{unsigned char YanShi=255;P0=WeiXuanBiao[w];wei=1;wei=0;P0=ShuMaGuan[s]|ShuMaGuan[d];//d显示小数点11,10关闭duan=1;while(YanShi--);duan=0;
}
void ZhengShuXianShi(unsigned char wei,unsigned long i/*整数拆分到数码管显示*/)
{do{ShuMaXianShi(i % 10,wei--,10);}while(i/=10);
}
void ZhongDuanSheZhi()
{TH0+=0X4c;/*定时器赋初值,定时50ms触发中断,自动补偿方式*/TL0+=0X00; TMOD=0X01;TR0=1; ET0=1; //开启定时器EA=1; //全局中断开关
}
void XianShi()
{
// ShuMaXianShi((55-Mo)/10,1,10);
// ShuMaXianShi((55-Mo)%10,2,11);//红绿灯转换秒数预设为55秒
// ShuMaXianShi(Hm,3,10);
// ShuMaXianShi(Dj/10%10,4,10);
// ShuMaXianShi(Dj%10,5,10);
// ShuMaXianShi((55-Mo)/10,6,10);
// ShuMaXianShi((55-Mo)%10,7,11);
// ShuMaXianShi(Hm,8,10);
}
void JiShi(/*时间计算*/)
{
// if(Js%2)
// if(!k&&++Hm>=10) Hm=0;
// if(Js>=20)
// {
// if(++Mo>=60) Mo=0;
// Js=0;
// }
}
unsigned char a=0,b=0,c=0;
void FanBo(/*控制输出不同频率方波*/)
{if(++a>=2){a=0;NanBeiHuan=!NanBeiHuan;}if(++b>=4){Js=b=0;NanBeiLu=!NanBeiLu;}if(++c>=6){c=0;NanBeiHong=!NanBeiHong;}//if(c>=2/*调节占空比1~5之间*/)NanBeiHong=0;else NanBeiHong=1;
}
void ZhongDuanAnNiu()
{if(!QiTin){//XianShi(/*巧借8个数码显示消抖*/);unsigned char Yan_Shi=Js;if(!TR0)TR0=k=1;//重启中断while(Js==Yan_Shi)XianShi();//中断消抖if(!QiTin)if(!k)TR0=k; else k=0;//关闭中断,消抖后清除标记while(!QiTin) XianShi(/*按下期间显示*/);}
}
unsigned char JianPanShaoMiao(/*使用行列反转扫描法*/)
{unsigned char H=15,L=240;P3=15;if(P3!=15){unsigned char Yan_Shi=Js;while(Js==Yan_Shi);//中断消抖H=P3;P3=240;L=P3;while(P3!=240)k=1;}P3 = 255;return (H+L);
}
unsigned char JianPanQuZhi(/*定义按钮*/)
{
// unsigned char Jian_Zhi=0,LinShi=JianPanShaoMiao();
// while(LinShi!=ShaoMiaoZhi[Jian_Zhi])if(++Jian_Zhi > 15)break;
// return (Jian_Zhi > 15 ? LinShi : Jian_Zhi);
}
void main()
{unsigned char JZhi=0;unsigned int YS=54000,a=55555;
// unsigned long aa=0;
// aa=aa+YS+a;ZhongDuanSheZhi();while(1){// ZhengShuXianShi(8,aa);
// YS=54000;while(YS--);
// P1=(k?255:0);k=!k;//南北红灯//东西绿灯//南北显示时间,东西显示时间雷同不重写了
// if(Mo==0&&Mo<=50){NanBeiHong=DongXiLu=d;DongXiHong=NanBeiLu=!d;}
// if(Mo>=50&&Mo<=53)//闪烁
// {
// if(Hm>=5)
// {
// NanBeiHong=DongXiLu=d;
// DongXiHong=NanBeiLu=!d;
// }
// if(Hm==0)
// {
// NanBeiHong=DongXiLu=!d;
// DongXiHong=NanBeiLu=d;
// }
// }
// if(Mo>=53&&Mo<=55)
// {
// NanBeiHuan=DongXiHuan=0;//南北黄灯//东西黄灯
// NanBeiHong=DongXiLu=DongXiHong=NanBeiLu=1;//南北东西红灯绿灯
// }
// if(Mo>=55){Mo=Hm=Js=0;d=!d;NanBeiHuan=DongXiHuan=1;}//初始化各时间
// if(!k){JZhi=JianPanQuZhi(/*16路抢答器中断式控制*/);JiShi();}
// Dj=k?JZhi+1:59-Mo;
// if(k)Mo=Hm=Js=0;
// if(Mo>=59){TR0=0;k=1;Mo=Hm=Js=0;}
// XianShi();
// ZhongDuanAnNiu();}
}
void AnNiu()
{
// if(!QiTin)
// {
// unsigned char Yan_Shi=Js;
// if(!TR0)TR0=k=1;//重启中断
// while(Js==Yan_Shi);//中断消抖
// if(!QiTin&&!k){TR0=k;P1 = d;}//关闭中断
// else k=0;//消抖后清除标记
// while(!QiTin);
// }
}
void YanShi()
{
// unsigned char a=5,b=126,c=200;
// while(a--)while(b--)while(c--)if(!QiTin)break;
}
void main1()
{
// unsigned char d=0;
// ZhongDuanSheZhi();TR0=0;
// P1 = 254;
// while(1)
// {
// if(!TR0)
// {
// d=P1 = _crol_(P1,1);
// YanShi();
// }
// else
// {
// JiShi();
// P1 = !(Mo%2)?0:255;
// if(Mo>=10)//闪烁5次
// {
// Js=Mo=TR0=0;
// P1 = d;
// }
// }
// AnNiu();
// }
}
void ZhongDuan() interrupt 1
{++Js;FanBo();TH0+=0X4c;TL0+=0X00;
}
这篇关于51单片机使用中断输出不同频率的方波https://bbs.csdn.net/topics/392659369的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!