mBed code
mBed code
The controller (www.mbed.org) is a full C++ based micro controller, with many built in features. It does not natively support PWM input however. After scouring mbed.org, and found a header PwmIn.h that did exactly what I needed, by duration (or period, or percent).
nBed with mBed, it’s as easy as pi. (a sine reference)
//Alex McAlpine 06/30/2011 www.BrainLubeOnline.com
//This is an example of the mBed acting as the shift-registers for the SNES
//5 pins connect SNES
//1 white to 5+
//7 Brown to GND
//5 (12 mbed)When Latch (Orange) is HIGH function to read 16 bit variable
//6 (11 mbed)each pulse from clock (Yellow/Red) read from 16 bit variable
//4 (13 mbed)Send data out in pulses
//Pins 21-26 are PwmIn Pins
//preprocessor directives
#include "mbed.h"
#include "PwmIn.h"
//Diagnostic declarations
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
//PWM In from Futaba R617FS 6 channel receiver
PwmIn UpDown_chan(p21);
PwmIn LR_chan(p22);
PwmIn XY_chan(p23);
PwmIn AB_chan(p24);
PwmIn Start_chan(p25);
PwmIn Select_chan(p26);
//Shift Register operations declarations
InterruptIn clock_in(p11);
DigitalIn latch(p12);
DigitalOut data_out(p13);
int index = 0; //index for output of register
bool bit_field_16[16] = {1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}; //The last are for
//the super scope and other attachments they are always HIGH in the standard comntroller
//This could also be accomplished via bit manipulation with a single 16-bit short unsigned int
//More can be found on this at http://en.wikipedia.org/wiki/Bit_manipulation
//For example:
short unsigned int 16BITregister = 61440; //1111 0000 0000 0000
void get_input()
{
//if UP then bit 5 HIGH else LOW
if(UpDown_chan.pulsewidth()< .0015) {led1 = 1;bit_field_16[4] = 1;} else {led1 = 0;bit_field_16[4] = 0;}
//if DOWN then bit 6 HIGH else LOW
if(UpDown_chan.pulsewidth() > .0016) {led2 = 1;bit_field_16[5] = 1;} else {led2 = 0;bit_field_16[5] = 0;}
//if LEFT then bit 7 HIGH else LOW
if(LR_chan.pulsewidth()< .0015) {bit_field_16[6] = 1;led3 = 1;} else {bit_field_16[6] = 0;led3 = 0;}
//if RIGHT then bit 8 HIGH else LOW
if(LR_chan.pulsewidth()> .0016) {bit_field_16[7] = 1;led4 = 1;} else {bit_field_16[7] = 0;led4 = 0;}
//if 'A' then bit 9 HIGH else LOW
if(AB_chan.pulsewidth()<.0015) {bit_field_16[8] = 1;} else {bit_field_16[8] = 0;}
//if 'B' then bit 1 HIGH else LOW
if(AB_chan.pulsewidth()>.0016) {bit_field_16[0] = 1;} else {bit_field_16[0] = 0;}
//if 'X' then bit 10 HIGH else LOW
if(XY_chan.pulsewidth() <.0015) {bit_field_16[9] = 1;} else {bit_field_16[9] = 0;}
//if 'Y' then bit 2 HIGH else LOW
if(XY_chan.pulsewidth() >.0016) {bit_field_16[1] = 1;} else{bit_field_16[1] = 0;}
//if 'L' then bit 11 HIGH else LOW
// if(L) {bit_field_16[10] = 1;} //Top L and R buttons not connected at this time.
//if 'R' then bit 12 HIGH else LOW
//if(R){bit_field_16[11] = 1;}
//if 'SELECT' then bit 3 HIGH else LOW
if(Select_chan.pulsewidth()< .0015) {bit_field_16[2] = 1; led3 = 1;} else {bit_field_16[2] = 0; led3 = 0;}
//if 'START' then bit 4 HIGH else LOW
if(Start_chan.pulsewidth()< .0015) {bit_field_16[3] = 1; led4 = 1;} else {bit_field_16[3] = 0; led4 = 0;}
}
void write_input() {
data_out = bit_field_16[index];
}
int main()
{
while(1)
{
while(latch)
{
clock_in.rise(&write_input);
index++;
}
index = 0;
get_input();
} //main while
} //main
This is the code for the mBed acting as the Shift Register
This is the code I used with the 4021 ICs.
//Alex McAlpine 06/30/2011 www.BrainLubeOnline.com
//This is the code I used. The mBed is connected directly to the shift registers
//Pins 21-26 are PwmIn Pins
//preprocessor directives
#include "mbed.h"
#include "PwmIn.h"
//Diagnostic declarations
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
//PWM In from Futaba R617FS 6 channel receiver
PwmIn UpDown_chan(p21);
PwmIn LR_chan(p22);
PwmIn XY_chan(p23);
PwmIn AB_chan(p24);
PwmIn Start_chan(p25);
PwmIn Select_chan(p26);
//Shift Register operations declarations
DigitalOut UP(p11);
DigitalOut DOWN(p14);
DigitalOut LEFT(p15);
DigitalOut RIGHT(p16);
DigitalOut A(p18);
DigitalOut B(p10);
DigitalOut X(p12);
DigitalOut Y(p9);
DigitalOut START(p13);
DigitalOut SELECT(p17);
DigitalOut L(p19);
DigitalOut R(p20);
void get_input()
{
//if UP then bit 5 HIGH else LOW
if(UpDown_chan.pulsewidth()< .0015) {led1 = 1;UP = 0;} else {led1 = 0;UP = 1;}
//if DOWN then bit 6 HIGH else LOW
if(UpDown_chan.pulsewidth() > .0016) {led2 = 1;DOWN = 0;} else {led2 = 0;DOWN = 1;}
//if LEFT then bit 7 HIGH else LOW
if(LR_chan.pulsewidth()< .0015) {LEFT = 0;led3 = 1;} else {LEFT = 1;led3 = 0;}
//if RIGHT then bit 8 HIGH else LOW
if(LR_chan.pulsewidth()> .0016) {RIGHT = 0;led4 = 1;} else {RIGHT = 1;led4 = 0;}
//if 'A' then bit 9 HIGH else LOW
if(AB_chan.pulsewidth()<.0015) {A = 0;} else {A = 1;}
//if 'B' then bit 1 HIGH else LOW
if(AB_chan.pulsewidth()>.0016) {B = 0;} else {B = 1;}
//if 'X' then bit 10 HIGH else LOW
if(XY_chan.pulsewidth() <.0015) {X = 0;} else {X = 1;}
//if 'Y' then bit 2 HIGH else LOW
if(XY_chan.pulsewidth() >.0016) {Y = 0;} else{Y = 1;}
//if 'L' then bit 11 HIGH else LOW
// if(L) {bit_field_16[10] = 1;} //Top L and R buttons not connected at this time.
//if 'R' then bit 12 HIGH else LOW
//if(R){bit_field_16[11] = 1;}
//if 'SELECT' then bit 3 HIGH else LOW
if(Select_chan.pulsewidth()< .0015) {SELECT = 0; led3 = 1;} else {SELECT = 1; led3 = 0;}
//if 'START' then bit 4 HIGH else LOW
if(Start_chan.pulsewidth()< .0015) {START = 0; led4 = 1;} else {START = 1; led4 = 0;}
}
int main()
{
while(1)
{
get_input();
} //main while
} //main