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