Sukkpranhachai Gatesichapakorn
/
TAIST_modbus
New group
Fork of TAIST_modbus by
main.cpp@5:843e67c675c1, 2018-03-14 (annotated)
- Committer:
- SaiiPruttapon
- Date:
- Wed Mar 14 08:03:00 2018 +0000
- Revision:
- 5:843e67c675c1
- Parent:
- 4:f6bce90e2872
- Child:
- 6:5bb5238f2f6e
gtrbsbtfrs
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 | 0:f306cb0263a6 | 3 | |
vsupacha | 0:f306cb0263a6 | 4 | DigitalOut myled(LED1); |
SaiiPruttapon | 1:f84cb7cea5ea | 5 | InterruptIn button(USER_BUTTON); |
vsupacha | 0:f306cb0263a6 | 6 | RawSerial pc(USBTX, USBRX); |
vsupacha | 0:f306cb0263a6 | 7 | |
SaiiPruttapon | 4:f6bce90e2872 | 8 | float cunt_pressed = 0.0; |
SaiiPruttapon | 4:f6bce90e2872 | 9 | float cunt_released = 0.0; |
SaiiPruttapon | 4:f6bce90e2872 | 10 | float x,y; |
SaiiPruttapon | 1:f84cb7cea5ea | 11 | |
vsupacha | 0:f306cb0263a6 | 12 | void serial_callback() |
vsupacha | 0:f306cb0263a6 | 13 | { |
vsupacha | 0:f306cb0263a6 | 14 | uint8_t frame[6]; |
vsupacha | 0:f306cb0263a6 | 15 | char ch = pc.getc(); |
SaiiPruttapon | 1:f84cb7cea5ea | 16 | if (modbus_parser(ch, frame)) { |
vsupacha | 0:f306cb0263a6 | 17 | } |
vsupacha | 0:f306cb0263a6 | 18 | } |
SaiiPruttapon | 4:f6bce90e2872 | 19 | |
SaiiPruttapon | 1:f84cb7cea5ea | 20 | void pressed() |
SaiiPruttapon | 1:f84cb7cea5ea | 21 | { |
SaiiPruttapon | 5:843e67c675c1 | 22 | while(button=='0'){ |
SaiiPruttapon | 5:843e67c675c1 | 23 | cunt_pressed = cunt_pressed+1; |
SaiiPruttapon | 5:843e67c675c1 | 24 | if(cunt_released != 0) |
SaiiPruttapon | 5:843e67c675c1 | 25 | { |
SaiiPruttapon | 5:843e67c675c1 | 26 | y = cunt_released; |
SaiiPruttapon | 5:843e67c675c1 | 27 | printf("released = %f\n",y); |
SaiiPruttapon | 5:843e67c675c1 | 28 | cunt_released = 0; |
SaiiPruttapon | 5:843e67c675c1 | 29 | } |
SaiiPruttapon | 5:843e67c675c1 | 30 | wait(0.1); |
SaiiPruttapon | 4:f6bce90e2872 | 31 | } |
SaiiPruttapon | 1:f84cb7cea5ea | 32 | } |
SaiiPruttapon | 1:f84cb7cea5ea | 33 | |
SaiiPruttapon | 1:f84cb7cea5ea | 34 | void released() |
SaiiPruttapon | 1:f84cb7cea5ea | 35 | { |
SaiiPruttapon | 5:843e67c675c1 | 36 | while(button=='1'){ |
SaiiPruttapon | 5:843e67c675c1 | 37 | cunt_released = cunt_released+1; |
SaiiPruttapon | 5:843e67c675c1 | 38 | if(cunt_pressed != 0) |
SaiiPruttapon | 5:843e67c675c1 | 39 | { |
SaiiPruttapon | 5:843e67c675c1 | 40 | x = cunt_pressed; |
SaiiPruttapon | 5:843e67c675c1 | 41 | printf("pressed = %f\n",x); |
SaiiPruttapon | 5:843e67c675c1 | 42 | cunt_pressed = 0; |
SaiiPruttapon | 5:843e67c675c1 | 43 | } |
SaiiPruttapon | 5:843e67c675c1 | 44 | wait(0.1); |
SaiiPruttapon | 5:843e67c675c1 | 45 | } |
SaiiPruttapon | 1:f84cb7cea5ea | 46 | } |
SaiiPruttapon | 1:f84cb7cea5ea | 47 | |
vsupacha | 0:f306cb0263a6 | 48 | int main() { |
vsupacha | 0:f306cb0263a6 | 49 | // setup code |
vsupacha | 0:f306cb0263a6 | 50 | pc.attach(serial_callback); |
SaiiPruttapon | 1:f84cb7cea5ea | 51 | |
vsupacha | 0:f306cb0263a6 | 52 | // 1. button code |
SaiiPruttapon | 1:f84cb7cea5ea | 53 | button.fall(&pressed); |
SaiiPruttapon | 1:f84cb7cea5ea | 54 | button.rise(&released); |
SaiiPruttapon | 1:f84cb7cea5ea | 55 | |
vsupacha | 0:f306cb0263a6 | 56 | // 2. timer code |
SaiiPruttapon | 1:f84cb7cea5ea | 57 | |
vsupacha | 0:f306cb0263a6 | 58 | while(1) { |
vsupacha | 0:f306cb0263a6 | 59 | // loop code |
vsupacha | 0:f306cb0263a6 | 60 | myled = 1; // LED is ON |
vsupacha | 0:f306cb0263a6 | 61 | wait(0.2); // 200 ms |
vsupacha | 0:f306cb0263a6 | 62 | myled = 0; // LED is OFF |
vsupacha | 0:f306cb0263a6 | 63 | wait(1.0); // 1 sec |
vsupacha | 0:f306cb0263a6 | 64 | } |
vsupacha | 0:f306cb0263a6 | 65 | } |