標(biāo)題:
如何解決單片機(jī)t0和t1不能同時(shí)用的問(wèn)題?
[打印本頁(yè)]
作者:
524858514
時(shí)間:
2023-5-23 23:42
標(biāo)題:
如何解決單片機(jī)t0和t1不能同時(shí)用的問(wèn)題?
單片機(jī)定時(shí)器0做pwm輸出,定時(shí)器1做超聲波測(cè)距計(jì)時(shí)。但是不能同時(shí)使用,哪位可以幫忙解決下這個(gè)問(wèn)題
#include "reg52.h"
#include "intrins.h"
sbit tr=P1^4;
sbit echo=P1^3;
sbit led=P1^5;
sbit servo=P2^7;
int num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//段選
int num2[]={0x01,0x02,0x04,0x08};
int compare, count = 0;
void Delay10us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 25;
while (--i);
}
void display(int a)
{
unsigned char i;
for(i=0;i<4;i++)
{
P2=num2[i];
switch(i)
{
case 0:
P0=num[a/1000];
break;
case 1:
P0=num[a/100%10];
break;
case 2:
P0=num[a/10%10];
break;
case 3:
P0=num[a%10];
break;
}
Delay10us();
P0=0xff;
Delay10us();
}
}
void timer0_init(void)
{
TMOD=0X11;//定時(shí)器模式選擇
EA=1;//總中斷
ET1=1;//定時(shí)器1中斷
TH1=0;
TL1=0;
TL0 = 0x9c; //設(shè)置定時(shí)初值
TH0 = 0xff; //設(shè)置定時(shí)初值
TR0=1;//定時(shí)器開(kāi)關(guān)
TR1=0;
}
void setangle(int anglenum)
{
compare = anglenum + 7;
}
void main(void)
{
int ss,a=0;
int i;
timer0_init();
loop:tr=1;
Delay10us();
Delay10us();
tr=0;
while(!echo)
{
i++;
if(i>20)
{
goto loop;
}
}
TR1=1;
while(echo);
TR1=0;
ss=(TH1*256+TL1-20)*0.174;
TH1=0;
TL1=0;
display(ss);
for(i=0;i<6;i++)
{
Delay10us();
}
if(ss <= 500)
{
led = 1;
}else{
led = 0;
}
setangle(0);
}
void timer0() interrupt 1
{
count ++;
if(count <= compare)
{
servo = 1;
}else{
servo = 0;
}
if(count == 200)
{
count = 0;
}
TL0 = 0x9c; //設(shè)置定時(shí)初值
TH0 = 0xff; //設(shè)置定時(shí)初值
}
復(fù)制代碼
作者:
xxxevery
時(shí)間:
2023-5-24 09:22
這程序?qū)懙模。!main函數(shù)里主要程序要在一個(gè)死循環(huán)里,要不能執(zhí)行下去也一下就停止了啊,還有
goto loop中的loop在哪啊
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1