Onshot program trigger for 555 chip

Dependencies:   SLCD mbed

Committer:
scohennm
Date:
Wed Apr 01 19:26:16 2015 +0000
Revision:
0:285365ada25d
Oneshot triggering program adapted from Arduino program of same name.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:285365ada25d 1 #include "mbed.h"
scohennm 0:285365ada25d 2 #include "SLCD.h"
scohennm 0:285365ada25d 3
scohennm 0:285365ada25d 4 // copied from Change state example for basis 02.22.2014 sc
scohennm 0:285365ada25d 5 #define PROGNAME "555_onshot trigger v3\r\n"
scohennm 0:285365ada25d 6 #define HIGH 1
scohennm 0:285365ada25d 7 #define LOW 0
scohennm 0:285365ada25d 8 #define PIN_2_555 D2
scohennm 0:285365ada25d 9 #define PIN_3_555 D3
scohennm 0:285365ada25d 10 #define PIN_3_CLONE LED_GREEN
scohennm 0:285365ada25d 11 #define LEDON LOW
scohennm 0:285365ada25d 12 #define LEDOFF HIGH
scohennm 0:285365ada25d 13 #define OUTPULSELEN 100 // ms
scohennm 0:285365ada25d 14 #define DATADELAY 1
scohennm 0:285365ada25d 15 #define SERIALSPEED 57600
scohennm 0:285365ada25d 16 #define MAX_OUT 1500 // ms
scohennm 0:285365ada25d 17 #define MIN_OUT 500
scohennm 0:285365ada25d 18 #define ABORTTIME 3500
scohennm 0:285365ada25d 19 #define INPUTSTART 1
scohennm 0:285365ada25d 20 #define DATARATE 1000 // ms time between data takes.
scohennm 0:285365ada25d 21 #define LCDLEN 10
scohennm 0:285365ada25d 22 #define STARTMESS "STRT"
scohennm 0:285365ada25d 23 #define BGNMESS "BEGN"
scohennm 0:285365ada25d 24
scohennm 0:285365ada25d 25 // this constant won't change:
scohennm 0:285365ada25d 26 DigitalOut oneShotIn (PIN_2_555);
scohennm 0:285365ada25d 27 // the pin that the pushbutton is attached to
scohennm 0:285365ada25d 28 DigitalIn oneShotOut (PIN_3_555);
scohennm 0:285365ada25d 29 DigitalOut ledPin(PIN_3_CLONE); // the pin that the LED is attached to
scohennm 0:285365ada25d 30 Serial pc(USBTX, USBRX);
scohennm 0:285365ada25d 31 SLCD slcd;
scohennm 0:285365ada25d 32 Timer milliTimer;
scohennm 0:285365ada25d 33
scohennm 0:285365ada25d 34 long oneShotLen;
scohennm 0:285365ada25d 35 long timeOut = ABORTTIME;
scohennm 0:285365ada25d 36 long nextDataTake = DATARATE;
scohennm 0:285365ada25d 37 long oneShotBegin;
scohennm 0:285365ada25d 38 long oneShotEnd;
scohennm 0:285365ada25d 39 // Variables will change:
scohennm 0:285365ada25d 40 int buttonPushCounter = 0; // counter for the number of button presses
scohennm 0:285365ada25d 41 int buttonState = 0; // current state of the button
scohennm 0:285365ada25d 42 int lastButtonState = 0; // previous state of the button
scohennm 0:285365ada25d 43
scohennm 0:285365ada25d 44
scohennm 0:285365ada25d 45 void LCDMess(char *lMess){
scohennm 0:285365ada25d 46 slcd.Home();
scohennm 0:285365ada25d 47 slcd.clear();
scohennm 0:285365ada25d 48 slcd.printf(lMess);
scohennm 0:285365ada25d 49 }
scohennm 0:285365ada25d 50 void pulseOut (long pulseLen){
scohennm 0:285365ada25d 51 oneShotIn.write(LOW);
scohennm 0:285365ada25d 52 wait_us(pulseLen);
scohennm 0:285365ada25d 53 oneShotIn.write(HIGH);
scohennm 0:285365ada25d 54 }
scohennm 0:285365ada25d 55
scohennm 0:285365ada25d 56 void setup() {
scohennm 0:285365ada25d 57
scohennm 0:285365ada25d 58 // initialize serial communication:
scohennm 0:285365ada25d 59
scohennm 0:285365ada25d 60 pc.printf(PROGNAME);
scohennm 0:285365ada25d 61 LCDMess(BGNMESS);
scohennm 0:285365ada25d 62 oneShotIn.write(HIGH);
scohennm 0:285365ada25d 63 oneShotOut.mode(PullNone);
scohennm 0:285365ada25d 64 ledPin.write(LOW);
scohennm 0:285365ada25d 65 milliTimer.start();
scohennm 0:285365ada25d 66 milliTimer.reset();
scohennm 0:285365ada25d 67 nextDataTake = DATARATE;
scohennm 0:285365ada25d 68 lastButtonState = LOW;
scohennm 0:285365ada25d 69 }
scohennm 0:285365ada25d 70
scohennm 0:285365ada25d 71
scohennm 0:285365ada25d 72 void loop() {
scohennm 0:285365ada25d 73 char lcdData[LCDLEN];
scohennm 0:285365ada25d 74 oneShotLen = 0;
scohennm 0:285365ada25d 75 if (milliTimer.read_ms() > nextDataTake){
scohennm 0:285365ada25d 76 LCDMess(STARTMESS);
scohennm 0:285365ada25d 77 pulseOut(OUTPULSELEN);
scohennm 0:285365ada25d 78 wait_ms(DATADELAY);
scohennm 0:285365ada25d 79 buttonState = oneShotOut.read();
scohennm 0:285365ada25d 80 if (buttonState == INPUTSTART) {
scohennm 0:285365ada25d 81 oneShotBegin = milliTimer.read_ms();
scohennm 0:285365ada25d 82 ledPin.write(LEDON);
scohennm 0:285365ada25d 83 while ((buttonState == INPUTSTART) && (oneShotLen < ABORTTIME)){
scohennm 0:285365ada25d 84 oneShotLen = milliTimer.read_ms();
scohennm 0:285365ada25d 85 //pc.printf("%d\r\n",oneShotLen);
scohennm 0:285365ada25d 86 buttonState = oneShotOut.read();
scohennm 0:285365ada25d 87 }
scohennm 0:285365ada25d 88 pc.printf("%d\r\n",oneShotLen);
scohennm 0:285365ada25d 89 sprintf(lcdData,"%4d",oneShotLen);
scohennm 0:285365ada25d 90 LCDMess(lcdData);
scohennm 0:285365ada25d 91 ledPin.write(LEDOFF);
scohennm 0:285365ada25d 92 milliTimer.reset();
scohennm 0:285365ada25d 93 nextDataTake = DATARATE;
scohennm 0:285365ada25d 94 }
scohennm 0:285365ada25d 95
scohennm 0:285365ada25d 96 }
scohennm 0:285365ada25d 97
scohennm 0:285365ada25d 98 }
scohennm 0:285365ada25d 99
scohennm 0:285365ada25d 100 int main(){
scohennm 0:285365ada25d 101 setup();
scohennm 0:285365ada25d 102 while (true) {
scohennm 0:285365ada25d 103 loop();
scohennm 0:285365ada25d 104 }
scohennm 0:285365ada25d 105 }