大家好,我想请教一下大家就是我用一个按钮控制N79E815A的P01口的pwm问题。想没按按钮时,没有pwm输出,引脚输出电压为0.一按下按钮时pwm引脚输出占空比50%的电压。就是目前我用的官网提供的程序,然后我把按钮还没按时的程序就调用自己写的占空比为0的程序,因为我想占空比为0,那么该引脚应该会输出电压为0.然后现在的结果是,按钮按与不按都没有pwm的输出。一下是我的程序,请各位指教,非常感谢
#define PWMP 0x0DD
#define PWM0_Duty 0x06F
#define PWM1_Duty 0x06F
#define PWM2_Duty 0x06F
#define PWM3_Duty 0x06F
typedef enum
{
E_CHANNEL0,
E_CHANNEL1,
E_CHANNEL2,
E_CHANNEL3,
} E_PWMCNL_SEL;
#include <stdio.h>
#include "N79E81x.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include <intrins.h>
#include "Version.h"
#include "Delay.h"
#include <string.h>
sbit key=P0^0;
bit flag=0;
bit sflag=0;
bit a;
bit b;
UINT16 PWM_shadow;
//-----------------------------------------------------------------------------------------------------------
void Init_PWM(void)
{
PWM_shadow = PWMP;
PWMPH = HIBYTE(PWM_shadow);
PWMPL = LOBYTE(PWM_shadow);
PWMCON0 = 0xD0;
}
void PWM_Pin_Recover_IO(void) //复位函数
{
PWMCON0 = 0x00;//
PWMCON1 = 0x00;//
PWMCON2 = 0x00;//
PWMPH = 0x00;//
PWMPL = 0x00;//
PWM0H = 0x00;//
PWM0L = 0x01;//
PWM1H = 0x00;//
PWM1L = 0x01;//
PWM2H = 0x00;//
PWM2L = 0x01;//
PWM3H = 0x00;//
PWM3L = 0x01; // PWMP < PWMn Duty, PWM Pin Output High(no-active)
PWMCON0 = 0xD0;// PWM Run 运行PWM
_nop_ (); // NOP
PWMCON0 = 0x2F;// PWM Stop
}
void PWM_Channel(E_PWMCNL_SEL Channel)//选择PWM通道
{
switch(Channel)
{
case E_CHANNEL0:
PWM_shadow=PWM0_Duty;
PWM0H = HIBYTE(PWM_shadow);// PWM0 Duty (P0.1)
PWM0L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL1:
PWM_shadow=PWM1_Duty;
PWM1H = HIBYTE(PWM_shadow);// PWM1 Duty (P1.6)
PWM1L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL2:
PWM_shadow=PWM2_Duty;
PWM2H = HIBYTE(PWM_shadow);// PWM2 Duty (P1.7)
PWM2L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL3:
PWM_shadow=PWM3_Duty;
PWM3H = HIBYTE(PWM_shadow);// PWM3 Duty (P0.0)
PWM3L = LOBYTE(PWM_shadow);
break;
}
}
void PWM_Close(E_PWMCNL_SEL Channel)//选择PWM通道
{
switch(Channel)
{
case E_CHANNEL0:
PWM_shadow=0x0DD;
PWM0H = HIBYTE(PWM_shadow);// PWM0 Duty (P0.1)
PWM0L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL1:
PWM_shadow=0x0DD;
PWM1H = HIBYTE(PWM_shadow);// PWM1 Duty (P1.6)
PWM1L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL2:
PWM_shadow=0x0DD;
PWM2H = HIBYTE(PWM_shadow);// PWM2 Duty (P1.7)
PWM2L = LOBYTE(PWM_shadow);
break;
case E_CHANNEL3:
PWM_shadow=0x0DD;
PWM3H = HIBYTE(PWM_shadow);// PWM3 Duty (P0.0)
PWM3L = LOBYTE(PWM_shadow);
break;
}
}
void Enabled_Brake_Function(void) //钳位函数
{
PWMCON1 = 0x30;
}
void Scan(void)//按键子程序
{
if((a==1)&&(b==1))
{
flag=~flag;
a=0;
b=0;
}
if(key==1)
{
a=1;
}
if(key==0)
{
Delay1ms(5);
if(key==0)
{
b=1;
}
while(key==0);
}
}
void Disp(void)//led显示程序
{
if(flag==1)
{
P12=0;
P30=0;
PWM_Channel(E_CHANNEL0);
Init_PWM();
Enabled_Brake_Function();
}
else if(flag==0)
{
P12=1;
P30=1;
PWM_Close(E_CHANNEL0);
Init_PWM();
Enabled_Brake_Function();
}
}
void main(void)
{
P02_Input_Mode;
P03_Input_Mode;
P04_Input_Mode;
P30_Quasi_Mode;
P13_OpenDrain_Mode;
P12_OpenDrain_Mode;
P01_PushPull_Mode;
P07_Input_Mode;
P05_Input_Mode;
P11_Input_Mode;
P10_Quasi_Mode;
P00_Input_Mode;
P10=0;
P01=0;
flag=0;
while(1)
{
Scan();
Disp();
}
}
|