New group

Dependencies:   mbed

Fork of TAIST_modbus by Supachai Vorapojpisut

Committer:
nengzix
Date:
Wed Mar 14 14:16:11 2018 +0000
Revision:
16:02568a44616b
Parent:
11:316c0fb5423b
Child:
17:98631c9faf5f
fix merge;

Who changed what in which revision?

UserRevisionLine numberNew 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);
nengzix 9:4ddee8c61bad 7
SaiiPruttapon 8:5c3395345dc1 8 Ticker ticker;
vsupacha 0:f306cb0263a6 9
nengzix 7:774c2b305d2d 10 int cunt_pressed = 0;
nengzix 7:774c2b305d2d 11 int cunt_released = 0;
fzaq 6:5bb5238f2f6e 12 int x,y;
SaiiPruttapon 1:f84cb7cea5ea 13
vsupacha 0:f306cb0263a6 14 void serial_callback()
vsupacha 0:f306cb0263a6 15 {
nengzix 11:316c0fb5423b 16 //Test with :000300000000FD for register 0
nengzix 11:316c0fb5423b 17 //Test with :000301000000FC for register 1
vsupacha 0:f306cb0263a6 18 uint8_t frame[6];
vsupacha 0:f306cb0263a6 19 char ch = pc.getc();
nengzix 9:4ddee8c61bad 20 if(modbus_parser(ch, frame)){
nengzix 16:02568a44616b 21 modbus_gen(frame);
nengzix 9:4ddee8c61bad 22 }
vsupacha 0:f306cb0263a6 23 }
SaiiPruttapon 4:f6bce90e2872 24
SaiiPruttapon 1:f84cb7cea5ea 25 void pressed()
SaiiPruttapon 1:f84cb7cea5ea 26 {
fzaq 6:5bb5238f2f6e 27 if(cunt_released != 0)
SaiiPruttapon 5:843e67c675c1 28 {
SaiiPruttapon 5:843e67c675c1 29 y = cunt_released;
fzaq 6:5bb5238f2f6e 30 modbus_update(0, y);
nengzix 9:4ddee8c61bad 31 printf("released = %.1f sec\n",modbus_read(0)*0.1);
SaiiPruttapon 5:843e67c675c1 32 cunt_released = 0;
SaiiPruttapon 5:843e67c675c1 33 }
SaiiPruttapon 8:5c3395345dc1 34 if(button==0){
fzaq 6:5bb5238f2f6e 35 cunt_pressed = cunt_pressed+1;
SaiiPruttapon 8:5c3395345dc1 36 ticker.attach(callback(&pressed),0.1);
SaiiPruttapon 4:f6bce90e2872 37 }
SaiiPruttapon 1:f84cb7cea5ea 38 }
SaiiPruttapon 1:f84cb7cea5ea 39
SaiiPruttapon 1:f84cb7cea5ea 40 void released()
SaiiPruttapon 1:f84cb7cea5ea 41 {
fzaq 6:5bb5238f2f6e 42 if(cunt_pressed != 0)
SaiiPruttapon 5:843e67c675c1 43 {
SaiiPruttapon 5:843e67c675c1 44 x = cunt_pressed;
fzaq 6:5bb5238f2f6e 45 modbus_update(1, x);
nengzix 9:4ddee8c61bad 46 printf("pressed = %.1f sec\n",modbus_read(1)*0.1);
SaiiPruttapon 5:843e67c675c1 47 cunt_pressed = 0;
SaiiPruttapon 5:843e67c675c1 48 }
SaiiPruttapon 8:5c3395345dc1 49 if(button==1){
fzaq 6:5bb5238f2f6e 50 cunt_released = cunt_released+1;
SaiiPruttapon 8:5c3395345dc1 51 ticker.attach(callback(&released),0.1);
SaiiPruttapon 5:843e67c675c1 52 }
SaiiPruttapon 1:f84cb7cea5ea 53 }
SaiiPruttapon 1:f84cb7cea5ea 54
vsupacha 0:f306cb0263a6 55 int main() {
vsupacha 0:f306cb0263a6 56 // setup code
vsupacha 0:f306cb0263a6 57 pc.attach(serial_callback);
SaiiPruttapon 1:f84cb7cea5ea 58
vsupacha 0:f306cb0263a6 59 // 1. button code
fzaq 6:5bb5238f2f6e 60 button.mode(PullNone);
SaiiPruttapon 1:f84cb7cea5ea 61 button.fall(&pressed);
SaiiPruttapon 1:f84cb7cea5ea 62 button.rise(&released);
SaiiPruttapon 1:f84cb7cea5ea 63
vsupacha 0:f306cb0263a6 64 // 2. timer code
SaiiPruttapon 8:5c3395345dc1 65 Ticker ticker;
SaiiPruttapon 8:5c3395345dc1 66
SaiiPruttapon 8:5c3395345dc1 67
vsupacha 0:f306cb0263a6 68 while(1) {
vsupacha 0:f306cb0263a6 69 // loop code
vsupacha 0:f306cb0263a6 70 myled = 1; // LED is ON
vsupacha 0:f306cb0263a6 71 wait(0.2); // 200 ms
vsupacha 0:f306cb0263a6 72 myled = 0; // LED is OFF
vsupacha 0:f306cb0263a6 73 wait(1.0); // 1 sec
vsupacha 0:f306cb0263a6 74 }
vsupacha 0:f306cb0263a6 75 }