# [PIC]Issues with PIC16F688 code

Status
Not open for further replies.

#### sachsoni

##### Newbie level 4
We wrote the code for PIC 16F688 and this is using for controlling DC/AC load as per WattHour consumption. We try to restrict the usage of load energy from battery. We sense the solar/battery voltage and solar/battery current and try to restrict the energy usage as per our requirement.

In this code the IC 16F688 will not detect the analog calculation and not providing corresponding output. We are using Pin no. 13 & 12as a analog i/p for solar and battery voltage sense and 11 & 3 as a analog i/p for solar and battery current by hall sensor. Output is taken from pin no. 5.

I attached the complete code for this project. Kindly go through it and resolve why the IC will not able to read the analog input and pls send if any error found in coding.

Thank you,

#### Attachments

• 2.7 KB Views: 8

#### betwixt

##### Super Moderator
Staff member
You are using the wrong bit to detect the end of ADC conversion. You are checking the enable bit 'ADON', you should be checking the conversion in progress bit 'GO_DONE'.

Brian.

#### sachsoni

##### Newbie level 4
Kindly provide us the info. in which line of program the changes will require to run code properly.

#### betwixt

##### Super Moderator
Staff member
All the lines like this:
Code:
while(ADCON0bits.ADON == 1);
should be
Code:
while(ADCON0bits.GO_DONE == 1);
The ADON bit is used to enable or disable the ADC module, it should always be '1' when you are using the ADC.
The GO_DONE bit is set to '1' to start the measurement process and the PIC resets it to '0' when the measuring is complete. This is what I think your code is intended to do.

Brian.

#### betwixt

##### Super Moderator
Staff member
Code:
            AnalogData = (unsigned int)(ADRESH&0x03);
AnalogData = AnalogData<<8;
will only return a partial result, did you mean:
Code:
            AnalogData = (unsigned int)(ADRESH&0x03) << 8;
AnalogData += ADRESL;
so you get a full 10 bit result?

Brian.

Status
Not open for further replies.