Dependencies:   mbed

Committer:
leejarcher
Date:
Sat Jan 29 12:35:32 2011 +0000
Revision:
0:ee3bdb31da2f
So far works just!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leejarcher 0:ee3bdb31da2f 1 #include "mbed.h"
leejarcher 0:ee3bdb31da2f 2
leejarcher 0:ee3bdb31da2f 3 LocalFileSystem local("local");
leejarcher 0:ee3bdb31da2f 4 Serial pc(USBTX, USBRX);
leejarcher 0:ee3bdb31da2f 5 Ticker timer; //interrupt to generate sine wave
leejarcher 0:ee3bdb31da2f 6 DigitalOut low(LED1);
leejarcher 0:ee3bdb31da2f 7 DigitalOut oc(LED2);
leejarcher 0:ee3bdb31da2f 8 DigitalOut high(LED3);
leejarcher 0:ee3bdb31da2f 9 AnalogIn level(p20);
leejarcher 0:ee3bdb31da2f 10 AnalogOut sine_wave(p18);
leejarcher 0:ee3bdb31da2f 11 PwmOut wave(p26);
leejarcher 0:ee3bdb31da2f 12
leejarcher 0:ee3bdb31da2f 13 float pulse_freq;
leejarcher 0:ee3bdb31da2f 14 float pulse_period;
leejarcher 0:ee3bdb31da2f 15 float pulse_width;
leejarcher 0:ee3bdb31da2f 16 float sine_freq;
leejarcher 0:ee3bdb31da2f 17 float sine_period;
leejarcher 0:ee3bdb31da2f 18 float sine_amp;
leejarcher 0:ee3bdb31da2f 19 float sine_vals[19], *sp, *last; //setup array for sine values, a pointer to access the array and one for the end of the array
leejarcher 0:ee3bdb31da2f 20 void setup();
leejarcher 0:ee3bdb31da2f 21 void sine();
leejarcher 0:ee3bdb31da2f 22 void sine_table();
leejarcher 0:ee3bdb31da2f 23
leejarcher 0:ee3bdb31da2f 24 int main() {
leejarcher 0:ee3bdb31da2f 25 float a;
leejarcher 0:ee3bdb31da2f 26
leejarcher 0:ee3bdb31da2f 27 FILE *fp=fopen("/local/setup.ini", "r");
leejarcher 0:ee3bdb31da2f 28 if(!fp) {
leejarcher 0:ee3bdb31da2f 29 pulse_period=1; //default values if no .ini file
leejarcher 0:ee3bdb31da2f 30 pulse_width=0.5;
leejarcher 0:ee3bdb31da2f 31 sine_freq=1000;
leejarcher 0:ee3bdb31da2f 32 sine_amp=3.3; // peak to peak sine amplitude in V
leejarcher 0:ee3bdb31da2f 33 }
leejarcher 0:ee3bdb31da2f 34 else {
leejarcher 0:ee3bdb31da2f 35 fscanf(fp,"%f %f %f %f",&pulse_period, &pulse_width, &sine_freq, &sine_amp); //read values from .ini file
leejarcher 0:ee3bdb31da2f 36 fclose(fp);
leejarcher 0:ee3bdb31da2f 37 }
leejarcher 0:ee3bdb31da2f 38 wave.period(pulse_period);
leejarcher 0:ee3bdb31da2f 39 wave=pulse_width;
leejarcher 0:ee3bdb31da2f 40 sine_table(); //load sine values into table and start interrupt
leejarcher 0:ee3bdb31da2f 41
leejarcher 0:ee3bdb31da2f 42 while(1) { //main loop
leejarcher 0:ee3bdb31da2f 43 a=level;
leejarcher 0:ee3bdb31da2f 44 low=(a<0.24); // light low led if voltage less then 0.8V
leejarcher 0:ee3bdb31da2f 45 high=(a>0.61); // light high led if voltage greater than 2V
leejarcher 0:ee3bdb31da2f 46 oc=(a>=0.24 && a<=0.61); //light oc LED if open circuit or indeterminate level
leejarcher 0:ee3bdb31da2f 47 if (pc.readable()) setup(); //if PC keyboard pressed setup waveforms via pc
leejarcher 0:ee3bdb31da2f 48 }
leejarcher 0:ee3bdb31da2f 49 }
leejarcher 0:ee3bdb31da2f 50
leejarcher 0:ee3bdb31da2f 51 void setup() { //function to get waveform values from pc
leejarcher 0:ee3bdb31da2f 52 printf("\f\n Pulse Frequency (0.1-1000000Hz):- \n");
leejarcher 0:ee3bdb31da2f 53 scanf("%f",&pulse_freq);
leejarcher 0:ee3bdb31da2f 54 if (pulse_freq>1000000) pulse_freq=1000000;
leejarcher 0:ee3bdb31da2f 55 if (pulse_freq<0.1) pulse_freq=0.1;
leejarcher 0:ee3bdb31da2f 56 pulse_period=1/pulse_freq;
leejarcher 0:ee3bdb31da2f 57 printf("\n Pulse Width (0.1-0.9):- \n");
leejarcher 0:ee3bdb31da2f 58 scanf("%f",&pulse_width);
leejarcher 0:ee3bdb31da2f 59 if (pulse_width>0.9) pulse_width=0.9;
leejarcher 0:ee3bdb31da2f 60 if (pulse_width<0.1) pulse_width=0.1;
leejarcher 0:ee3bdb31da2f 61 printf("\n Sine Frequency (0.1-1000Hz):- \n");
leejarcher 0:ee3bdb31da2f 62 scanf("%f",&sine_freq);
leejarcher 0:ee3bdb31da2f 63 if (sine_freq>1000) sine_freq=1000;
leejarcher 0:ee3bdb31da2f 64 if (sine_freq<0.1) sine_freq=0.1;
leejarcher 0:ee3bdb31da2f 65 printf("\n Sine Amplitude (0-3.3V p/p):- \n");
leejarcher 0:ee3bdb31da2f 66 scanf("%f",&sine_amp);
leejarcher 0:ee3bdb31da2f 67 if (sine_amp>3.3) sine_amp=3.3;
leejarcher 0:ee3bdb31da2f 68 if (sine_amp<0) sine_amp=0;
leejarcher 0:ee3bdb31da2f 69 printf("\n\n Pulse Frequency %f Hz \n Pulse Width %f \n",pulse_freq, pulse_width);
leejarcher 0:ee3bdb31da2f 70 printf("\n Sine Frequency %f Hz \n Sine Amplitude %f V \n",sine_freq, sine_amp);
leejarcher 0:ee3bdb31da2f 71 FILE *fp=fopen("/local/setup.ini","w"); // save values on flash drive
leejarcher 0:ee3bdb31da2f 72 fprintf(fp,"%f %f %f %f",pulse_period,pulse_width,sine_freq,sine_amp);
leejarcher 0:ee3bdb31da2f 73 fclose(fp);
leejarcher 0:ee3bdb31da2f 74 wave.period(pulse_period); //start PWM output
leejarcher 0:ee3bdb31da2f 75 wave=pulse_width;
leejarcher 0:ee3bdb31da2f 76 sine_table(); // load new values into sine table and start interrupt
leejarcher 0:ee3bdb31da2f 77 }
leejarcher 0:ee3bdb31da2f 78
leejarcher 0:ee3bdb31da2f 79 void sine_table() { // function to load sine values into array and start interrupt
leejarcher 0:ee3bdb31da2f 80 float *p, i;
leejarcher 0:ee3bdb31da2f 81
leejarcher 0:ee3bdb31da2f 82 sine_amp=sine_amp/3.3; //convert from p/p voltage to percentage
leejarcher 0:ee3bdb31da2f 83 p=sine_vals; //set p to point to beginning of array
leejarcher 0:ee3bdb31da2f 84 sp=p; //set sp also
leejarcher 0:ee3bdb31da2f 85 for (i=0;i<3;i+=0.15707965) {
leejarcher 0:ee3bdb31da2f 86 *p=sine_amp*sin(i);
leejarcher 0:ee3bdb31da2f 87 p++;
leejarcher 0:ee3bdb31da2f 88 }
leejarcher 0:ee3bdb31da2f 89 last=p-1; //last element in array
leejarcher 0:ee3bdb31da2f 90
leejarcher 0:ee3bdb31da2f 91 sine_period=(1000000/sine_freq)/19; // period between calling sine routine
leejarcher 0:ee3bdb31da2f 92 timer.attach_us(&sine,sine_period); // start the interrupt
leejarcher 0:ee3bdb31da2f 93 }
leejarcher 0:ee3bdb31da2f 94
leejarcher 0:ee3bdb31da2f 95 void sine() { // function to output sine wave to analogue output
leejarcher 0:ee3bdb31da2f 96
leejarcher 0:ee3bdb31da2f 97 if (sp>last) sp=sine_vals; //if at end of array reset sp back to beginning
leejarcher 0:ee3bdb31da2f 98 sine_wave.write(*sp);
leejarcher 0:ee3bdb31da2f 99 sp++;
leejarcher 0:ee3bdb31da2f 100 }