SMART CLEO Uart Ultrasonic

Committer:
SMART_CLEO
Date:
Thu Sep 28 03:42:40 2017 +0000
Revision:
0:5940ac32cd9f
SMART_CLEO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SMART_CLEO 0:5940ac32cd9f 1 #include "mbed.h"
SMART_CLEO 0:5940ac32cd9f 2 #include "HCSR04.h"
SMART_CLEO 0:5940ac32cd9f 3
SMART_CLEO 0:5940ac32cd9f 4 struct UART_buf
SMART_CLEO 0:5940ac32cd9f 5 {
SMART_CLEO 0:5940ac32cd9f 6 uint8_t STA;
SMART_CLEO 0:5940ac32cd9f 7 uint8_t MODE;
SMART_CLEO 0:5940ac32cd9f 8 uint8_t CMD;
SMART_CLEO 0:5940ac32cd9f 9 uint8_t LEN;
SMART_CLEO 0:5940ac32cd9f 10 uint8_t DATA[32];
SMART_CLEO 0:5940ac32cd9f 11 uint8_t END;
SMART_CLEO 0:5940ac32cd9f 12
SMART_CLEO 0:5940ac32cd9f 13 };
SMART_CLEO 0:5940ac32cd9f 14
SMART_CLEO 0:5940ac32cd9f 15 PinName pin_ECHO = PB_7;
SMART_CLEO 0:5940ac32cd9f 16 PinName pin_TRIG = PB_10;
SMART_CLEO 0:5940ac32cd9f 17
SMART_CLEO 0:5940ac32cd9f 18 HCSR04 Ultrasonic(pin_ECHO, pin_TRIG);
SMART_CLEO 0:5940ac32cd9f 19
SMART_CLEO 0:5940ac32cd9f 20 Serial SerialUART(PA_2, PA_3);
SMART_CLEO 0:5940ac32cd9f 21
SMART_CLEO 0:5940ac32cd9f 22 uint8_t Buffer[37];
SMART_CLEO 0:5940ac32cd9f 23
SMART_CLEO 0:5940ac32cd9f 24 UART_buf RX_BUF;
SMART_CLEO 0:5940ac32cd9f 25
SMART_CLEO 0:5940ac32cd9f 26 Timer Sensor_Timer;
SMART_CLEO 0:5940ac32cd9f 27
SMART_CLEO 0:5940ac32cd9f 28 uint32_t T_period = 1000;
SMART_CLEO 0:5940ac32cd9f 29 uint32_t timer_check = 0;
SMART_CLEO 0:5940ac32cd9f 30
SMART_CLEO 0:5940ac32cd9f 31 void SerialUARTRX_ISR(void);
SMART_CLEO 0:5940ac32cd9f 32 void Timer_setting(uint8_t cmd, uint8_t value);
SMART_CLEO 0:5940ac32cd9f 33 void Sensor_Read(void);
SMART_CLEO 0:5940ac32cd9f 34 float Ultrasonic_cm(void);
SMART_CLEO 0:5940ac32cd9f 35
SMART_CLEO 0:5940ac32cd9f 36 int main() {
SMART_CLEO 0:5940ac32cd9f 37
SMART_CLEO 0:5940ac32cd9f 38 SerialUART.baud(115200);
SMART_CLEO 0:5940ac32cd9f 39
SMART_CLEO 0:5940ac32cd9f 40 SerialUART.attach(&SerialUARTRX_ISR);
SMART_CLEO 0:5940ac32cd9f 41
SMART_CLEO 0:5940ac32cd9f 42 Timer_setting(0x06, 1);
SMART_CLEO 0:5940ac32cd9f 43
SMART_CLEO 0:5940ac32cd9f 44 while(1)
SMART_CLEO 0:5940ac32cd9f 45 {
SMART_CLEO 0:5940ac32cd9f 46 //SerialUART.printf("%d", Sensor_Timer.read_ms());
SMART_CLEO 0:5940ac32cd9f 47 if((timer_check + T_period) < Sensor_Timer.read_ms())
SMART_CLEO 0:5940ac32cd9f 48 {
SMART_CLEO 0:5940ac32cd9f 49 // SerialUART.printf("%d",T_period);
SMART_CLEO 0:5940ac32cd9f 50 Sensor_Read();
SMART_CLEO 0:5940ac32cd9f 51 timer_check += T_period;
SMART_CLEO 0:5940ac32cd9f 52 }
SMART_CLEO 0:5940ac32cd9f 53 //wait(0.5);
SMART_CLEO 0:5940ac32cd9f 54 }
SMART_CLEO 0:5940ac32cd9f 55 }
SMART_CLEO 0:5940ac32cd9f 56
SMART_CLEO 0:5940ac32cd9f 57 void SerialUARTRX_ISR(void)
SMART_CLEO 0:5940ac32cd9f 58 {
SMART_CLEO 0:5940ac32cd9f 59 static uint8_t RX_count = 0, RX_Len = 32, RX_Status = 0;
SMART_CLEO 0:5940ac32cd9f 60 uint8_t rx_da = SerialUART.getc();
SMART_CLEO 0:5940ac32cd9f 61 switch(RX_Status)
SMART_CLEO 0:5940ac32cd9f 62 {
SMART_CLEO 0:5940ac32cd9f 63 case 0:
SMART_CLEO 0:5940ac32cd9f 64 if(rx_da == 0x76)
SMART_CLEO 0:5940ac32cd9f 65 {
SMART_CLEO 0:5940ac32cd9f 66 RX_BUF.STA = rx_da;
SMART_CLEO 0:5940ac32cd9f 67 RX_Status++;
SMART_CLEO 0:5940ac32cd9f 68 }
SMART_CLEO 0:5940ac32cd9f 69 break;
SMART_CLEO 0:5940ac32cd9f 70 case 1:
SMART_CLEO 0:5940ac32cd9f 71 RX_BUF.MODE = rx_da;
SMART_CLEO 0:5940ac32cd9f 72 RX_Status++;
SMART_CLEO 0:5940ac32cd9f 73 break;
SMART_CLEO 0:5940ac32cd9f 74 case 2:
SMART_CLEO 0:5940ac32cd9f 75 RX_BUF.CMD = rx_da;
SMART_CLEO 0:5940ac32cd9f 76 RX_Status++;
SMART_CLEO 0:5940ac32cd9f 77 break;
SMART_CLEO 0:5940ac32cd9f 78 case 3:
SMART_CLEO 0:5940ac32cd9f 79 RX_BUF.LEN = rx_da;
SMART_CLEO 0:5940ac32cd9f 80 RX_Len = RX_BUF.LEN;
SMART_CLEO 0:5940ac32cd9f 81 RX_Status++;
SMART_CLEO 0:5940ac32cd9f 82 if(RX_Len == 0)
SMART_CLEO 0:5940ac32cd9f 83 RX_Status++;
SMART_CLEO 0:5940ac32cd9f 84 break;
SMART_CLEO 0:5940ac32cd9f 85 case 4:
SMART_CLEO 0:5940ac32cd9f 86 RX_BUF.DATA[RX_count] = rx_da;
SMART_CLEO 0:5940ac32cd9f 87 RX_count++;
SMART_CLEO 0:5940ac32cd9f 88 if(RX_count == RX_Len)
SMART_CLEO 0:5940ac32cd9f 89 {
SMART_CLEO 0:5940ac32cd9f 90 RX_Status++;
SMART_CLEO 0:5940ac32cd9f 91 RX_count = 0;
SMART_CLEO 0:5940ac32cd9f 92 RX_Len = 32;
SMART_CLEO 0:5940ac32cd9f 93 }
SMART_CLEO 0:5940ac32cd9f 94 break;
SMART_CLEO 0:5940ac32cd9f 95 case 5:
SMART_CLEO 0:5940ac32cd9f 96 if(rx_da == 0x3E)
SMART_CLEO 0:5940ac32cd9f 97 {
SMART_CLEO 0:5940ac32cd9f 98 RX_BUF.END = rx_da;
SMART_CLEO 0:5940ac32cd9f 99 RX_Status = 0;
SMART_CLEO 0:5940ac32cd9f 100 switch(RX_BUF.MODE)
SMART_CLEO 0:5940ac32cd9f 101 {
SMART_CLEO 0:5940ac32cd9f 102 case 0x04:
SMART_CLEO 0:5940ac32cd9f 103 Timer_setting(RX_BUF.CMD, RX_BUF.DATA[0]);
SMART_CLEO 0:5940ac32cd9f 104 break;
SMART_CLEO 0:5940ac32cd9f 105 }
SMART_CLEO 0:5940ac32cd9f 106 }
SMART_CLEO 0:5940ac32cd9f 107 break;
SMART_CLEO 0:5940ac32cd9f 108 }
SMART_CLEO 0:5940ac32cd9f 109 }
SMART_CLEO 0:5940ac32cd9f 110
SMART_CLEO 0:5940ac32cd9f 111 void Timer_setting(uint8_t cmd, uint8_t value)
SMART_CLEO 0:5940ac32cd9f 112 {
SMART_CLEO 0:5940ac32cd9f 113 double Time_value = 0;
SMART_CLEO 0:5940ac32cd9f 114 Sensor_Timer.stop();
SMART_CLEO 0:5940ac32cd9f 115 Sensor_Timer.reset();
SMART_CLEO 0:5940ac32cd9f 116 switch(cmd)
SMART_CLEO 0:5940ac32cd9f 117 {
SMART_CLEO 0:5940ac32cd9f 118 case 0x01:
SMART_CLEO 0:5940ac32cd9f 119 Time_value = 30;
SMART_CLEO 0:5940ac32cd9f 120 break;
SMART_CLEO 0:5940ac32cd9f 121 case 0x02:
SMART_CLEO 0:5940ac32cd9f 122 Time_value = 60;
SMART_CLEO 0:5940ac32cd9f 123 break;
SMART_CLEO 0:5940ac32cd9f 124 case 0x03:
SMART_CLEO 0:5940ac32cd9f 125 Time_value = 120;
SMART_CLEO 0:5940ac32cd9f 126 break;
SMART_CLEO 0:5940ac32cd9f 127 case 0x04:
SMART_CLEO 0:5940ac32cd9f 128 Time_value = 300;
SMART_CLEO 0:5940ac32cd9f 129 break;
SMART_CLEO 0:5940ac32cd9f 130 case 0x05:
SMART_CLEO 0:5940ac32cd9f 131 Time_value = 600;
SMART_CLEO 0:5940ac32cd9f 132 break;
SMART_CLEO 0:5940ac32cd9f 133 case 0x06:
SMART_CLEO 0:5940ac32cd9f 134 Time_value = value;
SMART_CLEO 0:5940ac32cd9f 135 Time_value = 1.0/Time_value;
SMART_CLEO 0:5940ac32cd9f 136 break;
SMART_CLEO 0:5940ac32cd9f 137 }
SMART_CLEO 0:5940ac32cd9f 138 Sensor_Timer.start();
SMART_CLEO 0:5940ac32cd9f 139 timer_check = Sensor_Timer.read_ms();
SMART_CLEO 0:5940ac32cd9f 140 T_period = Time_value * 1000;
SMART_CLEO 0:5940ac32cd9f 141 }
SMART_CLEO 0:5940ac32cd9f 142
SMART_CLEO 0:5940ac32cd9f 143 void Sensor_Read(void)
SMART_CLEO 0:5940ac32cd9f 144 {
SMART_CLEO 0:5940ac32cd9f 145 uint16_t dist = Ultrasonic_cm();
SMART_CLEO 0:5940ac32cd9f 146 Buffer[0] = 0x76;
SMART_CLEO 0:5940ac32cd9f 147 Buffer[1] = 0x01;
SMART_CLEO 0:5940ac32cd9f 148 Buffer[2] = 0x07;
SMART_CLEO 0:5940ac32cd9f 149 Buffer[3] = 0x02;
SMART_CLEO 0:5940ac32cd9f 150 Buffer[4] = dist >> 8;
SMART_CLEO 0:5940ac32cd9f 151 Buffer[5] = dist & 0xFF;
SMART_CLEO 0:5940ac32cd9f 152 Buffer[6] = 0x3E;
SMART_CLEO 0:5940ac32cd9f 153 for(int i=0; i<7; i++)
SMART_CLEO 0:5940ac32cd9f 154 SerialUART.putc(Buffer[i]);
SMART_CLEO 0:5940ac32cd9f 155 }
SMART_CLEO 0:5940ac32cd9f 156
SMART_CLEO 0:5940ac32cd9f 157 float Ultrasonic_cm(void)
SMART_CLEO 0:5940ac32cd9f 158 {
SMART_CLEO 0:5940ac32cd9f 159 Ultrasonic.startMeasurement();
SMART_CLEO 0:5940ac32cd9f 160
SMART_CLEO 0:5940ac32cd9f 161 while(!Ultrasonic.isNewDataReady());
SMART_CLEO 0:5940ac32cd9f 162
SMART_CLEO 0:5940ac32cd9f 163 return Ultrasonic.getDistance_cm();
SMART_CLEO 0:5940ac32cd9f 164 }