Вы находитесь на странице: 1из 4

c 


- Manish Man Shrestha
The following program generates 1KHz sine wave. It makes use of DAC0808 and
timer 0 interrupt. Also a led is blinked continuously.
The sine wave can be observed from the oscilloscope.
  
/****************************************
Author:
Manish Man Shrestha
Program:
Sine Wave Generator
*****************************************/
#include <at89x51.h>
#include "math.h"
#define PI 3.1415
#define dac_data P1
#define TIMERHI 0xB1 //20ms @12 MHZ
#define TIMERLO 0xE0
void blink();
void sine_wave();
void sine_value();
sbit led=P3^5;

char dat[50];
/*************************************
This interrupt is call in every 20 ms
***************************************/
void timer0_isr() interrupt 1
{

TR0=0;
sine_wave();
TH0 = TIMERHI;
TL0 = TIMERLO;
TR0 = 1;
}
/****************************
Function name:
sine_value
Input paramerter:
void
Return value:
null
Brief Description:
This function compute the digital value for sine wave of 1KHZ frequency
and store the value in the global variable dat[]
****************************/
void sine_value()
{
int idata dat1[50];
int i,f,temp;
float t = 0;
f = 1000;
//in Hz
for(i=0; i<50; i++)
{
dat1[i] = 10 * (2.4 * sin(2 * PI * f * t/1000)) * 51;
t = t + 20; //t in ms
}
for(i=0;i<50;i++)
{
temp=dat1[i]%10;
if(temp>4)
{
dat1[i]=dat1[i]+10;
}
}
for(i=0;i<50;i++)
{
dat1[i]=dat1[i]/10;
}
for(i=0;i<50;i++)
{
dat[i]=dat1[i];
}

}
void main()
{
int i;
sine_value();
EA=1;
while(1)
{
blink();
for(i = 0; i<10000; i++);
blink();
for(i = 0; i<10000; i++);
}
}
/******************************************************
Function name:
sine_wave
Input parameter:
void
Return value:
null
Brief description:
This function outputs the 8 bit digital value to the DAC every
time it is called
*******************************************************/
void sine_wave()
{
static int i = 0, flag=0;
if(flag==0)
{
dac_data = dat[i];
i++;
if(i == 50)
{
flag=1;
}
}
if(flag==1)
{
dac_data = ~dat[--i];
if(i==0)

{
flag=0;
}
}
}
void blink()
{
led = ~led;
}

Вам также может понравиться