![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LPC800-MAX RGB demo using SCT and MRT
LPC800-MAX RGB demo using State-Configurable Timer(SCT) and Multi-Rate Timer(MRT).
http://www.youtube.com/watch?v=PABxoWZB0YM
SCT_PWM.cpp@1:4e19f154ec21, 2013-10-14 (annotated)
- Committer:
- va009039
- Date:
- Mon Oct 14 03:59:30 2013 +0000
- Revision:
- 1:4e19f154ec21
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 1:4e19f154ec21 | 1 | #include "SCT_PWM.h" |
va009039 | 1:4e19f154ec21 | 2 | |
va009039 | 1:4e19f154ec21 | 3 | #define BIT_ON(A) (1<<A) |
va009039 | 1:4e19f154ec21 | 4 | |
va009039 | 1:4e19f154ec21 | 5 | // http://www.lpcware.com/content/nxpfile/lpc80-sct-cookbook-and-tutorial-code-examples |
va009039 | 1:4e19f154ec21 | 6 | |
va009039 | 1:4e19f154ec21 | 7 | SCT_PWM::SCT_PWM(int pin, int channel) : ch(channel) |
va009039 | 1:4e19f154ec21 | 8 | { |
va009039 | 1:4e19f154ec21 | 9 | static bool insted = false; |
va009039 | 1:4e19f154ec21 | 10 | |
va009039 | 1:4e19f154ec21 | 11 | if (!insted) { |
va009039 | 1:4e19f154ec21 | 12 | inst(); |
va009039 | 1:4e19f154ec21 | 13 | insted = true; |
va009039 | 1:4e19f154ec21 | 14 | } |
va009039 | 1:4e19f154ec21 | 15 | swm(pin); |
va009039 | 1:4e19f154ec21 | 16 | |
va009039 | 1:4e19f154ec21 | 17 | LPC_SCT->EVENT[ch + 1].STATE = LPC_SCT->EVENT[0].STATE; |
va009039 | 1:4e19f154ec21 | 18 | LPC_SCT->EVENT[ch + 1].CTRL = LPC_SCT->EVENT[0].CTRL | (ch+1)<<0; // match 1...5 |
va009039 | 1:4e19f154ec21 | 19 | |
va009039 | 1:4e19f154ec21 | 20 | LPC_SCT->OUT[ch].SET = BIT_ON(0); // event 0 |
va009039 | 1:4e19f154ec21 | 21 | LPC_SCT->OUT[ch].CLR = BIT_ON(ch+1); // event 1...5 |
va009039 | 1:4e19f154ec21 | 22 | } |
va009039 | 1:4e19f154ec21 | 23 | |
va009039 | 1:4e19f154ec21 | 24 | void SCT_PWM::inst() |
va009039 | 1:4e19f154ec21 | 25 | { |
va009039 | 1:4e19f154ec21 | 26 | LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8); // enable SCT |
va009039 | 1:4e19f154ec21 | 27 | LPC_SYSCON->PRESETCTRL |= (1<<8); // reset SCT |
va009039 | 1:4e19f154ec21 | 28 | |
va009039 | 1:4e19f154ec21 | 29 | LPC_SCT->EVENT[0].STATE = BIT_ON(0); // state 0 |
va009039 | 1:4e19f154ec21 | 30 | LPC_SCT->EVENT[0].CTRL = (1<<12) | (1<<14); |
va009039 | 1:4e19f154ec21 | 31 | |
va009039 | 1:4e19f154ec21 | 32 | LPC_SCT->LIMIT_L |= BIT_ON(0); // event 0 |
va009039 | 1:4e19f154ec21 | 33 | |
va009039 | 1:4e19f154ec21 | 34 | LPC_SCT->CONFIG |= (1<<0) | (1<<17); // unify, auto limit |
va009039 | 1:4e19f154ec21 | 35 | LPC_SCT->CTRL_L |= (SystemCoreClock/1000000-1)<<5; // set prescaler, SCT clock = 1 MHz |
va009039 | 1:4e19f154ec21 | 36 | |
va009039 | 1:4e19f154ec21 | 37 | LPC_SCT->CTRL_L &= ~(1<<2); // unhalt it by clearing bit 2 of CTRL reg |
va009039 | 1:4e19f154ec21 | 38 | } |
va009039 | 1:4e19f154ec21 | 39 | |
va009039 | 1:4e19f154ec21 | 40 | void SCT_PWM::swm(int pin) |
va009039 | 1:4e19f154ec21 | 41 | { |
va009039 | 1:4e19f154ec21 | 42 | switch(ch) { |
va009039 | 1:4e19f154ec21 | 43 | case 0: // CTOUT_0 |
va009039 | 1:4e19f154ec21 | 44 | LPC_SWM->PINASSIGN6 = (LPC_SWM->PINASSIGN6 & 0x00ffffff) | pin<<24; |
va009039 | 1:4e19f154ec21 | 45 | break; |
va009039 | 1:4e19f154ec21 | 46 | case 1: // CTOUT_1 |
va009039 | 1:4e19f154ec21 | 47 | LPC_SWM->PINASSIGN7 = (LPC_SWM->PINASSIGN7 & 0xffffff00) | pin<<0; |
va009039 | 1:4e19f154ec21 | 48 | break; |
va009039 | 1:4e19f154ec21 | 49 | case 2: // CTOUT_2 |
va009039 | 1:4e19f154ec21 | 50 | LPC_SWM->PINASSIGN7 = (LPC_SWM->PINASSIGN7 & 0xffff00ff) | pin<<8; |
va009039 | 1:4e19f154ec21 | 51 | break; |
va009039 | 1:4e19f154ec21 | 52 | case 3: // CTOUT_3 |
va009039 | 1:4e19f154ec21 | 53 | LPC_SWM->PINASSIGN7 = (LPC_SWM->PINASSIGN7 & 0xff00ffff) | pin<<16; |
va009039 | 1:4e19f154ec21 | 54 | break; |
va009039 | 1:4e19f154ec21 | 55 | } |
va009039 | 1:4e19f154ec21 | 56 | } |
va009039 | 1:4e19f154ec21 | 57 | |
va009039 | 1:4e19f154ec21 | 58 | void SCT_PWM::period(float seconds) |
va009039 | 1:4e19f154ec21 | 59 | { |
va009039 | 1:4e19f154ec21 | 60 | period_us(seconds * 1000000); |
va009039 | 1:4e19f154ec21 | 61 | } |
va009039 | 1:4e19f154ec21 | 62 | |
va009039 | 1:4e19f154ec21 | 63 | void SCT_PWM::period_ms(int ms) |
va009039 | 1:4e19f154ec21 | 64 | { |
va009039 | 1:4e19f154ec21 | 65 | period_us(ms * 1000); |
va009039 | 1:4e19f154ec21 | 66 | } |
va009039 | 1:4e19f154ec21 | 67 | |
va009039 | 1:4e19f154ec21 | 68 | void SCT_PWM::period_us(int us) |
va009039 | 1:4e19f154ec21 | 69 | { |
va009039 | 1:4e19f154ec21 | 70 | LPC_SCT->MATCHREL[0].U = us - 1; |
va009039 | 1:4e19f154ec21 | 71 | } |
va009039 | 1:4e19f154ec21 | 72 | |
va009039 | 1:4e19f154ec21 | 73 | void SCT_PWM::pulsewidth(float seconds) |
va009039 | 1:4e19f154ec21 | 74 | { |
va009039 | 1:4e19f154ec21 | 75 | pulsewidth_us(seconds * 1000000); |
va009039 | 1:4e19f154ec21 | 76 | } |
va009039 | 1:4e19f154ec21 | 77 | |
va009039 | 1:4e19f154ec21 | 78 | void SCT_PWM::pulsewidth_ms(int ms) |
va009039 | 1:4e19f154ec21 | 79 | { |
va009039 | 1:4e19f154ec21 | 80 | pulsewidth_us(ms * 1000); |
va009039 | 1:4e19f154ec21 | 81 | } |
va009039 | 1:4e19f154ec21 | 82 | |
va009039 | 1:4e19f154ec21 | 83 | void SCT_PWM::pulsewidth_us(int us) |
va009039 | 1:4e19f154ec21 | 84 | { |
va009039 | 1:4e19f154ec21 | 85 | LPC_SCT->MATCHREL[ch + 1].U = us; |
va009039 | 1:4e19f154ec21 | 86 | } |
va009039 | 1:4e19f154ec21 | 87 | |
va009039 | 1:4e19f154ec21 | 88 | void SCT_PWM::write(float value) |
va009039 | 1:4e19f154ec21 | 89 | { |
va009039 | 1:4e19f154ec21 | 90 | LPC_SCT->MATCHREL[ch + 1].U = value * LPC_SCT->MATCHREL[0].U; |
va009039 | 1:4e19f154ec21 | 91 | } |
va009039 | 1:4e19f154ec21 | 92 | |
va009039 | 1:4e19f154ec21 | 93 | float SCT_PWM::read() |
va009039 | 1:4e19f154ec21 | 94 | { |
va009039 | 1:4e19f154ec21 | 95 | if (LPC_SCT->MATCHREL[0].U == 0) { |
va009039 | 1:4e19f154ec21 | 96 | return 0.0; |
va009039 | 1:4e19f154ec21 | 97 | } |
va009039 | 1:4e19f154ec21 | 98 | return (float)LPC_SCT->MATCHREL[ch + 1].U / LPC_SCT->MATCHREL[0].U; |
va009039 | 1:4e19f154ec21 | 99 | } |