Function Generator and Oscilloscope

Dependencies:   C12832_lcd DebounceIn mbed

Revision:
0:dfc39b05ea05
Child:
1:e31325194990
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 22 18:43:41 2014 +0000
@@ -0,0 +1,216 @@
+#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);
+*/
+
+bool  en_saw=false, en_dc=false, en_square=false, en_sine=false, en_display=false;
+
+// 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(const void *arg)
+{
+    double i, vert, horiz, outval;
+    while(true) {       // thread loop
+    	while( en_sine ) {
+        	horiz = pot1.read();  // get value of pot 1
+        	vert = pot2.read();	// scale the height of wave
+        //	pc.printf("horiz=%f, vert=%f\r\n", horiz, vert);
+        	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
+           		//pc.printf("Output is: %f\r\n", outval);
+           		Thread::wait(0.00005);          	         // Controls the sine wave period
+        	}
+        }
+        Thread::wait(200);
+    }
+}
+
+// Thread squareWave - called if user selects squarewave
+// pot1 controls the width of pulse
+// pot2 controls the height of pulse
+void squareWave(const void *arg)
+{
+	unsigned int i;
+	float width, height, w;
+    while(true) {       // thread loop
+    	while( en_square ) {
+        	width = pot1.read_u16();  // get value of
+        	w = width*2;
+        	height = pot2.read();	// scale the height of wave
+        	for (i=0; i<w; i++) {
+            	if( i >= width ) 
+            		Aout.write(0);
+            	else
+            		Aout.write(height);
+        	}
+        	Thread::wait(5);
+        }
+        Thread::wait(200);
+    }
+}
+
+// Thread flatdc - called if user selects dc sig
+// pot2 controls the height of dc signal
+void flatdc(const void *arg)
+{
+    while(true) {       // thread loop
+    	while( en_dc ) {
+         	Aout.write(pot2.read());	// scale the height of wave
+        	Thread::wait(10);
+        }
+        Thread::wait(200);
+    }
+}
+	
+void displayWave(const void *arg)
+{
+	unsigned int i;
+    unsigned int h = LCD.height(), w = LCD.width(), hhh;
+    unsigned int time_base = 0.5;
+    float trigger, curstate;
+    while(true) {       // thread loop
+    	while( en_display ) {
+        	for (i=0; i<w; i++) {
+            	hhh = (int)((h-(h*Ain.read()))+4);
+            	LCD.pixel(i, hhh ,1);           // print pixel
+            	Thread::wait(time_base);
+        	}
+        	LCD.copy_to_lcd();  // LCD.pixel does not update the lcd
+        	Thread::wait(20);  
+        	LCD.cls();
+        	LCD.rect(0,0,w,h,1);
+        	trigger = pot1.read();
+        	while( Ain.read() < trigger ) Thread::wait(0.5);
+        	if( false ) { //en_square ) {
+        		while( Ain.read() > 0.2 ) Thread::wait(0.5);
+        		while( Ain.read() < trigger ) Thread::wait(0.5);
+        	}
+        }
+        LCD.cls();
+    }
+}
+	
+	
+int main()
+{
+    bool do_sinewave, do_squarewave, do_sawtooth, do_dc;
+    pc.baud(19200);
+    //testout.period_ms(6);
+    //testout.write(0.5);
+    testout.period_ms(127);
+    testout.pulsewidth_ms(127*pot2.read());
+    en_display = true;
+    Thread display(displayWave);
+ //	Thread square(squareWave);
+ 	//Thread saw(sawtooth);
+ //	Thread dc(flatdc); 
+ //	Thread sine(sineWave);
+ 	while(1) {
+ 		//tled = testout;
+ 		testout.pulsewidth_ms(127*pot2.read());
+ 		Thread::wait(500);
+ 	}
+    while(1)  {
+    	while( !OnOff ) { 
+    		//pc.printf("havent selected yet\r\n"); 
+    		if( up ) {
+    			do_sinewave = false;
+    			do_squarewave = false;
+    			do_sawtooth = true;
+    			do_dc = false;
+    		}
+    		if( down ) {
+    			do_sinewave = false;
+    			do_squarewave = false;
+    			do_sawtooth = false;
+    			do_dc = true;
+    		}
+    		if( left ) {
+    			do_sinewave = true;
+    			do_squarewave = false;
+    			do_sawtooth = false;
+    			do_dc = false;
+    		}
+    		if( right ) {
+    			do_sinewave = false;
+    			do_squarewave = true;
+    			do_sawtooth = false;
+    			do_dc = false;
+    		}
+    		Thread::wait(20);
+    	}
+     	if( do_squarewave ) {
+     		pc.printf("I selected square\r\n"); 
+    		en_square = true;
+ 		}
+ 		else if( do_sawtooth ) {
+ 			pc.printf("I selected saw\r\n"); 
+ 			en_saw = true;
+ 		}
+ 		else if( do_dc ) {
+ 			pc.printf("I selected DC\r\n"); 
+ 			en_dc = true;
+ 		}
+ 		else {
+ 			pc.printf("I selected sine\r\n"); 
+ 			en_sine = true;
+ 		}
+ 		en_display = true;
+ 		while( OnOff ) {
+ 		//	pc.printf("In first\r\n");
+ 			Thread::wait(200);
+ 		}
+ 		while( !OnOff ) {
+ 		//	pc.printf("In Second\r\n");
+ 			Thread::wait(200);
+ 		}
+ 		while( OnOff ) {
+ 		//	pc.printf("In Third\r\n");
+ 			Thread::wait(100);
+ 		}
+ 		if( do_squarewave ) {
+     		pc.printf("I terminated square\r\n"); 
+    		en_square = false;
+ 		}
+ 		else if( do_sawtooth ) {
+ 			pc.printf("I terminated saw\r\n"); 
+ 			en_saw = false;
+ 		}
+ 		else if( do_dc ) {
+ 			pc.printf("I terminated DC\r\n"); 
+ 			en_dc = false; 
+ 		}
+ 		else {
+ 			pc.printf("I terminated sine\r\n"); 
+ 			en_sine = false;		
+ 		}
+ 		en_display=false;
+ 		LCD.cls();
+    }
+}
+