![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
my code as off 16/12/2018
Dependencies: mbed BMP280 ELEC350-Practicals-FZ429_2
set_time_on_startup.cpp@1:e274a5cc021d, 2018-12-16 (annotated)
- Committer:
- ChrisHayes
- Date:
- Sun Dec 16 15:25:04 2018 +0000
- Revision:
- 1:e274a5cc021d
this is my code up two 16/12/2018
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ChrisHayes | 1:e274a5cc021d | 1 | #include "mbed.h" |
ChrisHayes | 1:e274a5cc021d | 2 | #include "TextLCD.h" |
ChrisHayes | 1:e274a5cc021d | 3 | #include "sample_hardware.hpp" |
ChrisHayes | 1:e274a5cc021d | 4 | #include "set_time_on_startup.hpp" |
ChrisHayes | 1:e274a5cc021d | 5 | |
ChrisHayes | 1:e274a5cc021d | 6 | TextLCD lcdp(D9, D8, D7, D6, D4, D2); // rs, e, d4 |
ChrisHayes | 1:e274a5cc021d | 7 | Timer tmr; |
ChrisHayes | 1:e274a5cc021d | 8 | int state =0; |
ChrisHayes | 1:e274a5cc021d | 9 | |
ChrisHayes | 1:e274a5cc021d | 10 | int year = 1970; |
ChrisHayes | 1:e274a5cc021d | 11 | int month = 0; |
ChrisHayes | 1:e274a5cc021d | 12 | int day = 0; |
ChrisHayes | 1:e274a5cc021d | 13 | int hours = 0; |
ChrisHayes | 1:e274a5cc021d | 14 | int mins = 0; |
ChrisHayes | 1:e274a5cc021d | 15 | int seconds = 0; |
ChrisHayes | 1:e274a5cc021d | 16 | |
ChrisHayes | 1:e274a5cc021d | 17 | //-------------------------------------------------------------------------------- |
ChrisHayes | 1:e274a5cc021d | 18 | void settime() |
ChrisHayes | 1:e274a5cc021d | 19 | { |
ChrisHayes | 1:e274a5cc021d | 20 | switch (state) |
ChrisHayes | 1:e274a5cc021d | 21 | { |
ChrisHayes | 1:e274a5cc021d | 22 | case 0: |
ChrisHayes | 1:e274a5cc021d | 23 | if (tmr.read_ms() >= 50 && SW2.read()) |
ChrisHayes | 1:e274a5cc021d | 24 | { |
ChrisHayes | 1:e274a5cc021d | 25 | year++; |
ChrisHayes | 1:e274a5cc021d | 26 | if (year == 2100) |
ChrisHayes | 1:e274a5cc021d | 27 | { |
ChrisHayes | 1:e274a5cc021d | 28 | year=1970; |
ChrisHayes | 1:e274a5cc021d | 29 | } |
ChrisHayes | 1:e274a5cc021d | 30 | tmr.reset(); |
ChrisHayes | 1:e274a5cc021d | 31 | } |
ChrisHayes | 1:e274a5cc021d | 32 | break; |
ChrisHayes | 1:e274a5cc021d | 33 | //------------------------------------------------------ |
ChrisHayes | 1:e274a5cc021d | 34 | |
ChrisHayes | 1:e274a5cc021d | 35 | case 1: |
ChrisHayes | 1:e274a5cc021d | 36 | if ( tmr.read_ms() >= 50 && SW2.read()) |
ChrisHayes | 1:e274a5cc021d | 37 | { |
ChrisHayes | 1:e274a5cc021d | 38 | month++; |
ChrisHayes | 1:e274a5cc021d | 39 | |
ChrisHayes | 1:e274a5cc021d | 40 | if (month == 13) |
ChrisHayes | 1:e274a5cc021d | 41 | { |
ChrisHayes | 1:e274a5cc021d | 42 | month =0; |
ChrisHayes | 1:e274a5cc021d | 43 | } |
ChrisHayes | 1:e274a5cc021d | 44 | tmr.reset(); |
ChrisHayes | 1:e274a5cc021d | 45 | } |
ChrisHayes | 1:e274a5cc021d | 46 | break; |
ChrisHayes | 1:e274a5cc021d | 47 | //-------------------------------------------------------- |
ChrisHayes | 1:e274a5cc021d | 48 | |
ChrisHayes | 1:e274a5cc021d | 49 | case 2 : |
ChrisHayes | 1:e274a5cc021d | 50 | if ( tmr.read_ms() >= 50 && SW2.read()) |
ChrisHayes | 1:e274a5cc021d | 51 | { |
ChrisHayes | 1:e274a5cc021d | 52 | day++; |
ChrisHayes | 1:e274a5cc021d | 53 | if (day == 31) |
ChrisHayes | 1:e274a5cc021d | 54 | { |
ChrisHayes | 1:e274a5cc021d | 55 | day=0; |
ChrisHayes | 1:e274a5cc021d | 56 | } |
ChrisHayes | 1:e274a5cc021d | 57 | tmr.reset(); |
ChrisHayes | 1:e274a5cc021d | 58 | } |
ChrisHayes | 1:e274a5cc021d | 59 | break; |
ChrisHayes | 1:e274a5cc021d | 60 | //---------------------------------------------------------- |
ChrisHayes | 1:e274a5cc021d | 61 | |
ChrisHayes | 1:e274a5cc021d | 62 | case 3: |
ChrisHayes | 1:e274a5cc021d | 63 | if ( tmr.read_ms() >= 50 && SW2.read()) |
ChrisHayes | 1:e274a5cc021d | 64 | { |
ChrisHayes | 1:e274a5cc021d | 65 | hours++; |
ChrisHayes | 1:e274a5cc021d | 66 | if (hours == 25) |
ChrisHayes | 1:e274a5cc021d | 67 | { |
ChrisHayes | 1:e274a5cc021d | 68 | hours=0; |
ChrisHayes | 1:e274a5cc021d | 69 | } |
ChrisHayes | 1:e274a5cc021d | 70 | tmr.reset(); |
ChrisHayes | 1:e274a5cc021d | 71 | } |
ChrisHayes | 1:e274a5cc021d | 72 | break; |
ChrisHayes | 1:e274a5cc021d | 73 | //------------------------------------------------------------ |
ChrisHayes | 1:e274a5cc021d | 74 | |
ChrisHayes | 1:e274a5cc021d | 75 | case 4: |
ChrisHayes | 1:e274a5cc021d | 76 | if ( tmr.read_ms() >= 50 && SW2.read()) |
ChrisHayes | 1:e274a5cc021d | 77 | { |
ChrisHayes | 1:e274a5cc021d | 78 | mins++; |
ChrisHayes | 1:e274a5cc021d | 79 | if (mins == 61) |
ChrisHayes | 1:e274a5cc021d | 80 | { |
ChrisHayes | 1:e274a5cc021d | 81 | mins=0; |
ChrisHayes | 1:e274a5cc021d | 82 | } |
ChrisHayes | 1:e274a5cc021d | 83 | tmr.reset(); |
ChrisHayes | 1:e274a5cc021d | 84 | } |
ChrisHayes | 1:e274a5cc021d | 85 | break; |
ChrisHayes | 1:e274a5cc021d | 86 | |
ChrisHayes | 1:e274a5cc021d | 87 | |
ChrisHayes | 1:e274a5cc021d | 88 | case 5: |
ChrisHayes | 1:e274a5cc021d | 89 | if(tmr.read()) |
ChrisHayes | 1:e274a5cc021d | 90 | { |
ChrisHayes | 1:e274a5cc021d | 91 | if (SW2.read()) |
ChrisHayes | 1:e274a5cc021d | 92 | { |
ChrisHayes | 1:e274a5cc021d | 93 | state = 10; |
ChrisHayes | 1:e274a5cc021d | 94 | } |
ChrisHayes | 1:e274a5cc021d | 95 | } |
ChrisHayes | 1:e274a5cc021d | 96 | |
ChrisHayes | 1:e274a5cc021d | 97 | break; |
ChrisHayes | 1:e274a5cc021d | 98 | //---------------------------------------------------------------- |
ChrisHayes | 1:e274a5cc021d | 99 | |
ChrisHayes | 1:e274a5cc021d | 100 | default: |
ChrisHayes | 1:e274a5cc021d | 101 | |
ChrisHayes | 1:e274a5cc021d | 102 | break; |
ChrisHayes | 1:e274a5cc021d | 103 | |
ChrisHayes | 1:e274a5cc021d | 104 | } |
ChrisHayes | 1:e274a5cc021d | 105 | |
ChrisHayes | 1:e274a5cc021d | 106 | } |
ChrisHayes | 1:e274a5cc021d | 107 | |
ChrisHayes | 1:e274a5cc021d | 108 | |
ChrisHayes | 1:e274a5cc021d | 109 | |
ChrisHayes | 1:e274a5cc021d | 110 | void button_time (void) |
ChrisHayes | 1:e274a5cc021d | 111 | { |
ChrisHayes | 1:e274a5cc021d | 112 | |
ChrisHayes | 1:e274a5cc021d | 113 | tmr.start(); |
ChrisHayes | 1:e274a5cc021d | 114 | while(state < 10) |
ChrisHayes | 1:e274a5cc021d | 115 | { |
ChrisHayes | 1:e274a5cc021d | 116 | |
ChrisHayes | 1:e274a5cc021d | 117 | settime(); |
ChrisHayes | 1:e274a5cc021d | 118 | |
ChrisHayes | 1:e274a5cc021d | 119 | // lcd.printf("current time is..."); |
ChrisHayes | 1:e274a5cc021d | 120 | lcdp.printf("%.2d/%.2d/%.4d\r\n", day, month, year); |
ChrisHayes | 1:e274a5cc021d | 121 | lcdp.printf("%.2d:%.2d:%.2d\r\n", hours, mins, seconds); |
ChrisHayes | 1:e274a5cc021d | 122 | // lcd.setCursor(); |
ChrisHayes | 1:e274a5cc021d | 123 | // lcd.writeCommand(0x0D); |
ChrisHayes | 1:e274a5cc021d | 124 | // lcd.writeCommand(13); |
ChrisHayes | 1:e274a5cc021d | 125 | |
ChrisHayes | 1:e274a5cc021d | 126 | //Set up tasks on the main thread |
ChrisHayes | 1:e274a5cc021d | 127 | |
ChrisHayes | 1:e274a5cc021d | 128 | |
ChrisHayes | 1:e274a5cc021d | 129 | //Main queue event loop |
ChrisHayes | 1:e274a5cc021d | 130 | // queue.dispatch(); |
ChrisHayes | 1:e274a5cc021d | 131 | if ( tmr.read_ms() >= 300 && SW1.read()) |
ChrisHayes | 1:e274a5cc021d | 132 | { |
ChrisHayes | 1:e274a5cc021d | 133 | state++; |
ChrisHayes | 1:e274a5cc021d | 134 | if (state==6) |
ChrisHayes | 1:e274a5cc021d | 135 | { |
ChrisHayes | 1:e274a5cc021d | 136 | state =0; |
ChrisHayes | 1:e274a5cc021d | 137 | } |
ChrisHayes | 1:e274a5cc021d | 138 | tmr.reset(); |
ChrisHayes | 1:e274a5cc021d | 139 | } |
ChrisHayes | 1:e274a5cc021d | 140 | |
ChrisHayes | 1:e274a5cc021d | 141 | |
ChrisHayes | 1:e274a5cc021d | 142 | } |
ChrisHayes | 1:e274a5cc021d | 143 | |
ChrisHayes | 1:e274a5cc021d | 144 | lcdp.cls(); |
ChrisHayes | 1:e274a5cc021d | 145 | lcdp.printf("TIME HAS BEEN SET!"); |
ChrisHayes | 1:e274a5cc021d | 146 | wait(1); |
ChrisHayes | 1:e274a5cc021d | 147 | |
ChrisHayes | 1:e274a5cc021d | 148 | } |
ChrisHayes | 1:e274a5cc021d | 149 | |
ChrisHayes | 1:e274a5cc021d | 150 | |
ChrisHayes | 1:e274a5cc021d | 151 | |
ChrisHayes | 1:e274a5cc021d | 152 | |
ChrisHayes | 1:e274a5cc021d | 153 | //setting time |