Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
lab10.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 BusOut leds(D14,D15);//creates a global busout that identifies the pins associated with the leds. 00004 bool keycode();//Function prototype for the function that checks if the correct keycode was entered(returns boolean) 00005 float readtempurature(AnalogIn *temp);//function prototype fot the readtempurature function(this function was done by ben) 00006 char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3]); //function prototype for the scan4by3keypad function(returns a char) 00007 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 00008 char usercode[4] = {' ',' ',' ',' '};//creates a global char array that is used to store the entered code. This array is initialized to nothing 00009 00010 00011 char const MGPIOB=0x13; 00012 char const MOLATB=0x15; //modified 00013 char const MIODIRB=0x01; 00014 char const MGPPUB=0x0D; 00015 char const MGPIOA=0x12; 00016 char const MOLATA=0x14; 00017 char const MIODIRA=0x00; 00018 char const MGPPUA=0x0C; 00019 00020 char const initWriteByte = 0b01000000; 00021 char const initReadByte = 0b01000001; 00022 00023 00024 00025 /************************************************************************************ 00026 Main function: 00027 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 00028 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. 00029 00030 Pins declared: 00031 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. 00032 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. 00033 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. 00034 00035 Variables: 00036 button(const char): a 4 by 3 array that is used to store the char equivalent to the keys on the keypad. 00037 access(bool): a boolean variable that is true if the code entered is correct, and is false if the code entered is incorrect. 00038 00039 Functions: 00040 lcd.cls(): a function of the lcd class that will clear the lcd display 00041 lcd.printf(""):a function of the lcd class that will print to the lcd display 00042 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. 00043 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 00044 the button pressed. 00045 wait(x): waits x amount of seconds then continues with the function. 00046 if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met 00047 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 00048 leds[x].write(y):writes y(either a 1 or 0) to the pin at the position x in the leds busout. 00049 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. 00050 for(;;){}: runs the code contained in the brackets forever and ever. 00051 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. 00052 *************************************************************************************/ 00053 int main() { 00054 SPI spi(PC_12,PC_11,PC_10); //mosi, miso, sclk 00055 DigitalOut cs(A5); 00056 AnalogIn temp (A1);//indentifies the pin A1 as an analogin pin named temp 00057 BusOut rowpins(D13,D12,D11,D10);//busout for my row pins 00058 BusIn columnpins(D9,D8,D7);//busin for my colummn pins 00059 columnpins.mode(PullDown);//enables the internal pulldown for the column pins 00060 const char button[4][3]= {{'1','2','3'}, 00061 {'4','5','6'}, 00062 {'7','8','9'}, 00063 {'4','0','#'}};//array which helps determine which button was 00064 bool access = false;//boolean variable that will change depending on whether the keycode is correct or incorrect. 00065 00066 spi.format(8,0); //set spi to transfer 8 bits with SPI MODE 0 (0,0) 00067 spi.frequency(1000000); //set spi clock to 1MHz 00068 00069 while(access == false)//loops through this code while the access is false (keycode entered is wrong) 00070 { 00071 lcd.cls();//clears the LCD display 00072 lcd.printf("Enter Code:\n");//prints "enter code" to the LCD display. 00073 lcd.locate(11,0);//moves the cursor to the 11th column of the 0th row on the lcd display 00074 usercode[0] = scan4by3keypad(&columnpins, &rowpins, button);//sets the first value of usercode to the char returned by the scan4by3keypad function. 00075 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 00076 wait(0.5);//waits half a second 00077 usercode[1] = scan4by3keypad(&columnpins, &rowpins, button);//sets the second value of usercode to the char returned by the scan4by3keypad function. 00078 lcd.locate(12,0);//moves the cursor to the 12th column of the 0th row on the lcd display 00079 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 00080 wait(0.5);//waits half a second 00081 usercode[2] = scan4by3keypad(&columnpins, &rowpins, button);//sets the third value of usercode to the char returned by the scan4by3keypad function. 00082 lcd.locate(13,0);//moves the cursor to the 13th column of the 0th row on the lcd display 00083 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 00084 wait(0.5);//waits half a second. 00085 usercode[3] = scan4by3keypad(&columnpins, &rowpins, button);//sets the fourth value of usercode to the char returned by the scan4by3keypad function. 00086 lcd.locate(14,0);//moves the cursor to the 14th column of the 0th row on the lcd display 00087 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 00088 wait(1);//waits one second 00089 if(keycode() == true)//if the keycode is correct this code will run (breaks the loop) 00090 { 00091 lcd.cls();//clears the lcd completely 00092 lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD 00093 lcd.printf("Access granted!\n");//prints "Access granted" to the LCD 00094 leds[0].write(1);//writes 1 to the 0th value of the leds busout. (green LED) 00095 wait(1);//waits one second 00096 leds[0].write(0);//writes 0 to the 0th value of the leds busout. (green LED) 00097 access = true;//sets access to true which breaks the while loop. 00098 } 00099 else//if the keycode is not correct this code will run (does not break the loop) 00100 { 00101 lcd.cls();//clears the lcd completely 00102 lcd.locate(0,0);//moves the cursor to the 0th column of the 0th row of the LCD 00103 lcd.printf("Access Denied!");//prints "Access Denied!" to the LCD display 00104 leds[1].write(1);//writes 1 to the 1st value of the leds busout(red led) 00105 wait(1);//waits one second 00106 leds[1].write(0);//writes 0 to the 1st value of the leds busout(red led) 00107 00108 } 00109 } 00110 for(;;)//infinite loop 00111 { 00112 float tempurature; 00113 float y;//float value that stores the duty-cyle calculated on line 15 00114 int bite; 00115 tempurature=readtempurature(&temp); //calls the temperature function and stores its return value 00116 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 00117 //servo.period(0.02); // sets the period of the servo PWM to 20 ms as specifed in lesson 7 00118 //servo.write(y); //writes the calculated duty-cycle to the servo 00119 bite=((28.333333333333*(y*100))-85); 00120 //printf("The tempurature is: %f\n",tempurature);//prints the temperature in celcius to the serial terminal 00121 //wait(0.5); //waits for half a second before running the next iteration 00122 cs = 0; 00123 spi.write(bite); //writes the calculated duty-cycle to the servo 00124 printf("Bite = %.2f", bite); 00125 wait(0.5); 00126 cs = 1; 00127 //printf("The tempurature is: %f\n",temperature);//prints the temperature in celcius to the serial terminal 00128 //wait(0.5); //waits for half a second before running the next iteration 00129 lcd.locate(0,1);//moves the cursor to the 0th column of the first row on the LCD 00130 lcd.printf("Temp: %.2f",tempurature);//prints the value returns by the readtempurature function.(.2f will cutoff the value after the second decimal place. 00131 wait(1.5);//waits 1.5 seconds as to no make the display look all glitchy. 00132 } 00133 } 00134 /******************************************************************************** 00135 keycode function: 00136 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 00137 return false if the code is not correct. 00138 00139 Passed variables: 00140 None 00141 00142 Returned variables: 00143 status(bool) 00144 00145 Pins Declared: 00146 None 00147 00148 variables: 00149 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. 00150 usercode[]:A global array containing the user entered code. 00151 00152 functions: 00153 if(condition){}:the if statement will run the code contained in the curly brackets if the condition is met 00154 return:returns the variable or value to the function that this function was called from. 00155 ********************************************************************************/ 00156 bool keycode() 00157 { 00158 bool status = false;//creates a boolean variable called status that is initialized to false 00159 if(usercode[0] == '1' && usercode[1] == '2' && usercode[2] == '3' && usercode[3] == '4'||//if usercode array is 1234 or 4321 or 0000 00160 usercode[0] == '4' && usercode[1] == '3' && usercode[2] == '2' && usercode[3] == '1'|| 00161 usercode[0] == '0' && usercode[1] == '0' && usercode[2] == '0' && usercode[3] == '0') 00162 { 00163 status = true;//sets status to true 00164 } 00165 return status;//returns the value of status. 00166 } 00167 /************************************************* 00168 scan4by3keypad function: 00169 function: This function will loop through rows turning each row high and then reads the columns and if that column is high 00170 it can determine which button was pressed. This uses blocking polling. 00171 00172 variables: 00173 row (BusOut): creates the busout for my row pins 00174 col (BusOut): creates a busout for my column pins 00175 k (const char): an array containing the values of the buttons to be used when printing what button was pressed 00176 buttonpressed (char): a char buffer which stores the char value at the point in the k array depending what button is pressed. 00177 j (int): loop condition associated with my row pins 00178 isbuttonpressed (int): This int is the while loop condiition. 00179 00180 functions: 00181 pin->write(0b0001<<j): will write the byte to the pins specified and will shift depending on what value j is 00182 pin->read(): reads from the associated pin(s) 00183 printf(): prints to the terminal 00184 switch col->read(): will look at the case associated with what returns from the column read. 00185 while(condition): will keep looping what is inside of the loop until the condition is not met. This is used for my blocking polling. 00186 ****************************************************/ 00187 char scan4by3keypad(BusIn* col, BusOut* row, const char k[4][3]) 00188 { char buttonpressed; 00189 int isbuttonpressed=0; 00190 while(isbuttonpressed==0) 00191 { 00192 for(int j=0;j<4;j++)//loops j which will be used to turn the rows on 00193 { 00194 row->write(0b0001<<j);//writes a 1 to the jth pin 00195 switch (col->read()) 00196 { 00197 case 0b001://if the first column is high 00198 buttonpressed = k[j][0];//equals the array at the jth and zero position. 00199 isbuttonpressed=1;//breaks the while loop 00200 break; 00201 case 0b010://if the second column is high 00202 buttonpressed = k[j][1];//equals the array at the jth and one position. 00203 isbuttonpressed=1;//breaks the while loop 00204 break; 00205 case 0b100://if the third column is high 00206 buttonpressed = k[j][2];//equals the array at the jth and second position 00207 isbuttonpressed=1;//breaks the while loop 00208 break; 00209 } 00210 row->write(0b0001<<j);//resets the jth pin low so there is no confusion as to what button is pressed 00211 } 00212 } 00213 return buttonpressed;//returns the char value stored in the array 00214 } 00215 /********************************************************************************************************** 00216 Description: The purpose of this function is to calculate the temperature being read by the thermistor 00217 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. 00218 Below you will find a rough drawing of what the circuit should look like 00219 00220 3.3V---------Resistor---wire from A1-------Thermistor-------GND 00221 00222 Parameters: 00223 00224 temp->Defines the analog input pin used to measure the voltage of the thermistor 00225 00226 00227 Returns: 00228 celcius: Returns the tempurature measured by the thermistor in degrees celsius as a float value 00229 **********************************************************************************************************/ 00230 float readtempurature(AnalogIn *temp) 00231 { 00232 float tempVal; //variable that stores the voltage reading of the thermistor as a value between 1 and zero 00233 tempVal=temp->read(); //reads the voltage of the thermistor on analog pin 1 of the nucleo f411RE 00234 float vrt; //variable that stores the actual value of the voltage of the thermistor 00235 vrt=(tempVal*3.3);//this formula determines the actual voltage of the thermistor 00236 float top;//variable that defines the numerator of the formula to determine the resistance of the thermistor 00237 top=(vrt*10000);//numerator of the formula used to determine the thermisistor resistance 00238 float bottom; //variable that defines the denominator of the formula to determine the resistance of the thermistor 00239 bottom=(3.3-vrt); //denominator of the formula to determine the thermisistor resistance 00240 float rt=(top/bottom);//this is the formula to determine the thermisitor resistance 00241 float A=(3.354016e-3);//Constant A1 in the formula used to calculate the temperature given the resistance of the thermisitor 00242 float B=(2.569650e-4);//Constant B1 in the formula used to calculate the temperature given the resistance of the thermisitor 00243 float C=(2.620131e-6);//Constant C1 in the formula used to calculate the temperature given the resistance of the thermisitor 00244 float D=(6.383091e-8);//Constant D1 in the formula used to calculate the temperature given the resistance of the thermisitor 00245 float ln1=(log(rt/10000)); //First ln operation in the formula used to calculate the temperature given the resistance of the thermisitor 00246 float ln2=log(pow((rt/10000),2));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor 00247 float ln3=log(pow((rt/10000),3));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor 00248 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 00249 float celcius=((1/denominator)-273.15);//completed formula to calculate the tempurature given the resistance of the thermisitor 00250 00251 return celcius; //returns the value of the tempurature in degrees celsius 00252 }
Generated on Mon Aug 8 2022 14:09:34 by
1.7.2