单片机C语言中多个延时程序同时使用怎样编写?

单片机C语言中多个延时程序同时使用怎样编写?,第1张

int
x;
bit
led1,led2;
void
intter_0()
intterupt
0
{
led1
=
1;
}
void
timer0()
intterupt
1
{++x}
void
intter_2()
intterupt
2
{
led2
=
1;
}
void
main()
{
x
=
0;
TH0
=
xxxx;
TL0
=
xxxx;
TR0
=
1;
while(1)
{
if(led1
==
1)
{
if(x%十秒
==0)
{
p10
=
1;
led1
=
0;
}
}
if(led2
==
1)
{
if(x%十秒
==0)
{
p11
=
1;
led2
=0;
}
}
}
}
类似这样
主要用定时器取时间
用两个位来判断是否按下
单片中断什么的没初始化
自己设置

#include<AT89X51>
unsigned char sx[]={
oxfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
oxfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x01},
ml[]={0xff,0x00, 0xff,0x00, 0xff,0x00,
0xff,0x00, 0xff,0x00, 0x01};
unsigned int a=0,b=0;
del1ms(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
for(j=0;j<100;j++);
}
main()
{
while(1)
{
if(sx[]!=0x01)
{
P0=sx[a];
del1ms(20);
a++;
}
else
a=0;
if(lm[]!=0x01)
{
P0=lm[b];
del1ms(30);
b++;
}
else
b=0;
}
}
说明一下: 这样延时太短了,基本上看不出
对于定义来说:变量只是一个标识符而已,随便设,当变量多 的时候最好用带意思如:sec stu 等

#include<reg52h>
#define uchar unsigned char
#define uint unsigned int
sbit led_1s=P1^1;
sbit led_1min=P1^2;
uint flag1=0,flag2=0;
void main()
{
TMOD=0X00; //设置工作方式
TH0=(8192-4670)/32;//装初值 晶振频率为110952MHz 5ms
TL0=(8192-4670)/32;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1; //启动定时器0
while(1)
{
if(flag1==200)
{
led_1s=~led_1s;
flag1=0;
flag2++;
if(flag2==60)
{
led_1min=~led_1min;
flag2=0;
}
}
}
}
/
5ms定时
/
void T0_time() interrupt 1
{
TH0=(8192-4670)/32;//重装初值
TL0=(8192-4670)/32;
flag1++;
}
刚写的,亲测可用

定时器都是在中断方式下使用,多个定时器同时使用没有任何问题。使用它们时各有各的中断地址,互不影响。要用3个定时器,要看选的单片机有没有那么多,一般的最多两个。高档一些的才有3个以上的定时器。

