![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ecoder motor semaforo
Dependencies: mbed ssd1306_library SLCD
main.cpp@0:fb45c2082cb8, 2020-06-24 (annotated)
- Committer:
- 20172573073
- Date:
- Wed Jun 24 23:39:49 2020 +0000
- Revision:
- 0:fb45c2082cb8
motor_RTOS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
20172573073 | 0:fb45c2082cb8 | 1 | #include "mbed.h" |
20172573073 | 0:fb45c2082cb8 | 2 | #include "rtos.h" |
20172573073 | 0:fb45c2082cb8 | 3 | #include "ssd1306.h" |
20172573073 | 0:fb45c2082cb8 | 4 | #include "SLCD.h" |
20172573073 | 0:fb45c2082cb8 | 5 | |
20172573073 | 0:fb45c2082cb8 | 6 | SLCD slcd; |
20172573073 | 0:fb45c2082cb8 | 7 | SSD1306 lcd (D7,D6); |
20172573073 | 0:fb45c2082cb8 | 8 | InterruptIn botoncito(PTC12); |
20172573073 | 0:fb45c2082cb8 | 9 | InterruptIn botoncito2(PTC3); |
20172573073 | 0:fb45c2082cb8 | 10 | InterruptIn botoncito3(D2); |
20172573073 | 0:fb45c2082cb8 | 11 | PwmOut pwm1(D5); |
20172573073 | 0:fb45c2082cb8 | 12 | PwmOut pwm2(D8); |
20172573073 | 0:fb45c2082cb8 | 13 | DigitalOut mo(D9); |
20172573073 | 0:fb45c2082cb8 | 14 | int contador=0,a=1,c=0,r,h,q; |
20172573073 | 0:fb45c2082cb8 | 15 | bool s=0,k=0; |
20172573073 | 0:fb45c2082cb8 | 16 | Serial pc(USBTX,USBRX); |
20172573073 | 0:fb45c2082cb8 | 17 | Semaphore one_slot(1); |
20172573073 | 0:fb45c2082cb8 | 18 | Mutex mutex1; |
20172573073 | 0:fb45c2082cb8 | 19 | |
20172573073 | 0:fb45c2082cb8 | 20 | |
20172573073 | 0:fb45c2082cb8 | 21 | Thread thread; |
20172573073 | 0:fb45c2082cb8 | 22 | Thread thread1; |
20172573073 | 0:fb45c2082cb8 | 23 | Thread thread2; |
20172573073 | 0:fb45c2082cb8 | 24 | Thread thread3; |
20172573073 | 0:fb45c2082cb8 | 25 | Thread thread4; |
20172573073 | 0:fb45c2082cb8 | 26 | |
20172573073 | 0:fb45c2082cb8 | 27 | |
20172573073 | 0:fb45c2082cb8 | 28 | void herra() |
20172573073 | 0:fb45c2082cb8 | 29 | { |
20172573073 | 0:fb45c2082cb8 | 30 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 31 | one_slot.wait(); |
20172573073 | 0:fb45c2082cb8 | 32 | r=r+1; |
20172573073 | 0:fb45c2082cb8 | 33 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 34 | } |
20172573073 | 0:fb45c2082cb8 | 35 | |
20172573073 | 0:fb45c2082cb8 | 36 | void boton1() |
20172573073 | 0:fb45c2082cb8 | 37 | { |
20172573073 | 0:fb45c2082cb8 | 38 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 39 | one_slot.wait(); |
20172573073 | 0:fb45c2082cb8 | 40 | //one_slot.try_acquire(); |
20172573073 | 0:fb45c2082cb8 | 41 | s=!s; |
20172573073 | 0:fb45c2082cb8 | 42 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 43 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 44 | } |
20172573073 | 0:fb45c2082cb8 | 45 | void boton2() |
20172573073 | 0:fb45c2082cb8 | 46 | { |
20172573073 | 0:fb45c2082cb8 | 47 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 48 | one_slot.wait(); |
20172573073 | 0:fb45c2082cb8 | 49 | NVIC_SystemReset (); |
20172573073 | 0:fb45c2082cb8 | 50 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 51 | } |
20172573073 | 0:fb45c2082cb8 | 52 | |
20172573073 | 0:fb45c2082cb8 | 53 | |
20172573073 | 0:fb45c2082cb8 | 54 | |
20172573073 | 0:fb45c2082cb8 | 55 | void motor1() |
20172573073 | 0:fb45c2082cb8 | 56 | { |
20172573073 | 0:fb45c2082cb8 | 57 | while(1) { |
20172573073 | 0:fb45c2082cb8 | 58 | if(s==1) { |
20172573073 | 0:fb45c2082cb8 | 59 | c++; |
20172573073 | 0:fb45c2082cb8 | 60 | switch(c) { |
20172573073 | 0:fb45c2082cb8 | 61 | case 1: |
20172573073 | 0:fb45c2082cb8 | 62 | pwm2=0.3; |
20172573073 | 0:fb45c2082cb8 | 63 | q=30; |
20172573073 | 0:fb45c2082cb8 | 64 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 65 | Thread::wait(2000); |
20172573073 | 0:fb45c2082cb8 | 66 | break; |
20172573073 | 0:fb45c2082cb8 | 67 | case 2: |
20172573073 | 0:fb45c2082cb8 | 68 | pwm2=0.5; |
20172573073 | 0:fb45c2082cb8 | 69 | q=50; |
20172573073 | 0:fb45c2082cb8 | 70 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 71 | Thread::wait(2000); |
20172573073 | 0:fb45c2082cb8 | 72 | break; |
20172573073 | 0:fb45c2082cb8 | 73 | case 3: |
20172573073 | 0:fb45c2082cb8 | 74 | pwm2=0.7; |
20172573073 | 0:fb45c2082cb8 | 75 | q=70; |
20172573073 | 0:fb45c2082cb8 | 76 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 77 | Thread::wait(2000); |
20172573073 | 0:fb45c2082cb8 | 78 | break; |
20172573073 | 0:fb45c2082cb8 | 79 | case 4: |
20172573073 | 0:fb45c2082cb8 | 80 | pwm2=1; |
20172573073 | 0:fb45c2082cb8 | 81 | q=100; |
20172573073 | 0:fb45c2082cb8 | 82 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 83 | Thread::wait(6000); |
20172573073 | 0:fb45c2082cb8 | 84 | break; |
20172573073 | 0:fb45c2082cb8 | 85 | case 5: |
20172573073 | 0:fb45c2082cb8 | 86 | pwm2=0.7; |
20172573073 | 0:fb45c2082cb8 | 87 | q=70; |
20172573073 | 0:fb45c2082cb8 | 88 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 89 | Thread::wait(2000); |
20172573073 | 0:fb45c2082cb8 | 90 | break; |
20172573073 | 0:fb45c2082cb8 | 91 | case 6: |
20172573073 | 0:fb45c2082cb8 | 92 | pwm2=0.5; |
20172573073 | 0:fb45c2082cb8 | 93 | q=50; |
20172573073 | 0:fb45c2082cb8 | 94 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 95 | Thread::wait(2000); |
20172573073 | 0:fb45c2082cb8 | 96 | break; |
20172573073 | 0:fb45c2082cb8 | 97 | case 7: |
20172573073 | 0:fb45c2082cb8 | 98 | q=30; |
20172573073 | 0:fb45c2082cb8 | 99 | pwm2=0.3; |
20172573073 | 0:fb45c2082cb8 | 100 | mo=s; |
20172573073 | 0:fb45c2082cb8 | 101 | Thread::wait(2000); |
20172573073 | 0:fb45c2082cb8 | 102 | c=0; |
20172573073 | 0:fb45c2082cb8 | 103 | break; |
20172573073 | 0:fb45c2082cb8 | 104 | |
20172573073 | 0:fb45c2082cb8 | 105 | } |
20172573073 | 0:fb45c2082cb8 | 106 | } |
20172573073 | 0:fb45c2082cb8 | 107 | } |
20172573073 | 0:fb45c2082cb8 | 108 | } |
20172573073 | 0:fb45c2082cb8 | 109 | |
20172573073 | 0:fb45c2082cb8 | 110 | void oled() |
20172573073 | 0:fb45c2082cb8 | 111 | { |
20172573073 | 0:fb45c2082cb8 | 112 | while(1) { |
20172573073 | 0:fb45c2082cb8 | 113 | |
20172573073 | 0:fb45c2082cb8 | 114 | |
20172573073 | 0:fb45c2082cb8 | 115 | if(k==0){ |
20172573073 | 0:fb45c2082cb8 | 116 | lcd.init(); |
20172573073 | 0:fb45c2082cb8 | 117 | lcd.speed (SSD1306::Fast); |
20172573073 | 0:fb45c2082cb8 | 118 | lcd.cls(); |
20172573073 | 0:fb45c2082cb8 | 119 | lcd.locate (1,0); |
20172573073 | 0:fb45c2082cb8 | 120 | time_t seconds = time(NULL); |
20172573073 | 0:fb45c2082cb8 | 121 | lcd.printf("Fecha= %s", ctime(&seconds)); |
20172573073 | 0:fb45c2082cb8 | 122 | lcd.locate (4,1); |
20172573073 | 0:fb45c2082cb8 | 123 | lcd.printf ("nivel: %d ",q); |
20172573073 | 0:fb45c2082cb8 | 124 | lcd.locate (5,1); |
20172573073 | 0:fb45c2082cb8 | 125 | lcd.printf ("rpm: %d ",h); |
20172573073 | 0:fb45c2082cb8 | 126 | lcd.redraw(); |
20172573073 | 0:fb45c2082cb8 | 127 | |
20172573073 | 0:fb45c2082cb8 | 128 | }else if(k==1){ |
20172573073 | 0:fb45c2082cb8 | 129 | lcd.init(); |
20172573073 | 0:fb45c2082cb8 | 130 | lcd.speed (SSD1306::Fast); |
20172573073 | 0:fb45c2082cb8 | 131 | lcd.cls(); |
20172573073 | 0:fb45c2082cb8 | 132 | lcd.locate (2,1); |
20172573073 | 0:fb45c2082cb8 | 133 | lcd.printf ("error"); |
20172573073 | 0:fb45c2082cb8 | 134 | lcd.locate (5,0); |
20172573073 | 0:fb45c2082cb8 | 135 | lcd.printf ("por favor revise el sistema"); |
20172573073 | 0:fb45c2082cb8 | 136 | lcd.redraw(); |
20172573073 | 0:fb45c2082cb8 | 137 | } |
20172573073 | 0:fb45c2082cb8 | 138 | |
20172573073 | 0:fb45c2082cb8 | 139 | Thread::wait(300); |
20172573073 | 0:fb45c2082cb8 | 140 | } |
20172573073 | 0:fb45c2082cb8 | 141 | } |
20172573073 | 0:fb45c2082cb8 | 142 | void s7() |
20172573073 | 0:fb45c2082cb8 | 143 | { |
20172573073 | 0:fb45c2082cb8 | 144 | while(1) { |
20172573073 | 0:fb45c2082cb8 | 145 | if(s==1 && r==0){ |
20172573073 | 0:fb45c2082cb8 | 146 | k=1; |
20172573073 | 0:fb45c2082cb8 | 147 | |
20172573073 | 0:fb45c2082cb8 | 148 | slcd.Home(); |
20172573073 | 0:fb45c2082cb8 | 149 | slcd.putc('e'); |
20172573073 | 0:fb45c2082cb8 | 150 | slcd.putc('r'); |
20172573073 | 0:fb45c2082cb8 | 151 | r=0; |
20172573073 | 0:fb45c2082cb8 | 152 | Thread::wait(1000); |
20172573073 | 0:fb45c2082cb8 | 153 | slcd.clear(); |
20172573073 | 0:fb45c2082cb8 | 154 | |
20172573073 | 0:fb45c2082cb8 | 155 | }else{ |
20172573073 | 0:fb45c2082cb8 | 156 | k=0; |
20172573073 | 0:fb45c2082cb8 | 157 | slcd.Home(); |
20172573073 | 0:fb45c2082cb8 | 158 | h=r*60; |
20172573073 | 0:fb45c2082cb8 | 159 | h=h/4; |
20172573073 | 0:fb45c2082cb8 | 160 | slcd.printf("%d",h); |
20172573073 | 0:fb45c2082cb8 | 161 | r=0; |
20172573073 | 0:fb45c2082cb8 | 162 | Thread::wait(1000); |
20172573073 | 0:fb45c2082cb8 | 163 | slcd.clear(); |
20172573073 | 0:fb45c2082cb8 | 164 | |
20172573073 | 0:fb45c2082cb8 | 165 | } |
20172573073 | 0:fb45c2082cb8 | 166 | } |
20172573073 | 0:fb45c2082cb8 | 167 | } |
20172573073 | 0:fb45c2082cb8 | 168 | void serial() |
20172573073 | 0:fb45c2082cb8 | 169 | { |
20172573073 | 0:fb45c2082cb8 | 170 | while(1) { |
20172573073 | 0:fb45c2082cb8 | 171 | |
20172573073 | 0:fb45c2082cb8 | 172 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 173 | one_slot.wait(); |
20172573073 | 0:fb45c2082cb8 | 174 | |
20172573073 | 0:fb45c2082cb8 | 175 | time_t seconds = time(NULL); |
20172573073 | 0:fb45c2082cb8 | 176 | |
20172573073 | 0:fb45c2082cb8 | 177 | char buffer2[32]; |
20172573073 | 0:fb45c2082cb8 | 178 | strftime(buffer2, 32, "%m/%d/%y", localtime(&seconds)); |
20172573073 | 0:fb45c2082cb8 | 179 | pc.printf("%s", buffer2); |
20172573073 | 0:fb45c2082cb8 | 180 | |
20172573073 | 0:fb45c2082cb8 | 181 | char buffer1[32]; |
20172573073 | 0:fb45c2082cb8 | 182 | strftime(buffer1, 32, "%H:%M:%S", localtime(&seconds)); |
20172573073 | 0:fb45c2082cb8 | 183 | pc.printf(" , %s", buffer1); |
20172573073 | 0:fb45c2082cb8 | 184 | if(k==1) { |
20172573073 | 0:fb45c2082cb8 | 185 | pc.printf(" , %d ciclo, %d rpm, error",q,h); |
20172573073 | 0:fb45c2082cb8 | 186 | |
20172573073 | 0:fb45c2082cb8 | 187 | } else { |
20172573073 | 0:fb45c2082cb8 | 188 | pc.printf(" , %d ciclo, %d rpm, ok",q,h); |
20172573073 | 0:fb45c2082cb8 | 189 | } |
20172573073 | 0:fb45c2082cb8 | 190 | pc.printf("\n\r"); |
20172573073 | 0:fb45c2082cb8 | 191 | |
20172573073 | 0:fb45c2082cb8 | 192 | Thread::wait(100); |
20172573073 | 0:fb45c2082cb8 | 193 | one_slot.release(); |
20172573073 | 0:fb45c2082cb8 | 194 | |
20172573073 | 0:fb45c2082cb8 | 195 | } |
20172573073 | 0:fb45c2082cb8 | 196 | } |
20172573073 | 0:fb45c2082cb8 | 197 | int main() |
20172573073 | 0:fb45c2082cb8 | 198 | { |
20172573073 | 0:fb45c2082cb8 | 199 | |
20172573073 | 0:fb45c2082cb8 | 200 | set_time(1593041944); |
20172573073 | 0:fb45c2082cb8 | 201 | botoncito.mode(PullUp); |
20172573073 | 0:fb45c2082cb8 | 202 | botoncito.fall(&boton1); |
20172573073 | 0:fb45c2082cb8 | 203 | botoncito2.mode(PullUp); |
20172573073 | 0:fb45c2082cb8 | 204 | botoncito2.fall(&boton2); |
20172573073 | 0:fb45c2082cb8 | 205 | botoncito3.mode(PullUp); |
20172573073 | 0:fb45c2082cb8 | 206 | botoncito3.fall(&herra); |
20172573073 | 0:fb45c2082cb8 | 207 | |
20172573073 | 0:fb45c2082cb8 | 208 | thread1.start(oled); |
20172573073 | 0:fb45c2082cb8 | 209 | thread2.start(s7); |
20172573073 | 0:fb45c2082cb8 | 210 | thread3.start(serial); |
20172573073 | 0:fb45c2082cb8 | 211 | thread4.start((motor1)); |
20172573073 | 0:fb45c2082cb8 | 212 | thread4.join(); |
20172573073 | 0:fb45c2082cb8 | 213 | } |