Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC12F675 A/D problem

Status
Not open for further replies.

hhhsssmmm

Member level 1
Joined
May 14, 2009
Messages
37
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,732
pic12f675

Hello

Im using PIC12F675 with internal OSC running @ 4MHz. Im using MikroC compiler version 8.2.

I wish to do a simple ADC experiment. Please see the enclosed diagram.

Before the PIC is placed in the circuit, the 10K pot is first trimmed to get steady 5.03V MAX output from the 15V DC supply which is a steady variable DC power supply. The 5V supply for the PIC is provided from 78L05 regulator IC. The ground reference is the same for the entire circuit.

Now when the 15V DC supply is at 15V MAX, the 10K pot output reads on volt meter as 5.03V. Similarly if the 15V DC supply is lowered to 10V, then the 10k pot output reads on the volt meter as 3.4V. I assume that these voltages are also seen by the PIC ADC pin (AN1) as shown in the diagram.

My program below simply checks to see that if the ADC voltage is equal or above 3.4V, then the PIC turns on the LED. If the voltage drops below 3.4V, then the LED remains off.

Sadly this is not happening and the LED remains on even if the DC supply is lowered down to 6V. Then the corresponding voltage out from the POT reads 2V. I have tried varying the "ADC_Result" variable value to a much higher vaule (800), but even that does not help and the same problem remains.

In the mikroC compiler, I have configured my project as....

CPD_OFF
CP_OFF
BODEN_OFF
MCLRE_OFF
PWRTE_OFF
WDT_OFF
INTRC_OSC_NOCLKOUT = ON

Below is my program. Please can someone help me to solve this problem.

Thank you
Haseeb

Code:
unsigned int  ADC_Result; //ADC result holding variable

void main(void)
{

    OSCCAL = 0x80; //trimming internal 4MHz OSC down to 'CENTER'

    GPIO = 0; //intiallize the port

    //intillize the Comparitor pins (GP0 to GP1) to Digital I/O pins
    CMCON  = 7;

    ADCON0.ADFM = 1;  //Result Right justified
    ADCON0.VCFG = 1;  // Voltage Reference is Vref
    
    // Channel 01 (AN1) SELECTED
    ADCON0.CHS1 = 0;
    ADCON0.CHS0 = 1;
    
    ADCON0.GO_DONE = 0; //Stop ADC Conversion
    ADCON0.ADON = 0;    //ADC Disabled
    
    //A/D Conversion Clock is FOSC/8
    ANSEL.ADCS2 = 0;
    ANSEL.ADCS1 = 0;
    ANSEL.ADCS0 = 1;
    
    //AN1 is Analog ..... rest all is Digital
    ANSEL.ANS0 = 0;
    ANSEL.ANS1 = 1;
    ANSEL.ANS2 = 0;
    ANSEL.ANS3 = 0;

    TRISIO.GP5 = 0; //LED output
    TRISIO.GP1 = 1; //Channel AN1 input

    INTCON = 0; //disable all interrupts
    PIE1 = 0;  //disable all peripheral interrupts

    ADCON0.ADON = 1; //ADC Enabled

    while(1) //loop forever
    {

        ADCON0.GO_DONE = 1; //Start ADC Conversion

        while (ADCON0.GO_DONE == 1);  //Wait until Conversion finishes

        ADC_Result = (ADRESH * 256) + ADRESL; //Merging High byte with low byte

        if(ADC_Result >= 696) // if we get 3.4V

           GPIO.GP5 = 1;  //LED on

        else

           GPIO.GP5 = 0;  //LED off

    }

} //end of main()
 

adc program for pic12f675

Hi,

Can not help with the C code, but while you are testing why not connect the 10K trimmer to the +5v rail of the Pic , so you are on the same power level and reduce the risk of blowing you pic up.
 

pic12f675 interrupts

Dear Haseeb,
I've thoroughly read your code, according to me there is nothing wrong but i suggest you to replace the 3 lines:-
ADCON0.GO_DONE = 1; //Start ADC Conversion
while (ADCON0.GO_DONE == 1); //Wait until Conversion finishes
ADC_Result = (ADRESH * 256) + ADRESL; //Merging High byte with low byte
by:
ADC_Result = ADC_Read(1);

I think this will solve your problem. Since there is nothing wrong with your code, sometimes the compiler makes some mistakes while preparing HEX file, now it is very difficult to identify these mistakes.
Once i wrote int i; inside void main() in the middle of the program, according to the compiler there was an error within this line, but when i shifted this line to outside void main() OR even inside void main() after {, compiler compiled the program and BUILD SUCCESSFULLY.
Hope it works for you,
Regards,
BABAR KHAN
 

pic 12f675

GPIO.GP5 = 1; //LED on
GPIO.GP5 = 0;

is incorrect

it should be
GPIO.f5 = 1; //LED on
GPIO.f5 = 0;


f is not case sensitive so you can use either f or F & remember this is only for MikroC.
When you use a compiler you should read its HELP file & sample programs, they help alot.
 

Al parecer el problema esta en la conversion de los 10-bits que entrega como resultado de la conversion, en la linea... ADC_Result =ADC_Read(1);



unsigned int ADC_Result; // ADC result

void main(void)
{

OSCCAL = 0x80;
CMCON = 7;



ADCON0.CHS1 = 0;
ADCON0.CHS0 = 1;// Channel selected



ANSEL.ANS0 = 0;
ANSEL.ANS1 = 1;
ANSEL.ANS2 = 0;
ANSEL.ANS3 = 0;


TRISIO.GP1 = 1; //Channel input
TRISIO.GP0 = 0; // output pin


while(1)
{

ADC_Result =ADC_Read(1);

if(ADC_Result > 100)
{
GPIO.GP0 = 1; //LED on
Delay_ms(10000);
}
if(ADC_Result < 100)
{
GPIO.GP0 = 0; //LED off
Delay_ms(1);
}

}

}
 

Hi hhhsssmmm,

This statement is wrong:

ADC_Result = (ADRESH * 256) + ADRESL; //Merging High byte with low byte

simply because when you multiply ADRESH by 256 it overflows. The correct way to do this is:

ADC_Result=(unsigned int)ADRESH;
ADC_Result<<=8;
ADC_Result|=(unsigned int)ADRESL;

Now you would get a 10bit result.
 

Hi hhhsssmmm,
I wrote this program in mikroC v8.2 and the circuit (according to your design provided) works fine.
Source code:
Code:
int ADCResult;

void main(){
     CMCON = 7;
     TRISIO = 10;
     GPIO = 0;
     ANSEL = 2;
     while (1){
           ADCResult = ADC_Read(1);
           if (ADCResult >= 696)
              GPIO.GP5 = 1;
           else
              GPIO.GP5 = 0;
              delay_ms(10);
     }
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top