#include "LPC13xx.h" int main(void) { /*************************************************************************//** Set up 16 bit counters CT16B0 and CT16B1 to run as PWM with a cycle length of 4096 *****************************************************************************/ LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7); // Enable clock to timer 0, CT16B0 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8); // Enable clock to timer 1, CT16B1 // 16 bit timer 0 - CT16B0 // PIO0_8 IS A LPC_IOCON->PIO0_8 |= 0x02; // - set bit 1 to connect pin to CT16B0_MAT0 LPC_IOCON->PIO0_8 &= ~(1 << 3); // - clear bit 3 (and set 4: pull-up resistor) LPC_IOCON->PIO0_8 |= (1 << 4); // - set bit 4 // PIO0_9 IS C LPC_IOCON->PIO0_9 |= 0x02; // - set bit 1 to connect pin to CT16B0_MAT1 LPC_IOCON->PIO0_9 &= ~(1 << 3); // - clear bit 3 (and set 4: pull-up resistor) LPC_IOCON->PIO0_9 |= (1 << 4); // - set bit 4 // 16 bit timer 1 - CT16B1 // PIO1_9 IS B LPC_IOCON->PIO1_9 |= 0x01; // - set bit 0 to connect pin to CT16B1_MAT0 LPC_IOCON->PIO1_9 &= ~(1 << 3); // - clear bit 3 (and set 4: pull-up resistor) LPC_IOCON->PIO1_9 |= (1 << 4); // - set bit 4 // PIO1_10 IS D LPC_IOCON->PIO1_10 |= 0x02; // - set bit 1 to connect pin to CT16B1_MAT1 LPC_IOCON->PIO1_10 &= ~(1 << 3);// - clear bit 3 (and set 4: pull-up resistor) LPC_IOCON->PIO1_10 |= (1 << 4);// - set bit 4 LPC_TMR16B0->MCR |= (1 << 10);// Reset TC counter when it reaches Match Register 3 value LPC_TMR16B1->MCR |= (1 << 10);// Reset TC counter when it reaches Match Register 3 value LPC_TMR16B0->EMR |= 0x000000F0;// Toggle output when a match occurs with MR0 and MR1 LPC_TMR16B1->EMR |= 0x000000F0;// Toggle output when a match occurs with MR0 and MR1 LPC_TMR16B0->MR0 = 2048;// A duty cycle - TMR16B0 Match register 0 LPC_TMR16B0->MR1 = 2048; // C duty cycle - TMR16B0 Match register 1 LPC_TMR16B0->MR3 = 4096; // Cycle length 4096 (= 17.8kHz) - Match register 3 LPC_TMR16B1->MR0 = 2048; // B duty cycle - TMR16B1 Match register 0 LPC_TMR16B1->MR1 = 2048; // D duty cycle - TMR16B1 Match register 1 LPC_TMR16B1->MR3 = 4096; // Cycle length 4096 (= 17.8kHz) - Match register 3 LPC_TMR16B0->PWMC |= (1 << 0); // A Timer 0 MAT0 is PWM LPC_TMR16B0->PWMC |= (1 << 1); // C Timer 0 MAT1 is PWM LPC_TMR16B1->PWMC |= (1 << 0); // B Timer 1 MAT0 is PWM LPC_TMR16B1->PWMC |= (1 << 1); // D Timer 1 MAT1 is PWM LPC_GPIO0->DIR |= (1 << 8); // - select OUTPUT LPC_GPIO0->DIR |= (1 << 9); // - select OUTPUT LPC_GPIO1->DIR |= (1 << 9); // - select OUTPUT LPC_GPIO1->DIR |= (1 << 10); // - select OUTPUT LPC_TMR16B0->TCR = 1; //enable timer 0 LPC_TMR16B1->TCR = 1; //enable timer 1 while(1){} } |
/* Setup the external match register */ LPC_TMR16B1->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(2<<EMC0)|(1<<3)|(match_enable); |
/* comments... */ /* comments... */ /* comments... */ |
//switching PIO0_8 low/high/low/PWM(1kHz) i=0;while(i<100000){i++;} //delay LPC_TMR16B0->MR0 =LPC_TMR16B0->MR3+1; //match register > reset register => output low i=0;while(i<100000){i++;} //delay LPC_TMR16B0->MR0 =0; //match register = 0 => output high i=0;while(i<100000){i++;} //delay LPC_TMR16B0->MR0 =LPC_TMR16B0->MR3+1; //match register > reset register => output low i=0;while(i<100000){i++;} //delay //reset counter to get full PWM cycle again, don't use this command //if other MAT outputs of this timer are still working //LPC_TMR16B0->TC = 0; //reset counter LPC_TMR16B0->MR0 =LPC_TMR16B0->MR3/4; //valid match register again |
LPC_GPIO0->DIR |= (1 << 8); // - select OUTPUT LPC_GPIO0->DIR |= (1 << 9); // - select OUTPUT LPC_GPIO1->DIR |= (1 << 9); // - select OUTPUT LPC_GPIO1->DIR |= (1 << 10); // - select OUTPUT |
LPC_TMR16B0->EMR |= 0x000000F0; // Toggle output when a match occurs with MR0 and MR1 LPC_TMR16B1->EMR |= 0x000000F0; // Toggle output when a match occurs with MR0 and MR1 |
//PIO0_8 LPC_IOCON->PIO0_8 &= ~((1<<10)|(1<<5)|(3<<3)|(7<<0)); //reset func,mode,hys,OD LPC_IOCON->PIO0_8 |= ((2<<3)|(2<<0)); //set CT16B0 MAT0, pullup |
//set prescaler LPC_TMR16B0->PR = 71; //prescaler:72 -> time base: 1/1MHz = 1µs |
int main(void) { volatile unsigned int i; printf("PWM16 Sample\n"); //Init PWM LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7); //enables clock for 16-bit counter/timer 0 LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8); //enables clock for 16-bit counter/timer 1 //PIO0_8 LPC_IOCON->PIO0_8 &= ~((1<<10)|(1<<5)|(3<<3)|(7<<0)); //reset func,mode,hys,OD LPC_IOCON->PIO0_8 |= ((2<<3)|(2<<0)); //set CT16B0 MAT0, pullup //PIO0_9 LPC_IOCON->PIO0_9 &= ~((1<<10)|(1<<5)|(3<<3)|(7<<0)); //reset func,mode,hys,OD LPC_IOCON->PIO0_9 |= ((2<<3)|(2<<0)); //set CT16B0 MAT1, pullup //PIO1_9 LPC_IOCON->PIO1_9 &= ~((1<<10)|(1<<5)|(3<<3)|(7<<0)); //reset func,mode,hys,OD LPC_IOCON->PIO1_9 |= ((2<<3)|(1<<0)); //set CT16B1 MAT0, pullup //PIO1_10 LPC_IOCON->PIO1_10 &= ~((1<<10)|(1<<7)|(1<<5)|(3<<3)|(7<<0)); //reset func,mode,hys,admode,OD LPC_IOCON->PIO1_10 |= ((1<<7)|(2<<3)|(2<<0)); //set CT16B1 MAT1, pullup, digital //set match / interrupts LPC_TMR16B0->MCR = (1 << 10); //MR3 reset, no interrupts LPC_TMR16B1->MCR = (1 << 10); //MR3 reset, no interrupts //set prescaler LPC_TMR16B0->PR = 71; //prescaler:72 -> time base: 1/1MHz = 1µs LPC_TMR16B1->PR = 71; //prescaler:72 -> time base: 1/1MHz = 1µs LPC_TMR16B0->MR3 = 1000; //cycle: 1000µs = 1kHz LPC_TMR16B0->MR0 = LPC_TMR16B0->MR3/4; //MAT0 1/4 low, 3/4 high LPC_TMR16B0->MR1 = LPC_TMR16B0->MR3/2; //MAT1 1/2 low, 1/2 high LPC_TMR16B1->MR3 = 500; //cycle: 500µs = 2kHz LPC_TMR16B1->MR0 = LPC_TMR16B1->MR3/4; //MAT0 1/4 low, 3/4 high LPC_TMR16B1->MR1 = LPC_TMR16B1->MR3/2; //MAT1 1/2 low, 1/2 high LPC_TMR16B0->PWMC = ((1<<0)|(1<<1)); //enable PMM MAT0/MAT1 LPC_TMR16B1->PWMC = ((1<<0)|(1<<1)); //enable PMM MAT0/MAT1 LPC_TMR16B0->TCR = 1; //start timer 0 LPC_TMR16B1->TCR = 1; //start timer 1 while(1) { //switching PIO0_8 low/high/low/PWM(1kHz) i=0;while(i<100000){i++;} //delay LPC_TMR16B0->TCR = 0; //stop timer 0 LPC_TMR16B0->EMR &=~(1<<0); //force CT16B0 MAT0 low i=0;while(i<100000){i++;} //delay LPC_TMR16B0->EMR |= (1<<0); //force CT16B0 MAT0 high i=0;while(i<100000){i++;} //delay LPC_TMR16B0->EMR &=~(1<<0); //force CT16B0 MAT0 low i=0;while(i<100000){i++;} //delay LPC_TMR16B0->TC = 0; //reset counter LPC_TMR16B0->TCR = 1; //start timer 0 } return 0 ; } |