void Atomizer_Init() { // Select shunt value based on hardware version switch(Dataflash_info.hwVersion) { case 101: case 108: Atomizer_shuntRes = 125; break; case 103: case 104: case 105: case 106: Atomizer_shuntRes = 110; break; case 107: case 109: Atomizer_shuntRes = 120; break; case 110: case 111: Atomizer_shuntRes = 105; break; case 100: case 102: default: Atomizer_shuntRes = 115; break; } // Setup control pins PC1 = 0; GPIO_SetMode(PC, BIT1, GPIO_MODE_OUTPUT); PC3 = 0; GPIO_SetMode(PC, BIT3, GPIO_MODE_OUTPUT); // Both channels powered down Atomizer_ConfigureConverters(0, 0); // Configure 150kHz PWM PWM_ConfigOutputChannel(PWM0, ATOMIZER_PWMCH_BUCK, 150000, 0); PWM_ConfigOutputChannel(PWM0, ATOMIZER_PWMCH_BOOST, 150000, 0); // Start PWM PWM_EnableOutput(PWM0, PWM_CH_0_MASK); PWM_EnableOutput(PWM0, PWM_CH_2_MASK); PWM_Start(PWM0, PWM_CH_0_MASK); PWM_Start(PWM0, PWM_CH_2_MASK); // Set duty cycle to zero PWM_SET_CMR(PWM0, ATOMIZER_PWMCH_BUCK, 0); PWM_SET_CMR(PWM0, ATOMIZER_PWMCH_BOOST, 0); Atomizer_targetVolts = 0; Atomizer_curCmr = 0; Atomizer_curState = POWEROFF; // Setup 5kHz timer for negative feedback cycle // This function should run during system init, so // the user hasn't had time to create timers yet. Timer_CreateTimer(5000, 1, Atomizer_TimerCallback, 0); }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART to 115200-8n1 for print message */ UART0_Init(); printf("\n\nCPU @ %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock); printf("PWM0 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM0SEL_Msk) ? "CPU" : "PLL"); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will output PWM0 channel 0~3 with different\n"); printf(" frequency and duty, enable dead zone function of all PWM0 pairs.\n"); printf(" And also enable/disable PWM output every 1 second.\n"); printf(" I/O configuration:\n"); printf(" waveform output pin: PWM0_CH0(PC.0), PWM0_CH1(PC.1), PWM0_CH2(PC.2), PWM0_CH3(PC.3)\n"); /*Set Pwm mode as complementary mode*/ PWM_ENABLE_COMPLEMENTARY_MODE(PWM0); // PWM0 channel 0 frequency is 100Hz, duty 30%, PWM_ConfigOutputChannel(PWM0, 0, 100, 30); SYS_UnlockReg(); PWM_EnableDeadZone(PWM0, 0, 400); SYS_LockReg(); // PWM0 channel 2 frequency is 300Hz, duty 50% PWM_ConfigOutputChannel(PWM0, 2, 300, 50); SYS_UnlockReg(); PWM_EnableDeadZone(PWM0, 2, 200); SYS_LockReg(); // Enable output of PWM0 channel 0~3 PWM_EnableOutput(PWM0, 0xF); // Enable PWM0 channel 0 period interrupt, use channel 0 to measure time. PWM_EnablePeriodInt(PWM0, 0, 0); NVIC_EnableIRQ(PWM0P0_IRQn); // Start PWM_Start(PWM0, 0xF); while(1); }
/** * @brief This function set PWM output frequence and duty to drive Buzzer module * @return None */ void Write_Buzzer(unsigned int frequence, unsigned int duty) { // Config PWM1 channel 1 PWM_ConfigOutputChannel(PWM1, 1, frequence, duty); // Enable PWM1 channel 1 to drive buzzer module PWM_EnableOutput(PWM1, 0x02); }
void Motor_Start(void) { PWM_EnableOutput(PWM, 0x33); PWM_Start(PWM, 0x33); //MotorPwmOutput(0,0,0,0); //g_fSpeedControlIntegral = 0; }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART to 115200-8n1 for print message */ UART0_Init(); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will use PWM0 channel 0 to output waveform\n"); printf(" I/O configuration:\n"); printf(" waveform output pin: PWM0 channel 0(PA.12)\n"); printf("\nUse double buffer feature.\n"); /* PWM0 channel 0 waveform of this sample shown below: |<- CNR + 1 clk ->| CNR + 1 = 399 + 1 CLKs |<-CMR+1 clk ->| CMR + 1 = 199 + 1 CLKs |<- CNR + 1 ->| CNR + 1 = 99 + 1 CLKs |<CMR+1>| CMR + 1 = 39 + 1 CLKs __ ______________ _______ |______200_____| 200 |____60__| 40 |_____PWM waveform */ /* Configure PWM0 channel 0 init period and duty. Period is __HXT / (prescaler * clock divider * (CNR + 1)) Duty ratio = (CMR + 1) / (CNR + 1) Period = 12 MHz / (2 * 1 * (199 + 1)) = 30000 Hz Duty ratio = (99 + 1) / (199 + 1) = 50% */ // PWM0 channel 0 frequency is 100Hz, duty 30%, PWM_ConfigOutputChannel(PWM0, 0, 30000, 30); // Enable output of PWM0 channel 0 PWM_EnableOutput(PWM0, PWM_CH_0_MASK); // Enable PWM0 channel 0 period interrupt, use channel 0 to measure time. PWM_EnablePeriodInt(PWM0, 0, 0); NVIC_EnableIRQ(PWM0_IRQn); // Start PWM_Start(PWM0, PWM_CH_0_MASK); while(1); }
// ---------------------------------------------------------------------------------------- // Start PWM Output // ---------------------------------------------------------------------------------------- void Buzzer_Alerm() { BuzzerExecuteFlag=1; /* set PWMB channel 0 output configuration */ PWM_ConfigOutputChannel(PWM0, 0, 1200, 20); /* Enable PWM Output path for PWMB channel 0 */ PWM_EnableOutput(PWM0, PWM_CH_0_MASK); // Start PWM_Start(PWM0, PWM_CH_0_MASK); }
//========================================================================= //----- (00005C4C) -------------------------------------------------------- __myevic__ void InitPWM() { PWM_ConfigOutputChannel( PWM0, BBC_PWMCH_BUCK, BBC_PWM_FREQ, 0 ); PWM_ConfigOutputChannel( PWM0, BBC_PWMCH_BOOST, BBC_PWM_FREQ, 0 ); PWM_EnableOutput( PWM0, 1 << BBC_PWMCH_BUCK ); PWM_EnablePeriodInt( PWM0, BBC_PWMCH_BUCK, 0 ); PWM_EnableOutput( PWM0, 1 << BBC_PWMCH_BOOST ); PWM_EnablePeriodInt( PWM0, BBC_PWMCH_BOOST, 0 ); PWM_Start( PWM0, 1 << BBC_PWMCH_BUCK ); PWM_Start( PWM0, 1 << BBC_PWMCH_BOOST ); BoostDuty = 0; PWM_SET_CMR( PWM0, BBC_PWMCH_BOOST, 0 ); BuckDuty = 0; PWM_SET_CMR( PWM0, BBC_PWMCH_BUCK, 0 ); if ( ISVTCDUAL || ISCUBOID || ISCUBO200 || ISRX200S || ISRX23 || ISRX300 ) { PWM_ConfigOutputChannel( PWM0, BBC_PWMCH_CHARGER, BBC_PWM_FREQ, 0 ); PWM_EnableOutput( PWM0, 1 << BBC_PWMCH_CHARGER ); PWM_Start( PWM0, 1 << BBC_PWMCH_CHARGER ); ChargerDuty = 0; PWM_SET_CMR( PWM0, BBC_PWMCH_CHARGER, 0 ); if ( ISCUBO200 || ISRX200S || ISRX23 || ISRX300 ) { MaxChargerDuty = 512; } else { MaxChargerDuty = 256; } } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t PWM_DeadZone(void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will output all PWMA channels with different\n"); printf(" frequency and duty, enable dead zone function of all PWMA pairs.\n"); printf(" And also enable/disable PWM output every 1 second.\n"); printf(" I/O configuration:\n"); printf(" waveform output pin: PWM0(P2.0), PWM1(P2.1), PWM2(P2.2), PWM3(P2.3)\n"); // PWM0 frequency is 100Hz, duty 30%, PWM_ConfigOutputChannel(PWMA, PWM_CH0, 100, 30); PWM_EnableDeadZone(PWMA, PWM_CH0, 400); // PWM2 frequency is 300Hz, duty 50% PWM_ConfigOutputChannel(PWMA, PWM_CH2, 300, 50); PWM_EnableDeadZone(PWMA, PWM_CH2, 200); // Enable output of all PWMA channels PWM_EnableOutput(PWMA, 0xF); // Enable PWMA channel 0 period interrupt, use channel 0 to measure time. PWM_EnablePeriodInt(PWMA, PWM_CH0, 0); NVIC_EnableIRQ(PWMA_IRQn); // Start PWM_Start(PWMA, 0xF); while(1); }
/*---------------------------------------------------------------------------------------------------------*/ void TMR3_IRQHandler(void) { // clear Timer0 interrupt flag TIMER_ClearIntFlag(TIMER3); if(IR_Tx_stage==TRANSFER_START) { if(IR_bit_state==1) { // Enable PWM0 CH2 output PWM_DisableOutput(PWM0, PWM_CH_3_MASK); //Initial Timer0 to periodic mode with 222Hz(4.5ms) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, START_TRAN_SECOND); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); IR_bit_state=0; IR_Tx_stage=TRANSFER_CONT; } } else if(IR_Tx_stage==TRANSFER_CONT) { if(IR_TX_Byten<4) { IR_TranDATA(); } else { if(IRTx_StopFlag==0) { PWM_EnableOutput(PWM0, PWM_CH_3_MASK); //Initial Timer0 to periodic mode with 1786Hz(560us) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ZERO); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); IRTx_StopFlag=1; } else if(IRTx_StopFlag==1) { PWM_DisableOutput(PWM0, PWM_CH_3_MASK); IR_TX_Bitn=0; IR_TX_Byten=0; IR_Tx_stage=TRANSFER_START; IRTx_StopFlag = 0; IR_TxExecuteFLAG=0; } } } }
void PWM0CH0_IRQHandler(void) { static uint32_t cnt; static uint32_t out; // Channel 0 frequency is 100Hz, every 1 second enter this IRQ handler 100 times. if(++cnt == 100) { if(out) PWM_EnableOutput(PWM0, 0x3F); else PWM_DisableOutput(PWM0, 0x3F); out ^= 1; cnt = 0; } // Clear channel 0 period interrupt flag PWM_ClearPeriodIntFlag(PWM0, 0); }
/*---------------------------------------------------------------------------------------------------------*/ void IR_TranDATA(void) { if(IR_bit_state==0) { // Stop PWM0 CH3 output PWM_EnableOutput(PWM0, PWM_CH_3_MASK); //Initial Timer0 to periodic mode with 1786Hz(560us) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_FIRST); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); IR_bit_state=1; } else if (IR_bit_state==1) { if(TxIR_CODE[IR_TX_Byten] & (1 << (IR_DATA_BIT_NUMBER-IR_TX_Bitn-1))) { // Stop PWM0 CH3 output PWM_DisableOutput(PWM0, PWM_CH_3_MASK); //Initial Timer0 to periodic mode with 595Hz(1680us) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ONE); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); } else { // Enable PWM0 CH3 output PWM_DisableOutput(PWM0, PWM_CH_3_MASK); //Initial Timer0 to periodic mode with 1786Hz(560us) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ZERO); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); } if(IR_TX_Bitn<7) { IR_TX_Bitn++; } else { IR_TX_Bitn=0; IR_TX_Byten++; } IR_bit_state=0; } }
int32_t main (void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); printf("\nThis sample code demonstrate PWM channel 0 trigger ADC function\n"); /* Enable channel 5 */ ADC_Open(ADC, 0, 0, 0x01 << 5); /* Power on ADC */ ADC_POWER_ON(ADC); /* Enable PWM trigger */ ADC_EnableHWTrigger(ADC, ADC_TRIGGER_BY_PWM, ADC_FALLING_EDGE_TRIGGER); /* Enable ADC convert complete interrupt */ ADC_EnableInt(ADC, ADC_ADIF_INT); NVIC_EnableIRQ(ADC_IRQn); /* PWM frequency is 100Hz, duty 30% */ PWM_ConfigOutputChannel(PWM, 0, 100, 30); /* Enable output PWM channel 0 */ PWM_EnableOutput(PWM, 0x1); /* Set PWM channel 0 to center-aligned mode */ PWM_SET_ALIGNED_TYPE(PWM, 0, PWM_CENTER_ALIGNED); /* Enable PWM channel 0 center-triggered ADC */ PWM_EnableADCTrigger(PWM, 0, PWM_TRIGGER_ADC_CNTR_IS_CNR); /* PWM Start */ PWM_Start(PWM, 0x1); while(1); }
int32_t PWM_DeadZone(void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART0_Init(); printf("\nThis sample code will output PWM0 channel 0 to with different\n"); printf("frequency and duty, enable dead zone function of all PWM0 pairs.\n"); printf("And also enable/disable PWM0 output every 1 second.\n"); // PWM0 frequency is 100Hz, duty 30%, PWM_ConfigOutputChannel(PWM0, 0, 100, 30); PWM_EnableDeadZone(PWM0, 0, 400); // PWM2 frequency is 300Hz, duty 50% PWM_ConfigOutputChannel(PWM0, 2, 300, 50); PWM_EnableDeadZone(PWM0, 2, 200); // PWM4 frequency is 500Hz, duty 70% PWM_ConfigOutputChannel(PWM0, 4, 600, 70); PWM_EnableDeadZone(PWM0, 4, 100); // Enable complementary mode PWM_ENABLE_COMPLEMENTARY_MODE(PWM0); // Enable output of all PWM channels PWM_EnableOutput(PWM0, 0x3F); // Enable PWM channel 0 period interrupt, use channel 0 to measure time. PWM_EnablePeriodInt(PWM0, 0, 0); NVIC_EnableIRQ(PWM0CH0_IRQn); // Start PWM_Start(PWM0, 0x3F); while(1); }
/*---------------------------------------------------------------------------------------------------------*/ void TMR3_IRQHandler(void) { uint8_t LearnedDataByten; //LearnedDataByten transfered from IR_Tx_LearnedDataByten // clear Timer0 interrupt flag TIMER_ClearIntFlag(TIMER3); if(IR_Tx_stage==TRANSFER_START && IR_bit_state==1) { // Enable PWM0 CH2 output PWM_DisableOutput(PWM0, PWM_CH_3_MASK); //Initial Timer0 to periodic mode with 222Hz(4.5ms) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, START_TRAN_SECOND); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); IR_bit_state=0; IR_Tx_stage=TRANSFER_CONT; } else if(IR_Tx_stage==TRANSFER_CONT) { if(IR_LearnedFlag==0) { if(IR_TX_Byten<4) { IR_TranDATA(); } else { if(IRTx_StopFlag==0) { PWM_EnableOutput(PWM0, PWM_CH_3_MASK); //Initial Timer0 to periodic mode with 1786Hz(560us) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, TRAN_BIT_ZERO); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); IRTx_StopFlag=1; } else if(IRTx_StopFlag==1) { PWM_DisableOutput(PWM0, PWM_CH_3_MASK); IR_TX_Bitn = 0; IR_TX_Byten = 0; IR_Tx_stage=TRANSFER_START; IRTx_StopFlag = 0; IR_TxExecuteFLAG = 0; } } } else if(IR_LearnedFlag==1) { if(IRTx_StopFlag==1) { PWM_DisableOutput(PWM0, PWM_CH_3_MASK); IR_Tx_stage=TRANSFER_START; IR_TX_Bitn=0; IR_byte_state = 0; IRTx_StopFlag = 0; IR_TxExecuteFLAG = 0; } else if(IR_TX_Bitn%2==0) { LearnedDataByten = IR_Tx_LearnedDataByten*2+IR_byte_state; PWM_EnableOutput(PWM0, PWM_CH_3_MASK); if(IR_LearnedData[LearnedDataByten] & (1 << (IR_LearnedDataLeng[IR_Tx_LearnedDataByten]/2 - IR_TX_Bitn - 1)))//IR_TX_Bitn*2 //Initial Timer0 to periodic mode with 1786Hz(560us) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_FallingMaxFreq); else TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_FallingMinFreq); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); IR_TX_Bitn++; } else if(IR_TX_Bitn%2==1) { LearnedDataByten = IR_Tx_LearnedDataByten*2+IR_byte_state; PWM_DisableOutput(PWM0, PWM_CH_3_MASK); if(IR_LearnedData[LearnedDataByten] & (1 << (IR_LearnedDataLeng[IR_Tx_LearnedDataByten]/2-IR_TX_Bitn-1)))//IR_TX_Bitn*2 //Initial Timer0 to periodic mode with 1786Hz(560us) TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_RisingMaxFreq); else TIMER_Open(TIMER3, TIMER_ONESHOT_MODE, IR_RisingMinFreq); TIMER_EnableInt(TIMER3); TIMER_Start(TIMER3); IR_TX_Bitn++; } if(IR_TX_Bitn==32 && IR_byte_state == 0) { IR_TX_Bitn=0; IR_byte_state=1; } else if((IR_TX_Bitn+31==IR_LearnedDataLeng[IR_Tx_LearnedDataByten]) && (IR_byte_state == 1)) { IRTx_StopFlag = 1; } } } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will use PWM0 channel 0 to capture\n the signal from PWM1 channel 0.\n"); printf(" I/O configuration:\n"); printf(" PWM0_CH0(PA.12 PWM0 channel 0) <--> PWM1_CH0(PA.2 PWM1 channel 0)\n\n"); printf("Use PWM0 Channel 0(PA.12) to capture the PWM1 Channel 0(PA.2) Waveform\n"); while(1) { printf("Press any key to start PWM Capture Test\n"); getchar(); /*--------------------------------------------------------------------------------------*/ /* Set the PWM1 Channel 0 as PWM output function. */ /*--------------------------------------------------------------------------------------*/ /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows. duty ratio = (CMR+1)/(CNR+1) cycle time = CNR+1 High level = CMR+1 PWM clock source frequency = __HXT = 12000000 (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 23999 duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30% CMR = 7199 Prescale value is 1 : prescaler= 2 Clock divider is PWM_CSR_DIV1 : clock divider =1 */ /* set PWM1 channel 0 output configuration */ PWM_ConfigOutputChannel(PWM1, 0, 250, 30); /* Enable PWM Output path for PWM1 channel 0 */ PWM_EnableOutput(PWM1, PWM_CH_0_MASK); /* Enable Timer for PWM1 channel 0 */ PWM_Start(PWM1, PWM_CH_0_MASK); /*--------------------------------------------------------------------------------------*/ /* Set the PWM0 channel 0 for capture function */ /*--------------------------------------------------------------------------------------*/ /* If input minimum frequency is 250Hz, user can calculate capture settings by follows. Capture clock source frequency = __HXT = 12000000 in the sample code. (CNR+1) = Capture clock source frequency/prescaler/clock source divider/minimum input frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 0xFFFF (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.) */ /* set PWM0 channel 0 capture configuration */ PWM_ConfigCaptureChannel(PWM0, 0, 166, 0); /* Enable capture falling edge interrupt for PWM0 channel 0 */ //PWM_EnableCaptureInt(PWM0, 0, PWM_CAPTURE_INT_FALLING_LATCH); /* Enable PWM0 NVIC interrupt */ //NVIC_EnableIRQ(PWM0_IRQn); /* Enable Timer for PWM0 channel 0 */ PWM_Start(PWM0, PWM_CH_0_MASK); /* Enable Capture Function for PWM0 channel 0 */ PWM_EnableCapture(PWM0, PWM_CH_0_MASK); /* Enable falling capture reload */ PWM0->CAPCTL |= PWM_CAPCTL_FCRLDEN0_Msk; /* Wait until PWM0 channel 0 Timer start to count */ while((PWM0->CNT[0]) == 0); /* Capture the Input Waveform Data */ CalPeriodTime(PWM0, 0); /*---------------------------------------------------------------------------------------------------------*/ /* Stop PWM1 channel 0 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */ /*---------------------------------------------------------------------------------------------------------*/ /* Set PWM1 channel 0 loaded value as 0 */ PWM_Stop(PWM1, PWM_CH_0_MASK); /* Wait until PWM1 channel 0 Timer Stop */ while((PWM1->CNT[0] & PWM_CNT_CNT_Msk) != 0); /* Disable Timer for PWM1 channel 0 */ PWM_ForceStop(PWM1, PWM_CH_0_MASK); /* Disable PWM Output path for PWM1 channel 0 */ PWM_DisableOutput(PWM1, PWM_CH_0_MASK); /*---------------------------------------------------------------------------------------------------------*/ /* Stop PWM0 channel 0 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(Period) as 0. When PWM internal counter(CNT) reaches to 0, disable PWM Timer */ /*---------------------------------------------------------------------------------------------------------*/ /* Disable PWM0 NVIC */ //NVIC_DisableIRQ(PWM0_IRQn); /* Set loaded value as 0 for PWM0 channel 0 */ PWM_Stop(PWM0, PWM_CH_0_MASK); /* Wait until PWM0 channel 0 current counter reach to 0 */ while((PWM0->CNT[0] & PWM_CNT_CNT_Msk) != 0); /* Disable Timer for PWM0 channel 0 */ PWM_ForceStop(PWM0, PWM_CH_0_MASK); /* Disable Capture Function and Capture Input path for PWM0 channel 0 */ PWM_DisableCapture(PWM0, PWM_CH_0_MASK); /* Clear Capture Interrupt flag for PWM0 channel 0 */ PWM_ClearCaptureIntFlag(PWM0, 0, PWM_CAPTURE_INT_FALLING_LATCH); } }
void motorsInit() { #ifdef M451 CLK_EnableModuleClock(PWM0_MODULE); CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL2_PWM0SEL_PCLK0, 0); SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC0MFP_Msk) & (~SYS_GPC_MFPL_PC1MFP_Msk) & (~SYS_GPC_MFPL_PC2MFP_Msk) & (~SYS_GPC_MFPL_PC3MFP_Msk) #ifdef HEX6X & (~SYS_GPC_MFPL_PC4MFP_Msk) #endif ); #ifdef HEX6X SYS->GPD_MFPL = (SYS->GPD_MFPL & (~SYS_GPD_MFPL_PD7MFP_Msk) ); #endif SYS->GPC_MFPL |= (SYS_GPC_MFPL_PC0MFP_PWM0_CH0|SYS_GPC_MFPL_PC1MFP_PWM0_CH1| SYS_GPC_MFPL_PC2MFP_PWM0_CH2|SYS_GPC_MFPL_PC3MFP_PWM0_CH3 #ifdef HEX6X |SYS_GPC_MFPL_PC4MFP_PWM0_CH4 #endif ); #ifdef HEX6X SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD7MFP_PWM0_CH5); #endif PWM_ConfigOutputChannelf(PWM0, 0, ESC_UPDATE_FREQ, ESC_UPDATE_FREQ/10); PWM_ConfigOutputChannelf(PWM0, 1, ESC_UPDATE_FREQ, ESC_UPDATE_FREQ/10); PWM_ConfigOutputChannelf(PWM0, 2, ESC_UPDATE_FREQ, ESC_UPDATE_FREQ/10); PWM_ConfigOutputChannelf(PWM0, 3, ESC_UPDATE_FREQ, ESC_UPDATE_FREQ/10); #ifdef HEX6X PWM_ConfigOutputChannelf(PWM0, 4, ESC_UPDATE_FREQ, ESC_UPDATE_FREQ/10); PWM_ConfigOutputChannelf(PWM0, 5, ESC_UPDATE_FREQ, ESC_UPDATE_FREQ/10); #endif motorsSetRatio(MOTOR_M1, 0); motorsSetRatio(MOTOR_M2, 0); motorsSetRatio(MOTOR_M3, 0); motorsSetRatio(MOTOR_M4, 0); #ifdef HEX6X motorsSetRatio(MOTOR_M5, 0); motorsSetRatio(MOTOR_M6, 0); #endif PWM_EnableOutput(PWM0, PWM_CH_0_MASK); PWM_EnableOutput(PWM0, PWM_CH_1_MASK); PWM_EnableOutput(PWM0, PWM_CH_2_MASK); PWM_EnableOutput(PWM0, PWM_CH_3_MASK); #ifdef HEX6X PWM_EnableOutput(PWM0, PWM_CH_4_MASK); PWM_EnableOutput(PWM0, PWM_CH_5_MASK); #endif #else uint8_t u8Timer; DrvPWM_Open(); DrvGPIO_InitFunction(E_FUNC_PWM01); DrvGPIO_InitFunction(E_FUNC_PWM23); s_u32Pulse = 0; g_u8PWMCount = 1; g_u16Frequency = ESC_UPDATE_FREQ; sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; sPt.u32Frequency = g_u16Frequency; sPt.u8HighPulseRatio = 1; sPt.u8HighPulseBase = 1000000/g_u16Frequency; sPt.i32Inverter = 0; u8Timer = DRVPWM_TIMER0; DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK); DrvPWM_SetTimerClk(u8Timer, &sPt); DrvPWM_SetTimerIO(u8Timer, 1); u8Timer = DRVPWM_TIMER1; DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK); DrvPWM_SetTimerClk(u8Timer, &sPt); DrvPWM_SetTimerIO(u8Timer, 1); u8Timer = DRVPWM_TIMER2; DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK); DrvPWM_SetTimerClk(u8Timer, &sPt); DrvPWM_SetTimerIO(u8Timer, 1); u8Timer = DRVPWM_TIMER3; DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK); DrvPWM_SetTimerClk(u8Timer, &sPt); DrvPWM_SetTimerIO(u8Timer, 1); #endif }
int32_t main (void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); printf("\nThis sample code will output PWM channel 0 to with different duty\n"); printf(", and enable/disable Precise Center Align function.\n"); printf("Polling 1 period interrupt flag to get PWM channel 0 output.\n"); // PWM-Timer 0 enable and Auto-reload PWM->CTL = PWM_CTL_CNTEN0_Msk | PWM_CTL_CNTMODE0_Msk; PWM_SET_PRESCALER(PWM, 0, 1); PWM_SET_DIVIDER(PWM, 0, PWM_CLK_DIV_1); // Set the PWM aligned type PWM_SET_ALIGNED_TYPE(PWM, 0, PWM_CENTER_ALIGNED); // Enable PWM channel 0 output PWM_EnableOutput(PWM, BIT0); // Start PWM_Start(PWM, BIT0); /* Precise Center Align and Center Align PWM channel 0 waveform of this sample shown below: |<- CNR-(2*(CMR+1)) ->| CNR-(2*(CMR+1) = 401 -(2*(100+1)) CLKs |<- CNR -( 2 *( CMR + 1)) ->| CNR-(2*(CMR+1) = 402 -(2*(99+1)) CLKs |<- 2 *(CNR-CMR) clk ->| 2 * (CNR - CMR) = 2 * (401-100) CLKs |<- 2 * (CNR-CMR) clk ->| 2 * (CNR - CMR) = 2 * (402-99) CLKs ________ ____________ ________ ____________ ________ ____________ ________ ____________ ____| 7.96us |_8.08us_| 24.08us |_8.08us_| 8.08us |_8us_| 24.24us |_8us_| 7.96us |_8.08us_| 24.08us |_8.08us_| 8.08us |_8us_| 24.24us |_8us_ */ while(1) { // Enable PWM Precise Center Aligned Type PWM->PCACTL = PWM_PCACTL_PCAEN_Msk; // PWM Channel 0 Output : duty = 7.96u, low = 8.08u PWM_SET_CMR(PWM, 0, 100); PWM_SET_CNR(PWM, 0, 401); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); // Disable PWM Precise Center Aligned Type PWM->PCACTL &= ~(PWM_PCACTL_PCAEN_Msk); // PWM Channel 0 Output : duty = 24.08u, low = 8.08u PWM_SET_CMR(PWM, 0, 100); PWM_SET_CNR(PWM, 0, 401); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); // Enable PWM Precise Center Aligned Type PWM->PCACTL = PWM_PCACTL_PCAEN_Msk; // PWM Channel 0 Output : duty = 8.08u, low = 8u PWM_SET_CMR(PWM, 0, 99); PWM_SET_CNR(PWM, 0, 402); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); // Disable PWM Precise Center Aligned Type PWM->PCACTL &= ~(PWM_PCACTL_PCAEN_Msk); // PWM Channel 0 Output : duty = 24.24u, low = 8u PWM_SET_CMR(PWM, 0, 99); PWM_SET_CNR(PWM, 0, 402); // Polling, Wait 1 period interrupt flags while(PWM_GetPeriodIntFlag(PWM, 0) == 0); PWM_ClearPeriodIntFlag(PWM, 0); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will use PWMB channel 2 to capture\n the signal from PWMB channel 1.\n"); printf(" I/O configuration:\n"); printf(" PWM5(P2.5 PWMB channel 1) <--> PWM6(P2.6 PWMB channel 2)\n\n"); printf("Use PWMB Channel 2(P2.6) to capture the PWMB Channel 1(P2.5) Waveform\n"); while(1) { printf("Press any key to start PWM Capture Test\n"); getchar(); /*--------------------------------------------------------------------------------------*/ /* Set the PWMB Channel 1 as PWM output function. */ /*--------------------------------------------------------------------------------------*/ /* Assume PWM output frequency is 250Hz and duty ratio is 30%, user can calculate PWM settings by follows. duty ratio = (CMR+1)/(CNR+1) cycle time = CNR+1 High level = CMR+1 PWM clock source frequency = __HXT = 12000000 (CNR+1) = PWM clock source frequency/prescaler/clock source divider/PWM output frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 23999 duty ratio = 30% ==> (CMR+1)/(CNR+1) = 30% CMR = 7199 Prescale value is 1 : prescaler= 2 Clock divider is PWM_CSR_DIV1 : clock divider =1 */ /* set PWMB channel 1 output configuration */ PWM_ConfigOutputChannel(PWMB, PWM_CH1, 250, 30); /* Enable PWM Output path for PWMB channel 1 */ PWM_EnableOutput(PWMB, 0x2); /* Enable Timer for PWMB channel 1 */ PWM_Start(PWMB, 0x2); /*--------------------------------------------------------------------------------------*/ /* Set the PWMB channel 2 for capture function */ /*--------------------------------------------------------------------------------------*/ /* If input minimum frequency is 250Hz, user can calculate capture settings by follows. Capture clock source frequency = __HXT = 12000000 in the sample code. (CNR+1) = Capture clock source frequency/prescaler/clock source divider/minimum input frequency = 12000000/2/1/250 = 24000 (Note: CNR is 16 bits, so if calculated value is larger than 65536, user should increase prescale value.) CNR = 0xFFFF (Note: In capture mode, user should set CNR to 0xFFFF to increase capture frequency range.) */ /* set PWMB channel 2 capture configuration */ PWM_ConfigCaptureChannel(PWMB, PWM_CH2, 166, 0); /* Enable capture falling edge interrupt for PWMB channel 2 */ PWM_EnableCaptureInt(PWMB, PWM_CH2, PWM_CAPTURE_INT_FALLING_LATCH); /* Enable PWMB NVIC interrupt */ NVIC_EnableIRQ((IRQn_Type)(PWMB_IRQn)); /* Enable Timer for PWMB channel 2 */ PWM_Start(PWMB, 0x4); /* Enable Capture Function for PWMB channel 2 */ PWM_EnableCapture(PWMB, 0x4); /* Wait until PWMB channel 2 Timer start to count */ while(PWMB->PDR2 == 0); /* Capture the Input Waveform Data */ CalPeriodTime(PWMB, PWM_CH2); /*------------------------------------------------------------------------------------------------------*/ /* Stop PWMB channel 1 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */ /*------------------------------------------------------------------------------------------------------*/ /* Set PWMB channel 1 loaded value as 0 */ PWM_Stop(PWMB, 0x2); /* Wait until PWMB channel 1 Timer Stop */ while(PWMB->PDR1 != 0); /* Disable Timer for PWMB channel 1 */ PWM_ForceStop(PWMB, 0x2); /* Disable PWM Output path for PWMB channel 1 */ PWM_DisableOutput(PWMB, 0x2); /*------------------------------------------------------------------------------------------------------*/ /* Stop PWMB channel 2 (Recommended procedure method 1) */ /* Set PWM Timer loaded value(CNR) as 0. When PWM internal counter(PDR) reaches to 0, disable PWM Timer */ /*------------------------------------------------------------------------------------------------------*/ /* Disable PWMB NVIC */ NVIC_DisableIRQ((IRQn_Type)(PWMB_IRQn)); /* Set loaded value as 0 for PWMB channel 2 */ PWM_Stop(PWMB, 0x4); /* Wait until PWMB channel 2 current counter reach to 0 */ while(PWMB->PDR2 != 0); /* Disable Timer for PWMB channel 2 */ PWM_ForceStop(PWMB, 0x4); /* Disable Capture Function and Capture Input path for PWMB channel 2*/ PWM_DisableCapture(PWMB, 0x4); /* Clear Capture Interrupt flag for PWMB channel 2*/ PWM_ClearCaptureIntFlag(PWMB, PWM_CH2, PWM_CAPTURE_INT_FALLING_LATCH); } }
uint8_t Servo::attach(int ulPin, int min, int max, int freq) { uint32_t ulValue=0; bpwm = 0; #ifdef USE_BoardToPin if(ulPin > BoardToPin_MAX_COUNT) return 0; if(ulPin > 5 && ulPin < 10) { // BPWM pins - 6, 7, 8, 9 bpwm = 1; } if(ulPin > 17 && ulPin < 22) { // PWM at analog pins - A0, A1, A2, A3 ulPin = ulPin + 8; } if(BoardToPinInfo[ulPin].type!=PWM_TYPE) return 0; ulPin=BoardToPinInfo[ulPin].num; if(bpwm) { if(ulPin > BPWM_MAX_COUNT || BPWM_Desc[ulPin].P == NULL) return 0; } else { if(ulPin > PWM_MAX_COUNT || PWM_Desc[ulPin].P == NULL) return 0; } #else return 0; #endif if(bpwm) { BPWM_Config(BPWM_Desc[ulPin]); // Set Mutifunction pins BPWM_ConfigOutputChannel(BPWM_Desc[ulPin].P, BPWM_Desc[ulPin].ch, BPWM_Desc[ulPin].freq, ulValue); // Config BPWMs BPWM_EnableOutput(BPWM_Desc[ulPin].P, (1 << BPWM_Desc[ulPin].ch)); //Enable BPWM output BPWM_Start(BPWM_Desc[ulPin].P, (1 << BPWM_Desc[ulPin].ch)); //Start BPWM } else { PWM_Config(PWM_Desc[ulPin]); // Set Mutifunction pins PWM_ConfigOutputChannel(PWM_Desc[ulPin].P, PWM_Desc[ulPin].ch, PWM_Desc[ulPin].freq, ulValue); // Config PWMs PWM_EnableOutput(PWM_Desc[ulPin].P, (1 << PWM_Desc[ulPin].ch)); //Enable PWM output PWM_Start(PWM_Desc[ulPin].P, (1 << PWM_Desc[ulPin].ch)); //Start PWM } Servo_MAX = max; Servo_MIN = min; Servo_Pin = ulPin; Servo_Freq = freq; /* #if defined(__M451__) if(ulValue==100) { int32_t pin=PWM_Desc[ulPin].pintype.num; GPIO_Config(GPIO_Desc[pin]); GPIO_SetMode(GPIO_Desc[pin].P, GPIO_Desc[pin].bit, GPIO_MODE_OUTPUT); (GPIO_Desc[pin].P)->DOUT |= GPIO_Desc[pin].bit; pinEnabled[ulPin]= 0; fixValue[ulPin]=ulValue; //return 0; } #elif defined(__NUC240__) if(ulValue==0) { int32_t pin=PWM_Desc[ulPin].pintype.num; GPIO_Config(GPIO_Desc[pin]); GPIO_SetMode(GPIO_Desc[pin].P, GPIO_Desc[pin].bit, GPIO_PMD_OUTPUT); (GPIO_Desc[pin].P)->DOUT &= ~GPIO_Desc[pin].bit; pinEnabled[ulPin]= 0; fixValue[ulPin]=ulValue; //return 0; } #elif defined(__NANO100__) | defined(__NANO1X2__) if(ulValue==0) { int32_t pin=PWM_Desc[ulPin].pintype.num; GPIO_Config(GPIO_Desc[pin]); GPIO_SetMode(GPIO_Desc[pin].P, GPIO_Desc[pin].bit, GPIO_PMD_OUTPUT); (GPIO_Desc[pin].P)->DOUT &= ~GPIO_Desc[pin].bit; pinEnabled[ulPin]= 0; fixValue[ulPin]=ulValue; //return 0; } #elif defined(__NUC131__) if(ulValue==0) { uint32_t pin = PWM_Desc[ulPin].pintype.num; GPIO_Config(GPIO_Desc[pin]); GPIO_SetMode(GPIO_Desc[pin].P, GPIO_Desc[pin].bit, GPIO_PMD_OUTPUT); (GPIO_Desc[pin].P)->DOUT &= ~GPIO_Desc[pin].bit; pinEnabled[ulPin]= 0; fixValue[ulPin]=ulValue; //return 0; } #endif if (!pinEnabled[ulPin]){ //Set Mutifunction pins PWM_Config(PWM_Desc[ulPin]); //Config PWMs PWM_ConfigOutputChannel(PWM_Desc[ulPin].P,PWM_Desc[ulPin].ch,50,0); //Enable PWM output PWM_EnableOutput(PWM_Desc[ulPin].P,(1<<PWM_Desc[ulPin].ch)); //Start PWM PWM_Start(PWM_Desc[ulPin].P,(1<<PWM_Desc[ulPin].ch)); pinEnabled[ulPin] = 1; } //Config PWMs if(fixValue[ulPin]!=ulValue) { PWM_ConfigOutputChannel(PWM_Desc[ulPin].P,PWM_Desc[ulPin].ch,50,ulValue); fixValue[ulPin]=ulValue; } */ }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART to 115200-8n1 for print message */ UART0_Init(); printf("\n\nCPU @ %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock); printf("PWM0 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM0SEL_Msk) ? "CPU" : "PLL"); printf("+------------------------------------------------------------------------+\n"); printf("| PWM Driver Sample Code |\n"); printf("| |\n"); printf("+------------------------------------------------------------------------+\n"); printf(" This sample code will use PWM0 channel 0 to output waveform\n"); printf(" I/O configuration:\n"); printf(" waveform output pin: PWM0 channel 0(PC.0)\n"); printf("\nUse double buffer feature.\n"); /* PWM0 channel 0 waveform of this sample shown below: |<- CNR + 1 clk ->| CNR + 1 = 399 + 1 CLKs |<-CMR+1 clk ->| CMR + 1 = 199 + 1 CLKs |<- CNR + 1 ->| CNR + 1 = 99 + 1 CLKs |<CMR+1>| CMR + 1 = 39 + 1 CLKs __ ______________ _______ |______200_____| 200 |____60__| 40 |_____PWM waveform */ /* Configure PWM0 channel 0 init period and duty. Period is PLL / (prescaler * (CNR + 1)) Duty ratio = (CMR + 1) / (CNR + 1) Period = 72 MHz / (2 * (199 + 1)) = 180000 Hz Duty ratio = (99 + 1) / (199 + 1) = 50% */ // PWM0 channel 0 frequency is 180000Hz, duty 50%, PWM_ConfigOutputChannel(PWM0, 0, 180000, 50); // Enable output of PWM0 channel 0 PWM_EnableOutput(PWM0, PWM_CH_0_MASK); // Enable PWM0 channel 0 period interrupt, use channel 0 to measure time. PWM_EnablePeriodInt(PWM0, 0, 0); NVIC_EnableIRQ(PWM0P0_IRQn); // Start PWM_Start(PWM0, PWM_CH_0_MASK); while(1); }