Lab 1 Parts 1 and 2, and watchdog timer EC
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 PwmOut myled(p21); 00004 DigitalIn button1(p30); 00005 DigitalIn button2(p29); 00006 DigitalIn switch1(p8); 00007 DigitalOut myled4(LED4); 00008 DigitalOut myled3(LED3); 00009 float intensity = 0.5f; 00010 00011 00012 class Watchdog { 00013 public: 00014 // Load timeout value in watchdog timer and enable 00015 void kick(float s) { 00016 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK 00017 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 00018 LPC_WDT->WDTC = s * (float)clk; 00019 LPC_WDT->WDMOD = 0x3; // Enabled and Reset 00020 kick(); 00021 } 00022 // "kick" or "feed" the dog - reset the watchdog timer 00023 // by writing this required bit pattern 00024 void kick() { 00025 LPC_WDT->WDFEED = 0xAA; 00026 LPC_WDT->WDFEED = 0x55; 00027 } 00028 }; 00029 00030 // Setup the watchdog timer 00031 Watchdog wdt; 00032 00033 00034 int main() { 00035 00036 int count = 0; 00037 wdt.kick(5.0); 00038 if ((LPC_WDT->WDMOD >> 2) & 1) 00039 myled4 = 1; else myled3 = 1; 00040 wait(2); 00041 myled4 = 0; 00042 myled3 = 0; 00043 00044 00045 00046 while(1) { 00047 if (button1 == 0) { 00048 if (intensity <= 1.0f) { 00049 intensity += 0.07f; 00050 } 00051 } 00052 if (button2 == 0) { 00053 if (intensity >= 0.0f) { 00054 intensity -= 0.07f; 00055 } 00056 } 00057 if (switch1 == 0) { 00058 myled = intensity; 00059 } 00060 else { 00061 myled = 0.0f; 00062 } 00063 wait(0.1); 00064 count++; 00065 if (count == 70) { 00066 wait(11); 00067 } 00068 wdt.kick(); //kick the watchdog timer 00069 } 00070 }
Generated on Fri Jul 22 2022 12:19:42 by
1.7.2