|
這個(gè)程序目前可以實(shí)現(xiàn) 5-120hz可調(diào)
但是超過(guò)120的時(shí)候 這個(gè)TH0 =(65536-FOSC/(XHZ*256))/256;計(jì)算就會(huì)出錯(cuò) 我調(diào)試的時(shí)候發(fā)現(xiàn)這個(gè)=2了
但是我將FOSC/(XHZ*256)這個(gè)結(jié)果計(jì)算出來(lái)直接放進(jìn)去就可以正常輸出頻率 請(qǐng)問(wèn)這個(gè)是什么問(wèn)題啊 非常著急 希望告訴解答一下 謝謝
我想要的是0-300hz可調(diào)即可
單片機(jī)源程序如下:
#include <STC12C5A60S2.H>
#include <math.h>
sbit LED=P3^7;
unsigned int count ;
#define FOSC 18432000
#define XHZ 125
//#define T1MS (65536-FOSC/1000/256)
//unsigned char XHZ=100;
void Timer0Init(void)
{
AUXR |= 0x80; //定時(shí)器時(shí)鐘1T模式
TMOD &= 0xF0; //設(shè)置定時(shí)器模式
TMOD |= 0x01; //設(shè)置定時(shí)器模式
// TH0 =254;//(65536-24000000/(XHZ*256))/256; //設(shè)置定時(shí)初值
// TL0 =152;//(65536-24000000/(XHZ*256))%256; //設(shè)置定時(shí)初值
TH0 =(65536-FOSC/(XHZ*256))/256; //設(shè)置定時(shí)初值
TL0 =(65536-FOSC/(XHZ*256))%256; //設(shè)置定時(shí)初值
TF0 = 0; //清除TF0標(biāo)志
TR0 = 1; //定時(shí)器0開(kāi)始計(jì)時(shí)
EA=1;
ET0=1;
}
void main()
{
Timer0Init();
CCON = 0;
CL = 0;
CH = 0;
CMOD = 0x04;
// CCAP0H = CCAP0L = 0x80; //PWM0 port output 50% duty cycle square wave
// CCAPM0 = 0x42; //PCA module-0 work in 8-bit PWM mode and no PCA interrupt
CCAP1H = CCAP1L = 0x80; //PWM1 port output 0% duty cycle square wave
PCA_PWM1 = 0x00;
CCAPM1 = 0x42;
CR = 1; //PCA timer start run
while (1);
}
void tm0_isr() interrupt 1 using 1
{
// TH0 =254;//(65536-24000000/(XHZ*256))/256; //設(shè)置定時(shí)初值
// TL0 =152;//(65536-24000000/(XHZ*256))%256; //設(shè)置定時(shí)初值
TH0 =(65536-FOSC/(XHZ*256))/256; //設(shè)置定時(shí)初值
TL0 =(65536-FOSC/(XHZ*256))%256; //設(shè)置定時(shí)初值
}
|
評(píng)分
-
查看全部評(píng)分
|