#define PWMP 0x0DD
#define PWM0_Duty 0x06F
#define PWM1_Duty 0x06F
#define PWM2_Duty 0x06F
#define PWM3_Duty 0x06F
#define P00_Quasi_Mode P0M1&=~SET_BIT0;P0M2&=~SET_BIT0 //将P0.0口设置为准双向模式,作为输入
#define P30_Quasi_Mode P3M1&=~SET_BIT0;P3M2&=~SET_BIT0 //将P3.0口设置为准双向模式,作为输入
#define P01_PushPull_Mode P0M1&=~SET_BIT1;P0M2|=SET_BIT1//将P0.1口设置为准双向模式,作为输出
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;
UINT16 PWM_shadow;
void Init_PWM(void)
{
//AUXR1 |= Uart_Port_Sel; // Select P10/P11 as UART pin(default)
//InitialUART0_Timer1(9600); // 9600 Baud Rate @ 11.0592MHz
//printf ("\n*===================================================================");
//printf ("\n* Name: N79E81x Series PWM Sample Code.");
//printf ("\n*===================================================================");
//printf ("\nPWM Demo Start.\n");
PWM_shadow = PWMP;
PWMPH = HIBYTE(PWM_shadow); // PWM Period,根据PWMP可求出周期,分别放在计数寄存器高八位和低八位
PWMPL = LOBYTE(PWM_shadow);
PWMCON0 = 0xD0; // Enable PWM and select auto reload mode 使能且计数器溢出后,PWMP自动加载
}
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 Enabled_Brake_Function(void) //钳位函数
{
PWMCON1 = 0x30; // PWM is running. PWM will be stopped when P0.2 is low level.
} //打开钳位,当声明为钳位引脚,没有pwm输出,PWMRUN将被清零(0:pwm没有运行)且BKF标志置位。PWM输出条件依据PWMNB设置
//-----------------------------------------------------------------------------------------------------------
//按键启停子函数
void Scan(void)
{
if(key==0)
{
Delay1ms(300);
if(key==0)
{
flag=~flag;
}
while(key==0);//检测松手
}
}
//程序运行子函数
void Disp(void)
{
if(flag==1)
{
P30=0;
PWM_Channel(E_CHANNEL0); // Select PWM channel.
Init_PWM(); // Enable PWM function and set PWM period.
Enabled_Brake_Function(); // Enable Brake function.
}
else if(flag==0)
{
P30=1;
P01=0;
}
}
void main(void)
{
P00_Input_Mode;
P30_Quasi_Mode;
P01_PushPull_Mode;
flag=0;
while(1)
{
Scan();//循环检测是否有按键按下启停子程序
Disp();
}
}
|
请问各位大神,就是我是按照官网给的PWM程序,然后想P0.1输出占空比50%的pwm波,就是晶振我是用的P3.0 口的时钟,看资料说如果没设置就相当于默认使用内部时钟,因为我的电路是没有加外部晶振的。就是我的程序是想当P0.0口的按钮一按,P0.1输出pwm波,但是就是P0.1在没有按按钮和按按钮的情况都是一样的,输出0v,我能确定按钮是没有问题的。应该是程序哪里没设置对。求...