Modbus serial assignment

Dependencies:   mbed

Fork of TAIST_modbus by Supachai Vorapojpisut

Committer:
lhakpa
Date:
Wed Mar 14 14:55:36 2018 +0000
Revision:
1:b50d9b988d1e
Parent:
0:f306cb0263a6
modbus assignment

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"
lhakpa 1:b50d9b988d1e 3 //DigitalIn button1(USER_BUTTON);
lhakpa 1:b50d9b988d1e 4 InterruptIn mybtn(USER_BUTTON);
vsupacha 0:f306cb0263a6 5 DigitalOut myled(LED1);
vsupacha 0:f306cb0263a6 6 RawSerial pc(USBTX, USBRX);
lhakpa 1:b50d9b988d1e 7 Timer t1,t2,Timer1;
lhakpa 1:b50d9b988d1e 8 uint8_t id= 40001;
lhakpa 1:b50d9b988d1e 9 uint16_t offset=1;
lhakpa 1:b50d9b988d1e 10 uint16_t press=3,release=4,temp=0;
lhakpa 1:b50d9b988d1e 11 bool pushed = false;
vsupacha 0:f306cb0263a6 12
vsupacha 0:f306cb0263a6 13 void serial_callback()
lhakpa 1:b50d9b988d1e 14 {printf("serial call back\n");
vsupacha 0:f306cb0263a6 15 uint8_t frame[6];
lhakpa 1:b50d9b988d1e 16 char ch = pc.getc();
lhakpa 1:b50d9b988d1e 17 printf("ch = %c \n ",ch);
vsupacha 0:f306cb0263a6 18
lhakpa 1:b50d9b988d1e 19 modbus_init(id);
lhakpa 1:b50d9b988d1e 20 modbus_read(offset);
lhakpa 1:b50d9b988d1e 21
lhakpa 1:b50d9b988d1e 22 temp=modbus_update( offset, press);
lhakpa 1:b50d9b988d1e 23
lhakpa 1:b50d9b988d1e 24 printf("Temp= %f\n",temp);
lhakpa 1:b50d9b988d1e 25 if (modbus_parser(ch, frame)) {
lhakpa 1:b50d9b988d1e 26 printf("Modbus Parser\n");
vsupacha 0:f306cb0263a6 27 }
vsupacha 0:f306cb0263a6 28 }
vsupacha 0:f306cb0263a6 29
lhakpa 1:b50d9b988d1e 30
lhakpa 1:b50d9b988d1e 31 void ledON()
lhakpa 1:b50d9b988d1e 32 {
lhakpa 1:b50d9b988d1e 33 myled = 1; //LED ON
lhakpa 1:b50d9b988d1e 34 wait(0.2); // 200 ms
lhakpa 1:b50d9b988d1e 35 t1.start();
lhakpa 1:b50d9b988d1e 36 t2.stop();
lhakpa 1:b50d9b988d1e 37 if(t2.read()<0.1)
lhakpa 1:b50d9b988d1e 38 { printf("Bouncing\n");}
lhakpa 1:b50d9b988d1e 39 else
lhakpa 1:b50d9b988d1e 40 {printf("Button released for %.1f seconds.\r\n", t2.read());
lhakpa 1:b50d9b988d1e 41 release=t2.read();
lhakpa 1:b50d9b988d1e 42 t2.reset();
lhakpa 1:b50d9b988d1e 43 }
lhakpa 1:b50d9b988d1e 44 }
lhakpa 1:b50d9b988d1e 45
lhakpa 1:b50d9b988d1e 46 void ledOFF()
lhakpa 1:b50d9b988d1e 47 {
lhakpa 1:b50d9b988d1e 48 myled = 0; //LED OFF
lhakpa 1:b50d9b988d1e 49 wait(0.2); // 200 ms
lhakpa 1:b50d9b988d1e 50 t1.stop();
lhakpa 1:b50d9b988d1e 51 t2.start();
lhakpa 1:b50d9b988d1e 52 if(t1.read()<0.1)
lhakpa 1:b50d9b988d1e 53 { printf("Bouncing\n");}
lhakpa 1:b50d9b988d1e 54 else
lhakpa 1:b50d9b988d1e 55 {
lhakpa 1:b50d9b988d1e 56 printf("Button push for %.1f seconds.\r\n", t1.read());
lhakpa 1:b50d9b988d1e 57 press=t1.read();
lhakpa 1:b50d9b988d1e 58 t1.reset();
lhakpa 1:b50d9b988d1e 59 }
lhakpa 1:b50d9b988d1e 60 }
lhakpa 1:b50d9b988d1e 61
lhakpa 1:b50d9b988d1e 62
lhakpa 1:b50d9b988d1e 63
lhakpa 1:b50d9b988d1e 64
vsupacha 0:f306cb0263a6 65 int main() {
vsupacha 0:f306cb0263a6 66 // setup code
vsupacha 0:f306cb0263a6 67 pc.attach(serial_callback);
lhakpa 1:b50d9b988d1e 68
vsupacha 0:f306cb0263a6 69 // 1. button code
lhakpa 1:b50d9b988d1e 70 mybtn.fall(&ledON); // Interrupt occur
lhakpa 1:b50d9b988d1e 71 mybtn.rise(&ledOFF);
vsupacha 0:f306cb0263a6 72 // 2. timer code
lhakpa 1:b50d9b988d1e 73
vsupacha 0:f306cb0263a6 74 while(1) {
lhakpa 1:b50d9b988d1e 75
vsupacha 0:f306cb0263a6 76 }
lhakpa 1:b50d9b988d1e 77 }