学中断遇到个问题,求帮忙解答下
时间:10-02
整理:3721RD
点击:
#include <pic.h>
__CONFIG(HS&WDTDIS&LVPDIS);
#define E RB3
#define RW RB4
#define RS RB5
#define Beng RB7
unsigned int q=0;
int t=0;
void LCD1602A_busy(void)
{
TRISD7=1;
RS=0;
RW=1;
E=1;
while(RD7==1);
E=0;
TRISD7=0;
}
void LCD1602A_Write_data_busy(unsigned char databuf)
{
LCD1602A_busy();
RS=1;
RW=0;
PORTD=databuf;
E=1;
asm("NOP");
E=0;
}
void LCD1602A_Write_com_busy(unsigned char combuf)
{
LCD1602A_busy();
RS=0;
RW=0;
PORTD=combuf;
E=1;
asm("NOP");
E=0;
}
void LCD1602A_Write_address(unsigned char x,unsigned char y)
{
x&=0x0f;
y&=0x01;
LCD1602A_busy();
if(y==0)
LCD1602A_Write_com_busy(0x80+x);
else
LCD1602A_Write_com_busy(0x80+x+0x40);
}
void LCD1602A_Write_disp(unsigned char x,unsigned char y,unsigned char buf)
{
LCD1602A_Write_address(x,y);
LCD1602A_Write_data_busy(buf);
}
void LCD1602A_init(void)//复位函数
{
LCD1602A_Write_com_busy(0x38);
LCD1602A_Write_com_busy(0x01);
LCD1602A_Write_com_busy(0x06);
LCD1602A_Write_com_busy(0x0C);
}
void interrupt ISR(void)
{
if(TMR1IF==1)
{
TMR1H=0xFF;
TMR1L=0XFF;
TMR1IF=0;
if(++t>99) t=0;
}
else if(T0IF==1)
{
TMR0=0xFF;
T0IF=0;
if(--t<0) t=99;
}
else if(TMR2IF==1)
{
TMR2=0x12;
TMR2IF=0;
if(++q>(250*60)) //4000us中断一次,再计次250次后就是1000毫秒
{
q=0;
if(t==1) Beng=1;
t--;
}
}
}
void main(void)
{
TRISB=0B01000111; //初始化RB7-RB0的输入输出方向
TRISD=0B00000000; //初始化RD7-RD0的输入输出方向
PORTB=0B10000000; //初始化RB7-RB0的数值
PORTD=0B00000000; //初始化RD7-RD0的数值
LCD1602A_init();
T2CKPS1=1;
T2CKPS0=1;
TMR2=0X12;
TOUTPS3=0;
TOUTPS2=0;
TOUTPS1=0;
TOUTPS0=0;
TMR2IF=0;
TMR2IE=1;
TMR1CS=1;
T1CKPS1=0;
T1CKPS0=0;
TMR1H=0XFF;
TMR1L=0XFF;
TMR1ON=1;
TMR1IF=0;
TMR1IE=1;
PEIE=1;
GIE=1;
T0CS=1;
T0SE=0;
PSA=1;
PS0=1;
PS1=1;
PS2=0;
TMR0=0XFF;
T0IF=0;
T0IE=1;
GIE=1;
while(1)
{
if(t!=0)
{
TMR2ON=1;
Beng=0;
}
else
TMR2ON=0;
LCD1602A_Write_disp(0,0,t/10+'0');
LCD1602A_Write_disp(1,0,t%10+'0');
}
}
在调试过程中发现按T0和T1中断对应的外部给上升或者下降延对应的按键时,计时数会出现2,3个数字一跳的现象,比如直接从98减小为96之类的,不知道程序出错在哪
__CONFIG(HS&WDTDIS&LVPDIS);
#define E RB3
#define RW RB4
#define RS RB5
#define Beng RB7
unsigned int q=0;
int t=0;
void LCD1602A_busy(void)
{
TRISD7=1;
RS=0;
RW=1;
E=1;
while(RD7==1);
E=0;
TRISD7=0;
}
void LCD1602A_Write_data_busy(unsigned char databuf)
{
LCD1602A_busy();
RS=1;
RW=0;
PORTD=databuf;
E=1;
asm("NOP");
E=0;
}
void LCD1602A_Write_com_busy(unsigned char combuf)
{
LCD1602A_busy();
RS=0;
RW=0;
PORTD=combuf;
E=1;
asm("NOP");
E=0;
}
void LCD1602A_Write_address(unsigned char x,unsigned char y)
{
x&=0x0f;
y&=0x01;
LCD1602A_busy();
if(y==0)
LCD1602A_Write_com_busy(0x80+x);
else
LCD1602A_Write_com_busy(0x80+x+0x40);
}
void LCD1602A_Write_disp(unsigned char x,unsigned char y,unsigned char buf)
{
LCD1602A_Write_address(x,y);
LCD1602A_Write_data_busy(buf);
}
void LCD1602A_init(void)//复位函数
{
LCD1602A_Write_com_busy(0x38);
LCD1602A_Write_com_busy(0x01);
LCD1602A_Write_com_busy(0x06);
LCD1602A_Write_com_busy(0x0C);
}
void interrupt ISR(void)
{
if(TMR1IF==1)
{
TMR1H=0xFF;
TMR1L=0XFF;
TMR1IF=0;
if(++t>99) t=0;
}
else if(T0IF==1)
{
TMR0=0xFF;
T0IF=0;
if(--t<0) t=99;
}
else if(TMR2IF==1)
{
TMR2=0x12;
TMR2IF=0;
if(++q>(250*60)) //4000us中断一次,再计次250次后就是1000毫秒
{
q=0;
if(t==1) Beng=1;
t--;
}
}
}
void main(void)
{
TRISB=0B01000111; //初始化RB7-RB0的输入输出方向
TRISD=0B00000000; //初始化RD7-RD0的输入输出方向
PORTB=0B10000000; //初始化RB7-RB0的数值
PORTD=0B00000000; //初始化RD7-RD0的数值
LCD1602A_init();
T2CKPS1=1;
T2CKPS0=1;
TMR2=0X12;
TOUTPS3=0;
TOUTPS2=0;
TOUTPS1=0;
TOUTPS0=0;
TMR2IF=0;
TMR2IE=1;
TMR1CS=1;
T1CKPS1=0;
T1CKPS0=0;
TMR1H=0XFF;
TMR1L=0XFF;
TMR1ON=1;
TMR1IF=0;
TMR1IE=1;
PEIE=1;
GIE=1;
T0CS=1;
T0SE=0;
PSA=1;
PS0=1;
PS1=1;
PS2=0;
TMR0=0XFF;
T0IF=0;
T0IE=1;
GIE=1;
while(1)
{
if(t!=0)
{
TMR2ON=1;
Beng=0;
}
else
TMR2ON=0;
LCD1602A_Write_disp(0,0,t/10+'0');
LCD1602A_Write_disp(1,0,t%10+'0');
}
}
在调试过程中发现按T0和T1中断对应的外部给上升或者下降延对应的按键时,计时数会出现2,3个数字一跳的现象,比如直接从98减小为96之类的,不知道程序出错在哪
程序没错,是按键存在抖动问题。
用的学习板上的芯片是877A的
程序没错,是按键存在抖动问题。
谢了,能加个qq不?
可加QQ群(STC51-STM32):99794374