asd / Mbed 2 deprecated mbed_4180Lab1

Dependencies:   MCP23S17 PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lab1.cpp Source File

lab1.cpp

00001 // ECE 4180
00002 // Lab/Project 1
00003 // Jeremy M. Deremer
00004 // Due 26 January, 2017
00005 
00006 #include "mbed.h"
00007 #include "PinDetect.h"
00008 #include "LEDColor.h"
00009 #include "RGBLed.h"
00010 #include "MCP23S17.h"
00011 #include "Nav_Switch.h"
00012 
00013 PwmOut led(p24);
00014 
00015 //   Instantiate pushbuttons on analog pins
00016 PinDetect pb1(p17);
00017 PinDetect pb2(p18);
00018 PinDetect pb3(p19);
00019 
00020 //   Instantiate RGBLed and allocate default colors
00021 //RGBLed myRGBled(p23,p22,p21); //RGB PWM pins
00022 //const LEDColor off(0.0,0.0,0.0);
00023 //const LEDColor blue(0.0,0.0,1.0);
00024 
00025 //   Instantiate I/O Expander
00026 // Create SPI bus
00027 SPI spi(p5, p6, p7);
00028 char Opcode = 0x40;
00029 // Next create a MCP23S17
00030 MCP23S17 chip = MCP23S17(spi, p20, Opcode);
00031 DigitalOut led1(LED1); // mbed LED1 is used for test status display
00032 
00033 //    Instantiate Nav Joystick
00034 Nav_Switch myNav(p15, p12, p13, p11, p8); //pin order on Sparkfun breakout
00035 
00036 float inc = 0.1f;
00037 float val = 0.5f;
00038 
00039 // Callback routine is interrupt activated by a debounced pb1 hit
00040 void pb1_hit_callback (void)
00041 {
00042     val = led.read();
00043     if (val == 0.00f)
00044     {
00045         led.write(0.50f);  // 50% duty cycle
00046     }
00047     else
00048     {
00049         led.write(0.00f);  // 0% duty cycle
00050     }
00051 }
00052 
00053 // Callback routine is interrupt activated by a debounced pb2 hit
00054 void pb2_hit_callback (void)
00055 {
00056     // add brightness
00057     val = led.read();
00058     if (val < 1.00f)
00059     {
00060         val += inc; 
00061         led.write(val);
00062     }
00063 }
00064 
00065 // Callback routine is interrupt activated by a debounced pb3 hit
00066 void pb3_hit_callback (void)
00067 {
00068     // subtract brightness
00069     val = led.read();
00070     if (val > 0.0f)
00071     {
00072         val -= inc; 
00073         led.write(val);
00074     }
00075 }
00076 
00077 int main() {
00078     
00079     led.write(0.5f);  // 50% duty cycle
00080     
00081     // Use internal pullups for the three pushbuttons
00082     pb1.mode(PullUp);
00083     pb2.mode(PullUp);
00084     pb3.mode(PullUp);
00085     // Delay for initial pullup to take effect
00086     wait(.01);
00087     // Setup Interrupt callback functions for a pb hit
00088     pb1.attach_deasserted(&pb1_hit_callback);
00089     pb2.attach_deasserted(&pb2_hit_callback);
00090     pb3.attach_deasserted(&pb3_hit_callback);
00091     // Start sampling pb inputs using interrupts
00092     pb1.setSampleFrequency();
00093     pb2.setSampleFrequency();
00094     pb3.setSampleFrequency();
00095     // pushbuttons now setup and running
00096     
00097     //  Set all 8 Port A bits to output direction
00098     chip.direction(PORT_A, 0x00);
00099     //  Set all 8 Port B bits to input direction
00100     chip.direction(PORT_B, 0xFF);
00101     
00102     while(1)          // led flashing
00103     {
00104         // write 0xAA to MCP23S17 Port A
00105         chip.write(PORT_A, 0xAA);
00106         
00107         // read back value from MCP23S17 Port B and display B0 on mbed led1
00108         if ((chip.read(PORT_B)& 0x01) == 0x01)
00109         {
00110             chip.write(PORT_A, 0x55);
00111         }
00112         
00113         //chip.write(PORT_A, (chip.read(PORT_B)& 0x01));
00114         
00115         
00116         //with pullups a button hit is a "0" - "~" inverts data to leds
00117         mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs
00118         if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button)
00119         //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this
00120         //wait(0.02);
00121         
00122     }
00123 }