han back
/
CLEO_UART_SOUND
SMART CLEO Uart Sound
main.cpp@0:eafa8874b6b3, 2017-09-28 (annotated)
- Committer:
- SMART_CLEO
- Date:
- Thu Sep 28 03:39:58 2017 +0000
- Revision:
- 0:eafa8874b6b3
SMART_CLEO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SMART_CLEO | 0:eafa8874b6b3 | 1 | #include "mbed.h" |
SMART_CLEO | 0:eafa8874b6b3 | 2 | |
SMART_CLEO | 0:eafa8874b6b3 | 3 | struct UART_buf |
SMART_CLEO | 0:eafa8874b6b3 | 4 | { |
SMART_CLEO | 0:eafa8874b6b3 | 5 | uint8_t STA; |
SMART_CLEO | 0:eafa8874b6b3 | 6 | uint8_t MODE; |
SMART_CLEO | 0:eafa8874b6b3 | 7 | uint8_t CMD; |
SMART_CLEO | 0:eafa8874b6b3 | 8 | uint8_t LEN; |
SMART_CLEO | 0:eafa8874b6b3 | 9 | uint8_t DATA[32]; |
SMART_CLEO | 0:eafa8874b6b3 | 10 | uint8_t END; |
SMART_CLEO | 0:eafa8874b6b3 | 11 | |
SMART_CLEO | 0:eafa8874b6b3 | 12 | }; |
SMART_CLEO | 0:eafa8874b6b3 | 13 | |
SMART_CLEO | 0:eafa8874b6b3 | 14 | PinName pin_SOUND = PC_3; |
SMART_CLEO | 0:eafa8874b6b3 | 15 | |
SMART_CLEO | 0:eafa8874b6b3 | 16 | AnalogIn adc_SOUND(pin_SOUND); |
SMART_CLEO | 0:eafa8874b6b3 | 17 | |
SMART_CLEO | 0:eafa8874b6b3 | 18 | Serial SerialUART(PA_2, PA_3); |
SMART_CLEO | 0:eafa8874b6b3 | 19 | |
SMART_CLEO | 0:eafa8874b6b3 | 20 | uint8_t Buffer[37]; |
SMART_CLEO | 0:eafa8874b6b3 | 21 | |
SMART_CLEO | 0:eafa8874b6b3 | 22 | //UART_buf RX_BUF; |
SMART_CLEO | 0:eafa8874b6b3 | 23 | |
SMART_CLEO | 0:eafa8874b6b3 | 24 | //Ticker Sensor_Timer; |
SMART_CLEO | 0:eafa8874b6b3 | 25 | |
SMART_CLEO | 0:eafa8874b6b3 | 26 | //void SerialUARTRX_ISR(void); |
SMART_CLEO | 0:eafa8874b6b3 | 27 | //void Timer_setting(uint8_t cmd, uint8_t value); |
SMART_CLEO | 0:eafa8874b6b3 | 28 | //void Sensor_Read(void); |
SMART_CLEO | 0:eafa8874b6b3 | 29 | void Sensor_Read_Data(uint8_t data); |
SMART_CLEO | 0:eafa8874b6b3 | 30 | |
SMART_CLEO | 0:eafa8874b6b3 | 31 | int main() { |
SMART_CLEO | 0:eafa8874b6b3 | 32 | |
SMART_CLEO | 0:eafa8874b6b3 | 33 | uint8_t Sound_flag = 0; |
SMART_CLEO | 0:eafa8874b6b3 | 34 | int volt; |
SMART_CLEO | 0:eafa8874b6b3 | 35 | |
SMART_CLEO | 0:eafa8874b6b3 | 36 | SerialUART.baud(115200); |
SMART_CLEO | 0:eafa8874b6b3 | 37 | |
SMART_CLEO | 0:eafa8874b6b3 | 38 | //SerialUART.attach(&SerialUARTRX_ISR); |
SMART_CLEO | 0:eafa8874b6b3 | 39 | |
SMART_CLEO | 0:eafa8874b6b3 | 40 | //Timer_setting(0x06, 2); |
SMART_CLEO | 0:eafa8874b6b3 | 41 | |
SMART_CLEO | 0:eafa8874b6b3 | 42 | while(1) |
SMART_CLEO | 0:eafa8874b6b3 | 43 | { |
SMART_CLEO | 0:eafa8874b6b3 | 44 | volt = adc_SOUND.read()*3300; |
SMART_CLEO | 0:eafa8874b6b3 | 45 | if(Sound_flag && (volt <= 1000)) |
SMART_CLEO | 0:eafa8874b6b3 | 46 | { |
SMART_CLEO | 0:eafa8874b6b3 | 47 | Sensor_Read_Data(0); |
SMART_CLEO | 0:eafa8874b6b3 | 48 | Sound_flag = 0; |
SMART_CLEO | 0:eafa8874b6b3 | 49 | wait(1); |
SMART_CLEO | 0:eafa8874b6b3 | 50 | } |
SMART_CLEO | 0:eafa8874b6b3 | 51 | else if((Sound_flag == 0) && (volt > 1000)) |
SMART_CLEO | 0:eafa8874b6b3 | 52 | { |
SMART_CLEO | 0:eafa8874b6b3 | 53 | Sensor_Read_Data(1); |
SMART_CLEO | 0:eafa8874b6b3 | 54 | Sound_flag = 1; |
SMART_CLEO | 0:eafa8874b6b3 | 55 | wait(1); |
SMART_CLEO | 0:eafa8874b6b3 | 56 | } |
SMART_CLEO | 0:eafa8874b6b3 | 57 | } |
SMART_CLEO | 0:eafa8874b6b3 | 58 | } |
SMART_CLEO | 0:eafa8874b6b3 | 59 | |
SMART_CLEO | 0:eafa8874b6b3 | 60 | /*void SerialUARTRX_ISR(void) |
SMART_CLEO | 0:eafa8874b6b3 | 61 | { |
SMART_CLEO | 0:eafa8874b6b3 | 62 | static uint8_t RX_count = 0, RX_Len = 32, RX_Status = 0; |
SMART_CLEO | 0:eafa8874b6b3 | 63 | uint8_t rx_da = SerialUART.getc(); |
SMART_CLEO | 0:eafa8874b6b3 | 64 | switch(RX_Status) |
SMART_CLEO | 0:eafa8874b6b3 | 65 | { |
SMART_CLEO | 0:eafa8874b6b3 | 66 | case 0: |
SMART_CLEO | 0:eafa8874b6b3 | 67 | if(rx_da == 0x76) |
SMART_CLEO | 0:eafa8874b6b3 | 68 | { |
SMART_CLEO | 0:eafa8874b6b3 | 69 | RX_BUF.STA = rx_da; |
SMART_CLEO | 0:eafa8874b6b3 | 70 | RX_Status++; |
SMART_CLEO | 0:eafa8874b6b3 | 71 | } |
SMART_CLEO | 0:eafa8874b6b3 | 72 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 73 | case 1: |
SMART_CLEO | 0:eafa8874b6b3 | 74 | RX_BUF.MODE = rx_da; |
SMART_CLEO | 0:eafa8874b6b3 | 75 | RX_Status++; |
SMART_CLEO | 0:eafa8874b6b3 | 76 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 77 | case 2: |
SMART_CLEO | 0:eafa8874b6b3 | 78 | RX_BUF.CMD = rx_da; |
SMART_CLEO | 0:eafa8874b6b3 | 79 | RX_Status++; |
SMART_CLEO | 0:eafa8874b6b3 | 80 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 81 | case 3: |
SMART_CLEO | 0:eafa8874b6b3 | 82 | RX_BUF.LEN = rx_da; |
SMART_CLEO | 0:eafa8874b6b3 | 83 | RX_Len = RX_BUF.LEN; |
SMART_CLEO | 0:eafa8874b6b3 | 84 | RX_Status++; |
SMART_CLEO | 0:eafa8874b6b3 | 85 | if(RX_Len == 0) |
SMART_CLEO | 0:eafa8874b6b3 | 86 | RX_Status++; |
SMART_CLEO | 0:eafa8874b6b3 | 87 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 88 | case 4: |
SMART_CLEO | 0:eafa8874b6b3 | 89 | RX_BUF.DATA[RX_count] = rx_da; |
SMART_CLEO | 0:eafa8874b6b3 | 90 | RX_count++; |
SMART_CLEO | 0:eafa8874b6b3 | 91 | if(RX_count == RX_Len) |
SMART_CLEO | 0:eafa8874b6b3 | 92 | { |
SMART_CLEO | 0:eafa8874b6b3 | 93 | RX_Status++; |
SMART_CLEO | 0:eafa8874b6b3 | 94 | RX_count = 0; |
SMART_CLEO | 0:eafa8874b6b3 | 95 | RX_Len = 32; |
SMART_CLEO | 0:eafa8874b6b3 | 96 | } |
SMART_CLEO | 0:eafa8874b6b3 | 97 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 98 | case 5: |
SMART_CLEO | 0:eafa8874b6b3 | 99 | if(rx_da == 0x3E) |
SMART_CLEO | 0:eafa8874b6b3 | 100 | { |
SMART_CLEO | 0:eafa8874b6b3 | 101 | RX_BUF.END = rx_da; |
SMART_CLEO | 0:eafa8874b6b3 | 102 | RX_Status = 0; |
SMART_CLEO | 0:eafa8874b6b3 | 103 | switch(RX_BUF.MODE) |
SMART_CLEO | 0:eafa8874b6b3 | 104 | { |
SMART_CLEO | 0:eafa8874b6b3 | 105 | case 0x04: |
SMART_CLEO | 0:eafa8874b6b3 | 106 | Timer_setting(RX_BUF.CMD, RX_BUF.DATA[0]); |
SMART_CLEO | 0:eafa8874b6b3 | 107 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 108 | } |
SMART_CLEO | 0:eafa8874b6b3 | 109 | } |
SMART_CLEO | 0:eafa8874b6b3 | 110 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 111 | } |
SMART_CLEO | 0:eafa8874b6b3 | 112 | }*/ |
SMART_CLEO | 0:eafa8874b6b3 | 113 | |
SMART_CLEO | 0:eafa8874b6b3 | 114 | /*void Timer_setting(uint8_t cmd, uint8_t value) |
SMART_CLEO | 0:eafa8874b6b3 | 115 | { |
SMART_CLEO | 0:eafa8874b6b3 | 116 | double Time_value = 0; |
SMART_CLEO | 0:eafa8874b6b3 | 117 | switch(cmd) |
SMART_CLEO | 0:eafa8874b6b3 | 118 | { |
SMART_CLEO | 0:eafa8874b6b3 | 119 | case 0x01: |
SMART_CLEO | 0:eafa8874b6b3 | 120 | Time_value = 30; |
SMART_CLEO | 0:eafa8874b6b3 | 121 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 122 | case 0x02: |
SMART_CLEO | 0:eafa8874b6b3 | 123 | Time_value = 60; |
SMART_CLEO | 0:eafa8874b6b3 | 124 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 125 | case 0x03: |
SMART_CLEO | 0:eafa8874b6b3 | 126 | Time_value = 120; |
SMART_CLEO | 0:eafa8874b6b3 | 127 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 128 | case 0x04: |
SMART_CLEO | 0:eafa8874b6b3 | 129 | Time_value = 300; |
SMART_CLEO | 0:eafa8874b6b3 | 130 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 131 | case 0x05: |
SMART_CLEO | 0:eafa8874b6b3 | 132 | Time_value = 600; |
SMART_CLEO | 0:eafa8874b6b3 | 133 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 134 | case 0x06: |
SMART_CLEO | 0:eafa8874b6b3 | 135 | Time_value = value; |
SMART_CLEO | 0:eafa8874b6b3 | 136 | Time_value = 1.0/Time_value; |
SMART_CLEO | 0:eafa8874b6b3 | 137 | break; |
SMART_CLEO | 0:eafa8874b6b3 | 138 | } |
SMART_CLEO | 0:eafa8874b6b3 | 139 | Sensor_Timer.attach(&Sensor_Read, Time_value); |
SMART_CLEO | 0:eafa8874b6b3 | 140 | }*/ |
SMART_CLEO | 0:eafa8874b6b3 | 141 | |
SMART_CLEO | 0:eafa8874b6b3 | 142 | void Sensor_Read_Data(uint8_t data) |
SMART_CLEO | 0:eafa8874b6b3 | 143 | { |
SMART_CLEO | 0:eafa8874b6b3 | 144 | Buffer[0] = 0x76; |
SMART_CLEO | 0:eafa8874b6b3 | 145 | Buffer[1] = 0x01; |
SMART_CLEO | 0:eafa8874b6b3 | 146 | Buffer[2] = 0x06; |
SMART_CLEO | 0:eafa8874b6b3 | 147 | Buffer[3] = 0x01; |
SMART_CLEO | 0:eafa8874b6b3 | 148 | Buffer[4] = data; |
SMART_CLEO | 0:eafa8874b6b3 | 149 | Buffer[5] = 0x3E; |
SMART_CLEO | 0:eafa8874b6b3 | 150 | for(int i=0; i<6; i++) |
SMART_CLEO | 0:eafa8874b6b3 | 151 | SerialUART.putc(Buffer[i]); |
SMART_CLEO | 0:eafa8874b6b3 | 152 | } |
SMART_CLEO | 0:eafa8874b6b3 | 153 | /* |
SMART_CLEO | 0:eafa8874b6b3 | 154 | void Sensor_Read(void) |
SMART_CLEO | 0:eafa8874b6b3 | 155 | { |
SMART_CLEO | 0:eafa8874b6b3 | 156 | Buffer[0] = 0x76; |
SMART_CLEO | 0:eafa8874b6b3 | 157 | Buffer[1] = 0x01; |
SMART_CLEO | 0:eafa8874b6b3 | 158 | Buffer[2] = 0x05; |
SMART_CLEO | 0:eafa8874b6b3 | 159 | Buffer[3] = 0x01; |
SMART_CLEO | 0:eafa8874b6b3 | 160 | Buffer[4] = IR_Detect; |
SMART_CLEO | 0:eafa8874b6b3 | 161 | Buffer[5] = 0x3E; |
SMART_CLEO | 0:eafa8874b6b3 | 162 | for(int i=0; i<6; i++) |
SMART_CLEO | 0:eafa8874b6b3 | 163 | SerialUART.putc(Buffer[i]); |
SMART_CLEO | 0:eafa8874b6b3 | 164 | } |
SMART_CLEO | 0:eafa8874b6b3 | 165 | */ |