Sm.r.Tadayoni
Junior Member level 1
hi every body.
i have a problem about Analog to digital Processing in PIC18f458.
when i want to convert , the result is wrong, and its not work true.
this is my Code :
void set_registers(void)
{
// *********************** A/D Module **************
ADCON0bits.ADON = 1; // AD ON
ADCON1bits.ADCS2 = 1; // A/D Clock Source F_OSC/4
ADCON0bits.ADCS1 = 0;
ADCON0bits.ADCS0 = 0;
ADCON0bits.CHS2 = 0; // AD Port
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS0 = 0;
ADCON0bits.GO_DONE = 1; // AD GO_DONE
ADCON1bits.ADFM = 1; // Justify
ADCON1bits.PCFG3 = 1; // PORTA --> DDDDA;
ADCON1bits.PCFG2 = 1;
ADCON1bits.PCFG1 = 1;
ADCON1bits.PCFG0 = 0;
}
unsigned float AtoD_Load(void)
{ unsigned float v_out;
unsigned int v_AD;
ADCON0bits.GO_DONE = 1;
while (ADCON0bits.GO_DONE);
ADCON0bits.GO_DONE = 0;
v_AD = ADRESH * 256 + ADRESL;
v_out = (v_AD * v_max) / 1024;
return v_out;
}
void filtering(unsigned float v_disired)
{ unsigned float dv, v_out;
TRISAbits.TRISA0 = 1;
v_out = AtoD_Load();
dv = v_out - v_disired;
if (v_out != v_disired) { while (v_out < v_disired) { CCPR1L++;
v_out = AtoD_Load();
}
while (v_out > v_disired)
{ CCPR1L--;
v_out = AtoD_Load();
}
}
}
and this is my PWM Code :
//*************************** PWM Module *******************
void PWM_Signal(unsigned float v_disired)
{ unsigned float dutycycle;
unsigned char n;
TRISCbits.TRISC2 = 0;
CCP1CONbits.CCP1M3 = 1; //PWM mode
CCP1CONbits.CCP1M2 = 1;
T2CONbits.T2CKPS1 = 0; // Timer2 : prescaler, 1:1
T2CONbits.T2CKPS0 = 0;
PR2 = 255;
dutycycle = (v_disired * 1024)/v_max;
CCPR1L = dutycycle/4; // PWM Dusty Cycle
n = dutycycle/256;
CCP1CONbits.DC1B1 = /2;
CCP1CONbits.DC1B0 = %2;
T2CONbits.TMR2ON = 1; // Timer2 ON
filtering(v_disired);
}
what should i do?
Thanks.
i have a problem about Analog to digital Processing in PIC18f458.
when i want to convert , the result is wrong, and its not work true.
this is my Code :
void set_registers(void)
{
// *********************** A/D Module **************
ADCON0bits.ADON = 1; // AD ON
ADCON1bits.ADCS2 = 1; // A/D Clock Source F_OSC/4
ADCON0bits.ADCS1 = 0;
ADCON0bits.ADCS0 = 0;
ADCON0bits.CHS2 = 0; // AD Port
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS0 = 0;
ADCON0bits.GO_DONE = 1; // AD GO_DONE
ADCON1bits.ADFM = 1; // Justify
ADCON1bits.PCFG3 = 1; // PORTA --> DDDDA;
ADCON1bits.PCFG2 = 1;
ADCON1bits.PCFG1 = 1;
ADCON1bits.PCFG0 = 0;
}
unsigned float AtoD_Load(void)
{ unsigned float v_out;
unsigned int v_AD;
ADCON0bits.GO_DONE = 1;
while (ADCON0bits.GO_DONE);
ADCON0bits.GO_DONE = 0;
v_AD = ADRESH * 256 + ADRESL;
v_out = (v_AD * v_max) / 1024;
return v_out;
}
void filtering(unsigned float v_disired)
{ unsigned float dv, v_out;
TRISAbits.TRISA0 = 1;
v_out = AtoD_Load();
dv = v_out - v_disired;
if (v_out != v_disired) { while (v_out < v_disired) { CCPR1L++;
v_out = AtoD_Load();
}
while (v_out > v_disired)
{ CCPR1L--;
v_out = AtoD_Load();
}
}
}
and this is my PWM Code :
//*************************** PWM Module *******************
void PWM_Signal(unsigned float v_disired)
{ unsigned float dutycycle;
unsigned char n;
TRISCbits.TRISC2 = 0;
CCP1CONbits.CCP1M3 = 1; //PWM mode
CCP1CONbits.CCP1M2 = 1;
T2CONbits.T2CKPS1 = 0; // Timer2 : prescaler, 1:1
T2CONbits.T2CKPS0 = 0;
PR2 = 255;
dutycycle = (v_disired * 1024)/v_max;
CCPR1L = dutycycle/4; // PWM Dusty Cycle
n = dutycycle/256;
CCP1CONbits.DC1B1 = /2;
CCP1CONbits.DC1B0 = %2;
T2CONbits.TMR2ON = 1; // Timer2 ON
filtering(v_disired);
}
what should i do?
Thanks.