USB Homework #1

Dependencies:   mbed

Fork of HW1_Binary_counter by Dwayne Dilbeck

Committer:
jakowisp
Date:
Wed Jul 17 01:49:17 2013 +0000
Revision:
1:d090a3a87d87
Parent:
0:717993c337df
Usb Device: Interface, Architecture, protocols and programing.; Homework #1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakowisp 1:d090a3a87d87 1 //Load mbed basic function header
mbed_official 0:717993c337df 2 #include "mbed.h"
jakowisp 1:d090a3a87d87 3
jakowisp 1:d090a3a87d87 4 //Define the LEDs as a BUS
jakowisp 1:d090a3a87d87 5 BusOut myleds(LED1, LED2, LED3, LED4);
jakowisp 1:d090a3a87d87 6 DigitalIn updown(p14);
jakowisp 1:d090a3a87d87 7
jakowisp 1:d090a3a87d87 8 //Define a global count variable
jakowisp 1:d090a3a87d87 9 int cnt=0;
jakowisp 1:d090a3a87d87 10 bool direction=false;
jakowisp 1:d090a3a87d87 11
mbed_official 0:717993c337df 12
jakowisp 1:d090a3a87d87 13 //Define a generic increment by X function. The functions to increment is passed as a pointer. The increment amount is passed by value to the function
jakowisp 1:d090a3a87d87 14 void IncrementValueByX(int *value, int x) {
jakowisp 1:d090a3a87d87 15 *value+=x;
jakowisp 1:d090a3a87d87 16 }
jakowisp 1:d090a3a87d87 17
jakowisp 1:d090a3a87d87 18 //Define a generic wait function using the milliseconds, wait time is passed by value.
jakowisp 1:d090a3a87d87 19 void WaitForTimeInMilliSeconds(int mseconds) {
jakowisp 1:d090a3a87d87 20 wait_ms(mseconds);
jakowisp 1:d090a3a87d87 21 }
jakowisp 1:d090a3a87d87 22
jakowisp 1:d090a3a87d87 23 //Define a generic function to assign a nibble to a bus. The BUS is passed by reference and the value is passed
jakowisp 1:d090a3a87d87 24 // by value.
jakowisp 1:d090a3a87d87 25 void SetNibbleBusOut(BusOut *nibble,int value) {
jakowisp 1:d090a3a87d87 26 nibble->write(value & 0xF);
jakowisp 1:d090a3a87d87 27 }
jakowisp 1:d090a3a87d87 28
jakowisp 1:d090a3a87d87 29 //Determine if a button has been released
jakowisp 1:d090a3a87d87 30 bool CheckButtonRelease(int *buttonState,DigitalIn *inputPin) {
jakowisp 1:d090a3a87d87 31 int currentButton;
jakowisp 1:d090a3a87d87 32 bool retValue;
jakowisp 1:d090a3a87d87 33
jakowisp 1:d090a3a87d87 34 currentButton=inputPin->read();
jakowisp 1:d090a3a87d87 35 if( *buttonState != currentButton && currentButton == 0) {
jakowisp 1:d090a3a87d87 36 retValue=true;
jakowisp 1:d090a3a87d87 37 } else {
jakowisp 1:d090a3a87d87 38 retValue=false;
jakowisp 1:d090a3a87d87 39 }
jakowisp 1:d090a3a87d87 40 *buttonState=currentButton;
jakowisp 1:d090a3a87d87 41 return retValue;
jakowisp 1:d090a3a87d87 42 }
jakowisp 1:d090a3a87d87 43
jakowisp 1:d090a3a87d87 44 //Invert a boolean based on a boolean value.
jakowisp 1:d090a3a87d87 45 void InvertBoolBasedonValue(bool *direction, int buttonRelease){
jakowisp 1:d090a3a87d87 46 if(buttonRelease)
jakowisp 1:d090a3a87d87 47 *direction= !*direction;
jakowisp 1:d090a3a87d87 48 }
jakowisp 1:d090a3a87d87 49
jakowisp 1:d090a3a87d87 50 //Change a boolean value based on button release
jakowisp 1:d090a3a87d87 51 void ChangeDirectionOnButtonRelease(bool *direction, DigitalIn *inputPin){
jakowisp 1:d090a3a87d87 52 bool buttonRelease=0;
jakowisp 1:d090a3a87d87 53 static int buttonState;
jakowisp 1:d090a3a87d87 54
jakowisp 1:d090a3a87d87 55 buttonRelease=CheckButtonRelease(&buttonState,inputPin);
jakowisp 1:d090a3a87d87 56 InvertBoolBasedonValue(direction,buttonRelease);
jakowisp 1:d090a3a87d87 57 }
jakowisp 1:d090a3a87d87 58
jakowisp 1:d090a3a87d87 59
jakowisp 1:d090a3a87d87 60 //Main loop
mbed_official 0:717993c337df 61 int main() {
jakowisp 1:d090a3a87d87 62 while(true) {
jakowisp 1:d090a3a87d87 63 ChangeDirectionOnButtonRelease(&direction,&updown);
jakowisp 1:d090a3a87d87 64 if(direction) {
jakowisp 1:d090a3a87d87 65 IncrementValueByX(&cnt,1);
jakowisp 1:d090a3a87d87 66 } else {
jakowisp 1:d090a3a87d87 67 IncrementValueByX(&cnt,-1);
jakowisp 1:d090a3a87d87 68 }
jakowisp 1:d090a3a87d87 69 SetNibbleBusOut(&myleds,cnt);
jakowisp 1:d090a3a87d87 70 WaitForTimeInMilliSeconds(1000);
mbed_official 0:717993c337df 71 }
jakowisp 1:d090a3a87d87 72 }