Personal Test Environment
Dependencies: LCD_DISCO_F746NG TS_DISCO_F746NG mbed
TempSense.h@0:bab80a319f0d, 2018-03-06 (annotated)
- Committer:
- DirtyGray
- Date:
- Tue Mar 06 18:24:17 2018 +0000
- Revision:
- 0:bab80a319f0d
main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DirtyGray | 0:bab80a319f0d | 1 | #include "mbed.h" |
DirtyGray | 0:bab80a319f0d | 2 | |
DirtyGray | 0:bab80a319f0d | 3 | //--------------------------------------------------------- |
DirtyGray | 0:bab80a319f0d | 4 | //Enter amount of minutes between each log |
DirtyGray | 0:bab80a319f0d | 5 | int minutesPerLog = 15; |
DirtyGray | 0:bab80a319f0d | 6 | |
DirtyGray | 0:bab80a319f0d | 7 | //Enter amount of seconds for the log screen to show |
DirtyGray | 0:bab80a319f0d | 8 | int logTimer = 20; |
DirtyGray | 0:bab80a319f0d | 9 | //--------------------------------------------------------- |
DirtyGray | 0:bab80a319f0d | 10 | |
DirtyGray | 0:bab80a319f0d | 11 | //I2C pins |
DirtyGray | 0:bab80a319f0d | 12 | I2C i2c(PB_9, PB_8); //SDA, SCL | D14 , D15 |
DirtyGray | 0:bab80a319f0d | 13 | |
DirtyGray | 0:bab80a319f0d | 14 | int getTemp(void); |
DirtyGray | 0:bab80a319f0d | 15 | void displayLog(int *); |
DirtyGray | 0:bab80a319f0d | 16 | void initScreen(void); |
DirtyGray | 0:bab80a319f0d | 17 | |
DirtyGray | 0:bab80a319f0d | 18 | LCD_DISCO_F746NG LCDscreen; |
DirtyGray | 0:bab80a319f0d | 19 | TS_StateTypeDef touchStatus; |
DirtyGray | 0:bab80a319f0d | 20 | TS_DISCO_F746NG LCDtouchScreen; |
DirtyGray | 0:bab80a319f0d | 21 | |
DirtyGray | 0:bab80a319f0d | 22 | /** |
DirtyGray | 0:bab80a319f0d | 23 | * @brief Displays the temperature |
DirtyGray | 0:bab80a319f0d | 24 | * @retval None |
DirtyGray | 0:bab80a319f0d | 25 | */ |
DirtyGray | 0:bab80a319f0d | 26 | void tempDisplay(void) |
DirtyGray | 0:bab80a319f0d | 27 | { |
DirtyGray | 0:bab80a319f0d | 28 | //If the log is recording every 0 minutes do not show log |
DirtyGray | 0:bab80a319f0d | 29 | if (minutesPerLog == 0) { |
DirtyGray | 0:bab80a319f0d | 30 | return; |
DirtyGray | 0:bab80a319f0d | 31 | } |
DirtyGray | 0:bab80a319f0d | 32 | |
DirtyGray | 0:bab80a319f0d | 33 | initScreen(); |
DirtyGray | 0:bab80a319f0d | 34 | i2c.frequency(100000); |
DirtyGray | 0:bab80a319f0d | 35 | |
DirtyGray | 0:bab80a319f0d | 36 | int counter = 0; |
DirtyGray | 0:bab80a319f0d | 37 | unsigned short StringX = 0; |
DirtyGray | 0:bab80a319f0d | 38 | unsigned short StringY = 250; |
DirtyGray | 0:bab80a319f0d | 39 | unsigned char helpDisplay[100] = {"Hold screen for Log"}; |
DirtyGray | 0:bab80a319f0d | 40 | unsigned char tempDisplay[100]; |
DirtyGray | 0:bab80a319f0d | 41 | int tempLog[10] = {0}; |
DirtyGray | 0:bab80a319f0d | 42 | |
DirtyGray | 0:bab80a319f0d | 43 | //Reset screen after log display |
DirtyGray | 0:bab80a319f0d | 44 | back: |
DirtyGray | 0:bab80a319f0d | 45 | LCDscreen.Clear(LCD_COLOR_BLUE); |
DirtyGray | 0:bab80a319f0d | 46 | LCDscreen.DisplayStringAt(0, 0, helpDisplay, LEFT_MODE); |
DirtyGray | 0:bab80a319f0d | 47 | |
DirtyGray | 0:bab80a319f0d | 48 | //Main loop |
DirtyGray | 0:bab80a319f0d | 49 | while(1) { |
DirtyGray | 0:bab80a319f0d | 50 | counter++; |
DirtyGray | 0:bab80a319f0d | 51 | //Grabs temp and puts it into display portion of log [0], also adds one |
DirtyGray | 0:bab80a319f0d | 52 | //second delay |
DirtyGray | 0:bab80a319f0d | 53 | int temp = getTemp(); |
DirtyGray | 0:bab80a319f0d | 54 | tempLog[0] = temp; |
DirtyGray | 0:bab80a319f0d | 55 | |
DirtyGray | 0:bab80a319f0d | 56 | //Checks to see if time has been met for a log update |
DirtyGray | 0:bab80a319f0d | 57 | if (counter >= (minutesPerLog * 60)) { |
DirtyGray | 0:bab80a319f0d | 58 | for (int i = 9; i > 0; i--) { |
DirtyGray | 0:bab80a319f0d | 59 | tempLog[i] = tempLog[i - 1]; |
DirtyGray | 0:bab80a319f0d | 60 | } |
DirtyGray | 0:bab80a319f0d | 61 | counter = 0; |
DirtyGray | 0:bab80a319f0d | 62 | } |
DirtyGray | 0:bab80a319f0d | 63 | |
DirtyGray | 0:bab80a319f0d | 64 | //Displays the current temperature |
DirtyGray | 0:bab80a319f0d | 65 | if (temp < 100) { |
DirtyGray | 0:bab80a319f0d | 66 | sprintf((char*)tempDisplay, "Current Temperature = %dF", temp); |
DirtyGray | 0:bab80a319f0d | 67 | } else if (temp >= 100) { |
DirtyGray | 0:bab80a319f0d | 68 | sprintf((char*)tempDisplay, "Current Temperature =%dF", temp); |
DirtyGray | 0:bab80a319f0d | 69 | } |
DirtyGray | 0:bab80a319f0d | 70 | LCDscreen.DisplayStringAt(StringX, StringY, tempDisplay, LEFT_MODE); |
DirtyGray | 0:bab80a319f0d | 71 | |
DirtyGray | 0:bab80a319f0d | 72 | //Checks for touch to display log |
DirtyGray | 0:bab80a319f0d | 73 | LCDtouchScreen.GetState(&touchStatus); |
DirtyGray | 0:bab80a319f0d | 74 | if (touchStatus.touchDetected) { |
DirtyGray | 0:bab80a319f0d | 75 | displayLog(tempLog); |
DirtyGray | 0:bab80a319f0d | 76 | counter = counter + logTimer; |
DirtyGray | 0:bab80a319f0d | 77 | goto back; |
DirtyGray | 0:bab80a319f0d | 78 | } |
DirtyGray | 0:bab80a319f0d | 79 | } |
DirtyGray | 0:bab80a319f0d | 80 | } |
DirtyGray | 0:bab80a319f0d | 81 | |
DirtyGray | 0:bab80a319f0d | 82 | /** |
DirtyGray | 0:bab80a319f0d | 83 | * @brief Retrieves temperature from chip |
DirtyGray | 0:bab80a319f0d | 84 | * @retval Temperature |
DirtyGray | 0:bab80a319f0d | 85 | */ |
DirtyGray | 0:bab80a319f0d | 86 | int getTemp(void) |
DirtyGray | 0:bab80a319f0d | 87 | { |
DirtyGray | 0:bab80a319f0d | 88 | char tempData[2]; |
DirtyGray | 0:bab80a319f0d | 89 | char getTemp[1] = {0xAA}; |
DirtyGray | 0:bab80a319f0d | 90 | char stopConvert[1] = {0x22}; |
DirtyGray | 0:bab80a319f0d | 91 | char startConvert[1] = {0xEE}; |
DirtyGray | 0:bab80a319f0d | 92 | int temp; |
DirtyGray | 0:bab80a319f0d | 93 | |
DirtyGray | 0:bab80a319f0d | 94 | //Grabs the temperature from the DS1621, converts to F and returns value |
DirtyGray | 0:bab80a319f0d | 95 | //Convert tells the DS1621 to update its temperature reading |
DirtyGray | 0:bab80a319f0d | 96 | //Read and Write have different addresses because I2C communication address |
DirtyGray | 0:bab80a319f0d | 97 | //uses a 1 for write and a 0 for read at end of binary address |
DirtyGray | 0:bab80a319f0d | 98 | i2c.write(145, startConvert, 1, false); |
DirtyGray | 0:bab80a319f0d | 99 | wait(1); |
DirtyGray | 0:bab80a319f0d | 100 | i2c.write(145, stopConvert, 1, false); |
DirtyGray | 0:bab80a319f0d | 101 | i2c.write(145, getTemp, 1, false); |
DirtyGray | 0:bab80a319f0d | 102 | i2c.read(144, tempData, 1); |
DirtyGray | 0:bab80a319f0d | 103 | temp = (tempData[0] * 1.8) + 32; |
DirtyGray | 0:bab80a319f0d | 104 | return (temp); |
DirtyGray | 0:bab80a319f0d | 105 | } |
DirtyGray | 0:bab80a319f0d | 106 | |
DirtyGray | 0:bab80a319f0d | 107 | /** |
DirtyGray | 0:bab80a319f0d | 108 | * @brief Displays the temperature log |
DirtyGray | 0:bab80a319f0d | 109 | * @param tempLog[10]: Array of temperature logs |
DirtyGray | 0:bab80a319f0d | 110 | * @retval None |
DirtyGray | 0:bab80a319f0d | 111 | */ |
DirtyGray | 0:bab80a319f0d | 112 | void displayLog(int tempLog[10]) |
DirtyGray | 0:bab80a319f0d | 113 | { |
DirtyGray | 0:bab80a319f0d | 114 | LCDscreen.SetBackColor(LCD_COLOR_BLUE); |
DirtyGray | 0:bab80a319f0d | 115 | LCDscreen.SetTextColor(LCD_COLOR_WHITE); |
DirtyGray | 0:bab80a319f0d | 116 | |
DirtyGray | 0:bab80a319f0d | 117 | unsigned char logDisplay[500]; |
DirtyGray | 0:bab80a319f0d | 118 | unsigned char helpDisplay2[100]; |
DirtyGray | 0:bab80a319f0d | 119 | |
DirtyGray | 0:bab80a319f0d | 120 | sprintf((char*)helpDisplay2, "Log is updated every %d minutes", minutesPerLog); |
DirtyGray | 0:bab80a319f0d | 121 | |
DirtyGray | 0:bab80a319f0d | 122 | //Displays 10 logs, the first log is the current temperature. If the time |
DirtyGray | 0:bab80a319f0d | 123 | //of one log is divisible by 60 it will note that log as being one hour ago |
DirtyGray | 0:bab80a319f0d | 124 | //Each log is added to the master string "logDisplay" |
DirtyGray | 0:bab80a319f0d | 125 | LCDscreen.Clear(LCD_COLOR_BLUE); |
DirtyGray | 0:bab80a319f0d | 126 | sprintf((char*)logDisplay, "Log:"); |
DirtyGray | 0:bab80a319f0d | 127 | LCDscreen.DisplayStringAt(0, 0, logDisplay, LEFT_MODE); |
DirtyGray | 0:bab80a319f0d | 128 | for (int i = 0; i < 10; i++) { |
DirtyGray | 0:bab80a319f0d | 129 | if (i == 0) { |
DirtyGray | 0:bab80a319f0d | 130 | sprintf((char*)logDisplay, "%dF - Current Temperature", tempLog[i]); |
DirtyGray | 0:bab80a319f0d | 131 | } else if (i == (60 / minutesPerLog)) { |
DirtyGray | 0:bab80a319f0d | 132 | sprintf((char*)logDisplay, "%d: %dF - One Hour Ago", i, tempLog[i]); |
DirtyGray | 0:bab80a319f0d | 133 | } else { |
DirtyGray | 0:bab80a319f0d | 134 | sprintf((char*)logDisplay, "%d: %dF", i, tempLog[i]); |
DirtyGray | 0:bab80a319f0d | 135 | } |
DirtyGray | 0:bab80a319f0d | 136 | LCDscreen.DisplayStringAt(0, (i + 1) * 20, logDisplay, LEFT_MODE); |
DirtyGray | 0:bab80a319f0d | 137 | } |
DirtyGray | 0:bab80a319f0d | 138 | LCDscreen.SetFont(&Font20); |
DirtyGray | 0:bab80a319f0d | 139 | LCDscreen.DisplayStringAt(0, 240, helpDisplay2, LEFT_MODE); |
DirtyGray | 0:bab80a319f0d | 140 | LCDscreen.SetFont(&Font24); |
DirtyGray | 0:bab80a319f0d | 141 | |
DirtyGray | 0:bab80a319f0d | 142 | wait(logTimer); |
DirtyGray | 0:bab80a319f0d | 143 | } |
DirtyGray | 0:bab80a319f0d | 144 | |
DirtyGray | 0:bab80a319f0d | 145 | /** |
DirtyGray | 0:bab80a319f0d | 146 | * @brief Initializes display |
DirtyGray | 0:bab80a319f0d | 147 | * @retval None |
DirtyGray | 0:bab80a319f0d | 148 | */ |
DirtyGray | 0:bab80a319f0d | 149 | void initScreen(void) |
DirtyGray | 0:bab80a319f0d | 150 | { |
DirtyGray | 0:bab80a319f0d | 151 | LCDscreen.Clear(LCD_COLOR_WHITE); |
DirtyGray | 0:bab80a319f0d | 152 | LCDscreen.SetBackColor(LCD_COLOR_BLUE); |
DirtyGray | 0:bab80a319f0d | 153 | LCDscreen.SetTextColor(LCD_COLOR_WHITE); |
DirtyGray | 0:bab80a319f0d | 154 | LCDscreen.SetFont(&Font24); |
DirtyGray | 0:bab80a319f0d | 155 | LCDscreen.Clear(LCD_COLOR_BLUE); |
DirtyGray | 0:bab80a319f0d | 156 | } |