This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Function to generate Triangle wave form

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 */
		}
	}
}

Parents
  • 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

Reply
  • 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

Children