各位大哥 求指点 单片机测频 我这个程序存在一点问题
时间:10-02
整理:3721RD
点击:
利用T0 计数 T1定时 测量PWM频率
求指点,我的这个程序计数有问题 他最大显示不超过256
各位大神求指点
而且PWM驱动本来没问题,不知为啥跟他放一块 就没输出了 PWM输出口 P2.5,P2.6,P2.7
#include"reg51.h"
#include"PWM.c"
#define uint unsigned int
#define uchar unsigned char
sbit P3_0 = P3^0;
//sbit P3_2 = P3^2;
uchar time = 0,count = 0;
unsigned int HZ = 0;
code uchar LED[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x89};
void delay(uint x)
{
while(x--);
}
void time_init_0(void)interrupt 1 //T0用于计数 方式1
{
TH0 = 0;
TL0 = 0;
count++;
}
void time_init_1(void)interrupt 3 //T1定时器 方式1
{
TH1 = (65536 - 4000)/256;
TL1 = (65536 - 4000)%256;
time++;
if(time >= 250) //1秒到
{
HZ = count * 65536 + (TH0 >>8)*256 + TL0;
time = 0;
count = 0;
}
}
void display()
{
uchar cp;
P0 = 0xff;
switch(cp)
{
case 0:P3_0 = 0;P3_0 = 1;P0 = 0x01;P3_0 = 0;P0 = LED[ HZ%10];break;
case 1:P3_0 = 0;P3_0 = 1;P0 = 0x02;P3_0 = 0;P0 = LED[(HZ/10)%10];break;
case 2:P3_0 = 0;P3_0 = 1;P0 = 0x04;P3_0 = 0;P0 = LED[(HZ/100)%10];break;
case 3:P3_0 = 0;P3_0 = 1;P0 = 0x08;P3_0 = 0;P0 = LED[(HZ/1000)%10];break;
case 4:P3_0 = 0;P3_0 = 1;P0 = 0x10;P3_0 = 0;P0 = LED[(HZ/10000)%10];break;
case 5:P3_0 = 0;P3_0 = 1;P0 = 0x20;P3_0 = 0;P0 = 0xbf;break;
case 6:P3_0 = 0;P3_0 = 1;P0 = 0x40;P3_0 = 0;P0 = LED[2];break;
case 7:P3_0 = 0;P3_0 = 1;P0 = 0x80;P3_0 = 0;P0 = 0x89;break;
}
cp++;
if(cp >= 8)
cp = 0;
}
void init_time()
{
TMOD = 0x15; //0001 0101
TH0 = 0;
TL0 = 0;
TH1 = (65536 - 4000)/256;
TL1 = (65536 - 4000)%256;
EA = 1;
TR0 = 1;
ET0 = 1;
TR1 = 1;
ET1 = 1;
}
void main()
{
init_time();
STC_pwm_init();
while(1)
{
delay(250);
display();
CCAPM0 = 0x42;
CCAPM1 = 0x42;
CCAPM2 = 0x42;
}
}
/*********PWM驱动 **************************/
sfr CCON = 0xd8; //可编程计数器PCA的控制寄存器
sfr CMOD = 0xd9; //可编程计数器工作模式寄存器
sfr CL = 0xe9; //可编程计数器的低八位
sfr CH = 0xf9; //可编程计数器的高八位
sfr AUXR1 = 0XA2;
/*PWM模块0,P1.3输出*/
sfr CCAPM0 = 0xda; //PMW功能寄存器
sfr CCAP0L = 0xea; //高八位
sfr CCAP0H = 0xfa; //低八位
sfr PCA_PWM0 = 0xf2; //PWM辅助寄存器
/*PWM模块1,P1.4输出*/
sfr CCAPM1 = 0xdb;
sfr CCAP1L = 0xeb;
sfr CCAP1H = 0xfb;
sfr PCA_PWM1 = 0xf3;
/*PWM模块2,P1.5输出*/
sfr CCAPM2 = 0xdc;
sfr CCAP2L = 0xec;
sfr CCAP2H = 0xfc;
sfr PCA_PWM2 = 0xf4;
/***PWM模块初始化****/
void STC_pwm_init(void)
{
CMOD = 0x0e; //PWM频率 = 时钟频率/4/256,5.85kHZ
AUXR1 = 0X20; //0x00 P1.0,P1.1,P3.7; 0x10 P3.5,3.6,3.7 0x20 P2.5,P2.6,P2.7;
CL = 0x00;
CH = 0x00; //计数器高/低四位清零
/******模块0,P2.5输出***********/
PCA_PWM0 = 0x00; //模块0,8位PWM,第九位EPC0L为0
CCAPM0 = 0X42; //允许比较,P1.1输出;
/******模块1,P2.6输出***********/
PCA_PWM1= 0x00; //模块0,8位PWM,第九位EPC0L为0
CCAPM1 = 0X42; //允许比较,P1.0输出;
/******模块2,P2.7输出***********/
PCA_PWM2 = 0x00; //模块0,8位PWM,第九位EPC0L为0
CCAPM2 = 0X42; //允许比较,P3.7输出;
CCON = 0X40; //允许PAC计数,计数脉冲为时钟频率/4;
}
求指点,我的这个程序计数有问题 他最大显示不超过256
各位大神求指点
而且PWM驱动本来没问题,不知为啥跟他放一块 就没输出了 PWM输出口 P2.5,P2.6,P2.7
#include"reg51.h"
#include"PWM.c"
#define uint unsigned int
#define uchar unsigned char
sbit P3_0 = P3^0;
//sbit P3_2 = P3^2;
uchar time = 0,count = 0;
unsigned int HZ = 0;
code uchar LED[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x89};
void delay(uint x)
{
while(x--);
}
void time_init_0(void)interrupt 1 //T0用于计数 方式1
{
TH0 = 0;
TL0 = 0;
count++;
}
void time_init_1(void)interrupt 3 //T1定时器 方式1
{
TH1 = (65536 - 4000)/256;
TL1 = (65536 - 4000)%256;
time++;
if(time >= 250) //1秒到
{
HZ = count * 65536 + (TH0 >>8)*256 + TL0;
time = 0;
count = 0;
}
}
void display()
{
uchar cp;
P0 = 0xff;
switch(cp)
{
case 0:P3_0 = 0;P3_0 = 1;P0 = 0x01;P3_0 = 0;P0 = LED[ HZ%10];break;
case 1:P3_0 = 0;P3_0 = 1;P0 = 0x02;P3_0 = 0;P0 = LED[(HZ/10)%10];break;
case 2:P3_0 = 0;P3_0 = 1;P0 = 0x04;P3_0 = 0;P0 = LED[(HZ/100)%10];break;
case 3:P3_0 = 0;P3_0 = 1;P0 = 0x08;P3_0 = 0;P0 = LED[(HZ/1000)%10];break;
case 4:P3_0 = 0;P3_0 = 1;P0 = 0x10;P3_0 = 0;P0 = LED[(HZ/10000)%10];break;
case 5:P3_0 = 0;P3_0 = 1;P0 = 0x20;P3_0 = 0;P0 = 0xbf;break;
case 6:P3_0 = 0;P3_0 = 1;P0 = 0x40;P3_0 = 0;P0 = LED[2];break;
case 7:P3_0 = 0;P3_0 = 1;P0 = 0x80;P3_0 = 0;P0 = 0x89;break;
}
cp++;
if(cp >= 8)
cp = 0;
}
void init_time()
{
TMOD = 0x15; //0001 0101
TH0 = 0;
TL0 = 0;
TH1 = (65536 - 4000)/256;
TL1 = (65536 - 4000)%256;
EA = 1;
TR0 = 1;
ET0 = 1;
TR1 = 1;
ET1 = 1;
}
void main()
{
init_time();
STC_pwm_init();
while(1)
{
delay(250);
display();
CCAPM0 = 0x42;
CCAPM1 = 0x42;
CCAPM2 = 0x42;
}
}
/*********PWM驱动 **************************/
sfr CCON = 0xd8; //可编程计数器PCA的控制寄存器
sfr CMOD = 0xd9; //可编程计数器工作模式寄存器
sfr CL = 0xe9; //可编程计数器的低八位
sfr CH = 0xf9; //可编程计数器的高八位
sfr AUXR1 = 0XA2;
/*PWM模块0,P1.3输出*/
sfr CCAPM0 = 0xda; //PMW功能寄存器
sfr CCAP0L = 0xea; //高八位
sfr CCAP0H = 0xfa; //低八位
sfr PCA_PWM0 = 0xf2; //PWM辅助寄存器
/*PWM模块1,P1.4输出*/
sfr CCAPM1 = 0xdb;
sfr CCAP1L = 0xeb;
sfr CCAP1H = 0xfb;
sfr PCA_PWM1 = 0xf3;
/*PWM模块2,P1.5输出*/
sfr CCAPM2 = 0xdc;
sfr CCAP2L = 0xec;
sfr CCAP2H = 0xfc;
sfr PCA_PWM2 = 0xf4;
/***PWM模块初始化****/
void STC_pwm_init(void)
{
CMOD = 0x0e; //PWM频率 = 时钟频率/4/256,5.85kHZ
AUXR1 = 0X20; //0x00 P1.0,P1.1,P3.7; 0x10 P3.5,3.6,3.7 0x20 P2.5,P2.6,P2.7;
CL = 0x00;
CH = 0x00; //计数器高/低四位清零
/******模块0,P2.5输出***********/
PCA_PWM0 = 0x00; //模块0,8位PWM,第九位EPC0L为0
CCAPM0 = 0X42; //允许比较,P1.1输出;
/******模块1,P2.6输出***********/
PCA_PWM1= 0x00; //模块0,8位PWM,第九位EPC0L为0
CCAPM1 = 0X42; //允许比较,P1.0输出;
/******模块2,P2.7输出***********/
PCA_PWM2 = 0x00; //模块0,8位PWM,第九位EPC0L为0
CCAPM2 = 0X42; //允许比较,P3.7输出;
CCON = 0X40; //允许PAC计数,计数脉冲为时钟频率/4;
}