Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TAIST_modbus by
main.cpp
- Committer:
- paicaloid
- Date:
- 2018-03-18
- Revision:
- 5:8998772b4971
- Parent:
- 4:774fe9cd0748
- Child:
- 6:b6d3e73ad56f
File content as of revision 5:8998772b4971:
#include "mbed.h"
#include "modbus.h"
#include "RawSerial.h"
DigitalOut myled(LED1);
RawSerial pc(USBTX, USBRX);
InterruptIn button(USER_BUTTON);
Ticker timebase;
uint32_t Tick = 0;
uint32_t pressedTick = 0;
uint32_t releasedTick = 0;
uint16_t widthCount;
uint16_t skipCount;
void serial_callback()
{
uint8_t frame[6];
uint16_t offset, count;
uint8_t reg_map;
char ch = pc.getc();
if (modbus_parser(ch, frame)) {
// printf("TRUE\n") ;
offset = (frame[2] << 8) | (frame[3]);
count = (frame[4] << 8) | (frame[5]);
modbus_update(0x0000, skipCount);
modbus_update(0x0001, widthCount);
reg_map = modbus_check(offset, count);
modbus_response(reg_map);
modbus_update(0x0000, 0);
modbus_update(0x0001, 0);
skipCount = 0 ;
widthCount = 0 ;
}
}
void pressed_callback()
{
// uint16_t skipCount;
if ((Tick > pressedTick) && (Tick > releasedTick)) {
skipCount = Tick - releasedTick;
// modbus_update(0x0000, skipCount);
pressedTick = Tick;
// printf("Pressed: %d - %d - %d\r\n", pressedTick, releasedTick, skipCount);
}
}
void released_callback()
{
// uint16_t widthCount;
if ((Tick > pressedTick) && (Tick > releasedTick)) {
widthCount = Tick - pressedTick;
// modbus_update(0x0001, widthCount);
releasedTick = Tick;
// printf("Released: %d - %d - %d\r\n", pressedTick, releasedTick, widthCount);
}
}
void ticker_callback()
{
Tick++;
}
int main() {
// setup code
pc.attach(&serial_callback);
// 1. button code
button.rise(&pressed_callback);
button.fall(&released_callback);
// 2. timer code
timebase.attach(&ticker_callback, 0.1);
// printf("Starting\n");
// printf("Test: %02X\r\n", modbus_check(0x0000, 0x0001));
// printf("Test: %02X\r\n", modbus_check(0x0001, 0x0001));
// printf("Test: %02X\r\n", modbus_check(0x0000, 0x0002));
// printf("Test: %02X\r\n", modbus_check(0x0000, 0x0003));
// printf("Test: %02X\r\n", modbus_check(0x0001, 0x0002));
// printf("Test: %02X\r\n", modbus_check(0x0002, 0x0002));
while(1) {
// blinking LED
myled = !myled;
wait(2.5);
// printf("Detection: %d - %d\r\n", pressedTick, releasedTick);
// modbus_response(0x01);
// modbus_response(0x02);
// modbus_response(0x03);
}
}
