# Sine wave 50hz DAC PIC18f25k22 problem

Status
Not open for further replies.

#### dcom12

##### Junior Member level 1
Hi everyone , i want to generate sine wave 50hz with bult-in DAC module with PIC18f25k22 .This is my code in MikroC :

Code:
void InitTimer0(){
T0CON         = 0xC1;
TMR0L         = 0x24;
GIE_bit         = 1;
TMR0IE_bit         = 1;

}
void Interrupt(){
if (TMR0IF_bit){

TMR0IF_bit = 0;
TMR0L         = 0x24;

if(Sine_wave_step > 354){ Sine_wave_step = 4; } else {     Sine_wave_step++; }
VREFCON2 = sin_table[Sine_wave_step];
current = 0;

if(current_time1 == 20) {
current_millis++;
current_time1 = 0;
} else {
current_time1++;
}
}
}

void main() {
ANSELA  = 0;
ANSELB  = 0;
ANSELC  = 0x00;
C1ON_bit = 0;                                  // Disable comparators
C2ON_bit = 0;
INTCON2.RBPU =0;
LATB4_bit = 1;
LATB5_bit = 1;
LATB3_bit = 1;
LATB2_bit = 1;
osccon2.pllrdy = 1;
while(OSCCON2.PLLRDY != 1);

for (i = 4; i <=180; i++)
{
sin_table[i]=16+floor(15*sin(i * PI /180));
sin_table[i+180]=32-sin_table[i];
}

delay_ms(10);
InitTimer0();
delay_ms(1000);
Lcd_Init();
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt1);
Lcd_Out(2,1,txt2);
IntToStr(current_im,curr_im_ch);
IntToStr(current_pa,curr_pa_ch);
Lcd_Out(1,7,curr_im_ch);
Lcd_Out(2,8,curr_pa_ch);
VREFCON1 = 0b11110010;
VREFCON2 = 31;

while(1) {

}
}

This code generate the sine but is deformed . When i wrote sin_table steps from 0 and Sine_wave_step to start from 0 and end in 360 the sine is not right . What must be the right numbers and timer0 times to generate it ? And the amplitude is only 3 volts , i need 5 volts ?

- - - Updated - - -

16mhz crystal 64mghz cpu clock

#### KlausST

##### Super Moderator
Staff member
Hi,

You generate values from 4 to 180 degree, what about 0 to 4 degree?
Additionally you generatd every single degree, but use only every 4 degree, so you don't use 3 of 4 values.

Klaus

#### Vbase

##### Full Member level 6
Hi everyone , i want to generate sine wave 50hz with bult-in DAC module with PIC18f25k22 .This is my code in MikroC :

This code generate the sine but is deformed . When i wrote sin_table steps from 0 and Sine_wave_step to start from 0 and end in 360 the sine is not right . What must be the right numbers and timer0 times to generate it ? And the amplitude is only 3 volts , i need 5 volts ?

Try changing line for (i = 4; i <=180; i++) to for (i = 4; i <=180; i=i+4)

#### dcom12

##### Junior Member level 1
I solved this. Only amplitude is about 3 volts not 5 ?

#### Vbase

##### Full Member level 6
I solved this. Only amplitude is about 3 volts not 5 ?
I can't see the reason for 3V output in your code.
The output resistance of the DAC is high and the datasheet say that you must buffer the output with an op amp. Try to remove any load from the output and measure it with your oscilloscope probe on X10.

Last edited:

Status
Not open for further replies.

Replies
29
Views
14K
Replies
3
Views
2K
Replies
0
Views
1K
Replies
0
Views
3K
Replies
1
Views
3K