Friday, January 11, 2013

Triangular wave generator in c

Here is the code to generate a triangular wave of 8 bit resolution
1:  void main()  
2:  {  
3:  unsigned char i;  
4:  while(1)  
5:  {       
6:       for(i=0;i<0xff;i++)  
7:            { /* Generate rising edge */  
8:            delay_ us(1);  
9:            PORTB= i; //data output to port B  
10:            }  
11:       for(i=0xfe;i>0x00;i--)   
12:            {/* Generate falling edge */  
13:            delay_ us(1);  
14:            PORTB = i; //data output to port B  
15:            }  
16:  }  
17:  }  

You can directly give the output to any port in which DAC is connected or you can use it for internal reference also.
This can be used to generate triangular wave in any microcontroller like AVR, 8051, PIC, ARM.
And triangular waves are also essential in doing modulation.

6 comments:

  1. Hi keyur ,

    Thanks for posting and sharing this information .




    Embedded Systems india

    ReplyDelete
  2. thank for this program i didn't knew that you can use hex values in the for loop
    but this post helped me thanks again i would also like to know about saw tooth generation

    ReplyDelete
    Replies
    1. I'm not going to be able to produce the correct syntax here but that first for loop would just need to be moved down an replaced with a for i=xfe; i = 0. that restarts the wave at 0. then get rid of the last for statement.

      Delete
  3. Thanks for giving this information

    ReplyDelete
  4. Thanks for your information. please can you tell how to wire up pic with DAC for this circuit?

    ReplyDelete