User manual
LPCXpresso Experiment Kit - User’s Guide
Page 73
Copyright 2013 © Embedded Artists AB
**
** Parameters: cycleLength: set PWM cycle length in microseconds
**
** Returned value: None
**
*****************************************************************************/
void initPWM(uint16_t cycleLengthInUs)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8); /* Enable timer #1 (enable clock to block) */
//setup I/O pins to be MAT-outputs
LPC_IOCON->PIO1_9 &= ~0x07;
LPC_IOCON->PIO1_9 |= 0x01; /* 16-bit timer#1 MAT0 */
LPC_IOCON->PIO1_10 &= ~0x07;
LPC_IOCON->PIO1_10 |= 0x02; /* 16-bit timer#1 MAT1 */
LPC_TMR16B1->TCR = 0x02; /* reset timer */
/* Set prescaler so that timer counts in us-steps */
/*(SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV) = 48000000 => Timer clock is 48MHz */
LPC_TMR16B1->PR = ((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV) / 1000000) - 1;
LPC_TMR16B1->MR2 = cycleLengthInUs;
//Setup match registers to generate a PWM signal with 0% duty = constant low
LPC_TMR16B1->MR0 = LPC_TMR16B1->MR2;
LPC_TMR16B1->MR1 = LPC_TMR16B1->MR2;
LPC_TMR16B1->IR = 0xff; /* reset all interrupts (not needed) */
LPC_TMR16B1->MCR = (1<<7); /* reset timer on MR2 match */
LPC_TMR16B1->PWMC = (1<<0) | (1<<1); /* Enable PWM mode for MAT0 and MAT1 */
}
/*****************************************************************************
** Function name: startPWM
**
** Descriptions: Start 16-bit timer #1
**
** Parameters: None
**
** Returned value: None
**
*****************************************************************************/
void startPWM(void)
{
LPC_TMR16B1->TCR = 0x01; /* start timer (16B1) = start generating PWM signal(s) */
}
/*****************************************************************************
** Function name: stopPWM
**
** Descriptions: Stop 16-bit timer #1
**
** Parameters: None
**
** Returned value: None
**
*****************************************************************************/
void stopPWM(void)
{
LPC_TMR16B1->TCR = 0x00; /* stop timer (16B1) = stop generating PWM signal(s) */
}
/*****************************************************************************
** Function name: updatePWM
**
** Descriptions: Update the PWM output setting
**
** Parameters: channel: selects with PWM signals to update (0 or 1)
** value: set duty cycle (a value between 0 and 100)
**
** Returned value: None
**
*****************************************************************************/
void updatePWM( uint8_t channel, uint8_t value)
{