关于按键与定时器中断问题?
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include"smguan.h"
uint16 counter = 0;
uint8 n=0 ,temp ;
uint8 a[3];
code uint8 table_A[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff};
code uint8 ledcode_1[] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xff};
code uint8 ledcode_2[] = {0x40,0x79,0x24,0x30,0x19,
0x12,0x02,0x78,0x00,0x10,0xff}; //带小数点
void delay(void)
{
uint16 i=2000;
while (i--);
}
/*
*数码管的定义变量显示子函数
*/
void refresh_led()
{
static uint8 j = 0;
P0=0xff;
switch(j)
{
case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_1[a[0]];break;
case 1:ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_2[a[1]];break;
case 2:ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;j=0;P0 = table_A[a[2]];break;
default:break;
}
}
/*
键盘扫描
*/
void key_board()
{
if(!KeyIn1)
{
delay();
if(!KeyIn1)
{
if(n >= 50)
n = 0;
else
n++;
a[0] = n%10;
a[1] = n/10%10;
refresh_led();
while(!KeyIn1);
}
}
if(!KeyIn2)
{
delay();
if(!KeyIn2);
{
if(n<= 0)
n = 50;
else
n--;
a[0] = n%10;
a[1] = n/10%10;
refresh_led();
while(!KeyIn2);
}
}
if(!KeyIn3)
{
delay();
if(!KeyIn3);
{
temp=1;
while(!KeyIn3);
}
}
}
/*
*定时器中断初始化
*/
void timer1_init()
{
TMOD |= 0x10;
TMOD &= 0xdf;
TH1 = 0xFc; //1ms
TL1 = 0x67;
TR1 = 1;
EA = 1;
ET1 = 1;
}
/*
*主函数
*/
void main()
{
KeyOut1 =0;
ENLED1 = 0;ADDR3 = 1;
timer1_init();
while(1)
{
key_board();
}
}
/*
*中断服务函数
*/
void interrupt_timer1() interrupt 3
{
static uint32 sec = 0;
TH1 = 0xFC;
TL1 = 0x67;
counter++;
if(counter == 5000)
{
sec++;
counter = 0;
if(temp==1)
{
a[2] = sec%10;
}
}
refresh_led();
}
请问:怎么写,按键程序中 n 值(红色部分)才能控制if(counter == 5000) 中的5000(蓝色部分) 即n值为定时器中断中循环的次数?
#include"smguan.h"
uint16 counter = 0;
uint8 n=0 ,temp ;
uint8 a[3];
code uint8 table_A[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff};
code uint8 ledcode_1[] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xff};
code uint8 ledcode_2[] = {0x40,0x79,0x24,0x30,0x19,
0x12,0x02,0x78,0x00,0x10,0xff}; //带小数点
void delay(void)
{
uint16 i=2000;
while (i--);
}
/*
*数码管的定义变量显示子函数
*/
void refresh_led()
{
static uint8 j = 0;
P0=0xff;
switch(j)
{
case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_1[a[0]];break;
case 1:ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = ledcode_2[a[1]];break;
case 2:ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;j=0;P0 = table_A[a[2]];break;
default:break;
}
}
/*
键盘扫描
*/
void key_board()
{
if(!KeyIn1)
{
delay();
if(!KeyIn1)
{
if(n >= 50)
n = 0;
else
n++;
a[0] = n%10;
a[1] = n/10%10;
refresh_led();
while(!KeyIn1);
}
}
if(!KeyIn2)
{
delay();
if(!KeyIn2);
{
if(n<= 0)
n = 50;
else
n--;
a[0] = n%10;
a[1] = n/10%10;
refresh_led();
while(!KeyIn2);
}
}
if(!KeyIn3)
{
delay();
if(!KeyIn3);
{
temp=1;
while(!KeyIn3);
}
}
}
/*
*定时器中断初始化
*/
void timer1_init()
{
TMOD |= 0x10;
TMOD &= 0xdf;
TH1 = 0xFc; //1ms
TL1 = 0x67;
TR1 = 1;
EA = 1;
ET1 = 1;
}
/*
*主函数
*/
void main()
{
KeyOut1 =0;
ENLED1 = 0;ADDR3 = 1;
timer1_init();
while(1)
{
key_board();
}
}
/*
*中断服务函数
*/
void interrupt_timer1() interrupt 3
{
static uint32 sec = 0;
TH1 = 0xFC;
TL1 = 0x67;
counter++;
if(counter == 5000)
{
sec++;
counter = 0;
if(temp==1)
{
a[2] = sec%10;
}
}
refresh_led();
}
请问:怎么写,按键程序中 n 值(红色部分)才能控制if(counter == 5000) 中的5000(蓝色部分) 即n值为定时器中断中循环的次数?
n和蓝色部分不搭界,各控制各的
不知道你要实现个什么功能,有点乱,
看不懂