
v1
lab10.cpp@1:4715a1559604, 2019-11-20 (annotated)
- Committer:
- OJ_2k
- Date:
- Wed Nov 20 14:34:37 2019 +0000
- Revision:
- 1:4715a1559604
- Parent:
- 0:84b27625b7d0
v1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jEvans99 | 0:84b27625b7d0 | 1 | #include "mbed.h" |
jEvans99 | 0:84b27625b7d0 | 2 | #include "TextLCD.h" |
jEvans99 | 0:84b27625b7d0 | 3 | BusOut leds(D14,D15);//creates a global busout that identifies the pins associated with the leds. |
jEvans99 | 0:84b27625b7d0 | 4 | bool keycode();//Function prototype for the function that checks if the correct keycode was entered(returns boolean) |
jEvans99 | 0:84b27625b7d0 | 5 | float readtempurature(AnalogIn *temp);//function prototype fot the readtempurature function(this function was done by ben) |
jEvans99 | 0:84b27625b7d0 | 6 | char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3]); //function prototype for the scan4by3keypad function(returns a char) |
jEvans99 | 0:84b27625b7d0 | 7 | TextLCD lcd(D6, D5, D4, D3, D2, A0); // creates a global object of the TextLCD class called lcd with the pins D6,D5,D4,D3,D2,A0 assigned. These pins correspond to the pins rs, e, d4-d7 on the lcd |
jEvans99 | 0:84b27625b7d0 | 8 | char usercode[4] = {' ',' ',' ',' '};//creates a global char array that is used to store the entered code. This array is initialized to nothing |
OJ_2k | 1:4715a1559604 | 9 | |
OJ_2k | 1:4715a1559604 | 10 | |
OJ_2k | 1:4715a1559604 | 11 | char const MGPIOB=0x13; |
OJ_2k | 1:4715a1559604 | 12 | char const MOLATB=0x15; //modified |
OJ_2k | 1:4715a1559604 | 13 | char const MIODIRB=0x01; |
OJ_2k | 1:4715a1559604 | 14 | char const MGPPUB=0x0D; |
OJ_2k | 1:4715a1559604 | 15 | char const MGPIOA=0x12; |
OJ_2k | 1:4715a1559604 | 16 | char const MOLATA=0x14; |
OJ_2k | 1:4715a1559604 | 17 | char const MIODIRA=0x00; |
OJ_2k | 1:4715a1559604 | 18 | char const MGPPUA=0x0C; |
OJ_2k | 1:4715a1559604 | 19 | |
OJ_2k | 1:4715a1559604 | 20 | char const initWriteByte = 0b01000000; |
OJ_2k | 1:4715a1559604 | 21 | char const initReadByte = 0b01000001; |
OJ_2k | 1:4715a1559604 | 22 | |
OJ_2k | 1:4715a1559604 | 23 | |
OJ_2k | 1:4715a1559604 | 24 | |
jEvans99 | 0:84b27625b7d0 | 25 | /************************************************************************************ |
jEvans99 | 0:84b27625b7d0 | 26 | Main function: |
jEvans99 | 0:84b27625b7d0 | 27 | Purpose: The purpose of the main function is to determine the usercode entered from the keypad. If the code is correct the function will light the green |
jEvans99 | 0:84b27625b7d0 | 28 | led, print access granted, and then the temperature to the LCD display. if the code entered is incorrect, the function will prompt the user to try again and light the red LED. |
jEvans99 | 0:84b27625b7d0 | 29 | |
jEvans99 | 0:84b27625b7d0 | 30 | Pins declared: |
jEvans99 | 0:84b27625b7d0 | 31 | temp(A1,AnalogIn): This pin is used to bring the value of the thermistor onto the nucleo board since it is an analog value the pin is declared as AnalogIn. |
jEvans99 | 0:84b27625b7d0 | 32 | rowpins(D13,D12,D11,D10, BusOut): These pins are declared as a bus out and are connected to the row pins of the keypad. The pins will be used to turn the bus pins high. |
jEvans99 | 0:84b27625b7d0 | 33 | columnpins(D9,D8,D7, BusIn): These pins are declared as a bus in and are connected to the column pins of the keypad. The pins are used to read the state of the column pins to determine if a key has been pressed. |
jEvans99 | 0:84b27625b7d0 | 34 | |
jEvans99 | 0:84b27625b7d0 | 35 | Variables: |
jEvans99 | 0:84b27625b7d0 | 36 | button(const char): a 4 by 3 array that is used to store the char equivalent to the keys on the keypad. |
jEvans99 | 0:84b27625b7d0 | 37 | access(bool): a boolean variable that is true if the code entered is correct, and is false if the code entered is incorrect. |
jEvans99 | 0:84b27625b7d0 | 38 | |
jEvans99 | 0:84b27625b7d0 | 39 | Functions: |
jEvans99 | 0:84b27625b7d0 | 40 | lcd.cls(): a function of the lcd class that will clear the lcd display |
jEvans99 | 0:84b27625b7d0 | 41 | lcd.printf(""):a function of the lcd class that will print to the lcd display |
jEvans99 | 0:84b27625b7d0 | 42 | lcd.locate(c,r): a function of the lcd class that will move the cursor to the column(c) and row(r) on the lcd that is specified. |
jEvans99 | 0:84b27625b7d0 | 43 | scan4by3keypad(&columnpins, &rowpins, button):a function created previously that accepts the addresses of the row and column pins as well as the value of the button array. This function returns the char representing |
jEvans99 | 0:84b27625b7d0 | 44 | the button pressed. |
jEvans99 | 0:84b27625b7d0 | 45 | wait(x): waits x amount of seconds then continues with the function. |
jEvans99 | 0:84b27625b7d0 | 46 | if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met |
jEvans99 | 0:84b27625b7d0 | 47 | else{}: the else statment is attached to an if statement and runs the code contained in the curly brackets if the condition of the if statement is not met |
jEvans99 | 0:84b27625b7d0 | 48 | leds[x].write(y):writes y(either a 1 or 0) to the pin at the position x in the leds busout. |
jEvans99 | 0:84b27625b7d0 | 49 | while(condition){}:continues to loop through the code contained in the curly brackets while the condition is met. The loop will be broken if the condition is not met. |
jEvans99 | 0:84b27625b7d0 | 50 | for(;;){}: runs the code contained in the brackets forever and ever. |
jEvans99 | 0:84b27625b7d0 | 51 | readtempurature(&temp): A function created by Ben that returns a value of type float representing the temperature. This function accepts the address of the temp pins. |
jEvans99 | 0:84b27625b7d0 | 52 | *************************************************************************************/ |
jEvans99 | 0:84b27625b7d0 | 53 | int main() { |
OJ_2k | 1:4715a1559604 | 54 | SPI spi(PC_12,PC_11,PC_10); //mosi, miso, sclk |
OJ_2k | 1:4715a1559604 | 55 | DigitalOut cs(A5); |
OJ_2k | 1:4715a1559604 | 56 | AnalogIn temp (A1);//indentifies the pin A1 as an analogin pin named temp |
OJ_2k | 1:4715a1559604 | 57 | BusOut rowpins(D13,D12,D11,D10);//busout for my row pins |
jEvans99 | 0:84b27625b7d0 | 58 | BusIn columnpins(D9,D8,D7);//busin for my colummn pins |
jEvans99 | 0:84b27625b7d0 | 59 | columnpins.mode(PullDown);//enables the internal pulldown for the column pins |
jEvans99 | 0:84b27625b7d0 | 60 | const char button[4][3]= {{'1','2','3'}, |
jEvans99 | 0:84b27625b7d0 | 61 | {'4','5','6'}, |
jEvans99 | 0:84b27625b7d0 | 62 | {'7','8','9'}, |
jEvans99 | 0:84b27625b7d0 | 63 | {'4','0','#'}};//array which helps determine which button was |
OJ_2k | 1:4715a1559604 | 64 | bool access = false;//boolean variable that will change depending on whether the keycode is correct or incorrect. |
OJ_2k | 1:4715a1559604 | 65 | |
OJ_2k | 1:4715a1559604 | 66 | spi.format(8,0); //set spi to transfer 8 bits with SPI MODE 0 (0,0) |
OJ_2k | 1:4715a1559604 | 67 | spi.frequency(1000000); //set spi clock to 1MHz |
OJ_2k | 1:4715a1559604 | 68 | |
jEvans99 | 0:84b27625b7d0 | 69 | while(access == false)//loops through this code while the access is false (keycode entered is wrong) |
jEvans99 | 0:84b27625b7d0 | 70 | { |
jEvans99 | 0:84b27625b7d0 | 71 | lcd.cls();//clears the LCD display |
jEvans99 | 0:84b27625b7d0 | 72 | lcd.printf("Enter Code:\n");//prints "enter code" to the LCD display. |
jEvans99 | 0:84b27625b7d0 | 73 | lcd.locate(11,0);//moves the cursor to the 11th column of the 0th row on the lcd display |
jEvans99 | 0:84b27625b7d0 | 74 | usercode[0] = scan4by3keypad(&columnpins, &rowpins, button);//sets the first value of usercode to the char returned by the scan4by3keypad function. |
jEvans99 | 0:84b27625b7d0 | 75 | lcd.printf("*");//prints a * to the lcd at the 11th column of the 0th row ro show that a value has been entered into the usercode |
jEvans99 | 0:84b27625b7d0 | 76 | wait(0.5);//waits half a second |
jEvans99 | 0:84b27625b7d0 | 77 | usercode[1] = scan4by3keypad(&columnpins, &rowpins, button);//sets the second value of usercode to the char returned by the scan4by3keypad function. |
jEvans99 | 0:84b27625b7d0 | 78 | lcd.locate(12,0);//moves the cursor to the 12th column of the 0th row on the lcd display |
jEvans99 | 0:84b27625b7d0 | 79 | lcd.printf("*");//prints a * to the lcd at the 12th column of the 0th row ro show that a value has been entered into the usercode |
jEvans99 | 0:84b27625b7d0 | 80 | wait(0.5);//waits half a second |
jEvans99 | 0:84b27625b7d0 | 81 | usercode[2] = scan4by3keypad(&columnpins, &rowpins, button);//sets the third value of usercode to the char returned by the scan4by3keypad function. |
jEvans99 | 0:84b27625b7d0 | 82 | lcd.locate(13,0);//moves the cursor to the 13th column of the 0th row on the lcd display |
jEvans99 | 0:84b27625b7d0 | 83 | lcd.printf("*");//prints a * to the lcd at the 13th column of the 0th row ro show that a value has been entered into the usercode |
jEvans99 | 0:84b27625b7d0 | 84 | wait(0.5);//waits half a second. |
jEvans99 | 0:84b27625b7d0 | 85 | usercode[3] = scan4by3keypad(&columnpins, &rowpins, button);//sets the fourth value of usercode to the char returned by the scan4by3keypad function. |
jEvans99 | 0:84b27625b7d0 | 86 | lcd.locate(14,0);//moves the cursor to the 14th column of the 0th row on the lcd display |
jEvans99 | 0:84b27625b7d0 | 87 | lcd.printf("*");//prints a * to the lcd at the 14th column of the 0th row ro show that a value has been entered into the usercode |
jEvans99 | 0:84b27625b7d0 | 88 | wait(1);//waits one second |
jEvans99 | 0:84b27625b7d0 | 89 | if(keycode() == true)//if the keycode is correct this code will run (breaks the loop) |
jEvans99 | 0:84b27625b7d0 | 90 | { |
jEvans99 | 0:84b27625b7d0 | 91 | lcd.cls();//clears the lcd completely |
jEvans99 | 0:84b27625b7d0 | 92 | lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD |
jEvans99 | 0:84b27625b7d0 | 93 | lcd.printf("Access granted!\n");//prints "Access granted" to the LCD |
jEvans99 | 0:84b27625b7d0 | 94 | leds[0].write(1);//writes 1 to the 0th value of the leds busout. (green LED) |
jEvans99 | 0:84b27625b7d0 | 95 | wait(1);//waits one second |
jEvans99 | 0:84b27625b7d0 | 96 | leds[0].write(0);//writes 0 to the 0th value of the leds busout. (green LED) |
jEvans99 | 0:84b27625b7d0 | 97 | access = true;//sets access to true which breaks the while loop. |
jEvans99 | 0:84b27625b7d0 | 98 | } |
jEvans99 | 0:84b27625b7d0 | 99 | else//if the keycode is not correct this code will run (does not break the loop) |
jEvans99 | 0:84b27625b7d0 | 100 | { |
jEvans99 | 0:84b27625b7d0 | 101 | lcd.cls();//clears the lcd completely |
jEvans99 | 0:84b27625b7d0 | 102 | lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD |
jEvans99 | 0:84b27625b7d0 | 103 | lcd.printf("Access Denied!");//prints "Access Denied!" to the LCD display |
jEvans99 | 0:84b27625b7d0 | 104 | leds[1].write(1);//writes 1 to the 1st value of the leds busout(red led) |
jEvans99 | 0:84b27625b7d0 | 105 | wait(1);//waits one second |
jEvans99 | 0:84b27625b7d0 | 106 | leds[1].write(0);//writes 0 to the 1st value of the leds busout(red led) |
jEvans99 | 0:84b27625b7d0 | 107 | |
jEvans99 | 0:84b27625b7d0 | 108 | } |
jEvans99 | 0:84b27625b7d0 | 109 | } |
jEvans99 | 0:84b27625b7d0 | 110 | for(;;)//infinite loop |
jEvans99 | 0:84b27625b7d0 | 111 | { |
OJ_2k | 1:4715a1559604 | 112 | float tempurature; |
OJ_2k | 1:4715a1559604 | 113 | float y;//float value that stores the duty-cyle calculated on line 15 |
OJ_2k | 1:4715a1559604 | 114 | int bite; |
OJ_2k | 1:4715a1559604 | 115 | tempurature=readtempurature(&temp); //calls the temperature function and stores its return value |
OJ_2k | 1:4715a1559604 | 116 | y = (((0.25*tempurature) + 3)/100); //formula that converts the temperature measured by the thermistor into an equivalent duty-cycle, in order for the servo to display an accurate 1:1 scale, change the constant from 0.3 to 0.05 |
OJ_2k | 1:4715a1559604 | 117 | //servo.period(0.02); // sets the period of the servo PWM to 20 ms as specifed in lesson 7 |
OJ_2k | 1:4715a1559604 | 118 | //servo.write(y); //writes the calculated duty-cycle to the servo |
OJ_2k | 1:4715a1559604 | 119 | bite=((28.333333333333*(y*100))-85); |
OJ_2k | 1:4715a1559604 | 120 | //printf("The tempurature is: %f\n",tempurature);//prints the temperature in celcius to the serial terminal |
OJ_2k | 1:4715a1559604 | 121 | //wait(0.5); //waits for half a second before running the next iteration |
OJ_2k | 1:4715a1559604 | 122 | cs = 0; |
OJ_2k | 1:4715a1559604 | 123 | spi.write(bite); //writes the calculated duty-cycle to the servo |
OJ_2k | 1:4715a1559604 | 124 | printf("Bite = %.2f", bite); |
OJ_2k | 1:4715a1559604 | 125 | wait(0.5); |
OJ_2k | 1:4715a1559604 | 126 | cs = 1; |
OJ_2k | 1:4715a1559604 | 127 | //printf("The tempurature is: %f\n",temperature);//prints the temperature in celcius to the serial terminal |
OJ_2k | 1:4715a1559604 | 128 | //wait(0.5); //waits for half a second before running the next iteration |
jEvans99 | 0:84b27625b7d0 | 129 | lcd.locate(0,1);//moves the cursor to the 0th column of the first row on the LCD |
OJ_2k | 1:4715a1559604 | 130 | lcd.printf("Temp: %.2f",tempurature);//prints the value returns by the readtempurature function.(.2f will cutoff the value after the second decimal place. |
jEvans99 | 0:84b27625b7d0 | 131 | wait(1.5);//waits 1.5 seconds as to no make the display look all glitchy. |
jEvans99 | 0:84b27625b7d0 | 132 | } |
jEvans99 | 0:84b27625b7d0 | 133 | } |
jEvans99 | 0:84b27625b7d0 | 134 | /******************************************************************************** |
jEvans99 | 0:84b27625b7d0 | 135 | keycode function: |
jEvans99 | 0:84b27625b7d0 | 136 | purpose: The purpose of this function is compare the value of the global array usercode to the correct codes specified by the PM. this function returns true if the code is one of the correct codes and will |
jEvans99 | 0:84b27625b7d0 | 137 | return false if the code is not correct. |
jEvans99 | 0:84b27625b7d0 | 138 | |
jEvans99 | 0:84b27625b7d0 | 139 | Passed variables: |
jEvans99 | 0:84b27625b7d0 | 140 | None |
jEvans99 | 0:84b27625b7d0 | 141 | |
jEvans99 | 0:84b27625b7d0 | 142 | Returned variables: |
jEvans99 | 0:84b27625b7d0 | 143 | status(bool) |
jEvans99 | 0:84b27625b7d0 | 144 | |
jEvans99 | 0:84b27625b7d0 | 145 | Pins Declared: |
jEvans99 | 0:84b27625b7d0 | 146 | None |
jEvans99 | 0:84b27625b7d0 | 147 | |
jEvans99 | 0:84b27625b7d0 | 148 | variables: |
jEvans99 | 0:84b27625b7d0 | 149 | status(bool): a boolean variable that will be set true if the keycode is correct, the variable will be set to false if the keycode is incorrect. |
jEvans99 | 0:84b27625b7d0 | 150 | usercode[]:A global array containing the user entered code. |
jEvans99 | 0:84b27625b7d0 | 151 | |
jEvans99 | 0:84b27625b7d0 | 152 | functions: |
jEvans99 | 0:84b27625b7d0 | 153 | if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met |
jEvans99 | 0:84b27625b7d0 | 154 | return:returns the variable or value to the function that this function was called from. |
jEvans99 | 0:84b27625b7d0 | 155 | ********************************************************************************/ |
jEvans99 | 0:84b27625b7d0 | 156 | bool keycode() |
jEvans99 | 0:84b27625b7d0 | 157 | { |
jEvans99 | 0:84b27625b7d0 | 158 | bool status = false;//creates a boolean variable called status that is initialized to false |
jEvans99 | 0:84b27625b7d0 | 159 | if(usercode[0] == '1' && usercode[1] == '2' && usercode[2] == '3' && usercode[3] == '4'||//if usercode array is 1234 or 4321 or 0000 |
jEvans99 | 0:84b27625b7d0 | 160 | usercode[0] == '4' && usercode[1] == '3' && usercode[2] == '2' && usercode[3] == '1'|| |
jEvans99 | 0:84b27625b7d0 | 161 | usercode[0] == '0' && usercode[1] == '0' && usercode[2] == '0' && usercode[3] == '0') |
jEvans99 | 0:84b27625b7d0 | 162 | { |
jEvans99 | 0:84b27625b7d0 | 163 | status = true;//sets status to true |
jEvans99 | 0:84b27625b7d0 | 164 | } |
jEvans99 | 0:84b27625b7d0 | 165 | return status;//returns the value of status. |
jEvans99 | 0:84b27625b7d0 | 166 | } |
jEvans99 | 0:84b27625b7d0 | 167 | /************************************************* |
jEvans99 | 0:84b27625b7d0 | 168 | scan4by3keypad function: |
jEvans99 | 0:84b27625b7d0 | 169 | function: This function will loop through rows turning each row high and then reads the columns and if that column is high |
jEvans99 | 0:84b27625b7d0 | 170 | it can determine which button was pressed. This uses blocking polling. |
jEvans99 | 0:84b27625b7d0 | 171 | |
jEvans99 | 0:84b27625b7d0 | 172 | variables: |
jEvans99 | 0:84b27625b7d0 | 173 | row (BusOut): creates the busout for my row pins |
jEvans99 | 0:84b27625b7d0 | 174 | col (BusOut): creates a busout for my column pins |
jEvans99 | 0:84b27625b7d0 | 175 | k (const char): an array containing the values of the buttons to be used when printing what button was pressed |
jEvans99 | 0:84b27625b7d0 | 176 | buttonpressed (char): a char buffer which stores the char value at the point in the k array depending what button is pressed. |
jEvans99 | 0:84b27625b7d0 | 177 | j (int): loop condition associated with my row pins |
jEvans99 | 0:84b27625b7d0 | 178 | isbuttonpressed (int): This int is the while loop condiition. |
jEvans99 | 0:84b27625b7d0 | 179 | |
jEvans99 | 0:84b27625b7d0 | 180 | functions: |
jEvans99 | 0:84b27625b7d0 | 181 | pin->write(0b0001<<j): will write the byte to the pins specified and will shift depending on what value j is |
jEvans99 | 0:84b27625b7d0 | 182 | pin->read(): reads from the associated pin(s) |
jEvans99 | 0:84b27625b7d0 | 183 | printf(): prints to the terminal |
jEvans99 | 0:84b27625b7d0 | 184 | switch col->read(): will look at the case associated with what returns from the column read. |
jEvans99 | 0:84b27625b7d0 | 185 | while(condition): will keep looping what is inside of the loop until the condition is not met. This is used for my blocking polling. |
jEvans99 | 0:84b27625b7d0 | 186 | ****************************************************/ |
jEvans99 | 0:84b27625b7d0 | 187 | char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3]) |
jEvans99 | 0:84b27625b7d0 | 188 | { char buttonpressed; |
jEvans99 | 0:84b27625b7d0 | 189 | int isbuttonpressed=0; |
jEvans99 | 0:84b27625b7d0 | 190 | while(isbuttonpressed==0) |
jEvans99 | 0:84b27625b7d0 | 191 | { |
jEvans99 | 0:84b27625b7d0 | 192 | for(int j=0;j<4;j++)//loops j which will be used to turn the rows on |
jEvans99 | 0:84b27625b7d0 | 193 | { |
jEvans99 | 0:84b27625b7d0 | 194 | row->write(0b0001<<j);//writes a 1 to the jth pin |
jEvans99 | 0:84b27625b7d0 | 195 | switch (col->read()) |
jEvans99 | 0:84b27625b7d0 | 196 | { |
jEvans99 | 0:84b27625b7d0 | 197 | case 0b001://if the first column is high |
jEvans99 | 0:84b27625b7d0 | 198 | buttonpressed = k[j][0];//equals the array at the jth and zero position. |
jEvans99 | 0:84b27625b7d0 | 199 | isbuttonpressed=1;//breaks the while loop |
jEvans99 | 0:84b27625b7d0 | 200 | break; |
jEvans99 | 0:84b27625b7d0 | 201 | case 0b010://if the second column is high |
jEvans99 | 0:84b27625b7d0 | 202 | buttonpressed = k[j][1];//equals the array at the jth and one position. |
jEvans99 | 0:84b27625b7d0 | 203 | isbuttonpressed=1;//breaks the while loop |
jEvans99 | 0:84b27625b7d0 | 204 | break; |
jEvans99 | 0:84b27625b7d0 | 205 | case 0b100://if the third column is high |
jEvans99 | 0:84b27625b7d0 | 206 | buttonpressed = k[j][2];//equals the array at the jth and second position |
jEvans99 | 0:84b27625b7d0 | 207 | isbuttonpressed=1;//breaks the while loop |
jEvans99 | 0:84b27625b7d0 | 208 | break; |
jEvans99 | 0:84b27625b7d0 | 209 | } |
jEvans99 | 0:84b27625b7d0 | 210 | row->write(0b0001<<j);//resets the jth pin low so there is no confusion as to what button is pressed |
jEvans99 | 0:84b27625b7d0 | 211 | } |
jEvans99 | 0:84b27625b7d0 | 212 | } |
jEvans99 | 0:84b27625b7d0 | 213 | return buttonpressed;//returns the char value stored in the array |
jEvans99 | 0:84b27625b7d0 | 214 | } |
jEvans99 | 0:84b27625b7d0 | 215 | /********************************************************************************************************** |
jEvans99 | 0:84b27625b7d0 | 216 | Description: The purpose of this function is to calculate the temperature being read by the thermistor |
jEvans99 | 0:84b27625b7d0 | 217 | In order for this code to work, you must connect a 10k resistor in series with the thermistor. The wire from A1 on the nucleo f411RE must be connected after the resistor. The voltage being used in this circuit is 3.3V. |
jEvans99 | 0:84b27625b7d0 | 218 | Below you will find a rough drawing of what the circuit should look like |
jEvans99 | 0:84b27625b7d0 | 219 | |
jEvans99 | 0:84b27625b7d0 | 220 | 3.3V---------Resistor---wire from A1-------Thermistor-------GND |
jEvans99 | 0:84b27625b7d0 | 221 | |
jEvans99 | 0:84b27625b7d0 | 222 | Parameters: |
jEvans99 | 0:84b27625b7d0 | 223 | |
jEvans99 | 0:84b27625b7d0 | 224 | temp->Defines the analog input pin used to measure the voltage of the thermistor |
jEvans99 | 0:84b27625b7d0 | 225 | |
jEvans99 | 0:84b27625b7d0 | 226 | |
jEvans99 | 0:84b27625b7d0 | 227 | Returns: |
jEvans99 | 0:84b27625b7d0 | 228 | celcius: Returns the tempurature measured by the thermistor in degrees celsius as a float value |
jEvans99 | 0:84b27625b7d0 | 229 | **********************************************************************************************************/ |
jEvans99 | 0:84b27625b7d0 | 230 | float readtempurature(AnalogIn *temp) |
jEvans99 | 0:84b27625b7d0 | 231 | { |
jEvans99 | 0:84b27625b7d0 | 232 | float tempVal; //variable that stores the voltage reading of the thermistor as a value between 1 and zero |
jEvans99 | 0:84b27625b7d0 | 233 | tempVal=temp->read(); //reads the voltage of the thermistor on analog pin 1 of the nucleo f411RE |
jEvans99 | 0:84b27625b7d0 | 234 | float vrt; //variable that stores the actual value of the voltage of the thermistor |
jEvans99 | 0:84b27625b7d0 | 235 | vrt=(tempVal*3.3);//this formula determines the actual voltage of the thermistor |
jEvans99 | 0:84b27625b7d0 | 236 | float top;//variable that defines the numerator of the formula to determine the resistance of the thermistor |
jEvans99 | 0:84b27625b7d0 | 237 | top=(vrt*10000);//numerator of the formula used to determine the thermisistor resistance |
jEvans99 | 0:84b27625b7d0 | 238 | float bottom; //variable that defines the denominator of the formula to determine the resistance of the thermistor |
jEvans99 | 0:84b27625b7d0 | 239 | bottom=(3.3-vrt); //denominator of the formula to determine the thermisistor resistance |
jEvans99 | 0:84b27625b7d0 | 240 | float rt=(top/bottom);//this is the formula to determine the thermisitor resistance |
jEvans99 | 0:84b27625b7d0 | 241 | float A=(3.354016e-3);//Constant A1 in the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 242 | float B=(2.569650e-4);//Constant B1 in the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 243 | float C=(2.620131e-6);//Constant C1 in the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 244 | float D=(6.383091e-8);//Constant D1 in the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 245 | float ln1=(log(rt/10000)); //First ln operation in the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 246 | float ln2=log(pow((rt/10000),2));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 247 | float ln3=log(pow((rt/10000),3));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 248 | float denominator=(A+(B*ln1)+(C*ln2)+(D*ln3)); //complete denominator of the formula used to calculate the temperature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 249 | float celcius=((1/denominator)-273.15);//completed formula to calculate the tempurature given the resistance of the thermisitor |
jEvans99 | 0:84b27625b7d0 | 250 | |
jEvans99 | 0:84b27625b7d0 | 251 | return celcius; //returns the value of the tempurature in degrees celsius |
jEvans99 | 0:84b27625b7d0 | 252 | } |