![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Potenciometro digital x9c103 con oled
Dependencies: BluetoothSerial SeeedShieldBot mbed
main.cpp@0:b2139d0d6c90, 21 months ago (annotated)
- Committer:
- dany54543
- Date:
- Sat Nov 26 21:29:37 2022 +0000
- Revision:
- 0:b2139d0d6c90
Potenciometro Digital x9c103 con pantalla oled
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dany54543 | 0:b2139d0d6c90 | 1 | #include "mbed.h" |
dany54543 | 0:b2139d0d6c90 | 2 | #include "Adafruit_SSD1306.h" |
dany54543 | 0:b2139d0d6c90 | 3 | #include "Adafruit_GFX.h" |
dany54543 | 0:b2139d0d6c90 | 4 | |
dany54543 | 0:b2139d0d6c90 | 5 | //Configuracion OLED |
dany54543 | 0:b2139d0d6c90 | 6 | class I2CPreInit : public I2C |
dany54543 | 0:b2139d0d6c90 | 7 | { |
dany54543 | 0:b2139d0d6c90 | 8 | public: |
dany54543 | 0:b2139d0d6c90 | 9 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
dany54543 | 0:b2139d0d6c90 | 10 | { |
dany54543 | 0:b2139d0d6c90 | 11 | frequency(100000); |
dany54543 | 0:b2139d0d6c90 | 12 | start(); |
dany54543 | 0:b2139d0d6c90 | 13 | }; |
dany54543 | 0:b2139d0d6c90 | 14 | }; |
dany54543 | 0:b2139d0d6c90 | 15 | I2C gI2C(PB_9,PB_8); |
dany54543 | 0:b2139d0d6c90 | 16 | //I2C i2c(PB_9, PB_8); |
dany54543 | 0:b2139d0d6c90 | 17 | Adafruit_SSD1306_I2c Oled(gI2C,D2); |
dany54543 | 0:b2139d0d6c90 | 18 | // |
dany54543 | 0:b2139d0d6c90 | 19 | |
dany54543 | 0:b2139d0d6c90 | 20 | int pct, PD=0; |
dany54543 | 0:b2139d0d6c90 | 21 | |
dany54543 | 0:b2139d0d6c90 | 22 | AnalogIn pot(A0); //Potenciometro Analogo |
dany54543 | 0:b2139d0d6c90 | 23 | |
dany54543 | 0:b2139d0d6c90 | 24 | DigitalOut CS(D1); //Pines del PD |
dany54543 | 0:b2139d0d6c90 | 25 | DigitalOut UD(D2); |
dany54543 | 0:b2139d0d6c90 | 26 | DigitalOut INC(D3); |
dany54543 | 0:b2139d0d6c90 | 27 | |
dany54543 | 0:b2139d0d6c90 | 28 | Thread hilo_cero; |
dany54543 | 0:b2139d0d6c90 | 29 | Thread hilo_mover; |
dany54543 | 0:b2139d0d6c90 | 30 | Thread hilo_porcentaje; |
dany54543 | 0:b2139d0d6c90 | 31 | |
dany54543 | 0:b2139d0d6c90 | 32 | void cero(void); //Llevar el PD a la posicion cero |
dany54543 | 0:b2139d0d6c90 | 33 | void mover(void); //Mover el PD, compara el valor actual y el nuevo valor |
dany54543 | 0:b2139d0d6c90 | 34 | int porcentaje(void); //Convierte el PA en un valor porcentual (0-100) |
dany54543 | 0:b2139d0d6c90 | 35 | |
dany54543 | 0:b2139d0d6c90 | 36 | int main() |
dany54543 | 0:b2139d0d6c90 | 37 | { |
dany54543 | 0:b2139d0d6c90 | 38 | hilo_cero.start(cero); |
dany54543 | 0:b2139d0d6c90 | 39 | hilo_mover.start(mover); |
dany54543 | 0:b2139d0d6c90 | 40 | hilo_porcentaje.start(porcentaje); |
dany54543 | 0:b2139d0d6c90 | 41 | |
dany54543 | 0:b2139d0d6c90 | 42 | Oled.begin(); |
dany54543 | 0:b2139d0d6c90 | 43 | Oled.display(); |
dany54543 | 0:b2139d0d6c90 | 44 | ThisThread::sleep_for(3000ms); |
dany54543 | 0:b2139d0d6c90 | 45 | Oled.clearDisplay(); |
dany54543 | 0:b2139d0d6c90 | 46 | Oled.display(); |
dany54543 | 0:b2139d0d6c90 | 47 | |
dany54543 | 0:b2139d0d6c90 | 48 | cero(); |
dany54543 | 0:b2139d0d6c90 | 49 | while (true) { |
dany54543 | 0:b2139d0d6c90 | 50 | pct = porcentaje(); |
dany54543 | 0:b2139d0d6c90 | 51 | mover(); |
dany54543 | 0:b2139d0d6c90 | 52 | ThisThread::sleep_for(198ms); |
dany54543 | 0:b2139d0d6c90 | 53 | PD=pct; |
dany54543 | 0:b2139d0d6c90 | 54 | Oled.setTextCursor(1, 1); |
dany54543 | 0:b2139d0d6c90 | 55 | Oled.setTextColor(1); |
dany54543 | 0:b2139d0d6c90 | 56 | Oled.setTextSize(1); |
dany54543 | 0:b2139d0d6c90 | 57 | Oled.printf("Valor del PD: %5u", PD); |
dany54543 | 0:b2139d0d6c90 | 58 | Oled.display(); |
dany54543 | 0:b2139d0d6c90 | 59 | } |
dany54543 | 0:b2139d0d6c90 | 60 | } |
dany54543 | 0:b2139d0d6c90 | 61 | |
dany54543 | 0:b2139d0d6c90 | 62 | void cero(void) { // Dejar en 0 el potenciometro |
dany54543 | 0:b2139d0d6c90 | 63 | CS = 1; |
dany54543 | 0:b2139d0d6c90 | 64 | INC = 1; |
dany54543 | 0:b2139d0d6c90 | 65 | |
dany54543 | 0:b2139d0d6c90 | 66 | CS = 0; |
dany54543 | 0:b2139d0d6c90 | 67 | wait_us(1); |
dany54543 | 0:b2139d0d6c90 | 68 | UD = 0; |
dany54543 | 0:b2139d0d6c90 | 69 | for (int n=0; n<100; n++){ |
dany54543 | 0:b2139d0d6c90 | 70 | INC = 0; |
dany54543 | 0:b2139d0d6c90 | 71 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 72 | INC=1; |
dany54543 | 0:b2139d0d6c90 | 73 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 74 | |
dany54543 | 0:b2139d0d6c90 | 75 | } |
dany54543 | 0:b2139d0d6c90 | 76 | |
dany54543 | 0:b2139d0d6c90 | 77 | INC = 1; |
dany54543 | 0:b2139d0d6c90 | 78 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 79 | CS = 1; |
dany54543 | 0:b2139d0d6c90 | 80 | } |
dany54543 | 0:b2139d0d6c90 | 81 | |
dany54543 | 0:b2139d0d6c90 | 82 | void mover(void) { //Valor del PA, Valor actual del PD y comparar |
dany54543 | 0:b2139d0d6c90 | 83 | |
dany54543 | 0:b2139d0d6c90 | 84 | int d=0; |
dany54543 | 0:b2139d0d6c90 | 85 | if (pct>PD){ |
dany54543 | 0:b2139d0d6c90 | 86 | d = pct-PD; |
dany54543 | 0:b2139d0d6c90 | 87 | UD = 1; |
dany54543 | 0:b2139d0d6c90 | 88 | CS = 0; |
dany54543 | 0:b2139d0d6c90 | 89 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 90 | for (int o=0; o<d; o++){ |
dany54543 | 0:b2139d0d6c90 | 91 | INC = 0; |
dany54543 | 0:b2139d0d6c90 | 92 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 93 | INC=1; |
dany54543 | 0:b2139d0d6c90 | 94 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 95 | } |
dany54543 | 0:b2139d0d6c90 | 96 | INC = 1; |
dany54543 | 0:b2139d0d6c90 | 97 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 98 | CS = 1; |
dany54543 | 0:b2139d0d6c90 | 99 | } |
dany54543 | 0:b2139d0d6c90 | 100 | |
dany54543 | 0:b2139d0d6c90 | 101 | if (pct<PD){ |
dany54543 | 0:b2139d0d6c90 | 102 | d = PD-pct; |
dany54543 | 0:b2139d0d6c90 | 103 | UD = 0; |
dany54543 | 0:b2139d0d6c90 | 104 | CS = 0; |
dany54543 | 0:b2139d0d6c90 | 105 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 106 | for (int o=0; o<d; o++){ |
dany54543 | 0:b2139d0d6c90 | 107 | INC = 0; |
dany54543 | 0:b2139d0d6c90 | 108 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 109 | INC=1; |
dany54543 | 0:b2139d0d6c90 | 110 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 111 | } |
dany54543 | 0:b2139d0d6c90 | 112 | INC = 1; |
dany54543 | 0:b2139d0d6c90 | 113 | wait_us (1); |
dany54543 | 0:b2139d0d6c90 | 114 | CS = 1; |
dany54543 | 0:b2139d0d6c90 | 115 | } |
dany54543 | 0:b2139d0d6c90 | 116 | |
dany54543 | 0:b2139d0d6c90 | 117 | Oled.setTextCursor(1, 40); |
dany54543 | 0:b2139d0d6c90 | 118 | Oled.setTextColor(1); |
dany54543 | 0:b2139d0d6c90 | 119 | Oled.setTextSize(1); |
dany54543 | 0:b2139d0d6c90 | 120 | Oled.printf("Pasos del PD: %u", d); |
dany54543 | 0:b2139d0d6c90 | 121 | Oled.display(); |
dany54543 | 0:b2139d0d6c90 | 122 | } |
dany54543 | 0:b2139d0d6c90 | 123 | |
dany54543 | 0:b2139d0d6c90 | 124 | int porcentaje(void){ |
dany54543 | 0:b2139d0d6c90 | 125 | int p, v=0; |
dany54543 | 0:b2139d0d6c90 | 126 | v = pot*3.3; |
dany54543 | 0:b2139d0d6c90 | 127 | p = (v*100)/3.3; |
dany54543 | 0:b2139d0d6c90 | 128 | return p; |
dany54543 | 0:b2139d0d6c90 | 129 | } |