Saturday, May 1, 2010

--> Creating a Sine Wave with a PIC16F84


  • I have been working on creating AX.25 packet with a PIC chip and while researching how to do it, I came across somthing that Byon N6BG of www.byonics.com used to do this, he used a resistor ladder network to create sine waves. In the diagram/schematic below, you can see how the ladder is wired to the PIC. It's a 4 bit ladder network, using 4 resistor values, each one about twice the value of the last. With 4 different values, you can theoretically have 16 different voltage levels from 0 VDC to 5 VDC. Since the PIC can not produce negative voltage levels for the down side of the sine, the middle of the voltage range is used as the center voltage, being 2.5 VDC. So, the sine would go from 2.5 VDC up to 5 VDC, then back down to 2.5 VDC and then down to 0 VDC and repeat the pattern to create the sine wave. The final voltage out can be run through a small value capacitor to help smooth out the signal. It will still have somewhat of a stair step to it, but it will be smoothed out some by the cap.



  • Theoretically the voltages would range from 0 to 5, since we are using resistors in parallel, the value at any given point will never be higher than the smallest resistor in the ladder. So, we are left with a voltage range of 0 to 3.20 VDC. This can still form a sine wave, just not with the amplitude of the full +/- 5 VDC.




  • Click here to view the source code to this project.



  •  


     


  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  •  

  • The table below shows the output voltage levels from each of the 16 possible resistor combinations. The right most bit represents Pin 6. The other pins go in sucession with the binary bits 2, 3 and 4. It also shows what angle of degree on the sine wave that the voltage level should fall. When I finish getting the voltages, I enter them into the table. The graph to the left of the table shows where the voltage levels would fall on the sine wave.





  • Voltage Out for BIT combinations
    Sine D AnglePins 9 8 7 6Vout
    27000000
    258.75, 281.2510000.48
    247.50, 292.5001000.87
    236.25, 303.7511001.21
    225.00, 315.0000101.55
    213.75, 326.2510101.79
    202.50, 337.5001101.99
    191.25, 348.7511102.17
    11.25, 168.7500012.51
    22.50, 157.5010012.63
    33.75, 146.2501012.75
    45.00, 135.0011012.85
    56.25, 123.7500112.96
    67.50, 112.5010113.05
    78.75, 101.2501113.12
    9011113.20


  • Since the center voltage falls in-between the 8th and 9th voltage levels, I did some quick math to show what the calculated center voltage of the sine should be: 2.51 - 2.17 = 0.34 Then 0.34 / 2 = 0.17 And Then 2.17 + 0.17 = 2.34 Leaving a center voltage of 2.34 VDC at 0, 180 and 360 degrees.

    ----------------------------------




  • 1 comment:

    Unknown said...

    You have the best blog network about microcontrollers. very good theoretical information and excellent explanations.

    Post a Comment