#include "p33FJ32MC204.h"
#include "dsp.h"
#include <xc.h>
#include <PPS.H>
/*****************Config bit settings****************/
// FBS
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = FRC //初始振荡源选择FRC Oscillator Mode (Internal Fast RC (FRC) with divide by N)
#pragma config IESO = ON // 使用内部FRC振荡源启动器件,然后自动切换为就绪的用户选择的振荡器源 Internal External Switch Over Mode (Start-up device with FRC, then automatically switch to user-selected oscillator source when ready)
// FOSC
#pragma config POSCMD = XT //Primary Oscillator Source (Primary Oscillator Disabled)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = OFF // 外设引脚选择配置位 允许多次配置 Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSECME // 必须要使得时钟使能 Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
// FWDT
#pragma config FWDTEN = OFF // 把看门狗关闭了 Watchdog Timer Enable (Watchdog timer always enabled)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
#pragma config LPOL = ON // Motor Control PWM Low Side Polarity bit (PWM module low side output pins have active-high output polarity)
#pragma config HPOL = ON // Motor Control PWM High Side Polarity bit (PWM module high side output pins have active-high output polarity)
#pragma config PWMPIN = ON // Motor Control PWM Module Pin Mode bit (PWM module pins controlled by PORT register at device Reset)
// FICD
#pragma config ICS = PGD3 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
void timer2();
void __attribute__((__interrupt__)) _OC1Interrupt( void );
int main(void)
{
//Perform a clock switch to 40MIPS (80Mhz)
PLLFBD = 38; //M=PLLFBD+2 = 40
CLKDIVbits.PLLPOST = 0; //N2=2(PLLPOST+1) = 2
CLKDIVbits.PLLPRE = 0; //N1=PLLPRE+2 = 2
//unlock OSCCON register
__builtin_write_OSCCONH(0x03);//切换到带PLL的主振荡器模式 经过测试 应该切换成功了
__builtin_write_OSCCONL(0x01);//时钟切换被使能,外设引脚未锁定,允许写入外设引脚选择寄存器,PLL处于失锁状态,禁止辅助振荡器,请求切换新的振荡器
//wait for clock to stabilize
while(OSCCONbits.COSC != 0b011);
//wait for PLL to lock
while(OSCCONbits.LOCK != 1) {};
//clock switch finished
TRISBbits.TRISB15 = 0;
//TRISCbits.TRISC4 = 0;//这里也可以不用设置,因为输入输出直接由输出比较控制了
//OC1配置
PPSUnLock;
_RP20R = 0b10010;//RP20连接到输出比较1
PPSLock;
OC1CONbits.OCM=0b00; // 禁用输出比较模式
OC1R = 1999;//写入第一个PWM周期的占空比
OC1RS = 2999;//写入第二个PWM周期的占空比
OC1CONbits.OCTSEL= 0; // 选择Timer2作为输出比较时基
OC1R= 100; // 加载比较寄存器值
IPC0bits.OC1IP = 1; // Setup OC1 interrupt priority level
IFS0bits.OC1IF = 0; // Clear OC1 Interrupt Status Flag
IEC0bits.OC1IE = 1; // Enable OC1 interrupt
OC1CONbits.OCM= 0b110;//选择输出比较模式
timer2();
while(1);//原地踏步
}
//采用内部时钟作为定时器时钟
void timer2()
{
//Timer 2 中断设置
IFS0bits.T2IF = 0; // Clear Timer1 Interrupt Flag
IEC0bits.T2IE = 1; // Enable Timer1 interrupt
IPC1bits.T2IP = 2; // Set Timer 2 Interrupt Priority Level 此程序可以不设定定时器2优先级,默认为自然优先级,自然优先级低于IC1的自然优先级
//TMR2设置,延时100us
TMR2 = 0;// Resetting TIMER
T2CONbits.TGATE = 0; //disable gated timer mode
T2CON = 0x0000; // reset timer configuration
PR2 = 3999; //load the period value
T2CONbits.TCS = 0; //内部时钟
T2CONbits.TCKPS = 0; //分频比选择1
T2CONbits.TON = 1; // Enable Timer2 定时器开启了就一直在工作,除了休眠和空闲模式
}
//Interrupt Service Routine 程序运行没有运行到中断这里
void __attribute__((__interrupt__,auto_psv)) _OC1Interrupt( void )
{
//PORTBbits.RB15 = !PORTBbits.RB15;//测试证实在PWM模式下输出比较不会运行到中断服务程序这里
IFS0bits.OC1IF = 0;
}
/* Timer 2 Interrupt */
void __attribute__((__interrupt__,auto_psv)) _T2Interrupt(void)
{
//OC1RS = 3500;
//PORTBbits.RB15 = !PORTBbits.RB15;//这段中断服务程序运行了
IFS0bits.T2IF = 0;
}
MPLAB软件仿真心得-仿真调试-输出比较(附上源代码)
于 2023-07-10 15:19:18 首次发布