This Program Demonstrate how to make LPC1768 ADC measure the analog voltage and report it to through the USB uart of mbed.

Dependencies:   mbed

Committer:
boseji
Date:
Thu Sep 30 15:36:20 2010 +0000
Revision:
0:c0f5fc067a00
First version with Power Save

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boseji 0:c0f5fc067a00 1 /* mbed PowerControl Library
boseji 0:c0f5fc067a00 2 * Copyright (c) 2010 Michael Wei
boseji 0:c0f5fc067a00 3 */
boseji 0:c0f5fc067a00 4
boseji 0:c0f5fc067a00 5 #ifndef MBED_POWERCONTROL_H
boseji 0:c0f5fc067a00 6 #define MBED_POWERCONTROL_H
boseji 0:c0f5fc067a00 7
boseji 0:c0f5fc067a00 8 //shouldn't have to include, but fixes weird problems with defines
boseji 0:c0f5fc067a00 9 #include "LPC1768/LPC17xx.h"
boseji 0:c0f5fc067a00 10
boseji 0:c0f5fc067a00 11 //System Control Register
boseji 0:c0f5fc067a00 12 // bit 0: Reserved
boseji 0:c0f5fc067a00 13 // bit 1: Sleep on Exit
boseji 0:c0f5fc067a00 14 #define LPC1768_SCR_SLEEPONEXIT 0x2
boseji 0:c0f5fc067a00 15 // bit 2: Deep Sleep
boseji 0:c0f5fc067a00 16 #define LPC1768_SCR_SLEEPDEEP 0x4
boseji 0:c0f5fc067a00 17 // bit 3: Resereved
boseji 0:c0f5fc067a00 18 // bit 4: Send on Pending
boseji 0:c0f5fc067a00 19 #define LPC1768_SCR_SEVONPEND 0x10
boseji 0:c0f5fc067a00 20 // bit 5-31: Reserved
boseji 0:c0f5fc067a00 21
boseji 0:c0f5fc067a00 22 //Power Control Register
boseji 0:c0f5fc067a00 23 // bit 0: Power mode control bit 0 (power-down mode)
boseji 0:c0f5fc067a00 24 #define LPC1768_PCON_PM0 0x1
boseji 0:c0f5fc067a00 25 // bit 1: Power mode control bit 1 (deep power-down mode)
boseji 0:c0f5fc067a00 26 #define LPC1768_PCON_PM1 0x2
boseji 0:c0f5fc067a00 27 // bit 2: Brown-out reduced power mode
boseji 0:c0f5fc067a00 28 #define LPC1768_PCON_BODRPM 0x4
boseji 0:c0f5fc067a00 29 // bit 3: Brown-out global disable
boseji 0:c0f5fc067a00 30 #define LPC1768_PCON_BOGD 0x8
boseji 0:c0f5fc067a00 31 // bit 4: Brown-out reset disable
boseji 0:c0f5fc067a00 32 #define LPC1768_PCON_BORD 0x10
boseji 0:c0f5fc067a00 33 // bit 5-7 : Reserved
boseji 0:c0f5fc067a00 34 // bit 8: Sleep Mode Entry Flag
boseji 0:c0f5fc067a00 35 #define LPC1768_PCON_SMFLAG 0x100
boseji 0:c0f5fc067a00 36 // bit 9: Deep Sleep Entry Flag
boseji 0:c0f5fc067a00 37 #define LPC1768_PCON_DSFLAG 0x200
boseji 0:c0f5fc067a00 38 // bit 10: Power Down Entry Flag
boseji 0:c0f5fc067a00 39 #define LPC1768_PCON_PDFLAG 0x400
boseji 0:c0f5fc067a00 40 // bit 11: Deep Power Down Entry Flag
boseji 0:c0f5fc067a00 41 #define LPC1768_PCON_DPDFLAG 0x800
boseji 0:c0f5fc067a00 42 // bit 12-31: Reserved
boseji 0:c0f5fc067a00 43
boseji 0:c0f5fc067a00 44 //"Sleep Mode" (WFI).
boseji 0:c0f5fc067a00 45 inline void Sleep(void)
boseji 0:c0f5fc067a00 46 {
boseji 0:c0f5fc067a00 47 __WFI();
boseji 0:c0f5fc067a00 48 }
boseji 0:c0f5fc067a00 49
boseji 0:c0f5fc067a00 50 //"Deep Sleep" Mode
boseji 0:c0f5fc067a00 51 inline void DeepSleep(void)
boseji 0:c0f5fc067a00 52 {
boseji 0:c0f5fc067a00 53 SCB->SCR |= LPC1768_SCR_SLEEPDEEP;
boseji 0:c0f5fc067a00 54 __WFI();
boseji 0:c0f5fc067a00 55 }
boseji 0:c0f5fc067a00 56
boseji 0:c0f5fc067a00 57 //"Power-Down" Mode
boseji 0:c0f5fc067a00 58 inline void PowerDown(void)
boseji 0:c0f5fc067a00 59 {
boseji 0:c0f5fc067a00 60 SCB->SCR |= LPC1768_SCR_SLEEPDEEP;
boseji 0:c0f5fc067a00 61 LPC_SC->PCON &= ~LPC1768_PCON_PM1;
boseji 0:c0f5fc067a00 62 LPC_SC->PCON |= LPC1768_PCON_PM0;
boseji 0:c0f5fc067a00 63 __WFI();
boseji 0:c0f5fc067a00 64 //reset back to normal
boseji 0:c0f5fc067a00 65 LPC_SC->PCON &= ~(LPC1768_PCON_PM1 | LPC1768_PCON_PM0);
boseji 0:c0f5fc067a00 66 }
boseji 0:c0f5fc067a00 67
boseji 0:c0f5fc067a00 68 //"Deep Power-Down" Mode
boseji 0:c0f5fc067a00 69 inline void DeepPowerDown(void)
boseji 0:c0f5fc067a00 70 {
boseji 0:c0f5fc067a00 71 SCB->SCR |= LPC1768_SCR_SLEEPDEEP;
boseji 0:c0f5fc067a00 72 LPC_SC->PCON |= LPC1768_PCON_PM1 | LPC1768_PCON_PM0;
boseji 0:c0f5fc067a00 73 __WFI();
boseji 0:c0f5fc067a00 74 //reset back to normal
boseji 0:c0f5fc067a00 75 LPC_SC->PCON &= ~(LPC1768_PCON_PM1 | LPC1768_PCON_PM0);
boseji 0:c0f5fc067a00 76 }
boseji 0:c0f5fc067a00 77
boseji 0:c0f5fc067a00 78 //shut down BOD during power-down/deep sleep
boseji 0:c0f5fc067a00 79 inline void BrownOut_ReducedPowerMode_Enable(void)
boseji 0:c0f5fc067a00 80 {
boseji 0:c0f5fc067a00 81 LPC_SC->PCON |= LPC1768_PCON_BODRPM;
boseji 0:c0f5fc067a00 82 }
boseji 0:c0f5fc067a00 83
boseji 0:c0f5fc067a00 84 //turn on BOD during power-down/deep sleep
boseji 0:c0f5fc067a00 85 inline void BrownOut_ReducedPowerMode_Disable(void)
boseji 0:c0f5fc067a00 86 {
boseji 0:c0f5fc067a00 87 LPC_SC->PCON &= ~LPC1768_PCON_BODRPM;
boseji 0:c0f5fc067a00 88 }
boseji 0:c0f5fc067a00 89
boseji 0:c0f5fc067a00 90 //turn off brown out circutry
boseji 0:c0f5fc067a00 91 inline void BrownOut_Global_Disable(void)
boseji 0:c0f5fc067a00 92 {
boseji 0:c0f5fc067a00 93 LPC_SC->PCON |= LPC1768_PCON_BOGD;
boseji 0:c0f5fc067a00 94 }
boseji 0:c0f5fc067a00 95
boseji 0:c0f5fc067a00 96 //turn on brown out circutry
boseji 0:c0f5fc067a00 97 inline void BrownOut_Global_Enable(void)
boseji 0:c0f5fc067a00 98 {
boseji 0:c0f5fc067a00 99 LPC_SC->PCON &= !LPC1768_PCON_BOGD;
boseji 0:c0f5fc067a00 100 }
boseji 0:c0f5fc067a00 101
boseji 0:c0f5fc067a00 102 //turn off brown out reset circutry
boseji 0:c0f5fc067a00 103 inline void BrownOut_Reset_Disable(void)
boseji 0:c0f5fc067a00 104 {
boseji 0:c0f5fc067a00 105 LPC_SC->PCON |= LPC1768_PCON_BORD;
boseji 0:c0f5fc067a00 106 }
boseji 0:c0f5fc067a00 107
boseji 0:c0f5fc067a00 108 //turn on brown outreset circutry
boseji 0:c0f5fc067a00 109 inline void BrownOut_Reset_Enable(void)
boseji 0:c0f5fc067a00 110 {
boseji 0:c0f5fc067a00 111 LPC_SC->PCON &= ~LPC1768_PCON_BORD;
boseji 0:c0f5fc067a00 112 }
boseji 0:c0f5fc067a00 113 //Peripheral Control Register
boseji 0:c0f5fc067a00 114 // bit 0: Reserved
boseji 0:c0f5fc067a00 115 // bit 1: PCTIM0: Timer/Counter 0 power/clock enable
boseji 0:c0f5fc067a00 116 #define LPC1768_PCONP_PCTIM0 0x2
boseji 0:c0f5fc067a00 117 // bit 2: PCTIM1: Timer/Counter 1 power/clock enable
boseji 0:c0f5fc067a00 118 #define LPC1768_PCONP_PCTIM1 0x4
boseji 0:c0f5fc067a00 119 // bit 3: PCUART0: UART 0 power/clock enable
boseji 0:c0f5fc067a00 120 #define LPC1768_PCONP_PCUART0 0x8
boseji 0:c0f5fc067a00 121 // bit 4: PCUART1: UART 1 power/clock enable
boseji 0:c0f5fc067a00 122 #define LPC1768_PCONP_PCUART1 0x10
boseji 0:c0f5fc067a00 123 // bit 5: Reserved
boseji 0:c0f5fc067a00 124 // bit 6: PCPWM1: PWM 1 power/clock enable
boseji 0:c0f5fc067a00 125 #define LPC1768_PCONP_PCPWM1 0x40
boseji 0:c0f5fc067a00 126 // bit 7: PCI2C0: I2C interface 0 power/clock enable
boseji 0:c0f5fc067a00 127 #define LPC1768_PCONP_PCI2C0 0x80
boseji 0:c0f5fc067a00 128 // bit 8: PCSPI: SPI interface power/clock enable
boseji 0:c0f5fc067a00 129 #define LPC1768_PCONP_PCSPI 0x100
boseji 0:c0f5fc067a00 130 // bit 9: PCRTC: RTC power/clock enable
boseji 0:c0f5fc067a00 131 #define LPC1768_PCONP_PCRTC 0x200
boseji 0:c0f5fc067a00 132 // bit 10: PCSSP1: SSP interface 1 power/clock enable
boseji 0:c0f5fc067a00 133 #define LPC1768_PCONP_PCSSP1 0x400
boseji 0:c0f5fc067a00 134 // bit 11: Reserved
boseji 0:c0f5fc067a00 135 // bit 12: PCADC: A/D converter power/clock enable
boseji 0:c0f5fc067a00 136 #define LPC1768_PCONP_PCADC 0x1000
boseji 0:c0f5fc067a00 137 // bit 13: PCCAN1: CAN controller 1 power/clock enable
boseji 0:c0f5fc067a00 138 #define LPC1768_PCONP_PCCAN1 0x2000
boseji 0:c0f5fc067a00 139 // bit 14: PCCAN2: CAN controller 2 power/clock enable
boseji 0:c0f5fc067a00 140 #define LPC1768_PCONP_PCCAN2 0x4000
boseji 0:c0f5fc067a00 141 // bit 15: PCGPIO: GPIOs power/clock enable
boseji 0:c0f5fc067a00 142 #define LPC1768_PCONP_PCGPIO 0x8000
boseji 0:c0f5fc067a00 143 // bit 16: PCRIT: Repetitive interrupt timer power/clock enable
boseji 0:c0f5fc067a00 144 #define LPC1768_PCONP_PCRIT 0x10000
boseji 0:c0f5fc067a00 145 // bit 17: PCMCPWM: Motor control PWM power/clock enable
boseji 0:c0f5fc067a00 146 #define LPC1768_PCONP_PCMCPWM 0x20000
boseji 0:c0f5fc067a00 147 // bit 18: PCQEI: Quadrature encoder interface power/clock enable
boseji 0:c0f5fc067a00 148 #define LPC1768_PCONP_PCQEI 0x40000
boseji 0:c0f5fc067a00 149 // bit 19: PCI2C1: I2C interface 1 power/clock enable
boseji 0:c0f5fc067a00 150 #define LPC1768_PCONP_PCI2C1 0x80000
boseji 0:c0f5fc067a00 151 // bit 20: Reserved
boseji 0:c0f5fc067a00 152 // bit 21: PCSSP0: SSP interface 0 power/clock enable
boseji 0:c0f5fc067a00 153 #define LPC1768_PCONP_PCSSP0 0x200000
boseji 0:c0f5fc067a00 154 // bit 22: PCTIM2: Timer 2 power/clock enable
boseji 0:c0f5fc067a00 155 #define LPC1768_PCONP_PCTIM2 0x400000
boseji 0:c0f5fc067a00 156 // bit 23: PCTIM3: Timer 3 power/clock enable
boseji 0:c0f5fc067a00 157 #define LPC1768_PCONP_PCQTIM3 0x800000
boseji 0:c0f5fc067a00 158 // bit 24: PCUART2: UART 2 power/clock enable
boseji 0:c0f5fc067a00 159 #define LPC1768_PCONP_PCUART2 0x1000000
boseji 0:c0f5fc067a00 160 // bit 25: PCUART3: UART 3 power/clock enable
boseji 0:c0f5fc067a00 161 #define LPC1768_PCONP_PCUART3 0x2000000
boseji 0:c0f5fc067a00 162 // bit 26: PCI2C2: I2C interface 2 power/clock enable
boseji 0:c0f5fc067a00 163 #define LPC1768_PCONP_PCI2C2 0x4000000
boseji 0:c0f5fc067a00 164 // bit 27: PCI2S: I2S interface power/clock enable
boseji 0:c0f5fc067a00 165 #define LPC1768_PCONP_PCI2S 0x8000000
boseji 0:c0f5fc067a00 166 // bit 28: Reserved
boseji 0:c0f5fc067a00 167 // bit 29: PCGPDMA: GP DMA function power/clock enable
boseji 0:c0f5fc067a00 168 #define LPC1768_PCONP_PCGPDMA 0x20000000
boseji 0:c0f5fc067a00 169 // bit 30: PCENET: Ethernet block power/clock enable
boseji 0:c0f5fc067a00 170 #define LPC1768_PCONP_PCENET 0x40000000
boseji 0:c0f5fc067a00 171 // bit 31: PCUSB: USB interface power/clock enable
boseji 0:c0f5fc067a00 172 #define LPC1768_PCONP_PCUSB 0x80000000
boseji 0:c0f5fc067a00 173
boseji 0:c0f5fc067a00 174 //Powers Up specified Peripheral(s)
boseji 0:c0f5fc067a00 175 inline unsigned int Peripheral_PowerUp(unsigned int bitMask)
boseji 0:c0f5fc067a00 176 {
boseji 0:c0f5fc067a00 177 return LPC_SC->PCONP |= bitMask;
boseji 0:c0f5fc067a00 178 }
boseji 0:c0f5fc067a00 179
boseji 0:c0f5fc067a00 180 //Powers Down specified Peripheral(s)
boseji 0:c0f5fc067a00 181 inline unsigned int Peripheral_PowerDown(unsigned int bitMask)
boseji 0:c0f5fc067a00 182 {
boseji 0:c0f5fc067a00 183 return LPC_SC->PCONP &= ~bitMask;
boseji 0:c0f5fc067a00 184 }
boseji 0:c0f5fc067a00 185
boseji 0:c0f5fc067a00 186 //returns if the peripheral is on or off
boseji 0:c0f5fc067a00 187 inline bool Peripheral_GetStatus(unsigned int peripheral)
boseji 0:c0f5fc067a00 188 {
boseji 0:c0f5fc067a00 189 return (LPC_SC->PCONP & peripheral) ? true : false;
boseji 0:c0f5fc067a00 190 }
boseji 0:c0f5fc067a00 191
boseji 0:c0f5fc067a00 192 #endif