Ultrasonic Sensor node code version 5
Dependencies: C12832_lcd HCSR04 mbed
main.cpp@0:2948cf3a6cb1, 2018-07-16 (annotated)
- Committer:
- mreda
- Date:
- Mon Jul 16 08:11:07 2018 +0000
- Revision:
- 0:2948cf3a6cb1
UM fsktm Malaysia
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mreda | 0:2948cf3a6cb1 | 1 | //Code to send serial comms on |
mreda | 0:2948cf3a6cb1 | 2 | //mbed appBoard with xbees |
mreda | 0:2948cf3a6cb1 | 3 | #include "mbed.h" |
mreda | 0:2948cf3a6cb1 | 4 | #include "C12832_lcd.h" // Include for LCD code |
mreda | 0:2948cf3a6cb1 | 5 | #include "stdlib.h" |
mreda | 0:2948cf3a6cb1 | 6 | #include"string" |
mreda | 0:2948cf3a6cb1 | 7 | #include"stdio.h" |
mreda | 0:2948cf3a6cb1 | 8 | #include "hcsr04.h" |
mreda | 0:2948cf3a6cb1 | 9 | |
mreda | 0:2948cf3a6cb1 | 10 | // watchdog |
mreda | 0:2948cf3a6cb1 | 11 | |
mreda | 0:2948cf3a6cb1 | 12 | class Watchdog { |
mreda | 0:2948cf3a6cb1 | 13 | public: |
mreda | 0:2948cf3a6cb1 | 14 | // Load timeout value in watchdog timer and enable |
mreda | 0:2948cf3a6cb1 | 15 | void kick(float s) { |
mreda | 0:2948cf3a6cb1 | 16 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
mreda | 0:2948cf3a6cb1 | 17 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
mreda | 0:2948cf3a6cb1 | 18 | LPC_WDT->WDTC = s * (float)clk; |
mreda | 0:2948cf3a6cb1 | 19 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
mreda | 0:2948cf3a6cb1 | 20 | kick(); |
mreda | 0:2948cf3a6cb1 | 21 | } |
mreda | 0:2948cf3a6cb1 | 22 | // "kick" or "feed" the dog - reset the watchdog timer |
mreda | 0:2948cf3a6cb1 | 23 | // by writing this required bit pattern |
mreda | 0:2948cf3a6cb1 | 24 | void kick() { |
mreda | 0:2948cf3a6cb1 | 25 | LPC_WDT->WDFEED = 0xAA; |
mreda | 0:2948cf3a6cb1 | 26 | LPC_WDT->WDFEED = 0x55; |
mreda | 0:2948cf3a6cb1 | 27 | } |
mreda | 0:2948cf3a6cb1 | 28 | }; |
mreda | 0:2948cf3a6cb1 | 29 | |
mreda | 0:2948cf3a6cb1 | 30 | Watchdog wdt; |
mreda | 0:2948cf3a6cb1 | 31 | |
mreda | 0:2948cf3a6cb1 | 32 | //int SendSt (string ); |
mreda | 0:2948cf3a6cb1 | 33 | HCSR04 usensor(p12,p13); // trig p12, echo p13 |
mreda | 0:2948cf3a6cb1 | 34 | //Initialize xbee |
mreda | 0:2948cf3a6cb1 | 35 | Serial xbee(p9, p10); |
mreda | 0:2948cf3a6cb1 | 36 | DigitalOut rst1(p30); |
mreda | 0:2948cf3a6cb1 | 37 | |
mreda | 0:2948cf3a6cb1 | 38 | //Initialize lcd |
mreda | 0:2948cf3a6cb1 | 39 | C12832_LCD lcd; |
mreda | 0:2948cf3a6cb1 | 40 | |
mreda | 0:2948cf3a6cb1 | 41 | int main() |
mreda | 0:2948cf3a6cb1 | 42 | { |
mreda | 0:2948cf3a6cb1 | 43 | wdt.kick(5.0); |
mreda | 0:2948cf3a6cb1 | 44 | // reset the xbees (at least 200ns) |
mreda | 0:2948cf3a6cb1 | 45 | rst1 = 0; |
mreda | 0:2948cf3a6cb1 | 46 | wait_ms(1); |
mreda | 0:2948cf3a6cb1 | 47 | rst1 = 1; |
mreda | 0:2948cf3a6cb1 | 48 | wait_ms(1); |
mreda | 0:2948cf3a6cb1 | 49 | //Setup LCD screen |
mreda | 0:2948cf3a6cb1 | 50 | lcd.cls(); |
mreda | 0:2948cf3a6cb1 | 51 | lcd.locate(0,1); |
mreda | 0:2948cf3a6cb1 | 52 | //--------------------------------variables------------------------------------ |
mreda | 0:2948cf3a6cb1 | 53 | int s[]={'0','1','2','3','4','5','6','7','8','9'}; |
mreda | 0:2948cf3a6cb1 | 54 | int s1[]={11,50,11,11}; |
mreda | 0:2948cf3a6cb1 | 55 | int i; |
mreda | 0:2948cf3a6cb1 | 56 | int n; |
mreda | 0:2948cf3a6cb1 | 57 | int dist, toSend,dist1; |
mreda | 0:2948cf3a6cb1 | 58 | while(1) |
mreda | 0:2948cf3a6cb1 | 59 | { |
mreda | 0:2948cf3a6cb1 | 60 | usensor.start(); |
mreda | 0:2948cf3a6cb1 | 61 | wait_ms(500); |
mreda | 0:2948cf3a6cb1 | 62 | dist=0; |
mreda | 0:2948cf3a6cb1 | 63 | for(int i=0;i<20;i++) |
mreda | 0:2948cf3a6cb1 | 64 | dist+=usensor.get_dist_cm(); |
mreda | 0:2948cf3a6cb1 | 65 | dist/=20; |
mreda | 0:2948cf3a6cb1 | 66 | dist1=dist; |
mreda | 0:2948cf3a6cb1 | 67 | xbee.putc('$'); |
mreda | 0:2948cf3a6cb1 | 68 | lcd.locate(0,0); |
mreda | 0:2948cf3a6cb1 | 69 | lcd.printf("The distance is : %d ",dist1); |
mreda | 0:2948cf3a6cb1 | 70 | |
mreda | 0:2948cf3a6cb1 | 71 | i=0; |
mreda | 0:2948cf3a6cb1 | 72 | do |
mreda | 0:2948cf3a6cb1 | 73 | { |
mreda | 0:2948cf3a6cb1 | 74 | toSend=dist%10; |
mreda | 0:2948cf3a6cb1 | 75 | s1[i]=s[toSend]; |
mreda | 0:2948cf3a6cb1 | 76 | i++; |
mreda | 0:2948cf3a6cb1 | 77 | dist/=10; |
mreda | 0:2948cf3a6cb1 | 78 | }while(dist!=0); |
mreda | 0:2948cf3a6cb1 | 79 | |
mreda | 0:2948cf3a6cb1 | 80 | for(n=i-1;n>=0;n--) |
mreda | 0:2948cf3a6cb1 | 81 | { |
mreda | 0:2948cf3a6cb1 | 82 | xbee.putc(s1[n]); |
mreda | 0:2948cf3a6cb1 | 83 | |
mreda | 0:2948cf3a6cb1 | 84 | } |
mreda | 0:2948cf3a6cb1 | 85 | |
mreda | 0:2948cf3a6cb1 | 86 | xbee.putc('$'); |
mreda | 0:2948cf3a6cb1 | 87 | wdt.kick(); |
mreda | 0:2948cf3a6cb1 | 88 | |
mreda | 0:2948cf3a6cb1 | 89 | }//while(1) |
mreda | 0:2948cf3a6cb1 | 90 | }// int main() |