Greeting all, I'm trying to write a simulate function to generate triangle wave form to test on Keil IDE, but I could not make it. I've read the code examples in Simulation section for sine, square and saw tooth, but could not figure out how to write triangle function. Can some one spend some time to help me to go through this. I'm novice in C, so please bear with me. Any inputs will be greatly appreciated. Have a great day. PS: With this function I can generate saw tooth wave form but I can't change or set frequency.
signal void Adc_0 () { float volts; float limit; limit = 5.0; while (1) /* forever */ { volts = 0; while (volts < limit) { ain0 = volts; /* analog input-0 */ swatch (0.0002); /* 200us */ volts += 0.1; /* increase voltage */ } volts = limit; while (volts > 0.0) { ain0 = volts; swatch (0.0002); /* 200us */ volts -= 0.1; /* decrease voltage */ } } }
I'm novice in C, so please bear with me. I'll gladly bear with you if you will stop the excessive use of floats. to generate triangle wave form ... With this function I can generate saw tooth wave form but I can't change or set frequency. as far as I can see it is triangle only swatch (0.0002); /* 200us */ a) this is where you would change the frequency, the frequency is ~ 1/(2* delay * steps) b) using a float, it will even be difficult to make the delay (I guess swatch 1s a delay routine) as short as 200uS c) a delay in C will NEVER be reproducable, use assembler for your delay routine. do something like this ;; delay routine, the parameter is how many multiples of 10uS the delay is to be. swatch (20); // 200 us Erik
Dear Erik, Thank you for your prompt reply. The code I used, primely is modified from the examples from Keil webpage. They use a lots of float variable there. By looking at the wave form, the triagle s just a little different from sine wave, and the sine wave is produced by the code below. I think I can modify the code to make triagle, but it din't work. Here is the sine wave function from Keil:
signal void Ain_0_Sin () { float volts; // peak-to-peak voltage float frequency; // output frequency in Hz float offset; // voltage offset float duration; // duration in Seconds float val; long i, end; volts = 2.5; offset = 2.5; frequency = 60; duration = 0.1; printf ("Sine Wave Signal on AD Channel 0.\n"); end = (duration * 1000000); for (i = 0 ; i < end; i++) { val = __sin (frequency * (((float) STATES) / CLOCK) * 2 * 3.1415926); ain0 = (val * volts) + offset; // swatch (0.00001); twatch (1); // in 10 uSec steps } }
swatch (1);
twatch (1);
signal void Ain_0_Tri (void) { float volts; // peak-to-peak volatage float frequency; // output frequency in Hz float offset; // volatge offset float duration; // duration in Seconds float val; long i, end, steps; volts = 4.9; offset = 0.1; frequency = 60; duration = 0.2; printf ("Tri Signal on AD Channel 0.\n"); steps = (100000 * (1/frequency)); end = (duration * 1000000); for (i = 0 ; i < end; i++) { val = (i % (steps)) / (1/(2*frequency)); ain0 = (val * volts) + offset; twatch (10); // in 10 uSec steps } }