// -------------------------------------------------- // Initialization of the backlight LCD. Using PWM. void Init_LCD_BackLight(void) { uint32_t pclk; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; pclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER); PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(LCD_BL_PWM_PERI_ID, PWM_MODE_TIMER, (void *) &PWMCfgDat); PINSEL_ConfigPin(LCD_BL_PWM_PORT, LCD_BL_PWM_PIN, 1); // funct.no.1 - PWM1[2] — Pulse Width Modulator 1, channel 2 output. PWM_MatchUpdate(LCD_BL_PWM_PERI_ID, LCD_BL_PWM_PERI_CHA, pclk / LCD_BL_PWM_BASE, PWM_MATCH_UPDATE_NOW); // UPDATE VALUE OF THE PWM DUTY CYCLE PWM_MatchUpdate(LCD_BL_PWM_PERI_ID, LCD_BL_PWM_PERI_CHB , 0 *(( pclk / LCD_BL_PWM_BASE) / 100), PWM_MATCH_UPDATE_NOW); // switch off backlight PWMMatchCfgDat.IntOnMatch = DISABLE; // without interrupt PWMMatchCfgDat.MatchChannel = LCD_BL_PWM_PERI_CHB; // Match channel register - duty cycle - xx % PWMMatchCfgDat.ResetOnMatch = DISABLE; // PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LCD_BL_PWM_PERI_ID, &PWMMatchCfgDat); // store it PWM_ChannelCmd(LCD_BL_PWM_PERI_ID, LCD_BL_PWM_PERI_CHB, ENABLE); // Enable PWM Channel Output PWM_ResetCounter(LCD_BL_PWM_PERI_ID); // reset and start counter PWM_CounterCmd(LCD_BL_PWM_PERI_ID, ENABLE); // start PWM Counter PWM_Cmd(LCD_BL_PWM_PERI_ID, ENABLE); // start PWM }
static void buzzer_pwm_stop (void) { /* Stop counter */ PWM_CounterCmd(LPC_PWM1, DISABLE); /* Stop PWM now */ PWM_Cmd(LPC_PWM1, DISABLE); }
static void buzzer_pwm_start (void) { /* Reset and Start counter */ PWM_ResetCounter(LPC_PWM1); PWM_CounterCmd(LPC_PWM1, ENABLE); /* Start PWM now */ PWM_Cmd(LPC_PWM1, ENABLE); }
void pwm_init(void) { PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; PINSEL_CFG_Type PinCfg; /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 500; PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat); /* Initialize PWM pin connect for PWM1[4] on P2[3] */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 2; PinCfg.Pinnum = (EN_PWM_CHN - 1); // hack PINSEL_ConfigPin(&PinCfg); /* Set match value for PWM match channel 0 = 256, update immediately */ PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW); /* PWM Timer/Counter will be reset when channel 0 matching * no interrupt when match * no stop when match */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Configure PWM channel edge option for EN_PWM_CHN */ PWM_ChannelConfig(LPC_PWM1, EN_PWM_CHN, PWM_CHANNEL_SINGLE_EDGE); /* Set initial match value for EN_PWM_CHN to disable output change */ PWM_MatchUpdate(LPC_PWM1, EN_PWM_CHN, 0, PWM_MATCH_UPDATE_NOW); /* Configure match option for EN_PWM_CHN*/ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = EN_PWM_CHN; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Enable PWM Channel Output for EN_PWM_CHN */ PWM_ChannelCmd(LPC_PWM1, EN_PWM_CHN, ENABLE); /* Reset and Start counter */ PWM_ResetCounter(LPC_PWM1); PWM_CounterCmd(LPC_PWM1, ENABLE); /* Start PWM now */ PWM_Cmd(LPC_PWM1, ENABLE); }
int _PWM_Cmd(uint8_t * args) { uint8_t * arg_ptr; LPC_PWM_TypeDef* PWMx; FunctionalState NewState; if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1; PWMx = (LPC_PWM_TypeDef*) strtoul((char *) arg_ptr, NULL, 16); if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1; NewState = (FunctionalState) strtoul((char *) arg_ptr, NULL, 16); PWM_Cmd(PWMx, NewState); return 0; }
void pwmInit(int base, int channel){ /*Initialise the pwm controller * @param base: Your base time for channel 0 * @param channel: The channel you wish to activat */ PINSEL_CFG_Type pinCfg; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void*) &PWMCfgDat); pinCfg.Funcnum = 1; pinCfg.OpenDrain = 0; pinCfg.Pinmode = 0; pinCfg.Portnum = 2; pinCfg.Pinnum = 5; PINSEL_ConfigPin(&pinCfg); PWM_MatchUpdate(LPC_PWM1, 0, base, PWM_MATCH_UPDATE_NOW); PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); PWM_ChannelConfig(LPC_PWM1, channel, PWM_CHANNEL_SINGLE_EDGE); PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = channel; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); PWM_ChannelCmd(LPC_PWM1, channel, ENABLE); PWM_ResetCounter(LPC_PWM1); PWM_CounterCmd(LPC_PWM1, ENABLE); PWM_Cmd(LPC_PWM1, ENABLE); }
/* Desc:Configures PINSEL to setup P2.0 with Function 1 which is PWM channel 1 (mbed pin 26), and P2.1 with Function 1 which is PWM channel 2 (mbed pin 25). NB! Does not set match registers to any default values. Inputs: uint32_t prescale - sets the PWM prescale value. */ void SetupPWM(uint32_t prescale){ PINSEL_CFG_Type PinCfg; PinCfg.Portnum = 2; PinCfg.Pinnum = 0; PinCfg.Funcnum = 1; PinCfg.Pinmode = 0; PinCfg.OpenDrain = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); PWM_TIMERCFG_Type pwm_countercfg; pwm_countercfg.PrescaleOption = PWM_TIMER_PRESCALE_USVAL; pwm_countercfg.PrescaleValue = prescale; PWM_Init(LPC_PWM1, PWM_MODE_TIMER, &pwm_countercfg); // Initializes the PWMx peripheral PWM_MATCHCFG_Type pwm_matchcfg; pwm_matchcfg.IntOnMatch = DISABLE; pwm_matchcfg.ResetOnMatch = DISABLE; pwm_matchcfg.StopOnMatch = DISABLE; pwm_matchcfg.MatchChannel = 0; PWM_ConfigMatch(LPC_PWM1, &pwm_matchcfg); pwm_matchcfg.MatchChannel = 1; PWM_ConfigMatch(LPC_PWM1, &pwm_matchcfg); pwm_matchcfg.MatchChannel = 2; PWM_ConfigMatch(LPC_PWM1, &pwm_matchcfg); PWM_CounterCmd(LPC_PWM1, ENABLE); PWM_ChannelCmd(LPC_PWM1, 1, ENABLE); // Enable PWM channel 1 output PWM_ChannelCmd(LPC_PWM1, 2, ENABLE); // Enable PWM channel 2 output PWM_Cmd(LPC_PWM1, ENABLE); // Enable PWM peripheral }
/*********************************************************************//** * @brief c_entry: Main PWM program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { uint8_t temp; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; PINSEL_CFG_Type PinCfg; /* PWM block section -------------------------------------------- */ /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat); /* * Initialize PWM pin connect * */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 2; for (temp = 0; temp <= 6; temp++){ PinCfg.Pinnum = temp; PINSEL_ConfigPin(&PinCfg); } /* Set match value for PWM match channel 0 = 100, update immediately */ PWM_MatchUpdate(LPC_PWM1, 0, 100, PWM_MATCH_UPDATE_NOW); /* PWM Timer/Counter will be reset when channel 0 matching * no interrupt when match * no stop when match */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Channel 2: Double Edge * - Channel 4: Double Edge * - Channel 5: Single Edge * The Match register values are as follows: * - MR0 = 100 (PWM rate) * - MR1 = 41, MR2 = 78 (PWM2 output) * - MR3 = 53, MR4 = 27 (PWM4 output) * - MR5 = 65 (PWM5 output) * PWM Duty on each PWM channel: * - Channel 2: Set by match 1, Reset by match 2. * - Channel 4: Set by match 3, Reset by match 4. * - Channel 5: Set by match 0, Reset by match 5. */ /* Edge setting ------------------------------------ */ PWM_ChannelConfig(LPC_PWM1, 2, PWM_CHANNEL_DUAL_EDGE); PWM_ChannelConfig(LPC_PWM1, 4, PWM_CHANNEL_DUAL_EDGE); PWM_ChannelConfig(LPC_PWM1, 5, PWM_CHANNEL_SINGLE_EDGE); /* Match value setting ------------------------------------ */ PWM_MatchUpdate(LPC_PWM1, 1, 41, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(LPC_PWM1, 2, 78, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(LPC_PWM1, 3, 53, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(LPC_PWM1, 4, 27, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(LPC_PWM1, 5, 65, PWM_MATCH_UPDATE_NOW); /* Match option setting ------------------------------------ */ for (temp = 1; temp < 6; temp++) { /* Configure match option */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = temp; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); } /* Enable PWM Channel Output ------------------------------------ */ /* Channel 2 */ PWM_ChannelCmd(LPC_PWM1, 2, ENABLE); /* Channel 4 */ PWM_ChannelCmd(LPC_PWM1, 4, ENABLE); /* Channel 5 */ PWM_ChannelCmd(LPC_PWM1, 5, ENABLE); /* Reset and Start counter */ PWM_ResetCounter(LPC_PWM1); PWM_CounterCmd(LPC_PWM1, ENABLE); /* Start PWM now */ PWM_Cmd(LPC_PWM1, ENABLE); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main PWM program body * @param[in] None * @return None **********************************************************************/ void c_entry(void) { uint8_t pwmChannel; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; /* PWM block section -------------------------------------------- */ /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(_USING_PWM_NO, PWM_MODE_TIMER, (void *) &PWMCfgDat); // Initialize PWM pin connect #if (_USING_PWM_NO == 1) for (pwmChannel = 0; pwmChannel <= 6; pwmChannel++) { PINSEL_ConfigPin (2, pwmChannel, 1); } #elif (_USING_PWM_NO == 0) PINSEL_ConfigPin (1, 2, 3);//PWM0.1 PINSEL_ConfigPin (1, 3, 3);//PWM0.2 PINSEL_ConfigPin (1, 5, 3);//PWM0.3 PINSEL_ConfigPin (1, 6, 3);//PWM0.4 PINSEL_ConfigPin (1, 7, 3);//PWM0.5 PINSEL_ConfigPin (1, 11, 3);//PWM0.6 #else return 0; #endif /* Set match value for PWM match channel 0 = 100, update immediately */ PWM_MatchUpdate(_USING_PWM_NO, 0, 100, PWM_MATCH_UPDATE_NOW); /* PWM Timer/Counter will be reset when channel 0 matching * no interrupt when match * no stop when match */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(_USING_PWM_NO, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Channel 2: Double Edge * - Channel 4: Double Edge * - Channel 5: Single Edge * The Match register values are as follows: * - MR0 = 100 (PWM rate) * - MR1 = 41, MR2 = 78 (PWM2 output) * - MR3 = 53, MR4 = 27 (PWM4 output) * - MR5 = 65 (PWM5 output) * PWM Duty on each PWM channel: * - Channel 2: Set by match 1, Reset by match 2. * - Channel 4: Set by match 3, Reset by match 4. * - Channel 5: Set by match 0, Reset by match 5. */ /* Edge setting ------------------------------------ */ PWM_ChannelConfig(_USING_PWM_NO, 2, PWM_CHANNEL_DUAL_EDGE); PWM_ChannelConfig(_USING_PWM_NO, 4, PWM_CHANNEL_DUAL_EDGE); PWM_ChannelConfig(_USING_PWM_NO, 5, PWM_CHANNEL_SINGLE_EDGE); /* Match value setting ------------------------------------ */ PWM_MatchUpdate(_USING_PWM_NO, 1, 41, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(_USING_PWM_NO, 2, 78, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(_USING_PWM_NO, 3, 53, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(_USING_PWM_NO, 4, 27, PWM_MATCH_UPDATE_NOW); PWM_MatchUpdate(_USING_PWM_NO, 5, 65, PWM_MATCH_UPDATE_NOW); /* Match option setting ------------------------------------ */ for (pwmChannel = 1; pwmChannel < 6; pwmChannel++) { /* Configure match option */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = pwmChannel; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(PWM_1, &PWMMatchCfgDat); } /* Enable PWM Channel Output ------------------------------------ */ /* Channel 2 */ PWM_ChannelCmd(_USING_PWM_NO, 2, ENABLE); /* Channel 4 */ PWM_ChannelCmd(_USING_PWM_NO, 4, ENABLE); /* Channel 5 */ PWM_ChannelCmd(_USING_PWM_NO, 5, ENABLE); /* Reset and Start counter */ PWM_ResetCounter(_USING_PWM_NO); PWM_CounterCmd(_USING_PWM_NO, ENABLE); /* Start PWM now */ PWM_Cmd(_USING_PWM_NO, ENABLE); /* Loop forever */ while(1); }
/*********************************************************************//** * @brief c_entry: Main PWM program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { uint8_t temp, temp2; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; PINSEL_CFG_Type PinCfg; /* PWM block section -------------------------------------------- */ /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat); /* * Initialize PWM pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 2; for (temp = 0; temp <= 6; temp++){ PinCfg.Pinnum = temp; PINSEL_ConfigPin(&PinCfg); } /* Set match value for PWM match channel 0 = 256, update immediately */ PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW); /* PWM Timer/Counter will be reset when channel 0 matching * no interrupt when match * no stop when match */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Single edge * - PWM Duty on each PWM channel determined by * the match on channel 0 to the match of that match channel. * Example: PWM Duty on PWM channel 1 determined by * the match on channel 0 to the match of match channel 1. */ /* Configure PWM channel edge option * Note: PWM Channel 1 is in single mode as default state and * can not be changed to double edge mode */ for (temp = 2; temp < 7; temp++) { PWM_ChannelConfig(LPC_PWM1, temp, PWM_CHANNEL_SINGLE_EDGE); } /* Configure match value for each match channel */ temp2 = 10; for (temp = 1; temp < 7; temp++) { /* Set up match value */ PWM_MatchUpdate(LPC_PWM1, temp, temp2, PWM_MATCH_UPDATE_NOW); /* Configure match option */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = temp; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Enable PWM Channel Output */ PWM_ChannelCmd(LPC_PWM1, temp, ENABLE); /* Increase match value by 10 */ temp2 += 10; } /* Reset and Start counter */ PWM_ResetCounter(LPC_PWM1); PWM_CounterCmd(LPC_PWM1, ENABLE); /* Start PWM now */ PWM_Cmd(LPC_PWM1, ENABLE); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief Main PWM program body **********************************************************************/ int c_entry(void) { uint8_t temp, temp2; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; PINSEL_CFG_Type PinCfg; // DeInit NVIC and SCBNVIC NVIC_DeInit(); NVIC_SCBDeInit(); /* Configure the NVIC Preemption Priority Bits: * two (2) bits of preemption priority, six (6) bits of sub-priority. * Since the Number of Bits used for Priority Levels is five (5), so the * actual bit number of sub-priority is three (3) */ NVIC_SetPriorityGrouping(0x05); // Set Vector table offset value #if (__RAM_MODE__==1) NVIC_SetVTOR(0x10000000); #else NVIC_SetVTOR(0x00000000); #endif /* Initialize debug */ debug_frmwrk_init(); // print welcome screen print_menu(); /* PWM block section -------------------------------------------- */ /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat); /* * Initialize PWM pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 2; for (temp = 0; temp <= 6; temp++){ PinCfg.Pinnum = temp; PINSEL_ConfigPin(&PinCfg); } /* Set match value for PWM match channel 0 = 256, update immediately */ PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW); /* PWM Timer/Counter will be reset when channel 0 matching * no interrupt when match * no stop when match */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Single edge * - PWM Duty on each PWM channel determined by * the match on channel 0 to the match of that match channel. * Example: PWM Duty on PWM channel 1 determined by * the match on channel 0 to the match of match channel 1. */ /* Configure PWM channel edge option * Note: PWM Channel 1 is in single mode as default state and * can not be changed to double edge mode */ for (temp = 2; temp < 7; temp++) { PWM_ChannelConfig(LPC_PWM1, temp, PWM_CHANNEL_SINGLE_EDGE); } /* Configure match value for each match channel */ temp2 = 10; for (temp = 1; temp < 7; temp++) { /* Set up match value */ PWM_MatchUpdate(LPC_PWM1, temp, temp2, PWM_MATCH_UPDATE_NOW); /* Configure match option */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = temp; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Enable PWM Channel Output */ PWM_ChannelCmd(LPC_PWM1, temp, ENABLE); /* Increase match value by 10 */ temp2 += 10; } /* Reset and Start counter */ PWM_ResetCounter(LPC_PWM1); PWM_CounterCmd(LPC_PWM1, ENABLE); /* Start PWM now */ PWM_Cmd(LPC_PWM1, ENABLE); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main PWM program body * @param[in] None * @return None **********************************************************************/ void c_entry(void) { uint8_t pwmChannel, channelVal; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; /* PWM block section -------------------------------------------- */ /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(_USING_PWM_NO, PWM_MODE_TIMER, (void *) &PWMCfgDat); // Initialize PWM pin connect #if (_USING_PWM_NO == 1) for (pwmChannel = 0; pwmChannel <= 6; pwmChannel++) { PINSEL_ConfigPin (2, pwmChannel, 1); } #elif (_USING_PWM_NO == 0) PINSEL_ConfigPin (1, 2, 3);//PWM0.1 PINSEL_ConfigPin (1, 3, 3);//PWM0.2 PINSEL_ConfigPin (1, 5, 3);//PWM0.3 PINSEL_ConfigPin (1, 6, 3);//PWM0.4 PINSEL_ConfigPin (1, 7, 3);//PWM0.5 PINSEL_ConfigPin (1, 11, 3);//PWM0.6 #else return 0; #endif /* Set match value for PWM match channel 0 = 256, update immediately */ PWM_MatchUpdate(_USING_PWM_NO, 0, 256, PWM_MATCH_UPDATE_NOW); /* PWM Timer/Counter will be reset when channel 0 matching * Enable interrupt when match * no stop when match */ PWMMatchCfgDat.IntOnMatch = ENABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(_USING_PWM_NO, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Single edge * - PWM Duty on each PWM channel determined by * the match on channel 0 to the match of that match channel. * Example: PWM Duty on PWM channel 1 determined by * the match on channel 0 to the match of match channel 1. */ /* Configure PWM channel edge option * Note: PWM Channel 1 is in single mode as default state and * can not be changed to double edge mode */ for (pwmChannel = 2; pwmChannel < 7; pwmChannel++) { PWM_ChannelConfig(_USING_PWM_NO, pwmChannel, PWM_CHANNEL_SINGLE_EDGE); } /* Setting interrupt for PWM ---------------------------------------------- */ /* Disable PWM interrupt */ #if (_USING_PWM_NO == 1) NVIC_DisableIRQ(PWM1_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(PWM1_IRQn, ((0x01<<3)|0x01)); #elif (_USING_PWM_NO == 0) NVIC_DisableIRQ(PWM0_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(PWM0_IRQn, ((0x01<<3)|0x01)); #endif /* Configure match value for each match channel */ channelVal = 10; for (pwmChannel = 1; pwmChannel < 7; pwmChannel++) { /* Set up match value */ PWM_MatchUpdate(_USING_PWM_NO, pwmChannel, channelVal, PWM_MATCH_UPDATE_NOW); /* Configure match option */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = pwmChannel; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(_USING_PWM_NO, &PWMMatchCfgDat); /* Enable PWM Channel Output */ PWM_ChannelCmd(_USING_PWM_NO, pwmChannel, ENABLE); /* Increase match value by 10 */ channelVal += 30; } /* Enable PWM interrupt */ #if (_USING_PWM_NO == 1) NVIC_EnableIRQ(PWM1_IRQn); #elif (_USING_PWM_NO == 0) NVIC_EnableIRQ(PWM0_IRQn); #endif /* Reset and Start counter */ PWM_ResetCounter(_USING_PWM_NO); PWM_CounterCmd(_USING_PWM_NO, ENABLE); /* Start PWM now */ PWM_Cmd(_USING_PWM_NO, ENABLE); // Update PWM1.1 every 0x1000 match interrupt while (1) { if (match_cnt >= 0x1000) { match_cnt = 0; long_duty++; if (long_duty >= 256) { // Reset duty long_duty = 0; } // Update PWM1.1 duty PWM_MatchUpdate(_USING_PWM_NO, 1, long_duty, PWM_MATCH_UPDATE_NOW); } } }
void platform_pwm_stop( unsigned id ) { PWM_Cmd(LPC_PWM1, DISABLE); }
void platform_pwm_start( unsigned id ) { PWM_Cmd(LPC_PWM1, ENABLE); }
void pwmDisable(){ PWM_CounterCmd(LPC_PWM1, DISABLE); PWM_Cmd(LPC_PWM1, DISABLE); }
/*********************************************************************//** * @brief c_entry: Main PWM program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { uint8_t temp, temp2; PWM_TIMERCFG_Type PWMCfgDat; PWM_MATCHCFG_Type PWMMatchCfgDat; PINSEL_CFG_Type PinCfg; /* PWM block section -------------------------------------------- */ /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat); /* * Initialize PWM pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 2; for (temp = 0; temp <= 6; temp++){ PinCfg.Pinnum = temp; PINSEL_ConfigPin(&PinCfg); } /* Set match value for PWM match channel 0 = 256, update immediately */ PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW); /* PWM Timer/Counter will be reset when channel 0 matching * Enable interrupt when match * no stop when match */ PWMMatchCfgDat.IntOnMatch = ENABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Single edge * - PWM Duty on each PWM channel determined by * the match on channel 0 to the match of that match channel. * Example: PWM Duty on PWM channel 1 determined by * the match on channel 0 to the match of match channel 1. */ /* Configure PWM channel edge option * Note: PWM Channel 1 is in single mode as default state and * can not be changed to double edge mode */ for (temp = 2; temp < 7; temp++) { PWM_ChannelConfig(LPC_PWM1, temp, PWM_CHANNEL_SINGLE_EDGE); } /* Setting interrupt for PWM ---------------------------------------------- */ /* Disable PWM interrupt */ NVIC_DisableIRQ(PWM1_IRQn); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(PWM1_IRQn, ((0x01<<3)|0x01)); /* Configure match value for each match channel */ temp2 = 10; for (temp = 1; temp < 7; temp++) { /* Set up match value */ PWM_MatchUpdate(LPC_PWM1, temp, temp2, PWM_MATCH_UPDATE_NOW); /* Configure match option */ PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = temp; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat); /* Enable PWM Channel Output */ PWM_ChannelCmd(LPC_PWM1, temp, ENABLE); /* Increase match value by 10 */ temp2 += 10; } /* Enable PWM interrupt */ NVIC_EnableIRQ(PWM1_IRQn); /* Reset and Start counter */ PWM_ResetCounter(LPC_PWM1); PWM_CounterCmd(LPC_PWM1, ENABLE); /* Start PWM now */ PWM_Cmd(LPC_PWM1, ENABLE); // Update PWM1.1 every 0x1000 match interrupt while (1) { if (match_cnt >= 0x1000) { match_cnt = 0; long_duty++; if (long_duty >= 256) { // Reset duty long_duty = 0; // Print info //UART_Puts_(uartdev, "PWM1.1 is reset!"); } // Update PWM1.1 duty PWM_MatchUpdate(LPC_PWM1, 1, long_duty, PWM_MATCH_UPDATE_NOW); } } return 1; }
PWM::PWM(uint8_t port, uint8_t pin, float dutyCycle, float frequency) { this->port = port; this->pin = pin; this->dutyCycle = dutyCycle; this->frequency = frequency; PINSEL_CFG_Type PinCfg; if(port == 1 && pin == 18) { PinCfg.Funcnum = 2; this->channel = 1; this->peripheral = LPC_PWM1; } else if(port == 1 && pin == 20) { PinCfg.Funcnum = 2; this->channel = 2; this->peripheral = LPC_PWM1; } else if(port == 1 && pin == 21) { PinCfg.Funcnum = 2; this->channel = 3; this->peripheral = LPC_PWM1; } else if(port == 1 && pin == 23) { PinCfg.Funcnum = 2; this->channel = 4; this->peripheral = LPC_PWM1; } else if(port == 1 && pin == 24) { PinCfg.Funcnum = 2; this->channel = 5; this->peripheral = LPC_PWM1; } else if(port == 1 && pin == 26) { PinCfg.Funcnum = 2; this->channel = 6; this->peripheral = LPC_PWM1; } else if(port == 2 && pin == 0) { PinCfg.Funcnum = 1; this->channel = 1; this->peripheral = LPC_PWM1; } else if(port == 2 && pin == 1) { PinCfg.Funcnum = 1; this->channel = 2; this->peripheral = LPC_PWM1; } else if(port == 2 && pin == 2) { PinCfg.Funcnum = 1; this->channel = 3; this->peripheral = LPC_PWM1; } else if(port == 2 && pin == 3) { PinCfg.Funcnum = 1; this->channel = 4; this->peripheral = LPC_PWM1; } else if(port == 2 && pin == 4) { PinCfg.Funcnum = 1; this->channel = 5; this->peripheral = LPC_PWM1; } else if(port == 2 && pin == 5) { PinCfg.Funcnum = 1; this->channel = 6; this->peripheral = LPC_PWM1; } else if(port == 3 && pin == 25) { PinCfg.Funcnum = 3; this->channel = 2; this->peripheral = LPC_PWM1; } else if(port == 3 && pin == 26) { PinCfg.Funcnum = 3; this->channel = 3; this->peripheral = LPC_PWM1; } else { //invalid port/pin specified // check_failed((uint8_t *)__FILE__, __LINE__); while(1); } PinCfg.Portnum = port; PinCfg.Pinnum = pin; PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE; PINSEL_ConfigPin(&PinCfg); if(isInitialized) { uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1); /* Set match value for PWM match channel 0 and reset on match to set the period for all channels */ PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW); PWM_MATCHCFG_Type PWMMatchCfgDat; PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Single edge * - PWM Duty on each PWM channel determined by * the match on channel 0 to the match of that match channel. * Example: PWM Duty on PWM channel 1 determined by * the match on channel 0 to the match of match channel 1. */ PWM_ChannelConfig(this->peripheral, this->channel, PWM_CHANNEL_SINGLE_EDGE); /* Set up match value */ PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NOW); PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = this->channel; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat); /* Enable PWM Channel Output */ PWM_ChannelCmd(this->peripheral, this->channel, ENABLE); return; } isInitialized = true; /* PWM block section -------------------------------------------- */ /* Initialize PWM peripheral, timer mode * PWM prescale value = 1 (absolute value - tick value) */ PWM_TIMERCFG_Type PWMCfgDat; PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL; PWMCfgDat.PrescaleValue = 1; PWM_Init(this->peripheral, PWM_MODE_TIMER, (void*)&PWMCfgDat); uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1); /* Set match value for PWM match channel 0 and reset on match to set the period for all channels */ PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW); PWM_MATCHCFG_Type PWMMatchCfgDat; PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = 0; PWMMatchCfgDat.ResetOnMatch = ENABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat); /* Configure each PWM channel: --------------------------------------------- */ /* - Single edge * - PWM Duty on each PWM channel determined by * the match on channel 0 to the match of that match channel. * Example: PWM Duty on PWM channel 1 determined by * the match on channel 0 to the match of match channel 1. */ PWM_ChannelConfig(this->peripheral, this->channel, PWM_CHANNEL_SINGLE_EDGE); /* Set up match value */ PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NOW); PWMMatchCfgDat.IntOnMatch = DISABLE; PWMMatchCfgDat.MatchChannel = this->channel; PWMMatchCfgDat.ResetOnMatch = DISABLE; PWMMatchCfgDat.StopOnMatch = DISABLE; PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat); /* Enable PWM Channel Output */ PWM_ChannelCmd(this->peripheral, this->channel, ENABLE); /* Reset and Start counter */ PWM_ResetCounter(this->peripheral); PWM_CounterCmd(this->peripheral, ENABLE); /* Start PWM now */ PWM_Cmd(this->peripheral, ENABLE); }
/* Pre-Reqs:SetupPWM() Desc: Sets PWM's match registers Inputs: uint8_t match_channel - match register to update uint32_t match_value - value to update the match register with */ void SetRawPWM(uint8_t match_channel, uint32_t match_value){ PWM_Cmd(LPC_PWM1, DISABLE); PWM_MatchUpdate(LPC_PWM1, match_channel, match_value, PWM_MATCH_UPDATE_NOW); PWM_Cmd(LPC_PWM1, ENABLE); }