| 本帖最后由 天灵灵地灵灵 于 2024-10-24 20:20 编辑 
 创建MCC工程,设置如下:
 
 
 
 最后设置成250KHz的频率
 IO配置如下
 
 
 然后代码里对PWM从小到大,再从大到小
 
 
int main(void)
{
    uint16_t pwm_t=0;
    uint8_t flag=0;
    SYSTEM_Initialize();
    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts 
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts 
    // Use the following macros to: 
    // Enable the Global Interrupts 
    //INTERRUPT_GlobalInterruptEnable(); 
    // Disable the Global Interrupts 
    //INTERRUPT_GlobalInterruptDisable(); 
    // Enable the Peripheral Interrupts 
    //INTERRUPT_PeripheralInterruptEnable(); 
    // Disable the Peripheral Interrupts 
    //INTERRUPT_PeripheralInterruptDisable(); 
    while(1)
    {
        if(flag==0)
        {
            PWM1_LoadDutyValue(pwm_t++);
            DELAY_milliseconds(100);
            if(pwm_t==126) flag=1;
        }
        else
        {
            PWM1_LoadDutyValue(pwm_t--);
            DELAY_milliseconds(100);
            if(pwm_t==1) flag=0;
        }
    }    
}
 
 |