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.
Dependencies: Encoder_dspic33f mbed
Diff: main.cpp
- Revision:
- 0:72dfed48bbcd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Feb 27 02:15:35 2014 +0000 @@ -0,0 +1,85 @@ +#include "mbed.h" +#include "common.h" +#include "Encoder_dspic33f.h" + +using namespace std; + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +Serial pc(USBTX, USBRX); + +// dspic Encoder +const int enc_resolution = 10000 ; +const int address = 0x18 ; +Encoder_dspic33f encoder(p9, p10, enc_resolution, address); +Ticker printer; + +int extractInt(char *buf) +{ + char tmp[10]; + int tmpInt; + strcpy(tmp, buf+1), + + tmpInt =atof(tmp); + return tmpInt; +} + +void printdata() +{ + toggle(led2); + double data[2]; + encoder.read(data); + pc.printf("Data Read: %f | %f \r", toDegrees(data[0]), toDegrees(data[1])); +} + + +int main() +{ + char cmd[10]; + int i = 0; + pc.baud(115200); + + pc.printf("\r\n\r\n\r\n"); + pc.printf("Shalab - Tutorial004_dspic33fI2cCom\r\n"); + encoder.set_resolution(enc_resolution); + encoder.reset(); + pc.printf("Command list:\r\n" + "r: reset encoder\r\n" + "e: set encoder resolution, usage: e1024\r\n" + "p: print encoder values\r\n"); + wait(1); + while(1) { + toggle(led1); + ++i; + pc.printf("[%3d]Enter command: ",i); + pc.scanf("%s", &cmd); + + switch (cmd[0]){ + case 'r': { + pc.printf("Reset encoder\r\n"); + encoder.reset(); + break;} + case 'p': { // print data until enter interrupt + pc.printf("Reading encoder\r\n"); + wait(0.5); + printer.attach(&printdata, 0.05); + pc.getc(); + pc.printf("Stopping Printing\r\n"); + printer.detach(); + + break;} + case 'e':{ + int enc_resolution; + + enc_resolution = extractInt(cmd); + pc.printf("Set encoder resolution: %d\r\n", enc_resolution); + encoder.set_resolution(enc_resolution); + break;} + default: + break; + } + } +} \ No newline at end of file