这个可以吗?肯定要改下
//
//实验六 数码管电子钟
//by 阿朱 893437703@qqcom 转载请注明来源
//通电后,数码管电子钟一直处于秒表状态,蜂鸣5秒响一次,要求使用T0,T1中断
//与一般的应用不同,本实验在数码管前面未使用译码器
//思路:定时器中断的使用:
//1、初始化
//注意:P0口的P04接蜂鸣
//
//
//数码管数据 p2口,高电平有效
//数码管控制p00~P03,低电平有效
//P04输出,接蜂鸣器
//P1输入,接矩阵键盘 矩阵键盘定义: P10-P13为列线,P14-P17为行线
//
//
//本节知识要点:
//中断
//
#include <AT89X52H>
//宏定义
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long int
/
TH0和TL0是计数器0的高8位和低8位计数器,计算办法:TL0=(65536-C)%256;
TH0=(65536-C)/256,其中C为所要计数的次数即多长时间产生一次中断;TMOD是计数器
工作模式选择,0X01表示选用模式1,它有16位计数器,最大计数脉冲为65536,最长时
间为1ms65536=65536ms
/
#define V_TMOD 0x01 //工作方式1
#define V_TH0 0x3C //50ms延时常数 C=50000 //0XDC
#define V_TL0 0xB0 //50ms延时常数 C=50000 /0X58
//#define V_TH1 0xFF //5ms延时常数 C=5000 //0XDC
//#define V_TL1 0xFB //5ms延时常数 C=5000 /0X58
#define V_TH1 0xDC //1ms延时常数 C=1000 //0XDC
#define V_TL1 0x58 //1ms延时常数 C=1000 /0X58
#define MAXFUN 6 //功能切换,表示最多的功能状态,
sbit k10=P1^0;
//sbit BEEP = P3^7; //蜂鸣器驱动线----------------请修改为sbit BEEP = P0^4;
uchar bee; //蜂鸣器01开关
uchar key; //键顺序吗
uchar fun=10; //功能状态, <= MAXFUN
uchar it0=0,it1; //Timer0中断计数
uchar text=0; //数字
//uchar text_ctrl[4]={0xFE,0xFD,0xFB,0xF7}; //位选通值, 11111110, 11111101, 11111011, 11110111
uchar text_ctrl[4]={0xE,0xD,0xB,0x7}; //位选通值, 00001110, 00001101, 00001011, 00000111
//uchar text_code[11]={ 0x28, 0x7E, 0xA2, 0x62, 0x74, 0x61, 0x21, 0x7A, 0x20, 0x60,0xff};//0,1,2,3,4,5,6,7,8,9,关显示,数码管码表,高电平有效
//uchar text_code[17]= {0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60,0x30,0x25,0xa9,0x26,0xa1,0xb1};//数码管代码
uchar text_code[11]={ 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6,0xff};//0,1,2,3,4,5,6,7,8,9,关显示,数码管码表,高电平有效
uchar text_index=0; //当前显示第几个
uchar dis_buf[4]; //显示缓存
uchar refresh=0; //刷新否 T1蜂鸣
uchar min=0; //minutes
uchar sec=0; //scconds
uchar hour=0; //scconds
uchar onsetup=0;
uchar keydown0=0,keydown1=0;
uchar data PWM=0xFf ; //PWM值增加,则占空比减小,LED 灯渐暗。
//
// 延时子程序
//
void delay0(uchar x) //x014MS
{
uchar i;
while(x--)
for (i = 0; i<13; i++) {}
}
//
//T1蜂鸣
//
void beepT1()
{
if(refresh >0 )
{
refresh++;
bee=!bee;
if(refresh>=30)
refresh = 0;
}
}
//
//文字输出
//
void textout()
{
//P0 = 0xff; // 先关闭所有数码管
P2 = dis_buf[text_index]; //传入数字的码
bee = bee<<4;
P0 = text_ctrl[text_index]|bee; //选择位
//P0 = bee; //选择位
text_index++; //下一位
if( text_index >=4 )
text_index = 0;
}
//
//键扫描子程序
//
void keyscan(void)
{
uchar temp;
key = -1; //不按键
P1=0x0F; //低四位输入
delay0(12);
temp=P1; //读P1口
temp=temp&0x0F;
temp=~(temp|0xF0);
if(temp==1)
key=0;
else if(temp==2)
key=1;
else if(temp==4)
key=2;
else if(temp==8)
key=3;
else
key=16;

P1=0xF0; //高四位输入
delay0(12);
temp=P1; //读P1口
temp=temp&0xF0;
temp=~((temp>>4)|0xF0);
if(temp==1)
key=key+0;
else if(temp==2)
key=key+4;
else if(temp==4)
key=key+8;
else if(temp==8)
key=key+12;
else
key=16;

if( key!= -1)
fun = key;
//key =0;
//dis_buf=text_code[key]; //查表得键值
}
//
//判断键是否按下
//
int keydown(void)
{
P1=0xF0;
if(P1!=0xF0)
{
keyscan();
//delay0(250);
return 1;
//
//beep();
// while(P1!=0xF0); //等待键释放
}
return 0;
}
//
//设置显示缓存
//
void settext(uchar text0,uchar text1,uchar text2,uchar text3)
{
dis_buf[0]=text_code[text0];
dis_buf[1]=text_code[text1];
dis_buf[2]=text_code[text2];
dis_buf[3]=text_code[text3];
}
//
//fun10
//秒表 mm:ss
//
void fun10()
{
it0++;
if( it0==20 ) //1s
{
it0=0;
sec++;
if(sec==60)
{
sec = 0;
min++;

if(min==60)
{
min =0;
}
}
refresh = 1;
dis_buf[1] = text_code[min%10];
dis_buf[0] = text_code[min/10];
dis_buf[3] = text_code[sec%10];
dis_buf[2] = text_code[sec/10];
}
}
//
//fun11
//时钟 hh:mm
//
void fun11()
{
it0++;
if( it0==20 ) //1s
{
it0=0;
sec++;
if(sec==60)
{
sec = 0;
min++;
if(min==60)
{
min =0;
hour++;
if(hour==12)
{
hour =0;
}
//dis_buf[1] = text_code[hour%10];
//dis_buf[0] = text_code[hour/10];
}
refresh = 1;

}dis_buf[1] = text_code[hour%10];
dis_buf[0] = text_code[hour/10];
dis_buf[3] = text_code[min%10];
dis_buf[2] = text_code[min/10];
}
}
//
//fun12
//倒计时
//
void fun12()
{
it0++;
if( it0==20 ) //01s
{
it0=0;
sec--; //sec must >=1
if(sec==0)
{
sec = 60;
min--;
//dis_buf[1] = text_code[min%10];
//dis_buf[0] = text_code[min/10];
if(min==0)
{
min =60;
}
}
refresh = 1;
dis_buf[1] = text_code[min%10];
dis_buf[0] = text_code[min/10];
dis_buf[3] = text_code[sec%10];
dis_buf[2] = text_code[sec/10];
}
}
//
//fun13
//设置时间:hh:mm
//
void fun13()
{
onsetup =1;
sec=0;
min=0;
hour=0;
dis_buf[1] = text_code[min%10];
dis_buf[0] = text_code[min/10];
dis_buf[3] = text_code[sec%10];
dis_buf[2] = text_code[sec/10];
}
//
//fun3
//设置时间:hh:mm
//
void fun130()
{
if( keydown1!= keydown0)
{
keydown0 = keydown1;
if(keydown1==0 ) return;//按键d起
if(onsetup==1)
hour = key10;
else if(onsetup==2)
hour += key;
else if(onsetup==3)
min = key10;
else if(onsetup==4)
min += key;
onsetup++;
dis_buf[1] = text_code[hour%10];
dis_buf[0] = text_code[hour/10];
dis_buf[3] = text_code[min%10];
dis_buf[2] = text_code[min/10];
}
}
//
//fun14
//设置
//
void fun14()
{
sec=0;
min=0;
hour=0;
}
//
// 定时器0中断服务程序, 用于数码管的动态扫描
//T0定时器,50ms激发一次,间隔1秒执行一次输出(需要20次)
//
void timer0() interrupt 1
{
//TR1=0 ;
TH0=V_TH0; //1ms延时常数
TL0=V_TL0; //频率调节
//TH1=PWM ;
//TR1=1 ;

keydown1 = keydown();
//if( keydown1!= keydown0)
{
if( fun <=9 )
fun130();
else
switch(fun)
{
case 10:fun10();break;
case 11:fun11();break;
case 12:fun12();break;
case 13:fun13();break;
case 14:fun14();break;
default:break;
}
}

}
//
// 定时器1中断服务程序, 用于数码管的动态扫描
//T1定时器,5ms激发一次,间隔5ms执行一次输出
//
void timer1() interrupt 3
{
//TR1=0;
//TH1=PWM ;
//处理一行
TH1 = V_TH1;
TL1 = V_TL1;
it1++;
//if( it1==200 )
{
it1=0;
beepT1();
textout();
}
}
//
//系统初始化
// 函数功能:对系统进行初始化,包括定时器初始化和变量初始化/
//
void init(void)
{
//变量初始化
bee=1;
P0 = 0xFF;
P2 = 0xFF;
text_index = 0;
dis_buf[0]=text_code[0];
dis_buf[1]=text_code[0];
dis_buf[2]=text_code[0];
dis_buf[3]=text_code[0];
//定时器初始化/
TMOD=V_TMOD;
TH0=V_TH0; //延时常数
TL0=V_TL0; //延时常数
TH1=PWM; //脉宽调节
TL1=0;
IE = 0x82;
ET0=1; //定时/计数器T0中断允许
ET1=1; //定时/计数器T1中断允许
TR0=1 ; //T0启动
TR1=1 ; //T1启动/
}
//
//主程序
//处理按键时加上了按键消抖
//
void main(void)
{
init(); //系统初始化
while(1);
//连run()都没了
}
//
//蜂鸣
/
void beep()
{
uchar i,j;
for (i=0;i<100;i++)
{
for(j=20;j>7;j--)
{
delay0(j);
BEEP=!BEEP; //BEEP取反
}
}
BEEP=1; //关闭蜂鸣器
}
/

ea=1;开cpu所有中断使能
et0=1;开定时器0中断使能
tmod=0x01;tmod是定时器工作寄存器,设定时器工作模式
th0=-5000/256;定时器0的高8位
tl0=-5000%256;定时器0的低8位
tr0=1;启动定时器0
ie0=0
;ie0是定时器0中断标志


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13324658.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-15
下一篇 2023-07-15

发表评论

登录后才能评论

评论列表(0条)

保存