rmrps
Member level 2
![Member level 2](/styles/images/ranks/dlevel2.gif)
Hi Friends I am using dspic30f2010 microcontroller with 12.288Mhz i want to sense the ac voltage this is my code in that i am using timer interrupt for sense the analog input the problem is the devise get reset please give me the suggestion where to change the code also give me the suggestion some other methods to sense the ac voltage thanks in advance
int main(void)
{
TRISB=0xFF;
TRISE=0x00;
Lcd_Initialize() ;
ADCON1 = 0;
ADCON2 = 0;
ADCON3 = 0x9;
ADPCFG = 0x0;
ADCHS = 0x07;
ADCON1bits.ADON = 1;
R_Ph_Rms_Vol=0;
Phase_interrupt =0;
T1CON=0;
TMR1=0;
PR1=16000;
IFS0bits.T1IF=0;
IPC0bits.T1IP=4;
IEC0bits.T1IE=1;
T1CONbits.TCS = 0;
T1CON=0x8000;
while(1)
{
if(Time_Update==1)
{
for(R_i=0;R_i<20;R_i++)
{
R_Ph_Voltage[R_i] = R_Ph_Voltage[R_i]-512;
R_Ph_Ana_Vol[R_i] = (R_Ph_Voltage[R_i]*5.0)/1023.0;
R_Ph_Ana_Vol[R_i] = R_Ph_Ana_Vol[R_i] * R_Ph_Ana_Vol[R_i];
}
R_Ph_Rms_Vol = 0;
for(R_i=0;R_i<20;R_i++)
{
R_Ph_Rms_Vol = R_Ph_Rms_Vol + R_Ph_Ana_Vol[R_i];
}
R_Phase = R_Ph_Rms_Vol; R_Phase = sqrt(R_Phase/20.0);
R_Phase = R_Phase *284.00;
Phase1 = (int)R_Phase;
R_Ph_Rms_Vol=0;
Phase_interrupt =0;
k=Phase1;
l=k/1000; Status1[6] = l + 48; k=k%1000;
l=k/100; Status1[7] = l + 48; k=k%100;
l=k/10; Status1[8] = l + 48; k=k%10;
Status1[9] = k + 48;
Lcd_Command(0x80);
for(l=0;l<=18;l++)
{
Lcd_Send_data(Status1[l]);
}
Time_Update = Time_Update & 0x01;
}
}
return (0);
}
void __attribute__((interrupt,auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0;
if(Phase_interrupt!=20)
{
R_Ph_Voltage[Phase_interrupt] = Read_Adc(0);
Phase_interrupt++;
}
}
int Read_Adc(int channel)
{
unsigned char Delay=0;
if(channel > 0x000F) return(0);
ADCHS = channel;
ADCON1bits.SAMP = 1;
for(Delay = 0; Delay < 5; Delay++);
IFS0bits.ADIF = 0;
ADCON1bits.SAMP = 0;
while(!IFS0bits.ADIF);
return(ADCBUF0);
}
int main(void)
{
TRISB=0xFF;
TRISE=0x00;
Lcd_Initialize() ;
ADCON1 = 0;
ADCON2 = 0;
ADCON3 = 0x9;
ADPCFG = 0x0;
ADCHS = 0x07;
ADCON1bits.ADON = 1;
R_Ph_Rms_Vol=0;
Phase_interrupt =0;
T1CON=0;
TMR1=0;
PR1=16000;
IFS0bits.T1IF=0;
IPC0bits.T1IP=4;
IEC0bits.T1IE=1;
T1CONbits.TCS = 0;
T1CON=0x8000;
while(1)
{
if(Time_Update==1)
{
for(R_i=0;R_i<20;R_i++)
{
R_Ph_Voltage[R_i] = R_Ph_Voltage[R_i]-512;
R_Ph_Ana_Vol[R_i] = (R_Ph_Voltage[R_i]*5.0)/1023.0;
R_Ph_Ana_Vol[R_i] = R_Ph_Ana_Vol[R_i] * R_Ph_Ana_Vol[R_i];
}
R_Ph_Rms_Vol = 0;
for(R_i=0;R_i<20;R_i++)
{
R_Ph_Rms_Vol = R_Ph_Rms_Vol + R_Ph_Ana_Vol[R_i];
}
R_Phase = R_Ph_Rms_Vol; R_Phase = sqrt(R_Phase/20.0);
R_Phase = R_Phase *284.00;
Phase1 = (int)R_Phase;
R_Ph_Rms_Vol=0;
Phase_interrupt =0;
k=Phase1;
l=k/1000; Status1[6] = l + 48; k=k%1000;
l=k/100; Status1[7] = l + 48; k=k%100;
l=k/10; Status1[8] = l + 48; k=k%10;
Status1[9] = k + 48;
Lcd_Command(0x80);
for(l=0;l<=18;l++)
{
Lcd_Send_data(Status1[l]);
}
Time_Update = Time_Update & 0x01;
}
}
return (0);
}
void __attribute__((interrupt,auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0;
if(Phase_interrupt!=20)
{
R_Ph_Voltage[Phase_interrupt] = Read_Adc(0);
Phase_interrupt++;
}
}
int Read_Adc(int channel)
{
unsigned char Delay=0;
if(channel > 0x000F) return(0);
ADCHS = channel;
ADCON1bits.SAMP = 1;
for(Delay = 0; Delay < 5; Delay++);
IFS0bits.ADIF = 0;
ADCON1bits.SAMP = 0;
while(!IFS0bits.ADIF);
return(ADCBUF0);
}
Last edited: