本帖最后由 mo812 于 2019-10-4 22:56 编辑
我这边使用新塘N76E003 的单片机设计产品,PWM设置成推挽输出模式时,无法输出PWM波形。
原理图如下:
程序如下:
#include <stdio.h>
#include "N76E003.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
//***************** The Following is in define in Fucntion_define.h ***************************
//****** Always include Function_define.h call the define you want, detail see main(void) *******
//***********************************************************************************************
#if 0
//#define Enable_ADC_AIN0 ADCCON0&=0xF0;P17_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT0;ADCCON1|=SET_BIT0 //P17
//#define Enable_ADC_AIN1 ADCCON0&=0xF0;ADCCON0|=0x01;P30_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT1;ADCCON1|=SET_BIT0 //P30
//#define Enable_ADC_AIN2 ADCCON0&=0xF0;ADCCON0|=0x02;P07_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT2;ADCCON1|=SET_BIT0 //P07
//#define Enable_ADC_AIN3 ADCCON0&=0xF0;ADCCON0|=0x03;P06_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT3;ADCCON1|=SET_BIT0 //P06
//#define Enable_ADC_AIN4 ADCCON0&=0xF0;ADCCON0|=0x04;P05_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT4;ADCCON1|=SET_BIT0 //P05
//#define Enable_ADC_AIN5 ADCCON0&=0xF0;ADCCON0|=0x05;P04_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT5;ADCCON1|=SET_BIT0 //P04
//#define Enable_ADC_AIN6 ADCCON0&=0xF0;ADCCON0|=0x06;P03_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT6;ADCCON1|=SET_BIT0 //P03
//#define Enable_ADC_AIN7 ADCCON0&=0xF0;ADCCON0|=0x07;P11_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT7;ADCCON1|=SET_BIT0 //P11
//#define Enable_ADC_BandGap ADCCON0|=SET_BIT3;ADCCON0&=0xF8; //Band-gap 1.22V
//#define PWM0_FALLINGEDGE_TRIG_ADC ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
//#define PWM2_FALLINGEDGE_TRIG_ADC ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
//#define PWM4_FALLINGEDGE_TRIG_ADC ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
//#define PWM0_RISINGEDGE_TRIG_ADC ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
//#define PWM2_RISINGEDGE_TRIG_ADC ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
//#define PWM4_RISINGEDGE_TRIG_ADC ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
//#define P04_FALLINGEDGE_TRIG_ADC ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
//#define P13_FALLINGEDGE_TRIG_ADC ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
//#define P04_RISINGEDGE_TRIG_ADC ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
//#define P13_RISINGEDGE_TRIG_ADC ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
#endif
//#define unsigned char uint8_t;
//#define unsigned int uint16_t;
//#define unsigned long uint32_t;
void Led_Mode(UINT8 led_modes);
/************************************************************************************************************
* PWM interrupt subroutine
************************************************************************************************************/
void PWM_ISR (void) interrupt 13
{
clr_PWMF; // clear PWM interrupt flag
//P12 = ~P12; // GPIO1 show PWM INT
}
void main(void)
{
//P11_OpenDrain_Mode;
//Set_All_GPIO_Quasi_Mode;
//P01_Quasi_Mode;
//P05_Quasi_Mode;
P01_PushPull_Mode;
P05_PushPull_Mode;
P11_PushPull_Mode; //LEDG_A P11推挽输出
P12_PushPull_Mode; //LEDR_A P12推挽输出
P13_PushPull_Mode; //LEDG_B P13推挽输出
P14_PushPull_Mode; //LEDR_B P14推挽输出
PWM0_P12_OUTPUT_DISABLE;
PWM2_P05_OUTPUT_ENABLE; //使能PWM2 通过P05引脚输出
PWM4_P01_OUTPUT_ENABLE; //使能PWM4 通过P01引脚输出
PWM5_P03_OUTPUT_DISABLE;
PWM_INT_PWM2;
PWM_INT_PWM4;
PWM_FALLING_INT; //Setting Interrupt happen when PWM0 falling signal
set_PWMTYP;
clr_PWMMOD0;
clr_PWMMOD1;
PWM_CLOCK_DIV_8;
PWMPH = 0x07;
PWMPL = 0xCF;
PWM2H = 0x03;
PWM2L = 0xE8;
set_SFRPAGE; //PWM4 and PWM5 duty seting is in SFP page 1
PWM4H = 0x03;
PWM4L = 0xE8;
clr_SFRPAGE;
LEDG_A_OFF;
LEDG_B_OFF;
LEDR_A_OFF;
LEDR_B_OFF;
set_EPWM; //Enable PWM interrupt
set_EA;
set_LOAD;
set_PWMRUN;
while(1)
{
UINT8 i;
for(i=0;i<4;i++)
{
//set_EPWM; //Enable PWM interrupt
//set_EA;
//set_LOAD;
//set_PWMRUN;
Led_Mode(i);
Timer0_Delay1ms(2000);
}
}
}
void Led_Mode(UINT8 led_modes)
{
switch (led_modes)
{
case 0:
LEDG_A_ON;
LEDG_B_ON;
LEDR_A_OFF;
LEDR_B_OFF;
break;
case 1:
LEDG_A_ON;
LEDR_B_ON;
LEDR_A_OFF;
LEDG_B_OFF;
break;
case 2:
LEDR_A_ON;
LEDG_B_ON;
LEDG_A_OFF;
LEDR_B_OFF;
break;
case 3:
LEDR_A_ON;
LEDR_B_ON;
LEDG_A_OFF;
LEDG_B_OFF;
break;
default:
break;
}
}
补充原理图
一周热门 更多>