#include <p24F32KA302.h>
/*
Find the Period register value for a desired PWM frequency of 2 kHz,
where Fosc = 16 MHz (16 MHz device clock rate) and
a Timer3 prescaler setting of 1:1.
Tcy = 2/Fosc = 125 ns
PWM Period = 1/PWM Frequency = 1/2 kHz = 500 μs
PWM Period = (PR2 + 1) * Tcy * (Timer 3 Prescale Value) *PWM周期计算公式
500 μs = (PR2 + 1) * 125 ns * 1
PR2 = 4000-1
*/
void PWM_OUT(int Period,int rate);
void IRA_38KHZ(int Period,int rate);
void PWM_OUT(int Period,int rate)
{
OC1CON1 = 0;//It is a good practice to clear off the control bits initially
OC1CON2 = 0; //PWM调试通过
OC1CON2bits.SYNCSEL = 0x1F;
OC1CON2bits.OCTRIS = 0;
OC1CON1bits.OCSIDL = 0;// Output capture will continue to operate in CPU Idle mode
OC1CON1bits.OCTSEL = 1; //This selects the peripheral clock as the clock input to the OC1
//Select Timer3 as the clock input to the OC1
//111 = 系统时钟
//110 = 保留
//101 = 保留
//100 = Timer1
//011 = Timer5
//010 = Timer4
//001 = Timer3
//000 = Timer2
OC1CON1bits.OCM = 6 ; //110 = PWM模式:OCFA/B 禁用,当OCxTMR = 0 时输出设置为高电平,当OCxTMR = OCxR 时输出设
//置为低电平
T3CON = 0x00; //Stops any 16-bit Timer3 operation
TMR3 = 0x00; //Clear contents of the timer3 register
T3CONbits.TCKPS = 0; //11 = 1:256
//10 = 1:64
//01 = 1:8
//00 = 1:1
T3CONbits.TCS = 0;
IFS0bits.T3IF = 0; //Clear the Timer3 interrupt status flag
IEC0bits.T3IE = 0; //Enable Timer3 interrupts
IEC0bits.OC1IE = 0; //Disable Compare 1 interrupts
// / IFS0bits.OC1IF = 1;
IFS0bits.OC1IF = 0;
PR3 = Period; //Determine the period
OC1R = rate; //Initial Compare Register1 duty cycle
OC1RS = Period; //Initial Secondary Compare Register1 duty cycle
T3CONbits.TON = 1; //Start Timer3
/* T3CON = 0x00; // 定时器3通过
//Stops any 16-bit Timer3 operation
TMR3 = 0x00; //Clear contents of the timer3 register
PR3 = 2999; //Determine the period
T3CONbits.TCKPS = 0; //11 = 1:256
//10 = 1:64
//01 = 1:8
//00 = 1:1
T3CONbits.TCS = 0;
IFS0bits.T3IF = 0; //Clear the Timer3 interrupt status flag
IEC0bits.T3IE = 1; //Enable Timer3 interrupts
T3CONbits.TON = 1; //Start Timer3
*/
/*
T1CON = 0;
TMR1 = 0; //定时器1通过
PR1 = 1999;
T1CONbits.TCKPS = 3;//T1CON=0x8030 1:256 定时时间=(FOSC/2)*256*DELAY
T1CONbits.TCS = 0;//内部时钟FOSC/2
///// T1CON=0x8030;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 1;
////_IPL=3; //置CPU优先级为3
IPC0bits.T1IP = 4; //置定时器1优先级为4
T1CONbits.TON = 1;*/
}
void IRA_38KHZ(int Period,int rate)
{
OC3CON1 = 0;//It is a good practice to clear off the control bits initially
OC3CON2 = 0; //PWM调试通过
OC3CON2bits.SYNCSEL = 0x1F;
OC3CON2bits.OCTRIS = 0;
OC3CON1bits.OCSIDL = 0;// Output capture will continue to operate in CPU Idle mode
OC3CON1bits.OCTSEL = 1; //This selects the peripheral clock as the clock input to the OC1
//Select Timer3 as the clock input to the OC1
//111 = 系统时钟
//110 = 保留
//101 = 保留
//100 = Timer1
//011 = Timer5
//010 = Timer4
//001 = Timer3
//000 = Timer2
OC3CON1bits.OCM = 6 ; //110 = PWM模式:OCFA/B 禁用,当OCxTMR = 0 时输出设置为高电平,当OCxTMR = OCxR 时输出设
//置为低电平
T3CON = 0x00; //Stops any 16-bit Timer3 operation
TMR3 = 0x00; //Clear contents of the timer3 register
T3CONbits.TCKPS = 0; //11 = 1:256
//10 = 1:64
//01 = 1:8
//00 = 1:1
T3CONbits.TCS = 0;
IFS0bits.T3IF = 0; //Clear the Timer3 interrupt status flag
IEC0bits.T3IE = 0; //Enable Timer3 interrupts
IEC1bits.OC3IE = 0; //Disable Compare 1 interrupts
// / IFS0bits.OC1IF = 1;
IFS1bits.OC3IF = 0;
PR3 = Period; //Determine the period
OC3R = rate; //Initial Compare Register1 duty cycle
OC3RS = Period; //Initial Secondary Compare Register1 duty cycle
T3CONbits.TON = 1; //Start Timer3
}
pwm.rar_PIC24F pwm_pic24f
版权申诉
173 浏览量
2022-09-24
20:08:00
上传
评论
收藏 1KB RAR 举报
钱亚锋
- 粉丝: 85
- 资源: 1万+