![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
PID en pantalla LCD con botones incremental, decremental y cambiador de variable (Sp, Kp, Ki, Kd) que tambien suenan con diferente frecuencia cuando se pulsan.
Dependencies: Debounced TextLCD mbed
Fork of pid_teclas by
main.cpp@1:69f927a70859, 2014-04-02 (annotated)
- Committer:
- walterg
- Date:
- Wed Apr 02 17:37:10 2014 +0000
- Revision:
- 1:69f927a70859
- Parent:
- 0:9aa80672eb3d
This program use three buttons to increment, decrement and change of variable on a LCD, wich is the visual interface of a PID controller that show Sp, Kp, Ki, Kd. Aditionally, each button send a different PWM signal to make sound a little speaker.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lcorralesc1 | 0:9aa80672eb3d | 1 | #include "mbed.h" |
lcorralesc1 | 0:9aa80672eb3d | 2 | #include "DebouncedIn.h" |
lcorralesc1 | 0:9aa80672eb3d | 3 | #include "TextLCD.h" |
lcorralesc1 | 0:9aa80672eb3d | 4 | |
walterg | 1:69f927a70859 | 5 | float Tdo=1/2093.005; |
walterg | 1:69f927a70859 | 6 | float Pdo=Tdo/2; |
walterg | 1:69f927a70859 | 7 | float Tre=1/2637.02; |
walterg | 1:69f927a70859 | 8 | float Pre=Tre/2; |
walterg | 1:69f927a70859 | 9 | float Tmi=1/3135.963; |
walterg | 1:69f927a70859 | 10 | float Pmi=Tmi/2; |
walterg | 1:69f927a70859 | 11 | |
walterg | 1:69f927a70859 | 12 | |
lcorralesc1 | 0:9aa80672eb3d | 13 | TextLCD lcd(PTB10, PTB11, PTE2, PTE3, PTE4, PTE5); // rs, e, d4-d7 |
walterg | 1:69f927a70859 | 14 | |
lcorralesc1 | 0:9aa80672eb3d | 15 | DigitalOut led1(LED1); |
lcorralesc1 | 0:9aa80672eb3d | 16 | DigitalOut led2(LED2); |
lcorralesc1 | 0:9aa80672eb3d | 17 | DigitalOut led3(LED3); |
lcorralesc1 | 0:9aa80672eb3d | 18 | DebouncedIn button1(PTC12); |
lcorralesc1 | 0:9aa80672eb3d | 19 | DebouncedIn button2(PTC13); |
lcorralesc1 | 0:9aa80672eb3d | 20 | DebouncedIn button3(PTC16); |
lcorralesc1 | 0:9aa80672eb3d | 21 | |
walterg | 1:69f927a70859 | 22 | //DigitalOut Vparlante(PTC5); |
walterg | 1:69f927a70859 | 23 | PwmOut pw(PTA12); |
walterg | 1:69f927a70859 | 24 | |
lcorralesc1 | 0:9aa80672eb3d | 25 | int C1=0x0F; |
lcorralesc1 | 0:9aa80672eb3d | 26 | int sp=0,kp=0,kd=0,ki=0,p=1; |
lcorralesc1 | 0:9aa80672eb3d | 27 | int main() |
lcorralesc1 | 0:9aa80672eb3d | 28 | { |
lcorralesc1 | 0:9aa80672eb3d | 29 | lcd.cls(); |
lcorralesc1 | 0:9aa80672eb3d | 30 | lcd.writeCommand(C1);//escribimos un comando segun el manual del modulo LCD |
lcorralesc1 | 0:9aa80672eb3d | 31 | lcd.locate(8,0); |
lcorralesc1 | 0:9aa80672eb3d | 32 | lcd.printf("kp=%d", kp); |
lcorralesc1 | 0:9aa80672eb3d | 33 | lcd.locate(0,1); |
lcorralesc1 | 0:9aa80672eb3d | 34 | lcd.printf("Ki=%d", ki); |
lcorralesc1 | 0:9aa80672eb3d | 35 | lcd.locate(8,1); |
lcorralesc1 | 0:9aa80672eb3d | 36 | lcd.printf("Kd=%d", kd); |
lcorralesc1 | 0:9aa80672eb3d | 37 | lcd.locate(0,0); |
lcorralesc1 | 0:9aa80672eb3d | 38 | lcd.printf("Sp=%d", sp); |
walterg | 1:69f927a70859 | 39 | |
walterg | 1:69f927a70859 | 40 | |
lcorralesc1 | 0:9aa80672eb3d | 41 | while(1) |
lcorralesc1 | 0:9aa80672eb3d | 42 | { |
lcorralesc1 | 0:9aa80672eb3d | 43 | if (button1.falling()) |
lcorralesc1 | 0:9aa80672eb3d | 44 | { |
walterg | 1:69f927a70859 | 45 | pw.period(Tdo); |
walterg | 1:69f927a70859 | 46 | pw.pulsewidth(Pdo); |
walterg | 1:69f927a70859 | 47 | wait(0.1); |
walterg | 1:69f927a70859 | 48 | pw=0; |
walterg | 1:69f927a70859 | 49 | |
lcorralesc1 | 0:9aa80672eb3d | 50 | led1 =!led1; |
lcorralesc1 | 0:9aa80672eb3d | 51 | if (p==1) |
lcorralesc1 | 0:9aa80672eb3d | 52 | { |
lcorralesc1 | 0:9aa80672eb3d | 53 | ++sp; |
lcorralesc1 | 0:9aa80672eb3d | 54 | lcd.locate(3,0); |
lcorralesc1 | 0:9aa80672eb3d | 55 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 56 | lcd.locate(3,0); |
lcorralesc1 | 0:9aa80672eb3d | 57 | lcd.printf("%d", sp); |
lcorralesc1 | 0:9aa80672eb3d | 58 | } |
lcorralesc1 | 0:9aa80672eb3d | 59 | else if (p==2) |
lcorralesc1 | 0:9aa80672eb3d | 60 | { |
lcorralesc1 | 0:9aa80672eb3d | 61 | ++kp; |
lcorralesc1 | 0:9aa80672eb3d | 62 | lcd.locate(11,0); |
lcorralesc1 | 0:9aa80672eb3d | 63 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 64 | lcd.locate(11,0); |
lcorralesc1 | 0:9aa80672eb3d | 65 | lcd.printf("%d", kp); |
lcorralesc1 | 0:9aa80672eb3d | 66 | } |
lcorralesc1 | 0:9aa80672eb3d | 67 | else if (p==3) |
lcorralesc1 | 0:9aa80672eb3d | 68 | { |
lcorralesc1 | 0:9aa80672eb3d | 69 | ++ki; |
lcorralesc1 | 0:9aa80672eb3d | 70 | lcd.locate(3,1); |
lcorralesc1 | 0:9aa80672eb3d | 71 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 72 | lcd.locate(3,1); |
lcorralesc1 | 0:9aa80672eb3d | 73 | lcd.printf("%d", ki); |
lcorralesc1 | 0:9aa80672eb3d | 74 | } |
lcorralesc1 | 0:9aa80672eb3d | 75 | else if (p==4) |
lcorralesc1 | 0:9aa80672eb3d | 76 | { |
lcorralesc1 | 0:9aa80672eb3d | 77 | ++kd; |
lcorralesc1 | 0:9aa80672eb3d | 78 | lcd.locate(11,1); |
lcorralesc1 | 0:9aa80672eb3d | 79 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 80 | lcd.locate(11,1); |
lcorralesc1 | 0:9aa80672eb3d | 81 | lcd.printf("%d", kd); |
lcorralesc1 | 0:9aa80672eb3d | 82 | } |
lcorralesc1 | 0:9aa80672eb3d | 83 | } |
lcorralesc1 | 0:9aa80672eb3d | 84 | if (button2.falling()) |
lcorralesc1 | 0:9aa80672eb3d | 85 | { |
walterg | 1:69f927a70859 | 86 | pw.period(Tre); |
walterg | 1:69f927a70859 | 87 | pw.pulsewidth(Pre); |
walterg | 1:69f927a70859 | 88 | wait(0.1); |
walterg | 1:69f927a70859 | 89 | pw=0; |
walterg | 1:69f927a70859 | 90 | |
lcorralesc1 | 0:9aa80672eb3d | 91 | led2 =!led2; |
lcorralesc1 | 0:9aa80672eb3d | 92 | if (p==1) |
lcorralesc1 | 0:9aa80672eb3d | 93 | { |
lcorralesc1 | 0:9aa80672eb3d | 94 | if (sp==0) // no mostrar nada |
lcorralesc1 | 0:9aa80672eb3d | 95 | { |
lcorralesc1 | 0:9aa80672eb3d | 96 | } |
lcorralesc1 | 0:9aa80672eb3d | 97 | else |
lcorralesc1 | 0:9aa80672eb3d | 98 | { |
lcorralesc1 | 0:9aa80672eb3d | 99 | --sp; |
lcorralesc1 | 0:9aa80672eb3d | 100 | lcd.locate(3,0); |
lcorralesc1 | 0:9aa80672eb3d | 101 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 102 | lcd.locate(3,0); |
lcorralesc1 | 0:9aa80672eb3d | 103 | lcd.printf("%d", sp); |
lcorralesc1 | 0:9aa80672eb3d | 104 | } |
lcorralesc1 | 0:9aa80672eb3d | 105 | } |
lcorralesc1 | 0:9aa80672eb3d | 106 | if (p==2) |
lcorralesc1 | 0:9aa80672eb3d | 107 | { |
lcorralesc1 | 0:9aa80672eb3d | 108 | if (kp==0) // no mostrar nada |
lcorralesc1 | 0:9aa80672eb3d | 109 | { |
lcorralesc1 | 0:9aa80672eb3d | 110 | } |
lcorralesc1 | 0:9aa80672eb3d | 111 | else |
lcorralesc1 | 0:9aa80672eb3d | 112 | { |
lcorralesc1 | 0:9aa80672eb3d | 113 | --kp; |
lcorralesc1 | 0:9aa80672eb3d | 114 | lcd.locate(11,0); |
lcorralesc1 | 0:9aa80672eb3d | 115 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 116 | lcd.locate(11,0); |
lcorralesc1 | 0:9aa80672eb3d | 117 | lcd.printf("%d", kp); |
lcorralesc1 | 0:9aa80672eb3d | 118 | } |
lcorralesc1 | 0:9aa80672eb3d | 119 | } |
lcorralesc1 | 0:9aa80672eb3d | 120 | if (p==3) |
lcorralesc1 | 0:9aa80672eb3d | 121 | { |
lcorralesc1 | 0:9aa80672eb3d | 122 | if (ki==0) // no mostrar nada |
lcorralesc1 | 0:9aa80672eb3d | 123 | { |
lcorralesc1 | 0:9aa80672eb3d | 124 | } |
lcorralesc1 | 0:9aa80672eb3d | 125 | else |
lcorralesc1 | 0:9aa80672eb3d | 126 | { |
lcorralesc1 | 0:9aa80672eb3d | 127 | --ki; |
lcorralesc1 | 0:9aa80672eb3d | 128 | lcd.locate(3,1); |
lcorralesc1 | 0:9aa80672eb3d | 129 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 130 | lcd.locate(3,1); |
lcorralesc1 | 0:9aa80672eb3d | 131 | lcd.printf("%d", ki); |
lcorralesc1 | 0:9aa80672eb3d | 132 | } |
lcorralesc1 | 0:9aa80672eb3d | 133 | } |
lcorralesc1 | 0:9aa80672eb3d | 134 | if (p==4) |
lcorralesc1 | 0:9aa80672eb3d | 135 | { |
lcorralesc1 | 0:9aa80672eb3d | 136 | if (kd==0) // no mostrar nada |
lcorralesc1 | 0:9aa80672eb3d | 137 | { |
lcorralesc1 | 0:9aa80672eb3d | 138 | } |
lcorralesc1 | 0:9aa80672eb3d | 139 | else |
lcorralesc1 | 0:9aa80672eb3d | 140 | { |
lcorralesc1 | 0:9aa80672eb3d | 141 | --kd; |
lcorralesc1 | 0:9aa80672eb3d | 142 | lcd.locate(11,1); |
lcorralesc1 | 0:9aa80672eb3d | 143 | lcd.printf(" "); |
lcorralesc1 | 0:9aa80672eb3d | 144 | lcd.locate(11,1); |
lcorralesc1 | 0:9aa80672eb3d | 145 | lcd.printf("%d", kd); |
lcorralesc1 | 0:9aa80672eb3d | 146 | } |
lcorralesc1 | 0:9aa80672eb3d | 147 | } |
lcorralesc1 | 0:9aa80672eb3d | 148 | } |
lcorralesc1 | 0:9aa80672eb3d | 149 | if (button3.falling()) |
lcorralesc1 | 0:9aa80672eb3d | 150 | { |
walterg | 1:69f927a70859 | 151 | |
walterg | 1:69f927a70859 | 152 | pw.period(Tmi); |
walterg | 1:69f927a70859 | 153 | pw.pulsewidth(Pmi); |
walterg | 1:69f927a70859 | 154 | wait(0.1); |
walterg | 1:69f927a70859 | 155 | pw=0; |
walterg | 1:69f927a70859 | 156 | |
lcorralesc1 | 0:9aa80672eb3d | 157 | led3 =!led3; |
lcorralesc1 | 0:9aa80672eb3d | 158 | if (p==1) |
lcorralesc1 | 0:9aa80672eb3d | 159 | { |
lcorralesc1 | 0:9aa80672eb3d | 160 | ++p; |
lcorralesc1 | 0:9aa80672eb3d | 161 | lcd.locate(11,0); |
lcorralesc1 | 0:9aa80672eb3d | 162 | lcd.printf("%d", kp); |
lcorralesc1 | 0:9aa80672eb3d | 163 | |
lcorralesc1 | 0:9aa80672eb3d | 164 | |
lcorralesc1 | 0:9aa80672eb3d | 165 | } |
lcorralesc1 | 0:9aa80672eb3d | 166 | else if (p==2) |
lcorralesc1 | 0:9aa80672eb3d | 167 | { |
lcorralesc1 | 0:9aa80672eb3d | 168 | ++p; |
lcorralesc1 | 0:9aa80672eb3d | 169 | lcd.locate(3,1); |
lcorralesc1 | 0:9aa80672eb3d | 170 | lcd.printf("%d", ki); |
lcorralesc1 | 0:9aa80672eb3d | 171 | lcd.locate(3,1); |
lcorralesc1 | 0:9aa80672eb3d | 172 | |
lcorralesc1 | 0:9aa80672eb3d | 173 | } |
lcorralesc1 | 0:9aa80672eb3d | 174 | else if (p==3) |
lcorralesc1 | 0:9aa80672eb3d | 175 | { |
lcorralesc1 | 0:9aa80672eb3d | 176 | ++p; |
lcorralesc1 | 0:9aa80672eb3d | 177 | lcd.locate(11,1); |
lcorralesc1 | 0:9aa80672eb3d | 178 | lcd.printf("%d", kd); |
lcorralesc1 | 0:9aa80672eb3d | 179 | |
lcorralesc1 | 0:9aa80672eb3d | 180 | |
lcorralesc1 | 0:9aa80672eb3d | 181 | } |
lcorralesc1 | 0:9aa80672eb3d | 182 | else if (p==4) |
lcorralesc1 | 0:9aa80672eb3d | 183 | { |
lcorralesc1 | 0:9aa80672eb3d | 184 | p=1; |
lcorralesc1 | 0:9aa80672eb3d | 185 | lcd.locate(3,0); |
lcorralesc1 | 0:9aa80672eb3d | 186 | lcd.printf("%d", sp); |
lcorralesc1 | 0:9aa80672eb3d | 187 | |
lcorralesc1 | 0:9aa80672eb3d | 188 | |
lcorralesc1 | 0:9aa80672eb3d | 189 | } |
lcorralesc1 | 0:9aa80672eb3d | 190 | } |
lcorralesc1 | 0:9aa80672eb3d | 191 | } |
walterg | 1:69f927a70859 | 192 | } |