0% found this document useful (0 votes)
5 views3 pages

DAC Code for Sine and Sawtooth Waves

Uploaded by

prakashkmtpp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

DAC Code for Sine and Sawtooth Waves

Uploaded by

prakashkmtpp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Modified Code

DAC (Sine wave):

#include <REG51.h>
#define HIGH 1
#define LOW 0

sbit SCL = P2^0;


sbit SDA = P2^1;

void I2C_Start();
void I2C_Write(int data);
void Delay(int len);

unsigned char SineCoef[50] = { 0x80,0x8f,0x9f,0xae,0xbd,0xca,0xd6,0xe1,0xeb,0xf2,


0xf8,0xfc,0xfe,0xfe,0xfc,0xf8,0xf2,0xeb,0xe1,0xd6,
0xca,0xbd,0xae,0x9f,0x8f,0x80,0x71,0x61,0x52,0x43,
0x36,0x2a,0x1f,0x15,0x0e,0x08,0x04,0x02,0x02,0x04,
0x08,0x0e,0x15,0x1f,0x2a,0x36,0x43,0x52,0x61,0x71
};
// output digital = 80H+ 7FH*sin(angi),
//where angi =i*360/50(range of i=0 to 49 and range //of angi =0 to 49*360/50 for 50
samples).
void main()
{
int i;
I2C_Start();
I2C_Write(0x9C); // DAC address
I2C_Write(0x40); // DAC control byte
while(HIGH)
{
for(i = 0; i < 50; i++)
{
I2C_Write(SineCoef[i]);
}
}
}
void I2C_Start()
{
SDA = HIGH;
SCL = HIGH;
SDA = LOW;
SCL = LOW;
}
void I2C_Write(int data)
{
unsigned char mask;
for(mask = 0x80; mask != 0; mask >>= 1)
{
if(data & mask) SDA = HIGH;
else SDA = LOW;

SCL = HIGH;
SCL = LOW;
}

SDA = HIGH;
while(SDA); // Wait for ACK
SCL = HIGH;
SCL = LOW;
}

void Delay(int len)


{
int i, j;
for(i = 0; i < len; i++)
for(j = 0; j < i; j++);
}

DAC (Sawtooth wave):

#include<REG51.h>
#include<stdio.h>
sbit SCL=P2^0;
sbit SDA=P2^1;
void start();
void write(int dat);
void main()
{
int i;
while(1)
{
start();
write(0x9C);// Address
write(0x40);//Control byte
for(i=0xFF;i>0;i--)
{
write(i);
}
}}

void start()
{
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
void write(int dat)
{
unsigned char index;
for(index=0x80;index!=0;index>>=1)
{
if(dat&index)
SDA=1;
else
SDA=0;
SCL=1;
SCL=0;
}
//For Acknowledgement
SDA=1;
while(SDA);
SCL=1;
SCL=0;
}

You might also like