调试pwm,发现延时函数不起作用,求大神指点
#include<htc.h>
#ifndef _XTAL_FREQ
// Unless already defined assume 16MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 4000000
#endif
void init();
void pwm();
//***** PORTA definitions *****
#define bitset(var,bitno) ((var)|=(1<<(bitno)))
#define bitclr(var,bitno) ((var)&=~(1<<(bitno)))
#define Red_ON bitset(PORTA,2)
#define Red_OFF bitclr(PORTA,2)
#define Blu_ON bitset(PORTA,1)
#define Blu_OFF bitclr(PORTA,1)
#define Gr_ON bitset(PORTA,0)
#define Gr_OFF bitclr(PORTA,0)
void init()
{ OSCCON=0X7a;
FOSC_INTOSC;PLLEN_OFF;WDTE_OFF;MCLRE_ON;
PWRTE_OFF;CP_OFF;CPD_OFF;BOREN_OFF;CLKOUTEN_OFF;IESO_ON;FCMEN_ON;
LVP_OFF;
}
void SetPWMDutyCyle(unsigned int duty_cycle_value)
{
CCP1CONbits.DC1B = duty_cycle_value & 0x03; //first set the 2 lsb bits
CCPR1L = (duty_cycle_value >> 2); //now set upper 8 msb bits
}
void pwm()
{
APFCONbits.CCP1SEL=0;
TRISAbits.TRISA2 = 1; // disable pwm pin output for the moment
CCP1CONbits.CCP1M=0x0C; // select PWM mode for CCP module
CCP1CONbits.P1M=0x00; // select single output on CCP1 pin (RA2)
PR2 = 0xff;
CCPR1L = 0x00; // clear high 8 bits of PWM duty cycle
CCP1CONbits.DC1B=0x00; // clear low 2 bits of PWM Duty cycle
PIR1bits.TMR2IF=0; // clear TMR2 interrupt flag
T2CONbits.T2CKPS=0x00; // select TMR2 prescalar as divide by 1 as per our example above
T2CONbits.TMR2ON=1; // turn TMR2 on
TRISAbits.TRISA2 = 0; // turn PWM output back on
}
void main()
{
unsigned int DutyCycle;
void init();
ANSELAbits.ANSELA=0; // set all analog pins to digital I/O
ADCON0bits.ADON=0; // turn ADC off
DACCON0bits.DACEN=0; // turn DAC off
// PORT A Assignments
TRISA=0X20;
PORTA=0X00;
void pwm(); /*
Red_ON;
__delay_ms(100);
Red_OFF;*/
do{
DutyCycle=0;
while (DutyCycle <= 1023)
{
SetPWMDutyCyle(DutyCycle);
DutyCycle = DutyCycle + 32;Gr_ON;
__delay_ms(100);
}
} while (1);
Blu_ON;
}
|