![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
RTOS
Dependencies: mbed mbed-rtos hcsr04 TextLCD MMA8451Q
main.cpp@0:a07af2ea22c3, 2020-06-11 (annotated)
- Committer:
- Diego_Mbed
- Date:
- Thu Jun 11 02:07:45 2020 +0000
- Revision:
- 0:a07af2ea22c3
Rtos;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Diego_Mbed | 0:a07af2ea22c3 | 1 | #include "mbed.h" |
Diego_Mbed | 0:a07af2ea22c3 | 2 | #include "rtos.h" |
Diego_Mbed | 0:a07af2ea22c3 | 3 | #include "hcsr04.h" |
Diego_Mbed | 0:a07af2ea22c3 | 4 | #include "TextLCD.h" |
Diego_Mbed | 0:a07af2ea22c3 | 5 | //#include "platform/mbed_thread.h" |
Diego_Mbed | 0:a07af2ea22c3 | 6 | #include "MMA8451Q.h" |
Diego_Mbed | 0:a07af2ea22c3 | 7 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
Diego_Mbed | 0:a07af2ea22c3 | 8 | |
Diego_Mbed | 0:a07af2ea22c3 | 9 | BusOut Dis7seg(PTE20,PTB2,PTB0,PTE21,PTE23,PTB3,PTB1); |
Diego_Mbed | 0:a07af2ea22c3 | 10 | DigitalOut unidad(PTE2); |
Diego_Mbed | 0:a07af2ea22c3 | 11 | DigitalOut decena(PTE3); |
Diego_Mbed | 0:a07af2ea22c3 | 12 | DigitalOut centena(PTE5); |
Diego_Mbed | 0:a07af2ea22c3 | 13 | Serial pc(USBTX, USBRX); // tx, rx |
Diego_Mbed | 0:a07af2ea22c3 | 14 | HCSR04 usensor(PTD0,PTD5); // Trigger, Echo |
Diego_Mbed | 0:a07af2ea22c3 | 15 | PwmOut servo(PTA13); |
Diego_Mbed | 0:a07af2ea22c3 | 16 | DigitalOut led_1(LED1); |
Diego_Mbed | 0:a07af2ea22c3 | 17 | SPI spi(PTD2, NC, PTD1); // puertos (MOSI, MISO, SCLK) |
Diego_Mbed | 0:a07af2ea22c3 | 18 | DigitalOut cs(PTE31); // Chip select |
Diego_Mbed | 0:a07af2ea22c3 | 19 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //(SDA,SCL,Direc) |
Diego_Mbed | 0:a07af2ea22c3 | 20 | // I2C Communication |
Diego_Mbed | 0:a07af2ea22c3 | 21 | I2C i2c_lcd(PTE0,PTE1); // SDA, SCL |
Diego_Mbed | 0:a07af2ea22c3 | 22 | // LCD instantiation |
Diego_Mbed | 0:a07af2ea22c3 | 23 | TextLCD_I2C lcd(&i2c_lcd, 0x4E, TextLCD::LCD16x2, TextLCD::WS0010); |
Diego_Mbed | 0:a07af2ea22c3 | 24 | |
Diego_Mbed | 0:a07af2ea22c3 | 25 | unsigned int dist; |
Diego_Mbed | 0:a07af2ea22c3 | 26 | int distancias[12]; |
Diego_Mbed | 0:a07af2ea22c3 | 27 | int incremento; |
Diego_Mbed | 0:a07af2ea22c3 | 28 | int Decremento; |
Diego_Mbed | 0:a07af2ea22c3 | 29 | int uni =5; |
Diego_Mbed | 0:a07af2ea22c3 | 30 | int dec =2; |
Diego_Mbed | 0:a07af2ea22c3 | 31 | int n=0; |
Diego_Mbed | 0:a07af2ea22c3 | 32 | int p=0; |
Diego_Mbed | 0:a07af2ea22c3 | 33 | |
Diego_Mbed | 0:a07af2ea22c3 | 34 | //------------------------------------Funcion de números-------------------------------------------------------- |
Diego_Mbed | 0:a07af2ea22c3 | 35 | |
Diego_Mbed | 0:a07af2ea22c3 | 36 | void numeros(){ |
Diego_Mbed | 0:a07af2ea22c3 | 37 | switch(n){ |
Diego_Mbed | 0:a07af2ea22c3 | 38 | case 0: Dis7seg=0x01; break; |
Diego_Mbed | 0:a07af2ea22c3 | 39 | case 1: Dis7seg=0x4F; break; |
Diego_Mbed | 0:a07af2ea22c3 | 40 | case 2: Dis7seg=0x12; break; |
Diego_Mbed | 0:a07af2ea22c3 | 41 | case 3: Dis7seg=0x06; break; |
Diego_Mbed | 0:a07af2ea22c3 | 42 | case 4: Dis7seg=0x4C; break; |
Diego_Mbed | 0:a07af2ea22c3 | 43 | case 5: Dis7seg=0x24; break; |
Diego_Mbed | 0:a07af2ea22c3 | 44 | case 6: Dis7seg=0x60; break; |
Diego_Mbed | 0:a07af2ea22c3 | 45 | case 7: Dis7seg=0x0F; break; |
Diego_Mbed | 0:a07af2ea22c3 | 46 | case 8: Dis7seg=0x00; break; |
Diego_Mbed | 0:a07af2ea22c3 | 47 | case 9: Dis7seg=0x0C; break; |
Diego_Mbed | 0:a07af2ea22c3 | 48 | } |
Diego_Mbed | 0:a07af2ea22c3 | 49 | } |
Diego_Mbed | 0:a07af2ea22c3 | 50 | //---------------------------------Sensor ultrasonico con servo------------------------------------------------------------------- |
Diego_Mbed | 0:a07af2ea22c3 | 51 | void lidar(){ |
Diego_Mbed | 0:a07af2ea22c3 | 52 | servo.period_ms(20); |
Diego_Mbed | 0:a07af2ea22c3 | 53 | while(true){ |
Diego_Mbed | 0:a07af2ea22c3 | 54 | for(int i=0;i<=12;i++){ |
Diego_Mbed | 0:a07af2ea22c3 | 55 | incremento= i*159; |
Diego_Mbed | 0:a07af2ea22c3 | 56 | usensor.start(); |
Diego_Mbed | 0:a07af2ea22c3 | 57 | dist=usensor.get_dist_cm(); |
Diego_Mbed | 0:a07af2ea22c3 | 58 | distancias[i]=dist; |
Diego_Mbed | 0:a07af2ea22c3 | 59 | servo.pulsewidth_us(600+incremento); |
Diego_Mbed | 0:a07af2ea22c3 | 60 | ThisThread::sleep_for(120); |
Diego_Mbed | 0:a07af2ea22c3 | 61 | } |
Diego_Mbed | 0:a07af2ea22c3 | 62 | for(int i=0;i<=12;i++){ |
Diego_Mbed | 0:a07af2ea22c3 | 63 | Decremento= i*159; |
Diego_Mbed | 0:a07af2ea22c3 | 64 | usensor.start(); |
Diego_Mbed | 0:a07af2ea22c3 | 65 | usensor.get_dist_cm(); |
Diego_Mbed | 0:a07af2ea22c3 | 66 | distancias[i]=dist; |
Diego_Mbed | 0:a07af2ea22c3 | 67 | servo.pulsewidth_us(2508-Decremento); |
Diego_Mbed | 0:a07af2ea22c3 | 68 | ThisThread::sleep_for(120); |
Diego_Mbed | 0:a07af2ea22c3 | 69 | } |
Diego_Mbed | 0:a07af2ea22c3 | 70 | } |
Diego_Mbed | 0:a07af2ea22c3 | 71 | } |
Diego_Mbed | 0:a07af2ea22c3 | 72 | |
Diego_Mbed | 0:a07af2ea22c3 | 73 | //---------------------------------------Lcd_I2C--------------------------------------------------------- |
Diego_Mbed | 0:a07af2ea22c3 | 74 | |
Diego_Mbed | 0:a07af2ea22c3 | 75 | void Lcd_I2C(){ |
Diego_Mbed | 0:a07af2ea22c3 | 76 | |
Diego_Mbed | 0:a07af2ea22c3 | 77 | lcd.cls(); |
Diego_Mbed | 0:a07af2ea22c3 | 78 | lcd.locate(0,0); |
Diego_Mbed | 0:a07af2ea22c3 | 79 | lcd.setBacklight(TextLCD::LightOn); |
Diego_Mbed | 0:a07af2ea22c3 | 80 | |
Diego_Mbed | 0:a07af2ea22c3 | 81 | while(true) { |
Diego_Mbed | 0:a07af2ea22c3 | 82 | lcd.cls(); |
Diego_Mbed | 0:a07af2ea22c3 | 83 | lcd.locate(0,0); |
Diego_Mbed | 0:a07af2ea22c3 | 84 | lcd.printf("cm:%u hola mundo",dist); |
Diego_Mbed | 0:a07af2ea22c3 | 85 | ThisThread::sleep_for(100); |
Diego_Mbed | 0:a07af2ea22c3 | 86 | } |
Diego_Mbed | 0:a07af2ea22c3 | 87 | } |
Diego_Mbed | 0:a07af2ea22c3 | 88 | |
Diego_Mbed | 0:a07af2ea22c3 | 89 | //--------------------------------Visualizacion Display 7 segmentos--------------------------------------------- |
Diego_Mbed | 0:a07af2ea22c3 | 90 | |
Diego_Mbed | 0:a07af2ea22c3 | 91 | void Seg(){ |
Diego_Mbed | 0:a07af2ea22c3 | 92 | while (true) { |
Diego_Mbed | 0:a07af2ea22c3 | 93 | unidad=0b1; |
Diego_Mbed | 0:a07af2ea22c3 | 94 | decena=0b0; |
Diego_Mbed | 0:a07af2ea22c3 | 95 | n=uni; |
Diego_Mbed | 0:a07af2ea22c3 | 96 | numeros(); |
Diego_Mbed | 0:a07af2ea22c3 | 97 | thread_sleep_for(5); |
Diego_Mbed | 0:a07af2ea22c3 | 98 | unidad=0b0; |
Diego_Mbed | 0:a07af2ea22c3 | 99 | decena=0b1; |
Diego_Mbed | 0:a07af2ea22c3 | 100 | n=dec; |
Diego_Mbed | 0:a07af2ea22c3 | 101 | numeros(); |
Diego_Mbed | 0:a07af2ea22c3 | 102 | thread_sleep_for(5); |
Diego_Mbed | 0:a07af2ea22c3 | 103 | } |
Diego_Mbed | 0:a07af2ea22c3 | 104 | } |
Diego_Mbed | 0:a07af2ea22c3 | 105 | |
Diego_Mbed | 0:a07af2ea22c3 | 106 | //--------------------------Hilos tareas------------------------------------------------------------------- |
Diego_Mbed | 0:a07af2ea22c3 | 107 | |
Diego_Mbed | 0:a07af2ea22c3 | 108 | Thread Thread_lidar (osPriorityNormal1,OS_STACK_SIZE,nullptr, "tarea lidar"); |
Diego_Mbed | 0:a07af2ea22c3 | 109 | Thread Thread_Lcd (osPriorityNormal,OS_STACK_SIZE,nullptr, "tarea Lcd"); |
Diego_Mbed | 0:a07af2ea22c3 | 110 | Thread Thread_Dis7seg (osPriorityNormal,OS_STACK_SIZE,nullptr, "tarea 7seg"); |
Diego_Mbed | 0:a07af2ea22c3 | 111 | |
Diego_Mbed | 0:a07af2ea22c3 | 112 | |
Diego_Mbed | 0:a07af2ea22c3 | 113 | |
Diego_Mbed | 0:a07af2ea22c3 | 114 | int main(){ |
Diego_Mbed | 0:a07af2ea22c3 | 115 | |
Diego_Mbed | 0:a07af2ea22c3 | 116 | //Thread_lidar.start(lidar); |
Diego_Mbed | 0:a07af2ea22c3 | 117 | Thread_Lcd.start(Lcd_I2C); |
Diego_Mbed | 0:a07af2ea22c3 | 118 | Thread_Dis7seg.start(Seg); |
Diego_Mbed | 0:a07af2ea22c3 | 119 | |
Diego_Mbed | 0:a07af2ea22c3 | 120 | |
Diego_Mbed | 0:a07af2ea22c3 | 121 | } |
Diego_Mbed | 0:a07af2ea22c3 | 122 |