这个程序可以的
#include"H\SC93F8333_C.H"
#include"H\testdef.H"
#include"intrins.h"
void Timer2EXInit(void);
void main(void)
{
IoInit();
uartmode1();
Timer2EXInit();
TR2=0;
ET2=0;
TR2=1;
ET2=1;
EA=1;
while(1)
{
}
}
//捕获模式初始化
void Timer2EXInit(void)
{
TMCON=0X06; //12M 定时器时钟
T2CON=0X09; //使能EXT2,16位捕获模式
T2MOD=0X00;
RCAP2H=0X00;
RCAP2L=0X00;
}
//定时器中断
void Timer2Int(void) interrupt 5
{
static uint count1=0,count2=0,count=0;
static uchar flag=0;
TF2 = 0;
P17=~P17;
/*********捕获***************/
if((T2CON&0x40))
{
T2CON&=~0X40;
if(++flag>=12)
{
flag=0;
}
if(flag==10)
{
count1=((uint)(RCAP2H<<8)+RCAP2L);
}
if(flag==11)
{
count2=((uint)(RCAP2H<<8)+RCAP2L);
count=count2-count1;
CDEC(count);
SendUart(dec,5);
}
}
}
#include"H/SC93F8333_C.H"
#include"H/testdef.H"
uchar code charbyte[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
uchar uartread; //uart读取的数据
uchar dec[5] = "00000" ;
uchar hex[6] = "0x0000" ;
//P0做输入,P1,P2做输出
void IoInit(void)
{
P0CON=0X03;
P1CON=0XFF;
P2CON=0XFF;
P1=P2=0XFF;
}
//精确延时函数,1500=1ms
void delay(uint timer)
{
for(;timer>0;timer--);
}
//40ms 延时函数
void delay40ms(uchar timer)
{
for(;timer>0;timer--)
{
delay(60000);
}
}
//定时器10Khz输出,timer0
void timer10K(bit state)
{
TMCON=0X03;//4分频=6M
TMOD=0X01; //13bit定时
TH0=(65535-300)/256;
TL0=(65535-300)%256;
TR0=1;
EA=ET0=state;
}
//模式2。波特率为375000
void uartmode1(void)
{
SCON = 0x50; //0101(允许接收位) 0000 模式1
//定时器1波特率发生器
TMCON = 0X02; //FOSC/4分频
TMOD = 0X20; //设定T1定时器工作方式2
PCON |= 0X80; //SMOD=1
TH1= 0xD9; //9600波特率 215/216/217
TR1=1;
// IE|=0X90; //1001 0000
}
//发送一组uart数据
void SendUart(uchar *str,uchar length)
{
uchar i;
for(i=0;i<length;i++)
{
SBUF=*str++;
delay(5000);
}
SBUF='\n';
delay(5000);
}
//转换16进制传送
void CHEX(uint temp)
{
hex[2]= charbyte[(temp/4096)%16];
hex[3]= charbyte[(temp/256)%16];
hex[4]= charbyte[(temp/16)%16];
hex[5]= charbyte[temp%16];
}
//转换10进制传送
void CDEC(uint temp)
{
dec[0]= charbyte[(temp/10000)%10];
dec[1]= charbyte[(temp/1000)%10];
dec[2]= charbyte[(temp/100)%10];
dec[3]= charbyte[(temp/10)%10];
dec[4]= charbyte[(temp/1)%10];
}
//定时器0中断
void Timer0Int(void) interrupt 1
{
TH0=(65535-300)/256;
TL0=(65535-300)%256;
}
//uart中断
void uartint(void) interrupt 4
{
if(TI)
{
TI=0;
}
if(RI)
{
RI=0;
uartread=SBUF;
}
}
|