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.
Diff: main.cpp
- Revision:
- 0:3cbe4eec57ae
- Child:
- 1:6e34363efa95
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Mar 30 09:46:55 2011 +0000
@@ -0,0 +1,195 @@
+#include "mbed.h"
+
+DigitalOut motor1A(p9);
+DigitalOut motor1B(p10);
+DigitalOut motor2A(p11);
+DigitalOut motor2B(p12);
+DigitalOut motor3A(p13);
+DigitalOut motor3B(p14);
+DigitalOut motor4A(p15);
+DigitalOut motor4B(p16);
+
+DigitalOut digital1(p17);
+DigitalOut digital2(p18);
+DigitalOut digital3(p19);
+DigitalOut digital4(p20);
+
+SPI spi(p5, p6, p7); // mosi(out), miso(in), sclk(clock)
+DigitalOut cs(p8); // cs(select)
+
+PwmOut motor1(p21);
+PwmOut motor2(p22);
+PwmOut motor3(p23);
+PwmOut motor4(p24);
+
+DigitalOut led(LED4);
+
+Serial pc(USBTX, USBRX);
+
+uint8_t* values;
+uint8_t* location;
+
+char buffer[3];
+int i, inValue, bufferIndex;
+
+// maps the value x to a new given range
+double map( double x, double in_min, double in_max, double out_min, double out_max ) {
+ return ( x - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min;
+}
+
+// shifts 2 8 bit integers into one integer
+int getIntValue( uint8_t high, uint8_t low ) {
+ return ( high << 8 ) | low;
+}
+
+// sets the given pwm value
+void setPWM( int value, PwmOut pwm, DigitalOut a, DigitalOut b ) {
+ // block the motors
+ if ( value > 511 ) {
+ a = 0;
+ b = 0;
+ pwm = 1;
+ } else {
+ // clockwise run
+ if ( value > 255 ) {
+ value = value - 256;
+ a = 0;
+ b = 1;
+ } else {
+ a = 1;
+ b = 0;
+ }
+ // map the 0-255 value to 0-1
+ pwm = map( value, 0, 255, 0, 1 );
+ }
+}
+
+// specifies the action for the user input
+void specifyAction( char action, int value ) {
+ // specify the action
+ switch ( action ) {
+ case 44:
+ // print the analogs and digitals
+ values = location;
+ for ( int i=0; i<16; i++ ) {
+ pc.putc( *( values++ ) );
+ }
+
+ pc.putc( '\0' );
+ pc.putc( 13 );
+ break;
+ case 162:
+ // set the first PWMs value
+ setPWM( value, motor1, motor1A, motor1B );
+ break;
+ case 163:
+ // set the second PWMs value
+ setPWM( value, motor2, motor2A, motor2B );
+ break;
+ case 164:
+ // set the third PWMs value
+ setPWM( value, motor3, motor3A, motor3B );
+ break;
+ case 165:
+ // set the fourth PWMs value
+ setPWM( value, motor4, motor4A, motor4B );
+ break;
+ case 230:
+ // Goto _reset ?
+ break;
+ case 231:
+ // enable or disable servos
+ if ( value == 1 ) {
+ // enable servo ?
+ } else if ( value == 2 ) {
+ // disable servo ?
+ }
+ break;
+ case 233:
+ // set digitals values
+ digital1 = ( value & 1 );
+ digital2 = ( ( value >> 1 ) & 1 );
+ digital3 = ( ( value >> 2 ) & 1 );
+ digital4 = ( ( value >> 3 ) & 1 );
+ break;
+ default:
+ //pc.printf( "default\n" );
+ }
+}
+
+int readADC( int channel, DigitalOut select ) {
+ // select the device
+ select = 0;
+
+ // sending the 5 bits + 2 bits to ignore the null bits
+ // coming from the device, so the data that is sent is 11000101
+ uint8_t commandbits = 0xC0;
+ commandbits |= ( channel << 2 );
+ spi.write( commandbits );
+
+ // now the device sends back the readings 12 bits, 7 bits at a time
+ uint8_t high = spi.write(0x0);
+ uint8_t low = spi.write(0x0);
+ low = low >> 2;
+
+ // shift to get the high and low byte
+ high = ( high >> 3 );
+ low = ( high << 5 ) | low;
+
+ // deselect the device
+ select = 1;
+
+ // save the values
+ *(values++) = high;
+ *(values++) = low;
+
+ return 0;
+}
+
+void serialEvent() {
+ char c = pc.getc();
+ // if we received enter
+ if( c == 13 ) {
+ //pc.printf( "char=[%c] value=[%u]", buffer[0],getIntValue(buffer[1],buffer[2]) );
+ specifyAction( buffer[ 0 ], getIntValue( buffer[ 1 ], buffer[ 2 ] ) );
+ buffer[ 0 ] = '\0';
+ bufferIndex = 0;
+ }
+ else {
+ buffer[ bufferIndex++ ] = c;
+ }
+}
+
+// the main method
+int main() {
+ // Setup the spi for 7 bit data, high steady state clock,
+ // second edge capture, with a 1MHz clock rate
+ spi.format(7,0);
+ spi.frequency(1000000);
+
+ // specify the usb baud rate, supported are :
+ // 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600
+ pc.baud( 921600 );
+
+ // allocate 16 bytes of space for the adc values
+ values = (uint8_t*) malloc(16);
+ // remember the first values aadress
+ location = values;
+
+ // attach a serial interrupt
+ pc.attach( &serialEvent );
+
+ // to indicate programm is running
+ led = 1;
+
+ // endless loop
+ while ( true ) {
+ // start from the beginning
+ values = location;
+ // read the ADC values
+ for ( i = 0; i < 8; i++ ) {
+ readADC( i, cs );
+ }
+ }
+}
+