
TP_capteur_courant_v2
Dependencies: BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed
Fork of TP_Interfacage_Capteur_Courant by
main.cpp@0:3cfe1338ab3d, 2018-06-21 (annotated)
- Committer:
- MoussOudj
- Date:
- Thu Jun 21 08:16:30 2018 +0000
- Revision:
- 0:3cfe1338ab3d
- Child:
- 1:724f5e3b485a
TP_Capteur_courant_v1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MoussOudj | 0:3cfe1338ab3d | 1 | #include "mbed.h" |
MoussOudj | 0:3cfe1338ab3d | 2 | #include "TS_DISCO_F746NG.h" |
MoussOudj | 0:3cfe1338ab3d | 3 | #include "LCD_DISCO_F746NG.h" |
MoussOudj | 0:3cfe1338ab3d | 4 | #include "Smiley.h" |
MoussOudj | 0:3cfe1338ab3d | 5 | |
MoussOudj | 0:3cfe1338ab3d | 6 | LCD_DISCO_F746NG lcd; |
MoussOudj | 0:3cfe1338ab3d | 7 | TS_DISCO_F746NG ts; |
MoussOudj | 0:3cfe1338ab3d | 8 | AnalogIn anal(A4); |
MoussOudj | 0:3cfe1338ab3d | 9 | TS_StateTypeDef TS_State; |
MoussOudj | 0:3cfe1338ab3d | 10 | |
MoussOudj | 0:3cfe1338ab3d | 11 | ///////////////////////////////////Déclaration des variables/////////////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 12 | double tension = 0; |
MoussOudj | 0:3cfe1338ab3d | 13 | double amp = 0; |
MoussOudj | 0:3cfe1338ab3d | 14 | int active=0; |
MoussOudj | 0:3cfe1338ab3d | 15 | |
MoussOudj | 0:3cfe1338ab3d | 16 | |
MoussOudj | 0:3cfe1338ab3d | 17 | uint16_t x, y; |
MoussOudj | 0:3cfe1338ab3d | 18 | uint8_t text[30]; |
MoussOudj | 0:3cfe1338ab3d | 19 | uint8_t status; |
MoussOudj | 0:3cfe1338ab3d | 20 | uint8_t idx; |
MoussOudj | 0:3cfe1338ab3d | 21 | uint8_t cleared = 0; |
MoussOudj | 0:3cfe1338ab3d | 22 | uint8_t prev_nb_touches = 0; |
MoussOudj | 0:3cfe1338ab3d | 23 | ///////////////////////////////////Déclaration des variables/////////////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 24 | |
MoussOudj | 0:3cfe1338ab3d | 25 | |
MoussOudj | 0:3cfe1338ab3d | 26 | |
MoussOudj | 0:3cfe1338ab3d | 27 | //////////////////////////////////////////PROTOTYPE//////////////////////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 28 | void Init_TS(void); //Fonction d'initialisation du TouchScreen |
MoussOudj | 0:3cfe1338ab3d | 29 | void Init_Bouton(void); //Fonction d'initalisation des boutons |
MoussOudj | 0:3cfe1338ab3d | 30 | void Affichage_valeurs(void); //Fonction d'affichage de la tension et du courant |
MoussOudj | 0:3cfe1338ab3d | 31 | void Action_bouton(void); //Fonction d'action à l'appui des bouton |
MoussOudj | 0:3cfe1338ab3d | 32 | //////////////////////////////////////////PROTOTYPE//////////////////////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 33 | |
MoussOudj | 0:3cfe1338ab3d | 34 | |
MoussOudj | 0:3cfe1338ab3d | 35 | |
MoussOudj | 0:3cfe1338ab3d | 36 | |
MoussOudj | 0:3cfe1338ab3d | 37 | int main() |
MoussOudj | 0:3cfe1338ab3d | 38 | { |
MoussOudj | 0:3cfe1338ab3d | 39 | |
MoussOudj | 0:3cfe1338ab3d | 40 | |
MoussOudj | 0:3cfe1338ab3d | 41 | |
MoussOudj | 0:3cfe1338ab3d | 42 | Init_TS(); //Appel de la fonction d'initialisation du TouchScreen |
MoussOudj | 0:3cfe1338ab3d | 43 | |
MoussOudj | 0:3cfe1338ab3d | 44 | Init_Bouton(); //Appel de la fonction d'initialisation des boutons |
MoussOudj | 0:3cfe1338ab3d | 45 | |
MoussOudj | 0:3cfe1338ab3d | 46 | //lcd.DrawBitmap(20,20,(uint8_t *)test); |
MoussOudj | 0:3cfe1338ab3d | 47 | |
MoussOudj | 0:3cfe1338ab3d | 48 | |
MoussOudj | 0:3cfe1338ab3d | 49 | while(1) { |
MoussOudj | 0:3cfe1338ab3d | 50 | |
MoussOudj | 0:3cfe1338ab3d | 51 | |
MoussOudj | 0:3cfe1338ab3d | 52 | Affichage_valeurs(); //Appel de la fonction pour calculer et afficher sur l'ecran LCD la tension et le courant |
MoussOudj | 0:3cfe1338ab3d | 53 | |
MoussOudj | 0:3cfe1338ab3d | 54 | ts.GetState(&TS_State); //Récupération de l'état du TouchScreen |
MoussOudj | 0:3cfe1338ab3d | 55 | |
MoussOudj | 0:3cfe1338ab3d | 56 | if (TS_State.touchDetected) { //Si touché détecté |
MoussOudj | 0:3cfe1338ab3d | 57 | |
MoussOudj | 0:3cfe1338ab3d | 58 | |
MoussOudj | 0:3cfe1338ab3d | 59 | for (idx = 0; idx < TS_State.touchDetected; idx++) { //Récupération de l'axe x et y |
MoussOudj | 0:3cfe1338ab3d | 60 | x = TS_State.touchX[idx]; //Récupération de l'axe x |
MoussOudj | 0:3cfe1338ab3d | 61 | y = TS_State.touchY[idx]; //Récupération de l'axey |
MoussOudj | 0:3cfe1338ab3d | 62 | } |
MoussOudj | 0:3cfe1338ab3d | 63 | |
MoussOudj | 0:3cfe1338ab3d | 64 | |
MoussOudj | 0:3cfe1338ab3d | 65 | Action_bouton(); //Appel de la fonction qui permet d'effectuer l'action des boutons Start et Stop |
MoussOudj | 0:3cfe1338ab3d | 66 | |
MoussOudj | 0:3cfe1338ab3d | 67 | |
MoussOudj | 0:3cfe1338ab3d | 68 | } |
MoussOudj | 0:3cfe1338ab3d | 69 | } |
MoussOudj | 0:3cfe1338ab3d | 70 | } |
MoussOudj | 0:3cfe1338ab3d | 71 | |
MoussOudj | 0:3cfe1338ab3d | 72 | |
MoussOudj | 0:3cfe1338ab3d | 73 | |
MoussOudj | 0:3cfe1338ab3d | 74 | |
MoussOudj | 0:3cfe1338ab3d | 75 | |
MoussOudj | 0:3cfe1338ab3d | 76 | |
MoussOudj | 0:3cfe1338ab3d | 77 | |
MoussOudj | 0:3cfe1338ab3d | 78 | ///////////////////////////////////////Initialisation TouchScreen////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 79 | void Init_TS(void) |
MoussOudj | 0:3cfe1338ab3d | 80 | { |
MoussOudj | 0:3cfe1338ab3d | 81 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Capteur courant DEMO", CENTER_MODE); //Affichage titre projet |
MoussOudj | 0:3cfe1338ab3d | 82 | |
MoussOudj | 0:3cfe1338ab3d | 83 | wait(4); //Temporisation |
MoussOudj | 0:3cfe1338ab3d | 84 | |
MoussOudj | 0:3cfe1338ab3d | 85 | status = ts.Init(lcd.GetXSize(), lcd.GetYSize()); //Acquisition des dimensions de l'écran |
MoussOudj | 0:3cfe1338ab3d | 86 | |
MoussOudj | 0:3cfe1338ab3d | 87 | if (status != TS_OK) { //Si TouchScreen différent de OK |
MoussOudj | 0:3cfe1338ab3d | 88 | lcd.Clear(LCD_COLOR_RED); |
MoussOudj | 0:3cfe1338ab3d | 89 | lcd.SetBackColor(LCD_COLOR_RED); |
MoussOudj | 0:3cfe1338ab3d | 90 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MoussOudj | 0:3cfe1338ab3d | 91 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); //Afficher Etat fail |
MoussOudj | 0:3cfe1338ab3d | 92 | } else { //Sinon |
MoussOudj | 0:3cfe1338ab3d | 93 | lcd.Clear(LCD_COLOR_GREEN); |
MoussOudj | 0:3cfe1338ab3d | 94 | lcd.SetBackColor(LCD_COLOR_GREEN); |
MoussOudj | 0:3cfe1338ab3d | 95 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MoussOudj | 0:3cfe1338ab3d | 96 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); //Afficher Etat OK |
MoussOudj | 0:3cfe1338ab3d | 97 | } |
MoussOudj | 0:3cfe1338ab3d | 98 | |
MoussOudj | 0:3cfe1338ab3d | 99 | wait(1); //Temporisation |
MoussOudj | 0:3cfe1338ab3d | 100 | |
MoussOudj | 0:3cfe1338ab3d | 101 | |
MoussOudj | 0:3cfe1338ab3d | 102 | } |
MoussOudj | 0:3cfe1338ab3d | 103 | ///////////////////////////////////////Initialisation TouchScreen////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 104 | |
MoussOudj | 0:3cfe1338ab3d | 105 | |
MoussOudj | 0:3cfe1338ab3d | 106 | |
MoussOudj | 0:3cfe1338ab3d | 107 | ///////////////////////////////////////Initialisation Bouton/////////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 108 | void Init_Bouton(void) |
MoussOudj | 0:3cfe1338ab3d | 109 | { |
MoussOudj | 0:3cfe1338ab3d | 110 | |
MoussOudj | 0:3cfe1338ab3d | 111 | |
MoussOudj | 0:3cfe1338ab3d | 112 | lcd.Clear(LCD_COLOR_BLUE); //Effacer l'affichage actuel |
MoussOudj | 0:3cfe1338ab3d | 113 | lcd.SetFont(&Font16); |
MoussOudj | 0:3cfe1338ab3d | 114 | lcd.SetBackColor(LCD_COLOR_BLUE); //Background en bleu |
MoussOudj | 0:3cfe1338ab3d | 115 | |
MoussOudj | 0:3cfe1338ab3d | 116 | lcd.SetTextColor(LCD_COLOR_RED); //Dessine le premier bouton en rouge |
MoussOudj | 0:3cfe1338ab3d | 117 | lcd.DrawCircle(350, 230, 30); //Dessine le bouton (x,y,rayon) |
MoussOudj | 0:3cfe1338ab3d | 118 | lcd.FillCircle(350, 230, 30); //Remplis le bouton (x,y,rayon) |
MoussOudj | 0:3cfe1338ab3d | 119 | |
MoussOudj | 0:3cfe1338ab3d | 120 | lcd.SetTextColor(LCD_COLOR_GREEN); |
MoussOudj | 0:3cfe1338ab3d | 121 | lcd.DrawCircle(150, 230, 30); |
MoussOudj | 0:3cfe1338ab3d | 122 | lcd.FillCircle(150, 230, 30); |
MoussOudj | 0:3cfe1338ab3d | 123 | |
MoussOudj | 0:3cfe1338ab3d | 124 | lcd.SetTextColor(LCD_COLOR_WHITE); //Remettre l'écriture du texte en blanc |
MoussOudj | 0:3cfe1338ab3d | 125 | |
MoussOudj | 0:3cfe1338ab3d | 126 | |
MoussOudj | 0:3cfe1338ab3d | 127 | } |
MoussOudj | 0:3cfe1338ab3d | 128 | ///////////////////////////////////////Initialisation Bouton////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 129 | |
MoussOudj | 0:3cfe1338ab3d | 130 | |
MoussOudj | 0:3cfe1338ab3d | 131 | |
MoussOudj | 0:3cfe1338ab3d | 132 | ///////////////////////////////////////Affichage des valeurs////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 133 | void Affichage_valeurs(void) |
MoussOudj | 0:3cfe1338ab3d | 134 | { |
MoussOudj | 0:3cfe1338ab3d | 135 | |
MoussOudj | 0:3cfe1338ab3d | 136 | if(active==1) //Si flag à 1 |
MoussOudj | 0:3cfe1338ab3d | 137 | { |
MoussOudj | 0:3cfe1338ab3d | 138 | |
MoussOudj | 0:3cfe1338ab3d | 139 | tension = 0.8*tension + 0.2*((double)anal.read()*3.3); //Acquisition de la tension en entrée du port A4 |
MoussOudj | 0:3cfe1338ab3d | 140 | |
MoussOudj | 0:3cfe1338ab3d | 141 | amp = (23.307*tension) - (38.521); //Calcul du courant à l'aide de l'équation generée sur excel //y = 23.307x - 38.521 |
MoussOudj | 0:3cfe1338ab3d | 142 | |
MoussOudj | 0:3cfe1338ab3d | 143 | } |
MoussOudj | 0:3cfe1338ab3d | 144 | |
MoussOudj | 0:3cfe1338ab3d | 145 | lcd.ClearStringLine(7); //Effacement de la ligne pour actualiser la valeur |
MoussOudj | 0:3cfe1338ab3d | 146 | sprintf((char*)text, "Tension : %.2f",tension); //Ecriture de la valeur |
MoussOudj | 0:3cfe1338ab3d | 147 | lcd.DisplayStringAt(150, LINE(7),(uint8_t *)&text, LEFT_MODE); //Affichage de la valeur |
MoussOudj | 0:3cfe1338ab3d | 148 | |
MoussOudj | 0:3cfe1338ab3d | 149 | lcd.ClearStringLine(8); |
MoussOudj | 0:3cfe1338ab3d | 150 | sprintf((char*)text, "Intensite : %.2f",amp); |
MoussOudj | 0:3cfe1338ab3d | 151 | lcd.DisplayStringAt(150, LINE(8),(uint8_t *)&text, LEFT_MODE); |
MoussOudj | 0:3cfe1338ab3d | 152 | |
MoussOudj | 0:3cfe1338ab3d | 153 | wait(0.3); //Temporisation |
MoussOudj | 0:3cfe1338ab3d | 154 | |
MoussOudj | 0:3cfe1338ab3d | 155 | |
MoussOudj | 0:3cfe1338ab3d | 156 | } |
MoussOudj | 0:3cfe1338ab3d | 157 | ///////////////////////////////////////Affichage des valeurs////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 158 | |
MoussOudj | 0:3cfe1338ab3d | 159 | |
MoussOudj | 0:3cfe1338ab3d | 160 | |
MoussOudj | 0:3cfe1338ab3d | 161 | |
MoussOudj | 0:3cfe1338ab3d | 162 | ///////////////////////////////////////Action des boutons////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 163 | void Action_bouton(void) |
MoussOudj | 0:3cfe1338ab3d | 164 | { |
MoussOudj | 0:3cfe1338ab3d | 165 | |
MoussOudj | 0:3cfe1338ab3d | 166 | if ((TS_State.touchDetected) & (x>320&x<380) & (y>200&y<260)) { //Quand touche détectée à la position du bouton Stop |
MoussOudj | 0:3cfe1338ab3d | 167 | |
MoussOudj | 0:3cfe1338ab3d | 168 | sprintf((char*)text, " "); //Efface l'affichage de l'état Start |
MoussOudj | 0:3cfe1338ab3d | 169 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *)&text, LEFT_MODE); |
MoussOudj | 0:3cfe1338ab3d | 170 | |
MoussOudj | 0:3cfe1338ab3d | 171 | sprintf((char*)text, "Stop"); //Affichage de l'état Stop |
MoussOudj | 0:3cfe1338ab3d | 172 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE); |
MoussOudj | 0:3cfe1338ab3d | 173 | |
MoussOudj | 0:3cfe1338ab3d | 174 | lcd.ClearStringLine(8); //Mise à 0 du courant quand la lecture est stoppée |
MoussOudj | 0:3cfe1338ab3d | 175 | sprintf((char*)text, "Intensite : %.2f",amp); |
MoussOudj | 0:3cfe1338ab3d | 176 | lcd.DisplayStringAt(150, LINE(8),(uint8_t *)&text, LEFT_MODE); |
MoussOudj | 0:3cfe1338ab3d | 177 | amp = 0; |
MoussOudj | 0:3cfe1338ab3d | 178 | |
MoussOudj | 0:3cfe1338ab3d | 179 | lcd.ClearStringLine(7); //Mise à 0 de la tension quand la lecture est stoppée |
MoussOudj | 0:3cfe1338ab3d | 180 | sprintf((char*)text, "Tension : %.2f",tension); |
MoussOudj | 0:3cfe1338ab3d | 181 | lcd.DisplayStringAt(150, LINE(7),(uint8_t *)&text, LEFT_MODE); |
MoussOudj | 0:3cfe1338ab3d | 182 | tension = 0; |
MoussOudj | 0:3cfe1338ab3d | 183 | |
MoussOudj | 0:3cfe1338ab3d | 184 | |
MoussOudj | 0:3cfe1338ab3d | 185 | active=0; //mise à 0 du drapeau |
MoussOudj | 0:3cfe1338ab3d | 186 | |
MoussOudj | 0:3cfe1338ab3d | 187 | |
MoussOudj | 0:3cfe1338ab3d | 188 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MoussOudj | 0:3cfe1338ab3d | 189 | } |
MoussOudj | 0:3cfe1338ab3d | 190 | |
MoussOudj | 0:3cfe1338ab3d | 191 | else if((TS_State.touchDetected) & (x>130&x<180) & (y>200&y<260) ) { //Quand touche détectée à la position du bouton Start |
MoussOudj | 0:3cfe1338ab3d | 192 | |
MoussOudj | 0:3cfe1338ab3d | 193 | sprintf((char*)text, " "); //Efface l'affichage de l'état Stop |
MoussOudj | 0:3cfe1338ab3d | 194 | lcd.DisplayStringAt(0, LINE(2), (uint8_t *)&text, LEFT_MODE); |
MoussOudj | 0:3cfe1338ab3d | 195 | |
MoussOudj | 0:3cfe1338ab3d | 196 | sprintf((char*)text, "Start"); //Affichage de l'état Start |
MoussOudj | 0:3cfe1338ab3d | 197 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *)&text, LEFT_MODE); |
MoussOudj | 0:3cfe1338ab3d | 198 | |
MoussOudj | 0:3cfe1338ab3d | 199 | active=1; //mise à 1 du drapeau |
MoussOudj | 0:3cfe1338ab3d | 200 | |
MoussOudj | 0:3cfe1338ab3d | 201 | lcd.SetTextColor(LCD_COLOR_WHITE); |
MoussOudj | 0:3cfe1338ab3d | 202 | } |
MoussOudj | 0:3cfe1338ab3d | 203 | |
MoussOudj | 0:3cfe1338ab3d | 204 | |
MoussOudj | 0:3cfe1338ab3d | 205 | |
MoussOudj | 0:3cfe1338ab3d | 206 | } |
MoussOudj | 0:3cfe1338ab3d | 207 | ///////////////////////////////////////Action des boutons////////////////////////////////// |
MoussOudj | 0:3cfe1338ab3d | 208 | |
MoussOudj | 0:3cfe1338ab3d | 209 | |
MoussOudj | 0:3cfe1338ab3d | 210 | |
MoussOudj | 0:3cfe1338ab3d | 211 | |
MoussOudj | 0:3cfe1338ab3d | 212 | |
MoussOudj | 0:3cfe1338ab3d | 213 | |
MoussOudj | 0:3cfe1338ab3d | 214 | |
MoussOudj | 0:3cfe1338ab3d | 215 | |
MoussOudj | 0:3cfe1338ab3d | 216 | |
MoussOudj | 0:3cfe1338ab3d | 217 |