LVTTL HW Trigger with control via virtual Serial over USB. Works well with NUCLEO boards.

Dependencies:   mbed

main.cpp

Committer:
Neolker
Date:
2016-01-22
Revision:
0:46a9ecc2d2b8
Child:
1:26ff6101d6d5

File content as of revision 0:46a9ecc2d2b8:

/*
* ---------------
* TriggerNiNo 1.0
* ---------------
*
* LVTTL HW Trigger with control via virtual Serial over USB.
* Works well with NUCLEO-F031K6 and NUCLEO-F411RE.
*
* Copyright (C) <2015> Martin Wolker <neolker@gmail.com>
*
* TriggerNiNo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* If not, see <http://www.gnu.org/licenses/>.
*/

#include "mbed.h"                         //mbed official library.
#define TRIGGER_PULSE_LENGTH_MS   500     //Length of the trigger pulse in ms.

Serial pc(USBTX, USBRX);                  //Virtual Serial over USB with 9600 baud.
DigitalOut trigger_output_positive(D2);   //Declaration of positive trigger output.
DigitalOut trigger_output_negative(D3);   //Declaration of negative trigger output.
DigitalOut trigger_indication(LED1);      //Declaration of indication LED.

void trigger(void)
{
    trigger_output_positive =! trigger_output_positive;   //Change the state of the positive output.
    trigger_output_negative =! trigger_output_negative;   //Change the state of the negative output.
    trigger_indication =! trigger_indication;             //Change the state of the indication LED.
}

void interrupt(void)
{
    pc.getc();                          //Get the character from the buffer and clear.
    trigger();                          //Trigger ON.
    wait_ms(TRIGGER_PULSE_LENGTH_MS);   //Waiting when the trigger pulse is ON.
    trigger();                          //Trigger OFF.
}

int main(void)
{
    pc.attach(&interrupt);         //Call the interrupt, when any data are received from PC.
    trigger_output_positive = 0;   //Initial state of positive output in idle state.
    trigger_output_negative = 1;   //Initial state of negative output in idle state.
    trigger_indication = 0;        //Initial state of LED in idle state is OFF.
    while(1) {}                    //Infinity loop.
}