shalab shalab / Mbed 2 deprecated Tutorial04_dspic33fI2cCom

Dependencies:   Encoder_dspic33f mbed

Files at this revision

API Documentation at this revision

Comitter:
shalab
Date:
Thu Feb 27 02:15:35 2014 +0000
Commit message:
Initial commit

Changed in this revision

Encoder_dspic33f.lib Show annotated file Show diff for this revision Revisions of this file
common.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder_dspic33f.lib	Thu Feb 27 02:15:35 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/bediyap/code/Encoder_dspic33f/#3eaf636e9a0f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common.h	Thu Feb 27 02:15:35 2014 +0000
@@ -0,0 +1,6 @@
+#define toggle(a) {a=a^1;}
+
+//Convert from radians to degrees.
+inline double toDegrees(double x) {return x * 57.2957795;}
+//Convert from degrees to radians.
+inline double toRadians(double x) {return x * 0.01745329252;}
\ No newline at end of file
--- /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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 27 02:15:35 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/8e73be2a2ac1
\ No newline at end of file