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