Saturday, December 16, 2017

5. Interfacing DAC

PartB
5.Interface a DAC and generate Triangular and Square waveforms.
Theory:


Explanation:

This C code explains how Sine Wave [unipolar] is generated.
Let's find hex values for sine function which is given by  Y = A+Asin(θ)
where, A=1.25V
because we are generating unipolar sinewave let the swing be from 0V to 2.5V and hence 2.5/2= 1.25V is the mid value.
we can assign 0v with 0x00, mid value 1.25V with 0x7F and 2.5V with 0XFF.
considering 48 samples to construct one full cycle of sine wave
each sample differ with its adjacent sample by an angle of 360/48 = 7.5 degree/sample.

Example:
for θ=7.5'
Y = A + Asin(θ)
    = 1.25 + 1.25sin(7.5)
    = 1.41315V

for 2.5V    -----------------> 0xff
then 1.41315V    --------------->    ?
x=(255  *   1.41315)/2.5
  =143.57 =144=(90)h

for θ=15'
Y = A + Asin(θ)
    = 1.25 + 1.25sin(15)
    = 1.5735V
                                for 2.5V    -----------------> 0xff
then 1.5735V    --------------->    0xA1

similarly, after calculating for all 48 sample values form an array of those values.



#include <LPC17xx.H>

int count=0,sinevalue,value;
unsigned char sine_tab[49]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,
      0xFF,0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,
      0x80,0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,
      0x00,0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80};

int main(void)
{
    LPC_PINCON->PINSEL0 &= 0xFF0000FF ;// Configure P0.0 to P0.15 as GPIO
    LPC_GPIO0->FIODIR  |= 0x00000FF0 ;
count = 0;
while(1)
{
for(count=0;count<48;count++)
{
sinevalue = sine_tab[count];        
value= 0x00000FF0 & (sinevalue << 4);
LPC_GPIO0->FIOPIN = value;
}
}
}

*****************************triangular wave****************************************

#include <LPC17xx.H>

int main ()
{
unsigned long int temp=0x00000000;
unsigned int i=0;

        LPC_PINCON->PINSEL0 &= 0xFF0000FF ; // Configure P0.4 to P0.11 as GPIO
LPC_GPIO0->FIODIR  |= 0x00000FF0 ;

    while(1)
    {
    //output 0 to FE
        for(i=0;i!=0xFF;i++)    //keep on incrementing value from 00 till FF in step of 1
        {
        temp=i;
        temp = temp << 4;
        LPC_GPIO0->FIOPIN = temp;
        }
        // output FF to 1
        for(i=0xFF; i!=0;i--) //keep on decrementing value from FF till 00 in step of 1
        {
        temp=i;
        temp = temp << 4;
        LPC_GPIO0->FIOPIN = temp;
        }
}//End of while(1)
}//End of main()

*********************************square waveform**********************************
#include <LPC17xx.H>

void delay(void);

int main ()
{
    LPC_PINCON->PINSEL0 &= 0xFF0000FF ; // Configure P0.4 to P0.11 as GPIO
    LPC_GPIO0->FIODIR  |= 0x00000FF0 ;

while(1)
    {
    LPC_GPIO0->FIOPIN  = 0x00000FF0 ;
        delay();
        LPC_GPIO0->FIOCLR  = 0x00000FF0 ;
        delay();
    }
}

void delay(void)
{
unsigned int i=0;
    for(i=0;i<=9500;i++);
}
*********************************************************************************





Download:DAC to generate triangular and square waveforms

2 comments:

lab record

TECHNICAL SPECIFICATIONS of LPC1768 (15-16 of ARMCTXM3) ******************************************************************************...