A simple alarm clock for the 1 day workshop presented by Skool and ARM Hungary in 2015.
Dependencies: Skool_wkshp_lib2015 mbed
mbed_clock.cpp@1:f76b625bd36e, 2015-09-15 (annotated)
- Committer:
- lvagasi
- Date:
- Tue Sep 15 21:53:26 2015 +0000
- Revision:
- 1:f76b625bd36e
- Parent:
- 0:28b9efbdeffc
- Child:
- 2:e084bab7bc1c
Date/Time display fixed, working. Next is keypad and menu handling with FSMs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lvagasi | 0:28b9efbdeffc | 1 | #include "mbed.h" |
lvagasi | 0:28b9efbdeffc | 2 | #include "serial_lcd.h" |
lvagasi | 0:28b9efbdeffc | 3 | |
lvagasi | 0:28b9efbdeffc | 4 | DigitalOut myled(LED1); // On-board LED |
lvagasi | 0:28b9efbdeffc | 5 | Serial pc(SERIAL_TX, SERIAL_RX); // UART to communicate with PC |
lvagasi | 0:28b9efbdeffc | 6 | DigitalOut R_LED(PA_10); // RED part of the RGB LED |
lvagasi | 0:28b9efbdeffc | 7 | DigitalOut G_LED(PB_5); // GREEN part of the RGB LED |
lvagasi | 0:28b9efbdeffc | 8 | DigitalOut B_LED(PA_9); // BLUE part of the RGB LED |
lvagasi | 0:28b9efbdeffc | 9 | DigitalOut LCD_RST(PB_10); // LCD RST |
lvagasi | 0:28b9efbdeffc | 10 | DigitalOut LCD_BL(PA_8); // LCD BackLight |
lvagasi | 0:28b9efbdeffc | 11 | PwmOut speaker(PB_4); // Speaker |
lvagasi | 0:28b9efbdeffc | 12 | |
lvagasi | 0:28b9efbdeffc | 13 | I2C i2c1(I2C_SDA, I2C_SCL); // I2C interface for LCD display |
lvagasi | 0:28b9efbdeffc | 14 | |
lvagasi | 0:28b9efbdeffc | 15 | |
lvagasi | 0:28b9efbdeffc | 16 | // Define your own keypad values |
lvagasi | 0:28b9efbdeffc | 17 | const char Keytable[] = { '1', '2', '3', 'A', // r0 |
lvagasi | 0:28b9efbdeffc | 18 | '4', '5', '6', 'B', // r1 |
lvagasi | 0:28b9efbdeffc | 19 | '7', '8', '9', 'C', // r2 |
lvagasi | 0:28b9efbdeffc | 20 | '*', '0', '#', 'D' // r3 |
lvagasi | 0:28b9efbdeffc | 21 | }; |
lvagasi | 0:28b9efbdeffc | 22 | // c0 c1 c2 c3 |
lvagasi | 0:28b9efbdeffc | 23 | const uint16_t rows[4] = {GPIO_PIN_5, GPIO_PIN_6, GPIO_PIN_8, GPIO_PIN_9}; |
lvagasi | 0:28b9efbdeffc | 24 | const uint16_t cols[4] = {GPIO_PIN_10, GPIO_PIN_11, GPIO_PIN_12, 0xFFFF}; |
lvagasi | 0:28b9efbdeffc | 25 | //const int nrows = 4; |
lvagasi | 0:28b9efbdeffc | 26 | const int ncols = 3; |
lvagasi | 0:28b9efbdeffc | 27 | const char BCD2HEX[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
lvagasi | 1:f76b625bd36e | 28 | const char DOW[7][3] = { {'H',' ',' '}, {'K',' ',' '}, {'S','z','e'}, {'C','s',' '}, {'P',' ',' '}, {'S','z','o'}, {'V',' ',' '} }; |
lvagasi | 0:28b9efbdeffc | 29 | |
lvagasi | 0:28b9efbdeffc | 30 | uint32_t Index = 0xFF; |
lvagasi | 0:28b9efbdeffc | 31 | uint32_t new_key = 0; |
lvagasi | 0:28b9efbdeffc | 32 | uint32_t Systck_cnt = 0; |
lvagasi | 1:f76b625bd36e | 33 | uint32_t Systck_evt = 0; |
lvagasi | 1:f76b625bd36e | 34 | GPIO_InitTypeDef keypadInit; |
lvagasi | 0:28b9efbdeffc | 35 | |
lvagasi | 0:28b9efbdeffc | 36 | void Error(int err) { |
lvagasi | 0:28b9efbdeffc | 37 | switch (err) { |
lvagasi | 0:28b9efbdeffc | 38 | case 0: pc.printf("\033[44m\033[37mI2C communication error!\033[00m\r\n"); |
lvagasi | 0:28b9efbdeffc | 39 | break; |
lvagasi | 0:28b9efbdeffc | 40 | case 1: pc.printf("\033[44m\033[37mIncorrect calibration data error!\033[00m\r\n"); |
lvagasi | 0:28b9efbdeffc | 41 | break; |
lvagasi | 0:28b9efbdeffc | 42 | default: pc.printf("\033[44m\033[37mUnknown error!\033[00m\r\n"); |
lvagasi | 0:28b9efbdeffc | 43 | break; |
lvagasi | 0:28b9efbdeffc | 44 | } |
lvagasi | 0:28b9efbdeffc | 45 | while (1) { |
lvagasi | 0:28b9efbdeffc | 46 | myled = 1; |
lvagasi | 0:28b9efbdeffc | 47 | wait(0.1); |
lvagasi | 0:28b9efbdeffc | 48 | myled = !myled; |
lvagasi | 0:28b9efbdeffc | 49 | wait(0.1); |
lvagasi | 0:28b9efbdeffc | 50 | myled = !myled; |
lvagasi | 0:28b9efbdeffc | 51 | wait(0.1); |
lvagasi | 0:28b9efbdeffc | 52 | myled = !myled; |
lvagasi | 0:28b9efbdeffc | 53 | wait(2); |
lvagasi | 0:28b9efbdeffc | 54 | } |
lvagasi | 0:28b9efbdeffc | 55 | } |
lvagasi | 0:28b9efbdeffc | 56 | |
lvagasi | 0:28b9efbdeffc | 57 | void EXTI9_5_IRQHandler(void) { |
lvagasi | 0:28b9efbdeffc | 58 | HAL_NVIC_DisableIRQ(EXTI9_5_IRQn); |
lvagasi | 0:28b9efbdeffc | 59 | if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_5) != 0) { |
lvagasi | 0:28b9efbdeffc | 60 | __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_5); |
lvagasi | 0:28b9efbdeffc | 61 | HAL_GPIO_EXTI_Callback(0); |
lvagasi | 0:28b9efbdeffc | 62 | } else if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_6) != 0) { |
lvagasi | 0:28b9efbdeffc | 63 | __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_6); |
lvagasi | 0:28b9efbdeffc | 64 | HAL_GPIO_EXTI_Callback(1); |
lvagasi | 0:28b9efbdeffc | 65 | } else if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_7) != 0) { |
lvagasi | 0:28b9efbdeffc | 66 | __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_7); |
lvagasi | 0:28b9efbdeffc | 67 | HAL_GPIO_EXTI_Callback(4); |
lvagasi | 0:28b9efbdeffc | 68 | } else if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_8) != 0) { |
lvagasi | 0:28b9efbdeffc | 69 | __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_8); |
lvagasi | 0:28b9efbdeffc | 70 | HAL_GPIO_EXTI_Callback(2); |
lvagasi | 0:28b9efbdeffc | 71 | } else if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_9) != 0) { |
lvagasi | 0:28b9efbdeffc | 72 | __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_9); |
lvagasi | 0:28b9efbdeffc | 73 | HAL_GPIO_EXTI_Callback(3); |
lvagasi | 0:28b9efbdeffc | 74 | } |
lvagasi | 0:28b9efbdeffc | 75 | HAL_GPIO_WritePin(GPIOC, cols[0] | cols[1] | cols[2], GPIO_PIN_RESET); // All cols are driven Low for resume IRQ operation |
lvagasi | 1:f76b625bd36e | 76 | //NVIC_ClearPendingIRQ(EXTI9_5_IRQn); |
lvagasi | 1:f76b625bd36e | 77 | HAL_NVIC_ClearPendingIRQ(EXTI9_5_IRQn); |
lvagasi | 0:28b9efbdeffc | 78 | } |
lvagasi | 0:28b9efbdeffc | 79 | |
lvagasi | 0:28b9efbdeffc | 80 | void HAL_GPIO_EXTI_Callback(uint16_t pin) { |
lvagasi | 0:28b9efbdeffc | 81 | int col; |
lvagasi | 0:28b9efbdeffc | 82 | int temp; |
lvagasi | 0:28b9efbdeffc | 83 | |
lvagasi | 0:28b9efbdeffc | 84 | for (col = 0; col < ncols; col++) { |
lvagasi | 0:28b9efbdeffc | 85 | HAL_GPIO_WritePin(GPIOC, cols[0] | cols[1] | cols[2], GPIO_PIN_SET); // All cols are driven High |
lvagasi | 0:28b9efbdeffc | 86 | HAL_GPIO_WritePin(GPIOC, cols[col], GPIO_PIN_RESET); // 1 col is driven Low |
lvagasi | 0:28b9efbdeffc | 87 | wait_ms(50); |
lvagasi | 0:28b9efbdeffc | 88 | temp = GPIOC->IDR; |
lvagasi | 0:28b9efbdeffc | 89 | temp = (temp & 0x0360); |
lvagasi | 0:28b9efbdeffc | 90 | switch (temp) { |
lvagasi | 0:28b9efbdeffc | 91 | case 0x0340: Index = col; |
lvagasi | 0:28b9efbdeffc | 92 | break; |
lvagasi | 0:28b9efbdeffc | 93 | case 0x0320: Index = 4 + col; |
lvagasi | 0:28b9efbdeffc | 94 | break; |
lvagasi | 0:28b9efbdeffc | 95 | case 0x0260: Index = 8 + col; |
lvagasi | 0:28b9efbdeffc | 96 | break; |
lvagasi | 0:28b9efbdeffc | 97 | case 0x0160: Index = 12 + col; |
lvagasi | 0:28b9efbdeffc | 98 | break; |
lvagasi | 0:28b9efbdeffc | 99 | default: Index = 0xFF; |
lvagasi | 0:28b9efbdeffc | 100 | break; |
lvagasi | 0:28b9efbdeffc | 101 | } |
lvagasi | 0:28b9efbdeffc | 102 | if (Index != 0xFF) { |
lvagasi | 0:28b9efbdeffc | 103 | break; |
lvagasi | 0:28b9efbdeffc | 104 | } |
lvagasi | 0:28b9efbdeffc | 105 | } |
lvagasi | 0:28b9efbdeffc | 106 | new_key++; |
lvagasi | 0:28b9efbdeffc | 107 | } |
lvagasi | 0:28b9efbdeffc | 108 | void SysTick_Handler(void) { |
lvagasi | 0:28b9efbdeffc | 109 | HAL_SYSTICK_Callback(); |
lvagasi | 0:28b9efbdeffc | 110 | } |
lvagasi | 0:28b9efbdeffc | 111 | |
lvagasi | 0:28b9efbdeffc | 112 | void HAL_SYSTICK_Callback(void) { |
lvagasi | 0:28b9efbdeffc | 113 | Systck_cnt++; |
lvagasi | 1:f76b625bd36e | 114 | Systck_evt++; |
lvagasi | 0:28b9efbdeffc | 115 | } |
lvagasi | 0:28b9efbdeffc | 116 | |
lvagasi | 0:28b9efbdeffc | 117 | int write_ser_lcd(unsigned char data, bool mode) { |
lvagasi | 0:28b9efbdeffc | 118 | char wd[2]; |
lvagasi | 0:28b9efbdeffc | 119 | int status; |
lvagasi | 0:28b9efbdeffc | 120 | |
lvagasi | 0:28b9efbdeffc | 121 | wd[0] = ((mode) ? 0x40 : 0x00); |
lvagasi | 0:28b9efbdeffc | 122 | wd[1] = (char)data; |
lvagasi | 0:28b9efbdeffc | 123 | status = i2c1.write(ST7032I_ADDR, wd, 2, false); |
lvagasi | 0:28b9efbdeffc | 124 | return status; |
lvagasi | 0:28b9efbdeffc | 125 | } |
lvagasi | 0:28b9efbdeffc | 126 | |
lvagasi | 0:28b9efbdeffc | 127 | void write_ser_text(unsigned char *text, uint32_t len) { |
lvagasi | 0:28b9efbdeffc | 128 | int i; |
lvagasi | 0:28b9efbdeffc | 129 | char wd[41]; |
lvagasi | 0:28b9efbdeffc | 130 | int status; |
lvagasi | 0:28b9efbdeffc | 131 | |
lvagasi | 0:28b9efbdeffc | 132 | wd[0] = 0x40; |
lvagasi | 0:28b9efbdeffc | 133 | for (i = 0; i < len; i++) { |
lvagasi | 0:28b9efbdeffc | 134 | wd[i + 1] = (char)text[i]; |
lvagasi | 0:28b9efbdeffc | 135 | } |
lvagasi | 0:28b9efbdeffc | 136 | status = i2c1.write(ST7032I_ADDR, wd, len + 1, false); |
lvagasi | 0:28b9efbdeffc | 137 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 138 | Error(0); |
lvagasi | 0:28b9efbdeffc | 139 | } |
lvagasi | 0:28b9efbdeffc | 140 | } |
lvagasi | 0:28b9efbdeffc | 141 | |
lvagasi | 0:28b9efbdeffc | 142 | int init_ser_lcd(void) { |
lvagasi | 0:28b9efbdeffc | 143 | int status; |
lvagasi | 0:28b9efbdeffc | 144 | |
lvagasi | 0:28b9efbdeffc | 145 | LCD_RST = 0x0; // Generating Reset pulse |
lvagasi | 0:28b9efbdeffc | 146 | LCD_BL = 0x0; // BackLight off |
lvagasi | 0:28b9efbdeffc | 147 | // BL_LCD = 0.0; |
lvagasi | 0:28b9efbdeffc | 148 | wait_us(200); |
lvagasi | 0:28b9efbdeffc | 149 | LCD_RST = 0x1; |
lvagasi | 0:28b9efbdeffc | 150 | wait_ms(40); |
lvagasi | 0:28b9efbdeffc | 151 | status = write_ser_lcd(0x38, false); // Function set with IS = 0 |
lvagasi | 0:28b9efbdeffc | 152 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 153 | Error(0); |
lvagasi | 0:28b9efbdeffc | 154 | } |
lvagasi | 0:28b9efbdeffc | 155 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 156 | status = write_ser_lcd(0x39, false); // Function set with IS = 1 |
lvagasi | 0:28b9efbdeffc | 157 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 158 | Error(0); |
lvagasi | 0:28b9efbdeffc | 159 | } |
lvagasi | 0:28b9efbdeffc | 160 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 161 | status = write_ser_lcd(0x14, false); // Internal OSC frequency adjustment |
lvagasi | 0:28b9efbdeffc | 162 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 163 | Error(0); |
lvagasi | 0:28b9efbdeffc | 164 | } |
lvagasi | 0:28b9efbdeffc | 165 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 166 | status = write_ser_lcd(0x79, false); // Contrast set |
lvagasi | 0:28b9efbdeffc | 167 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 168 | Error(0); |
lvagasi | 0:28b9efbdeffc | 169 | } |
lvagasi | 0:28b9efbdeffc | 170 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 171 | status = write_ser_lcd(0x5C, false); // Power/Icon/Contrast control |
lvagasi | 0:28b9efbdeffc | 172 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 173 | Error(0); |
lvagasi | 0:28b9efbdeffc | 174 | } |
lvagasi | 0:28b9efbdeffc | 175 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 176 | status = write_ser_lcd(0x6E, false); // Follower control |
lvagasi | 0:28b9efbdeffc | 177 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 178 | Error(0); |
lvagasi | 0:28b9efbdeffc | 179 | } |
lvagasi | 0:28b9efbdeffc | 180 | wait_ms(200); |
lvagasi | 0:28b9efbdeffc | 181 | // wait_us(30); |
lvagasi | 0:28b9efbdeffc | 182 | status = write_ser_lcd(0x0C, false); // Display ON |
lvagasi | 0:28b9efbdeffc | 183 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 184 | Error(0); |
lvagasi | 0:28b9efbdeffc | 185 | } |
lvagasi | 0:28b9efbdeffc | 186 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 187 | status = write_ser_lcd(0x01, false); // Clear display |
lvagasi | 0:28b9efbdeffc | 188 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 189 | Error(0); |
lvagasi | 0:28b9efbdeffc | 190 | } |
lvagasi | 0:28b9efbdeffc | 191 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 192 | status = write_ser_lcd(0x06, false); // Entry mode set |
lvagasi | 0:28b9efbdeffc | 193 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 194 | Error(0); |
lvagasi | 0:28b9efbdeffc | 195 | } |
lvagasi | 0:28b9efbdeffc | 196 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 197 | status = write_ser_lcd(0x02, false); // Home |
lvagasi | 0:28b9efbdeffc | 198 | if (status != 0) { |
lvagasi | 0:28b9efbdeffc | 199 | Error(0); |
lvagasi | 0:28b9efbdeffc | 200 | } |
lvagasi | 0:28b9efbdeffc | 201 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 202 | LCD_BL = 0x1; // BackLight ON |
lvagasi | 0:28b9efbdeffc | 203 | // BL_LCD = 0.5; |
lvagasi | 0:28b9efbdeffc | 204 | return 0; |
lvagasi | 0:28b9efbdeffc | 205 | } |
lvagasi | 0:28b9efbdeffc | 206 | |
lvagasi | 0:28b9efbdeffc | 207 | int main() { |
lvagasi | 0:28b9efbdeffc | 208 | uint32_t RGB_sel = 0; |
lvagasi | 0:28b9efbdeffc | 209 | uint32_t currentHclk; |
lvagasi | 1:f76b625bd36e | 210 | uint32_t dummy; |
lvagasi | 0:28b9efbdeffc | 211 | |
lvagasi | 0:28b9efbdeffc | 212 | |
lvagasi | 0:28b9efbdeffc | 213 | HAL_StatusTypeDef HAL_status; |
lvagasi | 0:28b9efbdeffc | 214 | HAL_LockTypeDef HAL_lock; |
lvagasi | 0:28b9efbdeffc | 215 | RTC_HandleTypeDef rtch; |
lvagasi | 0:28b9efbdeffc | 216 | HAL_RTCStateTypeDef rtc_state; |
lvagasi | 0:28b9efbdeffc | 217 | RTC_InitTypeDef rtc_init; |
lvagasi | 0:28b9efbdeffc | 218 | RTC_TimeTypeDef rtc_time; |
lvagasi | 0:28b9efbdeffc | 219 | RTC_DateTypeDef rtc_date; |
lvagasi | 0:28b9efbdeffc | 220 | RTC_AlarmTypeDef rtc_alarm; |
lvagasi | 0:28b9efbdeffc | 221 | int tmp_date, tmp_sec; |
lvagasi | 0:28b9efbdeffc | 222 | |
lvagasi | 0:28b9efbdeffc | 223 | char tt[16]; |
lvagasi | 0:28b9efbdeffc | 224 | |
lvagasi | 0:28b9efbdeffc | 225 | R_LED = 1; |
lvagasi | 0:28b9efbdeffc | 226 | G_LED = 1; |
lvagasi | 0:28b9efbdeffc | 227 | B_LED = 1; |
lvagasi | 0:28b9efbdeffc | 228 | |
lvagasi | 1:f76b625bd36e | 229 | rtc_init.AsynchPrediv = 0x7F; |
lvagasi | 1:f76b625bd36e | 230 | rtc_init.SynchPrediv = 0xFF; |
lvagasi | 1:f76b625bd36e | 231 | rtc_init.HourFormat = RTC_HOURFORMAT_24; |
lvagasi | 1:f76b625bd36e | 232 | rtc_init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
lvagasi | 1:f76b625bd36e | 233 | rtc_init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
lvagasi | 1:f76b625bd36e | 234 | rtc_init.OutPut = RTC_OUTPUT_DISABLE; |
lvagasi | 1:f76b625bd36e | 235 | |
lvagasi | 1:f76b625bd36e | 236 | rtc_date.Date = 13; |
lvagasi | 1:f76b625bd36e | 237 | rtc_date.Month = 9; |
lvagasi | 1:f76b625bd36e | 238 | rtc_date.WeekDay = 7; |
lvagasi | 1:f76b625bd36e | 239 | rtc_date.Year = 15; |
lvagasi | 1:f76b625bd36e | 240 | tmp_date = 13; |
lvagasi | 1:f76b625bd36e | 241 | |
lvagasi | 1:f76b625bd36e | 242 | rtc_time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
lvagasi | 1:f76b625bd36e | 243 | rtc_time.Hours = 23; |
lvagasi | 1:f76b625bd36e | 244 | rtc_time.Minutes = 58; |
lvagasi | 1:f76b625bd36e | 245 | rtc_time.Seconds = 46; |
lvagasi | 1:f76b625bd36e | 246 | rtc_time.StoreOperation =RTC_STOREOPERATION_SET; |
lvagasi | 1:f76b625bd36e | 247 | tmp_sec = 0; |
lvagasi | 1:f76b625bd36e | 248 | |
lvagasi | 1:f76b625bd36e | 249 | rtch.Instance = RTC; |
lvagasi | 1:f76b625bd36e | 250 | rtch.Init = rtc_init; |
lvagasi | 1:f76b625bd36e | 251 | rtch.Lock = HAL_lock; |
lvagasi | 1:f76b625bd36e | 252 | rtch.State = rtc_state; |
lvagasi | 1:f76b625bd36e | 253 | |
lvagasi | 1:f76b625bd36e | 254 | |
lvagasi | 1:f76b625bd36e | 255 | __PWR_CLK_ENABLE(); |
lvagasi | 1:f76b625bd36e | 256 | HAL_PWR_EnableBkUpAccess(); |
lvagasi | 1:f76b625bd36e | 257 | // __HAL_RTC_WRITEPROTECTION_DISABLE(&rtch); |
lvagasi | 1:f76b625bd36e | 258 | __HAL_RCC_LSE_CONFIG(RCC_LSE_ON); |
lvagasi | 1:f76b625bd36e | 259 | __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); |
lvagasi | 1:f76b625bd36e | 260 | dummy = RCC->BDCR; |
lvagasi | 1:f76b625bd36e | 261 | dummy |= RCC_BDCR_RTCEN; |
lvagasi | 1:f76b625bd36e | 262 | RCC->BDCR = dummy; |
lvagasi | 1:f76b625bd36e | 263 | // __HAL_RCC_RTC_ENABLE(); |
lvagasi | 1:f76b625bd36e | 264 | HAL_status = HAL_RTC_Init(&rtch); |
lvagasi | 1:f76b625bd36e | 265 | pc.printf("HAL_Status: %d\r\n",HAL_status); |
lvagasi | 1:f76b625bd36e | 266 | HAL_status = HAL_RTC_SetDate(&rtch, &rtc_date, FORMAT_BIN); |
lvagasi | 1:f76b625bd36e | 267 | pc.printf("HAL_Status: %d\r\n",HAL_status); |
lvagasi | 1:f76b625bd36e | 268 | HAL_status = HAL_RTC_SetTime(&rtch, &rtc_time, FORMAT_BIN); |
lvagasi | 1:f76b625bd36e | 269 | pc.printf("HAL_Status: %d\r\n",HAL_status); |
lvagasi | 1:f76b625bd36e | 270 | |
lvagasi | 0:28b9efbdeffc | 271 | __GPIOC_CLK_ENABLE(); |
lvagasi | 0:28b9efbdeffc | 272 | keypadInit.Pin = cols[0] | cols[1] | cols[2]; |
lvagasi | 0:28b9efbdeffc | 273 | keypadInit.Mode = GPIO_MODE_OUTPUT_OD; |
lvagasi | 0:28b9efbdeffc | 274 | keypadInit.Pull = GPIO_NOPULL; |
lvagasi | 0:28b9efbdeffc | 275 | keypadInit.Speed = GPIO_SPEED_MEDIUM; |
lvagasi | 0:28b9efbdeffc | 276 | HAL_GPIO_Init(GPIOC, &keypadInit); |
lvagasi | 1:f76b625bd36e | 277 | |
lvagasi | 1:f76b625bd36e | 278 | HAL_GPIO_WritePin(GPIOC, cols[0] | cols[1] | cols[2], GPIO_PIN_SET); |
lvagasi | 1:f76b625bd36e | 279 | HAL_GPIO_WritePin(GPIOC, cols[2], GPIO_PIN_RESET); |
lvagasi | 0:28b9efbdeffc | 280 | |
lvagasi | 0:28b9efbdeffc | 281 | keypadInit.Pin = rows[0] | rows[1] | rows[2] | rows[3]; |
lvagasi | 1:f76b625bd36e | 282 | keypadInit.Mode = GPIO_MODE_INPUT; |
lvagasi | 1:f76b625bd36e | 283 | keypadInit.Pull = GPIO_PULLUP; |
lvagasi | 1:f76b625bd36e | 284 | HAL_GPIO_Init(GPIOC, &keypadInit); |
lvagasi | 1:f76b625bd36e | 285 | keypadInit.Pin = rows[3]; |
lvagasi | 0:28b9efbdeffc | 286 | keypadInit.Mode = GPIO_MODE_IT_FALLING; |
lvagasi | 0:28b9efbdeffc | 287 | keypadInit.Pull = GPIO_PULLUP; |
lvagasi | 0:28b9efbdeffc | 288 | HAL_GPIO_Init(GPIOC, &keypadInit); |
lvagasi | 0:28b9efbdeffc | 289 | NVIC_SetVector(EXTI9_5_IRQn, (uint32_t)EXTI9_5_IRQHandler); |
lvagasi | 0:28b9efbdeffc | 290 | HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); |
lvagasi | 0:28b9efbdeffc | 291 | |
lvagasi | 0:28b9efbdeffc | 292 | NVIC_SetVector(SysTick_IRQn, (uint32_t)SysTick_Handler); |
lvagasi | 0:28b9efbdeffc | 293 | HAL_SYSTICK_Config(14400000); |
lvagasi | 0:28b9efbdeffc | 294 | |
lvagasi | 0:28b9efbdeffc | 295 | __enable_irq(); |
lvagasi | 0:28b9efbdeffc | 296 | |
lvagasi | 0:28b9efbdeffc | 297 | pc.printf("\f\033[00m\033[33mmbed Clock with STM32F303RE NUCLEO board\033[00m\r\n"); |
lvagasi | 0:28b9efbdeffc | 298 | currentHclk = HAL_RCC_GetHCLKFreq(); |
lvagasi | 0:28b9efbdeffc | 299 | pc.printf("Current HCLK is: %d\r\n",currentHclk); |
lvagasi | 0:28b9efbdeffc | 300 | /* speaker.period(1.0/864.0); |
lvagasi | 0:28b9efbdeffc | 301 | speaker = 0.5; |
lvagasi | 0:28b9efbdeffc | 302 | wait(0.1); |
lvagasi | 0:28b9efbdeffc | 303 | speaker = 0.0; |
lvagasi | 0:28b9efbdeffc | 304 | */ |
lvagasi | 0:28b9efbdeffc | 305 | |
lvagasi | 0:28b9efbdeffc | 306 | init_ser_lcd(); |
lvagasi | 0:28b9efbdeffc | 307 | write_ser_lcd(0x80, false); // set DDRAM addr to 0x00 |
lvagasi | 0:28b9efbdeffc | 308 | write_ser_text((unsigned char *)"Hello", 5); |
lvagasi | 0:28b9efbdeffc | 309 | write_ser_lcd(0xC0, false); // set DDRAM addr to 0x40, beginning of 2nd line |
lvagasi | 0:28b9efbdeffc | 310 | write_ser_text((unsigned char *)"Teszt",5); |
lvagasi | 0:28b9efbdeffc | 311 | |
lvagasi | 0:28b9efbdeffc | 312 | wait(2.0); |
lvagasi | 0:28b9efbdeffc | 313 | |
lvagasi | 0:28b9efbdeffc | 314 | write_ser_lcd(0x01, false); // Clear display |
lvagasi | 0:28b9efbdeffc | 315 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 316 | write_ser_lcd(0x06, false); // Entry mode set |
lvagasi | 0:28b9efbdeffc | 317 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 318 | write_ser_lcd(0x80, false); // set DDRAM addr to 0x00, beginning of 1st line |
lvagasi | 0:28b9efbdeffc | 319 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 320 | write_ser_text((unsigned char *)"2015.09.12. Szo", 15); |
lvagasi | 0:28b9efbdeffc | 321 | write_ser_lcd(0xC0, false); // set DDRAM addr to 0x40, beginning of 2nd line |
lvagasi | 0:28b9efbdeffc | 322 | wait_us(30); |
lvagasi | 0:28b9efbdeffc | 323 | write_ser_text((unsigned char *)"23:24:56 23.2", 13); |
lvagasi | 0:28b9efbdeffc | 324 | write_ser_lcd(0xDF, true); |
lvagasi | 0:28b9efbdeffc | 325 | |
lvagasi | 0:28b9efbdeffc | 326 | while (1) { |
lvagasi | 0:28b9efbdeffc | 327 | if (new_key != 0) { |
lvagasi | 0:28b9efbdeffc | 328 | if (Index < 0xFF) { |
lvagasi | 0:28b9efbdeffc | 329 | pc.printf("Key pressed: %c\r\n",Keytable[Index]); |
lvagasi | 0:28b9efbdeffc | 330 | write_ser_lcd(0xCF, false); // set DDRAM addr to 0x4F, end of 2nd line |
lvagasi | 0:28b9efbdeffc | 331 | write_ser_lcd(Keytable[Index], true); |
lvagasi | 0:28b9efbdeffc | 332 | } else { |
lvagasi | 0:28b9efbdeffc | 333 | pc.printf("Incorrect Index value!\r\n"); |
lvagasi | 0:28b9efbdeffc | 334 | } |
lvagasi | 0:28b9efbdeffc | 335 | if (new_key > 1) { |
lvagasi | 0:28b9efbdeffc | 336 | pc.printf("There were missed keys: %d\r\n",new_key); |
lvagasi | 0:28b9efbdeffc | 337 | } |
lvagasi | 0:28b9efbdeffc | 338 | new_key = 0; |
lvagasi | 0:28b9efbdeffc | 339 | Index = 0xFF; |
lvagasi | 0:28b9efbdeffc | 340 | HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); |
lvagasi | 0:28b9efbdeffc | 341 | } |
lvagasi | 0:28b9efbdeffc | 342 | |
lvagasi | 0:28b9efbdeffc | 343 | |
lvagasi | 0:28b9efbdeffc | 344 | HAL_RTC_GetTime(&rtch, &rtc_time, FORMAT_BCD); |
lvagasi | 1:f76b625bd36e | 345 | HAL_RTC_GetDate(&rtch, &rtc_date, FORMAT_BCD); |
lvagasi | 0:28b9efbdeffc | 346 | if (rtc_time.Seconds != tmp_sec) { |
lvagasi | 0:28b9efbdeffc | 347 | tt[0] = BCD2HEX[((rtc_time.Hours & 0xF0) >> 4)]; |
lvagasi | 0:28b9efbdeffc | 348 | tt[1] = BCD2HEX[(rtc_time.Hours & 0x0F)]; |
lvagasi | 0:28b9efbdeffc | 349 | tt[2] = ':'; |
lvagasi | 0:28b9efbdeffc | 350 | tt[3] = BCD2HEX[((rtc_time.Minutes & 0xF0) >> 4)]; |
lvagasi | 0:28b9efbdeffc | 351 | tt[4] = BCD2HEX[(rtc_time.Minutes & 0x0F)]; |
lvagasi | 0:28b9efbdeffc | 352 | tt[5] = ':'; |
lvagasi | 0:28b9efbdeffc | 353 | tt[6] = BCD2HEX[((rtc_time.Seconds & 0xF0) >> 4)]; |
lvagasi | 0:28b9efbdeffc | 354 | tt[7] = BCD2HEX[(rtc_time.Seconds & 0x0F)]; |
lvagasi | 1:f76b625bd36e | 355 | tt[8] = 0x00; // For printf |
lvagasi | 1:f76b625bd36e | 356 | tmp_sec = rtc_time.Seconds; |
lvagasi | 0:28b9efbdeffc | 357 | write_ser_lcd(0xC0, false); |
lvagasi | 1:f76b625bd36e | 358 | pc.printf("\033[3;0H"); // Move the cursur to the beginning of the 3rd line. |
lvagasi | 1:f76b625bd36e | 359 | pc.printf(tt); |
lvagasi | 0:28b9efbdeffc | 360 | write_ser_text((unsigned char *)tt, 8); |
lvagasi | 1:f76b625bd36e | 361 | dummy = 0; |
lvagasi | 1:f76b625bd36e | 362 | |
lvagasi | 1:f76b625bd36e | 363 | if (rtc_date.Date != tmp_date) { |
lvagasi | 1:f76b625bd36e | 364 | tt[0] = BCD2HEX[2]; |
lvagasi | 1:f76b625bd36e | 365 | tt[1] = BCD2HEX[0]; |
lvagasi | 1:f76b625bd36e | 366 | tt[2] = BCD2HEX[((rtc_date.Year & 0xF0) >> 4)]; |
lvagasi | 1:f76b625bd36e | 367 | tt[3] = BCD2HEX[(rtc_date.Year & 0x0F)]; |
lvagasi | 1:f76b625bd36e | 368 | tt[4] = '.'; |
lvagasi | 1:f76b625bd36e | 369 | tt[5] = BCD2HEX[((rtc_date.Month & 0xF0) >> 4)]; |
lvagasi | 1:f76b625bd36e | 370 | tt[6] = BCD2HEX[(rtc_date.Month & 0x0F)]; |
lvagasi | 1:f76b625bd36e | 371 | tt[7] = '.'; |
lvagasi | 1:f76b625bd36e | 372 | tt[8] = BCD2HEX[((rtc_date.Date & 0xF0) >> 4)]; |
lvagasi | 1:f76b625bd36e | 373 | tt[9] = BCD2HEX[(rtc_date.Date & 0x0F)]; |
lvagasi | 1:f76b625bd36e | 374 | tt[10] = '.'; |
lvagasi | 1:f76b625bd36e | 375 | tmp_date = rtc_date.Date; |
lvagasi | 1:f76b625bd36e | 376 | tt[11] = ' '; |
lvagasi | 1:f76b625bd36e | 377 | tt[12] = DOW[rtc_date.WeekDay - 1][0]; |
lvagasi | 1:f76b625bd36e | 378 | tt[13] = DOW[rtc_date.WeekDay - 1][1]; |
lvagasi | 1:f76b625bd36e | 379 | tt[14] = DOW[rtc_date.WeekDay - 1][2]; |
lvagasi | 1:f76b625bd36e | 380 | tt[15] = 0x00; // For printf |
lvagasi | 1:f76b625bd36e | 381 | write_ser_lcd(0x80, false); |
lvagasi | 1:f76b625bd36e | 382 | pc.printf("\033[4;0H"); // Move the cursur to the beginning of the 4th line. |
lvagasi | 1:f76b625bd36e | 383 | pc.printf(tt); |
lvagasi | 1:f76b625bd36e | 384 | write_ser_text((unsigned char *)tt,15); |
lvagasi | 1:f76b625bd36e | 385 | } |
lvagasi | 0:28b9efbdeffc | 386 | } |
lvagasi | 0:28b9efbdeffc | 387 | |
lvagasi | 0:28b9efbdeffc | 388 | if (Systck_cnt > 5) { |
lvagasi | 0:28b9efbdeffc | 389 | switch (RGB_sel % 3) { |
lvagasi | 0:28b9efbdeffc | 390 | case 0: |
lvagasi | 0:28b9efbdeffc | 391 | R_LED = 0; |
lvagasi | 0:28b9efbdeffc | 392 | G_LED = 1; |
lvagasi | 0:28b9efbdeffc | 393 | B_LED = 1; |
lvagasi | 0:28b9efbdeffc | 394 | break; |
lvagasi | 0:28b9efbdeffc | 395 | case 1: |
lvagasi | 0:28b9efbdeffc | 396 | R_LED = 1; |
lvagasi | 0:28b9efbdeffc | 397 | G_LED = 0; |
lvagasi | 0:28b9efbdeffc | 398 | B_LED = 1; |
lvagasi | 0:28b9efbdeffc | 399 | break; |
lvagasi | 0:28b9efbdeffc | 400 | case 2: |
lvagasi | 0:28b9efbdeffc | 401 | R_LED = 1; |
lvagasi | 0:28b9efbdeffc | 402 | G_LED = 1; |
lvagasi | 0:28b9efbdeffc | 403 | B_LED = 0; |
lvagasi | 0:28b9efbdeffc | 404 | break; |
lvagasi | 0:28b9efbdeffc | 405 | default: |
lvagasi | 0:28b9efbdeffc | 406 | R_LED = 1; |
lvagasi | 0:28b9efbdeffc | 407 | G_LED = 1; |
lvagasi | 0:28b9efbdeffc | 408 | B_LED = 1; |
lvagasi | 0:28b9efbdeffc | 409 | break; |
lvagasi | 0:28b9efbdeffc | 410 | } |
lvagasi | 0:28b9efbdeffc | 411 | RGB_sel++; |
lvagasi | 0:28b9efbdeffc | 412 | Systck_cnt = 0; |
lvagasi | 0:28b9efbdeffc | 413 | } |
lvagasi | 0:28b9efbdeffc | 414 | //HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); |
lvagasi | 1:f76b625bd36e | 415 | //__WFI(); |
lvagasi | 1:f76b625bd36e | 416 | while (Systck_evt == 0) { |
lvagasi | 1:f76b625bd36e | 417 | dummy++; |
lvagasi | 1:f76b625bd36e | 418 | } |
lvagasi | 1:f76b625bd36e | 419 | Systck_evt = 0; |
lvagasi | 0:28b9efbdeffc | 420 | } |
lvagasi | 0:28b9efbdeffc | 421 | } |
lvagasi | 1:f76b625bd36e | 422 | |
lvagasi | 1:f76b625bd36e | 423 | |
lvagasi | 1:f76b625bd36e | 424 |