TAIST class assignment

Dependencies:   mbed

Fork of TAIST_modbus by Supachai Vorapojpisut

Committer:
Benjaphan
Date:
Wed Mar 14 14:55:52 2018 +0000
Revision:
1:d2e8a3c822f1
Parent:
0:f306cb0263a6
Ms. Benjaphan Sommana StudentID: 6022040445; Mr. Thotsawut Mhudchuay StudentID:6014552855; Mr. Pongporamat Charuchinda ID:6014552863; Ms. Nabeela Shaheen, ID:6014553142

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"
Benjaphan 1:d2e8a3c822f1 3 #include <math.h>
Benjaphan 1:d2e8a3c822f1 4 #include <stdio.h>
vsupacha 0:f306cb0263a6 5
Benjaphan 1:d2e8a3c822f1 6 Ticker t;
vsupacha 0:f306cb0263a6 7 DigitalOut myled(LED1);
Benjaphan 1:d2e8a3c822f1 8 InterruptIn mbtn(USER_BUTTON);
vsupacha 0:f306cb0263a6 9 RawSerial pc(USBTX, USBRX);
Benjaphan 1:d2e8a3c822f1 10 int a;
Benjaphan 1:d2e8a3c822f1 11 int b,c,d;
Benjaphan 1:d2e8a3c822f1 12 int casep = 0;
Benjaphan 1:d2e8a3c822f1 13 int16_t nub = 0;
Benjaphan 1:d2e8a3c822f1 14 int16_t time1;
Benjaphan 1:d2e8a3c822f1 15 int16_t time2;
Benjaphan 1:d2e8a3c822f1 16 void count()
Benjaphan 1:d2e8a3c822f1 17 { nub = nub +1;
Benjaphan 1:d2e8a3c822f1 18 }
Benjaphan 1:d2e8a3c822f1 19 void press() {
Benjaphan 1:d2e8a3c822f1 20 if((nub-c)>2){
Benjaphan 1:d2e8a3c822f1 21 d = nub;
Benjaphan 1:d2e8a3c822f1 22 time2 = d-c;
Benjaphan 1:d2e8a3c822f1 23 a = nub;
Benjaphan 1:d2e8a3c822f1 24 }
Benjaphan 1:d2e8a3c822f1 25 }
Benjaphan 1:d2e8a3c822f1 26 void release() {
Benjaphan 1:d2e8a3c822f1 27 if((nub-b)>2){
Benjaphan 1:d2e8a3c822f1 28 b = nub;
Benjaphan 1:d2e8a3c822f1 29 time1 = b-a;
Benjaphan 1:d2e8a3c822f1 30 c = nub ;
Benjaphan 1:d2e8a3c822f1 31 }
Benjaphan 1:d2e8a3c822f1 32 }
vsupacha 0:f306cb0263a6 33
vsupacha 0:f306cb0263a6 34 void serial_callback()
vsupacha 0:f306cb0263a6 35 {
vsupacha 0:f306cb0263a6 36 uint8_t frame[6];
vsupacha 0:f306cb0263a6 37 char ch = pc.getc();
vsupacha 0:f306cb0263a6 38 if (modbus_parser(ch, frame)) {
Benjaphan 1:d2e8a3c822f1 39 switch(frame[3]){
Benjaphan 1:d2e8a3c822f1 40 case 0: casep = 1; break; //Please enter :000300000003FA\r\n to show pushing time
Benjaphan 1:d2e8a3c822f1 41 case 1: casep = 2; break; //Please enter :000300010003F9\r\n to show releasing time
Benjaphan 1:d2e8a3c822f1 42 case 2: casep = 3; break; //Please enter :000300020003F8\r\n to show both pushing and releasing time
Benjaphan 1:d2e8a3c822f1 43
Benjaphan 1:d2e8a3c822f1 44 }
vsupacha 0:f306cb0263a6 45 }
vsupacha 0:f306cb0263a6 46 }
vsupacha 0:f306cb0263a6 47
vsupacha 0:f306cb0263a6 48 int main() {
Benjaphan 1:d2e8a3c822f1 49 mbtn.fall(&press);
Benjaphan 1:d2e8a3c822f1 50 mbtn.rise(&release);
Benjaphan 1:d2e8a3c822f1 51
Benjaphan 1:d2e8a3c822f1 52 t.attach(&count, 0.1);
vsupacha 0:f306cb0263a6 53 while(1) {
Benjaphan 1:d2e8a3c822f1 54 pc.attach(serial_callback);
Benjaphan 1:d2e8a3c822f1 55 switch(casep){
Benjaphan 1:d2e8a3c822f1 56 case 0: break;
Benjaphan 1:d2e8a3c822f1 57 case 1: pc.printf("The releasing time taken was %.1f seconds\n\n",time2/10.0); break;
Benjaphan 1:d2e8a3c822f1 58 case 2: pc.printf("The pushing time taken was %.1f seconds\n\n",time1/10.0); break;
Benjaphan 1:d2e8a3c822f1 59 case 3: pc.printf("The pushing time taken was %.1f seconds\nThe release time taken was %.1f seconds\n\n",time1/10.0,time2/10.0); break;
Benjaphan 1:d2e8a3c822f1 60 default: break;
Benjaphan 1:d2e8a3c822f1 61 }
Benjaphan 1:d2e8a3c822f1 62 casep = 0;
vsupacha 0:f306cb0263a6 63 }
Benjaphan 1:d2e8a3c822f1 64 }