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.

Ramp wave generator in c

Here is a simple ramp wave generator you can directly give data to DAC or use it for your internal reference...


1:  //Ramp wave generating in c   
2:  Void main()  
3:  {  
4:       Int I;  
5:       I=0;  
6:       While(1)  
7:       {  
8:            I++;  
9:            delay_ms(1)//this count decides the frequency   
10:            if(I>=256) // this count decides the amplitude  
11:            {  
12:                 I=0;  
13:            }  
14:       }  
15:  }