Function Generator and Oscilloscope

Dependencies:   C12832_lcd DebounceIn mbed

main.cpp

Committer:
trichards1138
Date:
2014-05-24
Revision:
2:218abf68fe93
Parent:
1:e31325194990
Child:
3:920cc6573be0

File content as of revision 2:218abf68fe93:

#include "mbed.h"
#include "C12832_lcd.h"
#include "DebounceIn.h"
//#include "rtos.h"

Serial pc(USBTX, USBRX);
C12832_LCD LCD;
AnalogIn Ain(p17);
AnalogOut Aout(p18);
AnalogIn pot1(p19);
AnalogIn pot2(p20);
DigitalIn up(p15);
DigitalIn down(p12);
DigitalIn left(p13);
DigitalIn right(p16);
BusIn joy(p15,p12,p13,p16);
DigitalIn OnOff(p14);
PwmOut testout(p21);
PwmOut tled(LED1);
/*
DebounceIn OnOff(p14);
DebounceIn up(p15);
DebounceIn down(p12);
DebounceIn left(p13);
DebounceIn right(p16);
*/


// SineWave thread - is initiated when the user selects sinewave output
// print a sin function in a small window
// the value of pot 1 changes the period of the sine wave
// The value of pot 2 changes the height of the sine wave
void sineWave(void)
{
    double i, vert, horiz, outval;
    while(!OnOff) {       // thread loop
       	horiz = pot1.read();  // get value of pot 1
       	vert = pot2.read();	// scale the height of wave
       	for (i=0; i<2; i=i+0.05) {
       		outval = 0.5 + 0.5*vert*sin(i*3.14159);
       		Aout.write(outval);  // Compute the sine value, + half the range
       		wait_ms(0.0002*horiz);          	         // Controls the sine wave period
       	}
	}
}

// Thread squareWave - called if user selects squarewave
// pot1 controls the width of pulse
// pot2 controls the height of pulse
void squareWave(void)
{
	float height=0;
	unsigned int width=0, cnt=0;
    while(!OnOff) {       // thread loop
   		if( cnt == 1000 ) {
       		width = pot1.read_u16();  // get value of
       		height = pot2.read();	// scale the height of wave
       		cnt = 0;
       	}
      	Aout.write(height);
       	wait_ms(0.1*width);
       	Aout.write(0);
       	wait_ms(0.1*width);
       	cnt++;
    }
}

// Thread flatdc - called if user selects dc sig
// pot2 controls the height of dc signal
void flatdc(void)
{
    while(!OnOff) {       // thread loop
       	Aout.write(pot2.read());	// scale the height of wave
       	wait_ms(10);
    }
}

void sawTooth(void)
{
}
	
int main()
{
    bool do_sine=false, do_saw=false;
    bool do_dc=false, do_square=false;
    pc.baud(19200);
    while(1)  {
		if( up ) {
			do_saw=true;
			do_sine=false;
			do_dc=false;
			do_square=false;
		}
		if( down ) {
			do_saw=false;
			do_sine=false;
			do_dc=true;
			do_square=false;
		}
		if( right ) {
			do_saw=false;
			do_sine=false;
			do_dc=false;
			do_square=true;
		}
		if( left ) {
			do_saw=false;
			do_sine=true;
			do_dc=false;
			do_square=false;
		}
 
 		if( OnOff ) {
 			if( do_saw )
				sawTooth();
			else if( do_dc )
				flatdc();
			else if( do_square )
				squareWave();
			else if( do_sine )
				sineWave();
			else
				sineWave();
		}
	}
}