用外部中断和定时器中断测速程序 为什么定时器中断用不了
时间:10-02
整理:3721RD
点击:
#include <REG51.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P1^0; //??′??÷??????£???RS???¨ò??aP2.0òy??
sbit RW=P1^1; //?áD′??????£???RW???¨ò??aP2.1òy??
sbit E=P1^2; //ê1?üD?o???£???E???¨ò??aP2.2òy??
sbit BF=P0^7; //?|?μ±ê????£?£???BF???¨ò??aP0.7òy??
sbit LED=P2^5;
sbit P32=P3^2;
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
uint cnt_int1,cnt_tim0;
bit Flag_s;//??±ê????
uchar Speed_cnt , Speed ;
uchar TempBuffer[10];
void init(void)
{
EX1=1; //?êDíía2??D??1
IT1=1; //1???μ?? 0μíμ???′¥·¢
ET0 = 1; //?¨ê±?÷0?D??′ò?a
TMOD = 0x1; //éè?¨ê±?÷0?a?£ê?1£?16???£ê?
TR0 = 1;
PT0=1;
EA=1;
}
/*****************************************************
oˉêy1|?ü£o?óê±1ms
***************************************************/
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
/*****************************************************
oˉêy1|?ü£o?óê±è??éoá??
è??ú2?êy£on
***************************************************/
void delay(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++)
delay1ms();
}
/*****************************************************
oˉêy1|?ü£o?D??òo?§?£?éμ??|?μ×′ì?
·μ???μ£oresult?£result=1£??|?μ;result=0£?2??|
***************************************************/
bit BusyTest(void)
{
bit result;
RS=0; //?ù?Y1??¨£?RS?aμíμ???£?RW?a??μ???ê±£??éò??á×′ì?
RW=1;
E=1; //E=1£?2??êDí?áD′
_nop_(); //??2ù×÷
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
result=BF; //???|?μ±ê??μ????3??result
E=0;
return result;
}
/*****************************************************
oˉêy1|?ü£o???£ê?éè????á??ò??ê?μ??·D′è?òo?§?£?é
è??ú2?êy£odictate
***************************************************/
void Write_com (unsigned char dictate)
{
while(BusyTest()==1); //è?1??|?íμè′y
RS=0; //?ù?Y1??¨£?RSoíR/Wí?ê±?aμíμ???ê±£??éò?D′è???á?
RW=0;
E=0; //E??μíμ???(D′??á?ê±£?
// ?íê?è?E′ó0μ?1·¢éú?yì?±?£??ùò?ó|?è??"0"
_nop_();
_nop_(); //??2ù×÷á????ú?÷?ü?ú£???ó2?t·′ó|ê±??
P0=dictate; //??êy?Y?íè?P0?ú£??′D′è???á??òμ??·
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=1; //E????μ???
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=0; //μ±Eóé??μ???ì?±?3éμíμ???ê±£?òo?§?£?é?aê??′DD?üá?
}
/*****************************************************
oˉêy1|?ü£o???¨×?·???ê?μ?êμ?êμ??·
è??ú2?êy£ox
***************************************************/
void WriteAddress(unsigned char x)
{
Write_com(x|0x80); //??ê?????μ?è·?¨·?·¨1??¨?a"80H+μ??·??x"
}
/*****************************************************
oˉêy1|?ü£o??êy?Y(×?·?μ?±ê×?ASCII??)D′è?òo?§?£?é
è??ú2?êy£oy(?a×?·?3£á?)
***************************************************/
void WriteData(unsigned char y)
{
while(BusyTest()==1);
RS=1; //RS?a??μ???£?RW?aμíμ???ê±£??éò?D′è?êy?Y
RW=0;
E=0; //E??μíμ???(D′??á?ê±£?£?
// ?íê?è?E′ó0μ?1·¢éú?yì?±?£??ùò?ó|?è??"0"
P0=y; //??êy?Y?íè?P0?ú£??′??êy?YD′è?òo?§?£?é
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=1; //E????μ???
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=0; //μ±Eóé??μ???ì?±?3éμíμ???ê±£?òo?§?£?é?aê??′DD?üá?
}
/*****************************************************
oˉêy1|?ü£o??LCDμ???ê??£ê???DD3?ê??ˉéè??
***************************************************/
void LcdInt(void)
{
delay(15); //?óê±15ms£?ê×′?D′??á?ê±ó|??LCDò?????3¤μ?·′ó|ê±??
Write_com(0x38); //??ê??£ê?éè??£o16?á2??ê?£?5?á7μ??ó£?8??êy?Y?ó?ú
delay(5); //?óê±5ms??
Write_com(0x38);
delay(5);
Write_com(0x38); //3′?D′ éè???£ê?
delay(5);
Write_com(0x0F); //??ê??£ê?éè??£o??ê??a£?óD1a±ê£?1a±êéá??
delay(5);
Write_com(0x06); //??ê??£ê?éè??£o1a±êóòò?£?×?·?2?ò?
delay(5);
Write_com(0x01); //???á????á?£???ò??°μ???ê??úèY??3y
delay(5);
}
void IntToStr(unsigned int t, unsigned char *str, unsigned char k)
{
unsigned char a[5];
char i, j;
a[0]=(t/10000)%10; //è?μ???êy?μμ?êy×é
a[1]=(t/1000)%10;
a[2]=(t/100)%10;
a[3]=(t/10)%10;
a[4]=(t/1)%10;
for(i=0; i<5; i++) //×a3éASCII??
a[i]=a[i]+'0';
for(i=0; a[i]=='0' && i<=3; i++);
for(j=5-k; j<i; j++) //ì?3?????
{
*str=' ';
str++;
}
for(; i<5; i++)
{
*str=a[i];
str++;
} //?óè?óDD§μ?êy×?
*str='\0';
}
void Print(unsigned char *str)
{
while(*str!='\0')
{
WriteData(*str);
str++;
}
}
void main(void)
{
init();
LcdInt();
delay(10);
while(1)
{
IntToStr(cnt_int1,&TempBuffer[0],5);
WriteAddress(0x00);
Print(&TempBuffer[0]);
Speed= Speed_cnt*72;
}
}
/****************ía2??D??1·t??oˉêy********/
void extern_int1(void) interrupt 2
{
EX1=0;
cnt_int1++;
EX1=1;
}
/***************?¨ê±?÷0·t??oˉêy********/
void Timer0IRQ(void) interrupt 1
{
TR0=0 ;
TH0=0xd8;
TL0=0xf0; //éè?¨ê±?μ?a10000us£¨10ms£?
cnt_tim0++;
if(cnt_tim0==100)
{
Flag_s=1;
Speed_cnt=cnt_int1;//?o′?
cnt_int1=0;//????êy
Flag_s=0;//??±ê??
}
TR0 = 1;
}
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P1^0; //??′??÷??????£???RS???¨ò??aP2.0òy??
sbit RW=P1^1; //?áD′??????£???RW???¨ò??aP2.1òy??
sbit E=P1^2; //ê1?üD?o???£???E???¨ò??aP2.2òy??
sbit BF=P0^7; //?|?μ±ê????£?£???BF???¨ò??aP0.7òy??
sbit LED=P2^5;
sbit P32=P3^2;
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
uint cnt_int1,cnt_tim0;
bit Flag_s;//??±ê????
uchar Speed_cnt , Speed ;
uchar TempBuffer[10];
void init(void)
{
EX1=1; //?êDíía2??D??1
IT1=1; //1???μ?? 0μíμ???′¥·¢
ET0 = 1; //?¨ê±?÷0?D??′ò?a
TMOD = 0x1; //éè?¨ê±?÷0?a?£ê?1£?16???£ê?
TR0 = 1;
PT0=1;
EA=1;
}
/*****************************************************
oˉêy1|?ü£o?óê±1ms
***************************************************/
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
/*****************************************************
oˉêy1|?ü£o?óê±è??éoá??
è??ú2?êy£on
***************************************************/
void delay(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++)
delay1ms();
}
/*****************************************************
oˉêy1|?ü£o?D??òo?§?£?éμ??|?μ×′ì?
·μ???μ£oresult?£result=1£??|?μ;result=0£?2??|
***************************************************/
bit BusyTest(void)
{
bit result;
RS=0; //?ù?Y1??¨£?RS?aμíμ???£?RW?a??μ???ê±£??éò??á×′ì?
RW=1;
E=1; //E=1£?2??êDí?áD′
_nop_(); //??2ù×÷
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
result=BF; //???|?μ±ê??μ????3??result
E=0;
return result;
}
/*****************************************************
oˉêy1|?ü£o???£ê?éè????á??ò??ê?μ??·D′è?òo?§?£?é
è??ú2?êy£odictate
***************************************************/
void Write_com (unsigned char dictate)
{
while(BusyTest()==1); //è?1??|?íμè′y
RS=0; //?ù?Y1??¨£?RSoíR/Wí?ê±?aμíμ???ê±£??éò?D′è???á?
RW=0;
E=0; //E??μíμ???(D′??á?ê±£?
// ?íê?è?E′ó0μ?1·¢éú?yì?±?£??ùò?ó|?è??"0"
_nop_();
_nop_(); //??2ù×÷á????ú?÷?ü?ú£???ó2?t·′ó|ê±??
P0=dictate; //??êy?Y?íè?P0?ú£??′D′è???á??òμ??·
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=1; //E????μ???
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=0; //μ±Eóé??μ???ì?±?3éμíμ???ê±£?òo?§?£?é?aê??′DD?üá?
}
/*****************************************************
oˉêy1|?ü£o???¨×?·???ê?μ?êμ?êμ??·
è??ú2?êy£ox
***************************************************/
void WriteAddress(unsigned char x)
{
Write_com(x|0x80); //??ê?????μ?è·?¨·?·¨1??¨?a"80H+μ??·??x"
}
/*****************************************************
oˉêy1|?ü£o??êy?Y(×?·?μ?±ê×?ASCII??)D′è?òo?§?£?é
è??ú2?êy£oy(?a×?·?3£á?)
***************************************************/
void WriteData(unsigned char y)
{
while(BusyTest()==1);
RS=1; //RS?a??μ???£?RW?aμíμ???ê±£??éò?D′è?êy?Y
RW=0;
E=0; //E??μíμ???(D′??á?ê±£?£?
// ?íê?è?E′ó0μ?1·¢éú?yì?±?£??ùò?ó|?è??"0"
P0=y; //??êy?Y?íè?P0?ú£??′??êy?YD′è?òo?§?£?é
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=1; //E????μ???
_nop_();
_nop_();
_nop_();
_nop_(); //??2ù×÷?????ú?÷?ü?ú£???ó2?t·′ó|ê±??
E=0; //μ±Eóé??μ???ì?±?3éμíμ???ê±£?òo?§?£?é?aê??′DD?üá?
}
/*****************************************************
oˉêy1|?ü£o??LCDμ???ê??£ê???DD3?ê??ˉéè??
***************************************************/
void LcdInt(void)
{
delay(15); //?óê±15ms£?ê×′?D′??á?ê±ó|??LCDò?????3¤μ?·′ó|ê±??
Write_com(0x38); //??ê??£ê?éè??£o16?á2??ê?£?5?á7μ??ó£?8??êy?Y?ó?ú
delay(5); //?óê±5ms??
Write_com(0x38);
delay(5);
Write_com(0x38); //3′?D′ éè???£ê?
delay(5);
Write_com(0x0F); //??ê??£ê?éè??£o??ê??a£?óD1a±ê£?1a±êéá??
delay(5);
Write_com(0x06); //??ê??£ê?éè??£o1a±êóòò?£?×?·?2?ò?
delay(5);
Write_com(0x01); //???á????á?£???ò??°μ???ê??úèY??3y
delay(5);
}
void IntToStr(unsigned int t, unsigned char *str, unsigned char k)
{
unsigned char a[5];
char i, j;
a[0]=(t/10000)%10; //è?μ???êy?μμ?êy×é
a[1]=(t/1000)%10;
a[2]=(t/100)%10;
a[3]=(t/10)%10;
a[4]=(t/1)%10;
for(i=0; i<5; i++) //×a3éASCII??
a[i]=a[i]+'0';
for(i=0; a[i]=='0' && i<=3; i++);
for(j=5-k; j<i; j++) //ì?3?????
{
*str=' ';
str++;
}
for(; i<5; i++)
{
*str=a[i];
str++;
} //?óè?óDD§μ?êy×?
*str='\0';
}
void Print(unsigned char *str)
{
while(*str!='\0')
{
WriteData(*str);
str++;
}
}
void main(void)
{
init();
LcdInt();
delay(10);
while(1)
{
IntToStr(cnt_int1,&TempBuffer[0],5);
WriteAddress(0x00);
Print(&TempBuffer[0]);
Speed= Speed_cnt*72;
}
}
/****************ía2??D??1·t??oˉêy********/
void extern_int1(void) interrupt 2
{
EX1=0;
cnt_int1++;
EX1=1;
}
/***************?¨ê±?÷0·t??oˉêy********/
void Timer0IRQ(void) interrupt 1
{
TR0=0 ;
TH0=0xd8;
TL0=0xf0; //éè?¨ê±?μ?a10000us£¨10ms£?
cnt_tim0++;
if(cnt_tim0==100)
{
Flag_s=1;
Speed_cnt=cnt_int1;//?o′?
cnt_int1=0;//????êy
Flag_s=0;//??±ê??
}
TR0 = 1;
}
有点乱码