![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
PIC16F877A AN4 Issue |
Posted: Sat Aug 20, 2016 11:23 pm |
|
|
Hi All,
I have having quite a strange issue that I simply can't get my head around.
I am measuring 4 MCP9700 temperature sensors (same as a TMP35) with a PIC16F877A. (I know it is old, but I am tiring to use up some stock on simple jobs like this).
The following is the output I am getting.
First number is the channel, the next numbers are the raw ADC reading (using 10 bits).
Code: |
0 166 164 165 166 166 164 165 165 165 166 166 165 165 166 165 165 165 166 166 165 165 166 166 165 166 164 165 166 166 165 165 166 164 165
min:164 max:166 delta:2 avg:330 temp:17.3
1 170 170 170 169 169 170 170 169 171 170 169 169 171 170 169 169 170 170 170 170 169 169 169 169 169 171 169 171 171 170 170 170 170 170
min:169 max:171 delta:2 avg:339 temp:18.9
2 165 166 165 167 167 166 165 165 165 167 166 166 166 165 165 165 167 166 166 166 166 165 165 167 167 166 166 166 166 165 165 167 166 166
min:165 max:167 delta:2 avg:331 temp:17.3
3 180 182 188 192 190 197 197 201 203 207 214 218 221 225 227 230 235 239 248 250 254 260 259 262 264 262 265 265 266 266 268 269 271 274
min:180 max:274 delta:94 avg:468 temp:44.8
|
As you may notice, first 3 are fine, in fact I think my design skills are getting better judging by the numbers
It is the last one that is a issue. It is always high and wild.
The other thing I notice is that the readings go up almost each time in the 34 times I read it. It will always start low, then go up. I have also tried to just keep reading AN4 (ch3) and the ADC reading will go up to about 500 before settling down.
I have a 4.094 VREF so that would mean the voltage would need to be about 2 volts. Looking at the pin with my scope, there is a little noise, but nothing that high.
I have tried to swap sensors around, problem stays on ch3.
I have tried swapping PICs, same thing.
If I measure the voltage, it looks about the same as the others at about 650mv.
Code is the same for all 4 sensors in a for loop so I can't see how that would have anything to do with it. In fact, even if I hardcode each loop to only read AN4, the reading coming back are always high like above.
Can anyone think of something I am not?
Thanks
Code: |
//// Project: Beer Controller ////
//// Compiler Version: CCS 5.025 ////
//// Start Date: 31/07/16 ////
//// Version: 0.0.1 ////
#include <16F877A.h>
#device ADC=10
#fuses HS, NOWDT, NODEBUG, PUT
#use delay(clock=20M)
#use rs232(baud=9600, UART1)
#define WORK_FREQUENCY 500 //number of timer0 timeouts between doing main work
#define MCP3700_OFFSET 490 //zero offset for the MCP9700 temp sensor
int16 timer0Tick;
int16 temperature[4];
#INT_TIMER0
void isr_TIMER0()
{
// output_toggle(DEBUG);
timer0Tick++;
}
void read_temperatures()
{
int i, j;
int16 ADCValue, ADCSum, oldTemperature, min, max;
int16 tAVG, delta;
disable_interrupts(INT_TIMER0);
printf("\n\n\n\n");
for (i = 0; i < 4; i++)
{
ADCValue = 0;
ADCSum = 0;
min = 30000;
max = 0;
if (i == 3) //we need to skip AN3 as we are using a VREF
{
set_adc_channel(4);
}
else
{
set_adc_channel(i);
}
delay_ms(300);
printf("\r\n\n%u ", i);
for (j = 0; j < 34; j++)
{
ADCValue = read_adc();
printf("%lu ", ADCValue);
ADCSum = ADCSum + ADCValue;
if (ADCValue > max)
{
max = ADCValue;
}
if (ADCValue < min)
{
min = ADCValue;
}
}
ADCSum = ADCSum - (max + min);
oldTemperature = temperature[i];
temperature[i] = (((ADCSum / 32) * 4) - MCP3700_OFFSET); //workout temperature using a 4.092 vREF (each 4mV of input = 1 ADC count)
temperature[i] = (temperature[i] + oldTemperature) / 2;
tavg = ADCSum / 16;
delta = max - min;
printf("\r\nmin:%lu max:%lu delta:%lu avg:%lu temp:%04.1w", min, max, delta, tavg, temperature[i]);
enable_interrupts(INT_TIMER0);
}
}
void main()
{
int i;
for(i=0; i<4; i++)
{
temperature[i] = 210; //set the temps as 21 so the first run avg will be close to correct
}
timer0Tick = 30000; //make sure we run on first loop
setup_adc(ADC_CLOCK_DIV_64);
setup_adc_ports(AN0_AN1_AN2_AN4_VSS_VREF);
set_adc_channel(0);
setup_timer_0(T0_INTERNAL | T0_DIV_256); //fires ever ~25ms
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_timer0(0);
while (1)
{
if (timer0Tick > WORK_FREQUENCY)
{
read_temperatures();
timer0Tick = 0;
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Sat Aug 20, 2016 11:53 pm |
|
|
Quick update.
If I apply 4v to AN4, the readings do not change. I am starting to think something is wrong with my channel selection of the ADC setup. I just can't see what.
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 21, 2016 12:01 am |
|
|
Can you check your connections ? AN4 is actually on pin RA5.
Do you accidentally have your sensor connected to pin RA4 ? |
|
![](templates/subSilver/images/spacer.gif) |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Sun Aug 21, 2016 12:10 am |
|
|
I can't believe I did that. I think I looked at the datasheet 10 times and still did not notice. It was connected to RA4.
Thanks for the help ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Sun Aug 21, 2016 12:25 am |
|
|
Anyone who says they have not made a mistake like this is probably lying....
It is designed to confuse. Beware also that RA4 (if you try to use it for digital I/O), is an 'open collector' drive only, and only has a protection diode to Vss, not to Vdd as well. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Sun Aug 21, 2016 5:05 am |
|
|
Welcome to the 'club'. I must be the president as I did that 20 years ago.....when the chip was kinda new !
I've also put 16F84 in backwards and wondered why it got hot! Crazy thing is, took it out, put in the correct way(that little dimple on DIPs is really small), and the PIC happily powered up and ran fine !!
These days I have to use a 5x mag light to read the PICs..joys of getting old...but they go in properly now.
Jay |
|
![](templates/subSilver/images/spacer.gif) |
Markdem
Joined: 24 Jun 2005 Posts: 206
|
|
Posted: Mon Aug 22, 2016 4:55 am |
|
|
I think what really got me here is that the readings where "kind of close" to what they should of been, so I did not think about hardware.
You live and learn I guess.
Thanks guys. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|