//*************************************************************** //函数名:PWMSinglePortSetting //功能 :PWM单独端口输出参数配置 //函数参数:3个 byte类型 //参数1: channel 代表当前配置的PWM通道号 // //参数2: period 周期配置参数 // Left Mode: // PWM Period=CLK Period * period // Center Mode: // PWM Period=CLK Period * period * 2 // //参数3: duty 占空比配置参数 // polarity=0起始低电平: // Duty Cycle=(period-duty)/period*100% // polarity=1起始高电平 // Duty Cycle=duty/period*100% //返回值:无 //**************************************************************** void PWMSinglePortSetting(byte channel,byte period,byte duty) { if(channel>7) channel=7; //禁止该通道PWM PWMDisable(channel); switch(channel) { case 0: //设置周期寄存器 PWMPER0=period; //设置占空比寄存器 PWMDTY0=duty; break; case 1: //设置周期寄存器 PWMPER1=period; //设置占空比寄存器 PWMDTY1=duty; break; case 2: //设置周期寄存器 PWMPER2=period; //设置占空比寄存器 PWMDTY2=duty; break; case 3: //设置周期寄存器 PWMPER3=period; //设置占空比寄存器 PWMDTY3=duty; break; case 4: //设置周期寄存器 PWMPER4=period; //设置占空比寄存器 PWMDTY4=duty; break; case 5: //设置周期寄存器 PWMPER5=period; //设置占空比寄存器 PWMDTY5=duty; break; case 6: //设置周期寄存器 PWMPER6=period; //设置占空比寄存器 PWMDTY6=duty; break; case 7: //设置周期寄存器 PWMPER7=period; //设置占空比寄存器 PWMDTY7=duty; break; default: break; } PWMEnable(channel); }
int main(void) { InitializeSystem(); // Wait for reset button to be released while (_PORT(PIO_BTN1) == HIGH) { } //ADCInitialize(); PWMInitialize(); PWMEnable(); USBDeviceInit(); //ADCStartCapture(); _LAT(PIO_LED1) = HIGH; ColourEngine::Initialize(); //ColourEngine::PowerOn(1000); // Fade in ColourEngine::SetPower(power, 0); //_LAT(PIO_LED2) = HIGH; while (1) { USBDeviceTasks(); USBUserProcess(); } return 0; }
void ColourEngine::PowerOn(uint fade) { if (fade > 0) { brightness_fader.speed = q15(max(Q15_MAXINT / (fade/4), 1)); // Equivalent to (1.0f / fade) brightness_fader.on_finished = NULL; brightness_fader.start(Q15(1.0)); } Update(); PWMEnable(); }
//*************************************************************** //函数名:PWMConcatenatePortSetting //功能 :PWM级联端口输出参数配置 //函数参数:3个 1个byte类型 2个word类型 //参数1: channel 代表当前配置的PWM通道号 // //参数2: period 周期配置参数 // Left Mode: // PWM Period=CLK Period * period // Center Mode: // PWM Period=CLK Period * period * 2 // //参数3: duty 占空比配置参数 // polarity=0起始低电平: // Duty Cycle=(period-duty)/period*100% // polarity=1起始高电平 // Duty Cycle=duty/period*100% //返回值:无 //**************************************************************** void PWMConcatenatePortSetting(byte channel,word period,word duty) { if(channel>7) channel=7; switch(channel) { case 0: case 1: PWMDisable(0); //禁止该通道PWM PWMDisable(1); //禁止该通道PWM //设置周期寄存器 PWMPER01=period; //设置占空比寄存器 PWMDTY01=duty; PWMEnable(0); PWMEnable(1); break; case 2: case 3: PWMDisable(2); //禁止该通道PWM PWMDisable(3); //禁止该通道PWM //设置周期寄存器 PWMPER23=period; //设置占空比寄存器 PWMDTY23=duty; PWMEnable(2); PWMEnable(3); break; case 4: case 5: PWMDisable(4); //禁止该通道PWM PWMDisable(5); //禁止该通道PWM //设置周期寄存器 PWMPER45=period; //设置占空比寄存器 PWMDTY45=duty; PWMEnable(4); PWMEnable(5); break; case 6: case 7: PWMDisable(6); //禁止该通道PWM PWMDisable(7); //禁止该通道PWM //设置周期寄存器 PWMPER67=period; //设置占空比寄存器 PWMDTY67=duty; PWMEnable(6); PWMEnable(7); break; default: break; } }