问题描述
- 通过超声测得的距离控制PWM输出的代码,不知道哪里错了,求大神帮忙看看
-
#include
#define SYSCLK 24500000 // Internal oscillator frequency in Hz
#define BAUDRATE 9600 // Baud rate of UART in bps
#define uchar unsigned char
#define uint unsigned int//L298输入控制端定义
sbit IN1 = P2^0;
sbit IN2 = P2^1;sbit ENA = P0^0;
sbit IN3 = P2^2;
sbit IN4 = P2^3;sbit ENB = P0^1;
sbit RX=P2^6;sbit TX=P2^7;
unsigned int time=0;
unsigned char pwm;
float S=0;void Oscillator_Init (void);
void Port_Init (void);
void PCA0_Init (void);
void Interrupt_Init(void);
void Timer0_Init(void);
void Timer3_Init(void);
void Conut(void);
void Timer_Count(void);
void StartModule(void);void delay(uchar n) //5*n,机器周期
{
while (n)
n --;}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<24000;i++);
}void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i
delay_1ms();
}
/********************************/
/* 主函数 */
/********************************/
main()
{
PCA0MD &= ~0x40; // Disable Watchdog timer
pwm=0;
Oscillator_Init(); // Initialize the system clock
PCA0_Init (); // Initialize PCA0;
Port_Init ();
Interrupt_Init();
Timer0_Init();
Timer3_Init();
Timer_Count();
IN1=1;
IN2=0;
IN3=1;
IN4=0;
while (1){
delay(20);
StartModule();
if(S>=10)
{pwm=50;
}
else{
pwm=100;
}}
}
void Port_Init (void)
{P1 |= 0x03; // Set port latches for P1.4 // and P1.5 to '1' P2 |= 0x0F; // Enable crossbar and weak pull-ups P0MDIN = 0xff; P0MDOUT = 0xCf; // Set CEX0 (P0.0) to push-pull P0SKIP = 0x00; P0 = 0x03; XBR1 = 0x42; // 010:CEX0、CEX1 TO P.0,P0.1 XBR0 = 0x01; //TxdRxd enable
}
void Oscillator_Init (void)
{
OSCICN = 0x87; // Set internal oscillator to run
// at its maximum frequency
CLKSEL = 0x00;RSTSRC = 0x04; // enable missing clock detector
}
void PCA0_Init (void)
{
// Configure PCA time base; overflow interrupt disabled
PCA0CN = 0x00; // Stop counter; clear all flags 233//PCA0MD = 0x08; // Use SYSCLK as time base PCA0MD = 0x00; //SYSCLK /12 PCA0CPM0 = 0x42; // Module 0 = 8-bit PWM mode
// Configure initial PWM duty cycle = 50%
PCA0CPH0 =228; PCA0CPL0 =228; // 256- (256 * 0.5);PCA0CPM1 = 0x42; // Module 0 = 8-bit PWM mode
// Configure initial PWM duty cycle = 50%
PCA0CPH1 =128; PCA0CPL1 =128; // 256- (256 * 0.5);// Start PCA counter
CR = 1;
}
void Interrupt_Init(void)
{
IT01CF = 0x07; //P0.7,作为外中断0的输入引脚
EX0= 1;
IT0 = 1;IP = 0;
EIP1 = 0;
EIP2 = 0;
PT0 = 1;
EA = 1;
}void Timer0_Init(void)
{
TMOD=0x01;
IE=0x82;
TH0=0xff;
TL0=0x9c;
TR0=1;
}
void Timer0_Int() interrupt 1//中断程序
{
TH0=0xff;
TL0=0x9c;
PCA0CPH0=256-pwm;
PCA0CPH1=256-pwm;}
void Timer3_Init(void)
{
uchar H3,L3;//b7:T3MH=0;b6:T3ML=0;
//T3_SYSCLK=24500000/12≈2.042MHz,0.49u;T3SPLIT=0,16位自动重载方式
CKCON &= ~0xC0;//TR3=0,关闭定时器T3工作
TMR3CN = 0x00;H3 = 0; // = (65536-2245)/256; //2245*0.49us=1100.05us
L3 = 0; // = (65536-2245)%256;
TMR3RLL = L3;TMR3RLH = H3;
TMR3L = L3;
TMR3H = H3;
// EIE1 = EIE1 | 0x80; //允许T3中断
}void Conut(void)
{
time=TMR3H<<8 +TMR3L;
TMR3H = 0;
TMR3L = 0;
S=(time*1.88)/100; //算出来是MM}
void Timer_Count(void)
{
TMR3CN |= 0x04; //开启计数
while(RX); //当RX为1计数并等待
TMR3CN &= ~0x04; //关闭计数
Conut(); //计算
}
void StartModule(void) //T1中断用来扫描数码管和计800MS启动模块
{
TX=1; //800MS 启动一次模块
delay_nms(800);
TX=0;
}