Sukkpranhachai Gatesichapakorn
/
TAIST_modbus
New group
Fork of TAIST_modbus by
main.cpp@26:e997527a78d7, 2018-03-16 (annotated)
- Committer:
- nengzix
- Date:
- Fri Mar 16 08:23:46 2018 +0000
- Revision:
- 26:e997527a78d7
- Parent:
- 25:4d1450e7cb88
- Child:
- 27:2b99f1ff1d22
done
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsupacha | 0:f306cb0263a6 | 1 | #include "mbed.h" |
vsupacha | 0:f306cb0263a6 | 2 | #include "modbus.h" |
vsupacha | 24:0007712e84a8 | 3 | #include "RawSerial.h" |
vsupacha | 0:f306cb0263a6 | 4 | |
vsupacha | 0:f306cb0263a6 | 5 | DigitalOut myled(LED1); |
vsupacha | 0:f306cb0263a6 | 6 | RawSerial pc(USBTX, USBRX); |
vsupacha | 24:0007712e84a8 | 7 | InterruptIn button(USER_BUTTON); |
vsupacha | 24:0007712e84a8 | 8 | Ticker timebase; |
vsupacha | 24:0007712e84a8 | 9 | |
vsupacha | 24:0007712e84a8 | 10 | uint32_t Tick = 0; |
vsupacha | 24:0007712e84a8 | 11 | uint32_t pressedTick = 0; |
vsupacha | 24:0007712e84a8 | 12 | uint32_t releasedTick = 0; |
vsupacha | 0:f306cb0263a6 | 13 | |
vsupacha | 0:f306cb0263a6 | 14 | void serial_callback() |
vsupacha | 0:f306cb0263a6 | 15 | { |
vsupacha | 0:f306cb0263a6 | 16 | uint8_t frame[6]; |
vsupacha | 24:0007712e84a8 | 17 | uint16_t offset, count; |
vsupacha | 24:0007712e84a8 | 18 | uint8_t reg_map; |
vsupacha | 0:f306cb0263a6 | 19 | |
vsupacha | 0:f306cb0263a6 | 20 | char ch = pc.getc(); |
vsupacha | 0:f306cb0263a6 | 21 | if (modbus_parser(ch, frame)) { |
vsupacha | 24:0007712e84a8 | 22 | offset = (frame[2] << 8) | (frame[3]); |
vsupacha | 24:0007712e84a8 | 23 | count = (frame[4] << 8) | (frame[5]); |
vsupacha | 24:0007712e84a8 | 24 | reg_map = modbus_check(offset, count); |
vsupacha | 24:0007712e84a8 | 25 | modbus_response(reg_map); |
vsupacha | 24:0007712e84a8 | 26 | } |
vsupacha | 24:0007712e84a8 | 27 | } |
vsupacha | 24:0007712e84a8 | 28 | |
vsupacha | 24:0007712e84a8 | 29 | void pressed_callback() |
vsupacha | 24:0007712e84a8 | 30 | { |
vsupacha | 24:0007712e84a8 | 31 | uint16_t skipCount; |
vsupacha | 24:0007712e84a8 | 32 | |
vsupacha | 24:0007712e84a8 | 33 | if ((Tick > pressedTick) && (Tick > releasedTick)) { |
vsupacha | 24:0007712e84a8 | 34 | skipCount = Tick - releasedTick; |
vsupacha | 24:0007712e84a8 | 35 | modbus_update(0x0000, skipCount); |
vsupacha | 24:0007712e84a8 | 36 | pressedTick = Tick; |
vsupacha | 24:0007712e84a8 | 37 | // printf("Pressed: %d - %d - %d\r\n", pressedTick, releasedTick, skipCount); |
vsupacha | 0:f306cb0263a6 | 38 | } |
vsupacha | 0:f306cb0263a6 | 39 | } |
vsupacha | 0:f306cb0263a6 | 40 | |
vsupacha | 24:0007712e84a8 | 41 | void released_callback() |
vsupacha | 24:0007712e84a8 | 42 | { |
vsupacha | 24:0007712e84a8 | 43 | uint16_t widthCount; |
vsupacha | 24:0007712e84a8 | 44 | |
vsupacha | 24:0007712e84a8 | 45 | if ((Tick > pressedTick) && (Tick > releasedTick)) { |
vsupacha | 24:0007712e84a8 | 46 | widthCount = Tick - pressedTick; |
vsupacha | 24:0007712e84a8 | 47 | modbus_update(0x0001, widthCount); |
vsupacha | 24:0007712e84a8 | 48 | releasedTick = Tick; |
vsupacha | 24:0007712e84a8 | 49 | // printf("Released: %d - %d - %d\r\n", pressedTick, releasedTick, widthCount); |
vsupacha | 24:0007712e84a8 | 50 | } |
vsupacha | 24:0007712e84a8 | 51 | } |
vsupacha | 24:0007712e84a8 | 52 | |
vsupacha | 24:0007712e84a8 | 53 | void ticker_callback() |
vsupacha | 24:0007712e84a8 | 54 | { |
vsupacha | 24:0007712e84a8 | 55 | Tick++; |
vsupacha | 24:0007712e84a8 | 56 | } |
vsupacha | 24:0007712e84a8 | 57 | |
vsupacha | 0:f306cb0263a6 | 58 | int main() { |
vsupacha | 0:f306cb0263a6 | 59 | // setup code |
vsupacha | 24:0007712e84a8 | 60 | pc.attach(&serial_callback); |
nengzix | 26:e997527a78d7 | 61 | modbus_init(0x0059); // init ID |
vsupacha | 24:0007712e84a8 | 62 | |
vsupacha | 0:f306cb0263a6 | 63 | // 1. button code |
vsupacha | 24:0007712e84a8 | 64 | button.rise(&pressed_callback); |
vsupacha | 24:0007712e84a8 | 65 | button.fall(&released_callback); |
vsupacha | 24:0007712e84a8 | 66 | |
vsupacha | 0:f306cb0263a6 | 67 | // 2. timer code |
vsupacha | 24:0007712e84a8 | 68 | timebase.attach(&ticker_callback, 0.1); |
nengzix | 25:4d1450e7cb88 | 69 | // printf("Starting\n"); |
nengzix | 25:4d1450e7cb88 | 70 | // printf("Test: %02X\r\n", modbus_check(0x0000, 0x0001)); |
nengzix | 25:4d1450e7cb88 | 71 | // printf("Test: %02X\r\n", modbus_check(0x0001, 0x0001)); |
nengzix | 25:4d1450e7cb88 | 72 | // printf("Test: %02X\r\n", modbus_check(0x0000, 0x0002)); |
nengzix | 25:4d1450e7cb88 | 73 | // printf("Test: %02X\r\n", modbus_check(0x0000, 0x0003)); |
nengzix | 25:4d1450e7cb88 | 74 | // printf("Test: %02X\r\n", modbus_check(0x0001, 0x0002)); |
nengzix | 25:4d1450e7cb88 | 75 | // printf("Test: %02X\r\n", modbus_check(0x0002, 0x0002)); |
vsupacha | 0:f306cb0263a6 | 76 | while(1) { |
vsupacha | 24:0007712e84a8 | 77 | // blinking LED |
vsupacha | 24:0007712e84a8 | 78 | myled = !myled; |
vsupacha | 24:0007712e84a8 | 79 | wait(2.5); |
nengzix | 25:4d1450e7cb88 | 80 | //printf("Detection: %d - %d\r\n", pressedTick, releasedTick); |
nengzix | 25:4d1450e7cb88 | 81 | //modbus_response(0x01); |
nengzix | 25:4d1450e7cb88 | 82 | //modbus_response(0x02); |
nengzix | 25:4d1450e7cb88 | 83 | //modbus_response(0x03); |
vsupacha | 0:f306cb0263a6 | 84 | } |
vsupacha | 0:f306cb0263a6 | 85 | } |