Wesley Schon / Mbed 2 deprecated ECE4180Lab1PowerManagement

Dependencies:   PowerControl mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PowerControl/PowerControl.h"
00003 #include "PowerControl/EthernetPowerControl.h"
00004 
00005 PwmOut myled(p21);
00006 DigitalIn button1(p30);
00007 DigitalIn button2(p29);
00008 DigitalIn switch1(p8);
00009 DigitalOut myled4(LED4);
00010 DigitalOut myled3(LED3);
00011 float intensity = 0.5f;
00012 
00013 // Function to power down magic USB interface chip with new firmware
00014 #define USR_POWERDOWN    (0x104)
00015 int semihost_powerdown() {
00016     uint32_t arg;
00017     return __semihost(USR_POWERDOWN, &arg);
00018 }
00019 
00020 class Watchdog {
00021 public:
00022 // Load timeout value in watchdog timer and enable
00023     void kick(float s) {
00024         LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
00025         uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
00026         LPC_WDT->WDTC = s * (float)clk;
00027         LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
00028         kick();
00029     }
00030 // "kick" or "feed" the dog - reset the watchdog timer
00031 // by writing this required bit pattern
00032     void kick() {
00033         LPC_WDT->WDFEED = 0xAA;
00034         LPC_WDT->WDFEED = 0x55;
00035     }
00036 };
00037  
00038 // Setup the watchdog timer
00039 Watchdog wdt;
00040 
00041 
00042 int main() {
00043     PHY_PowerDown();   //Power down Ethernet interface
00044     int result;
00045     result = semihost_powerdown();  //Power down USB interface
00046     int count = 0;
00047     Peripheral_PowerDown(0xFFFF7FFF);
00048 
00049     wdt.kick(4.0);
00050     if ((LPC_WDT->WDMOD >> 2) & 1)
00051         myled4 = 1; else myled3 = 1;
00052         wait(2);
00053         myled4 = 0;
00054         myled3 = 0;
00055 
00056      
00057     
00058     while(1) {
00059         if (button1 == 0)  {
00060             if (intensity <= 1.0f) {
00061                 intensity += 0.07f;
00062             }
00063         }
00064         if (button2 == 0) {
00065             if (intensity >= 0.0f) {
00066                 intensity -= 0.07f;
00067             }
00068         }
00069         if (switch1 == 0) {
00070             myled = intensity;
00071         }
00072         else {
00073             myled = 0.0f;
00074             }
00075         wait(0.1);
00076         count++;
00077         if (count == 80) {
00078             wait(11);
00079             }
00080         wdt.kick(); //kick the watchdog timer
00081     }
00082 }