CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

All adc channels are giving same reading

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dvizma



Joined: 04 Apr 2016
Posts: 3

View user's profile Send private message

All adc channels are giving same reading
PostPosted: Mon Apr 04, 2016 12:41 pm     Reply with quote

I am using PIC16F887 at 16Mhz. I am using four adc channels. However readings for all the channels are same. I used the same code with pic16f688 at 12Mhz and the readings were fine. Here is the code I am using.
Code:

#include <16F887.H>
#device ADC=10

#use delay(clock=16000000)
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT

void main()
{
output_high(led1);
delay_ms(200);
output_low(led1);
printf("\nStarted");
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);

//ext_int_edge(l_to_h);
//enable_interrupts(INT_EXT);
//enable_interrupts(GLOBAL);
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(sAN0|sAN1|sAN2|sAN4|VSS_VREF);

while(1)
{
unsigned int16 lcdA, lcdB, lcdC, lcdD;
set_adc_channel(4);
delay_us(20);
lcdA= read_adc();
set_adc_channel(2);
delay_us(20);
lcdB= read_adc();
set_adc_channel(1);
delay_us(20);
lcdC= read_adc();
set_adc_channel(0);
delay_us(20);
lcdD= read_adc();

printf("\n%lu,%lu,%lu,%lu",lcdA,lcdB,lcdC,lcdD);
delay_ms(20);
}
}
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Apr 04, 2016 1:10 pm     Reply with quote

while you did increase the master clock
i note, you did NOT change this line
Code:

setup_adc(ADC_CLOCK_DIV_32);


what would be nice is if you could div_64

but for this part - you can't - tough spot to be if
you MUST have 16mhz
dvizma



Joined: 04 Apr 2016
Posts: 3

View user's profile Send private message

PostPosted: Mon Apr 04, 2016 1:20 pm     Reply with quote

As per 16F887.H, adc can have only three modes


#define ADC_CLOCK_DIV_2 0x100
#define ADC_CLOCK_DIV_8 0x40
#define ADC_CLOCK_DIV_32 0x80

So I can't use divide by 64
Ttelmah



Joined: 11 Mar 2010
Posts: 19382

View user's profile Send private message

PostPosted: Mon Apr 04, 2016 1:31 pm     Reply with quote

Fosc/32, is legal to 20MHz.

Problems:
1) the code is incomplete. Printing to serial, yet no serial setup. Doesn't give us confidence that this is 'real'.....
2) What is feeding Vref?. If this was floating you might well get the same value from each channel, as the internal diode structures would clamp the Vref to each input as it was selected,,,,,
3) What is the impedance of the analog sources. Though you say the code is the same, has the hardware changed?.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Apr 04, 2016 2:15 pm     Reply with quote

Quote:
I used the same code with pic16f688


worries me

can you
a) POST YOUR CURRENT SCHEMATIC ??
b} what IS the value you read that never changes ??

i assume as it appears on your LCD
dvizma



Joined: 04 Apr 2016
Posts: 3

View user's profile Send private message

PostPosted: Tue Apr 05, 2016 12:22 am     Reply with quote

Sorry for the incomplete code. I am actually trying to build this
https://troels.leegaard.org/misc/grundtal20047/

When I started, I didn't have PIC16F887, so I read only the four back panel values using PIC16F688

Here is the working code for 16f688
Code:


#include <16F688.H>
#device ADC=10

#use delay(clock=12000000)
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT
#use rs232(baud = 9600,parity=N,xmit =PIN_c4,rcv= PIN_c5,ERRORS)

#define panA pin_a0
#define panB pic_a1
#define panC pin_a2
#define panD pin_c0

#define led pin_c2

void main()
{
char ch;
unsigned int16 A, B, C,D;
output_high(led);
setup_adc(ADC_CLOCK_DIV_16);
printf("\nStarting");
while(1)
{
set_adc_channel(0);
delay_us(20);
A= read_adc();
set_adc_channel(1);
delay_us(20);
B= read_adc();
set_adc_channel(2);
delay_us(20);
C= read_adc();
set_adc_channel(4);
delay_us(20);
D=read_adc();
printf("\n%lu,%lu,%lu,%lu",A,B,C,D);
delay_ms(40);
}
}



It gave me these readings

Code:

206,307,830,324
205,896,307,205
509,580,571,0
307,221,867,307
221,835,307,205
563,492,198,289
537,492,68,571
273,841,307,221
905,307,273,320
265,326,307,832
614,17,524,614
807,358,307,273
281,206,311,828
236,354,844,204
0,614,571,476
358,281,236,904


As you can see only one panel has a high value at every instant.

When switching to PIC16F887 I am using the exact code below:

Code:

#include <16F887.H>
#device ADC=10

#use delay(clock=16000000)
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7, bits=8,parity=N, ERRORS)

#define panA pin_a0
#define panB pin_a1
#define panC pin_a2
#define panD pin_a4

#define led1 pin_d6

unsigned int16 lcdA, lcdB, lcdC, lcdD;

void main()
{
output_high(led1);
delay_ms(200);
output_low(led1);
printf("\nStarted");
//setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);

//ext_int_edge(l_to_h);
//enable_interrupts(INT_EXT);
//enable_interrupts(GLOBAL);
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(sAN0|sAN1|sAN2|sAN4);

while(1)
{

set_adc_channel(4);
delay_us(20);
lcdA= read_adc();
set_adc_channel(2);
delay_us(20);
lcdB= read_adc();
set_adc_channel(1);
delay_us(20);
lcdC= read_adc();
set_adc_channel(0);
delay_us(20);
lcdD= read_adc();

printf("\n%lu,%lu,%lu,%lu",lcdA,lcdB,lcdC,lcdD);
delay_ms(30);
}
}



The readings which I am getting are:

Code:

284,313,353,377
560,466,432,421
558,524,500,480
284,368,396,417
281,315,348,376
560,464,432,421
560,525,499,476
283,379,420,432
561,492,475,469
560,537,526,516
284,371,409,436
284,313,352,376
560,462,435,428
561,533,508,495
284,379,418,440

'
I am changing the crystal to 20MHz. Let's see if it helps
temtronic



Joined: 01 Jul 2010
Posts: 9177
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 05, 2016 5:39 am     Reply with quote

OK, silly question......
If the first PIC and code functions 100% WHY don't you set the clock for the 2nd PIC to be 12MHz, same as the first one ?
This way the pulse widths and timings will be 100% the same...

Also

Is the physical setup for the 2nd PIC the same as the 1st? Reading 8 bit analog requires good PCB layout, 10 bit or more needs GREAT PCB layout !

And...
I see all of the ADC readings are not 'solid' or 'stable'. Is the scale in an enclosed box with a known weight? Are you using shilded cables? More information about the actual measuring setup would be helpful.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19382

View user's profile Send private message

PostPosted: Tue Apr 05, 2016 7:29 am     Reply with quote

An obvious screaming difference.

You are leaving the ADC set at it's default port settings in the first code.
In the second you are specifying to use the VREF input. As I said at the start _what have you got connected to Vref_?.....
temtronic



Joined: 01 Jul 2010
Posts: 9177
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 05, 2016 8:25 am     Reply with quote

I'm still trying to figure out..
where 'all the adc channels are giving the same reading ?
In neither the 'working' program or the 2nd, NONE of the data is the same for all 4 channels.
Looks like 'random' numbers.
I'd like to know more about the actual hardware setup.

Jay
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Apr 05, 2016 11:00 am     Reply with quote

Kind of afraid to ask, but this is on real hardware right? (not Proteus)
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 05, 2016 11:47 pm     Reply with quote

Quote:
I am using PIC16F887 at 16Mhz. I am using four adc channels. However readings for all the channels are same.

The readings which I am getting are:

284,313,353,377
560,466,432,421
558,524,500,480
284,368,396,417
281,315,348,376
560,464,432,421
560,525,499,476
283,379,420,432
561,492,475,469
560,537,526,516
284,371,409,436
284,313,352,376
560,462,435,428
561,533,508,495
284,379,418,440

Can you explain how these readings are "all the same" ? It looks to me
like they either increase or decrease as you look at each group of four
entries. They are not "all the same".
temtronic



Joined: 01 Jul 2010
Posts: 9177
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 06, 2016 5:20 am     Reply with quote

I had a look at the link the provided and if you then go to the 'A sketch' sub-link you can see what he's trying to do.
Simply put he's trying to read a muxed 4by7seg LCD display from a scale. By decoding which segments are on or off, you can decode the actual reading.That saves reading 4 strain gauges and complicated math.....
The code the OP presents is NOT a complete 'translation' so that's one reason we are struggling with his code.The code from the link is flawed (the function digitalREAD() is not posted). What isn't known is the speed of the 'LCD reader' micro!
I recall using the 'read-the-display' cheat 20 years ago, though using LEDs back then, seemed easier no backbplane signals to deal with.

Jay
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

PostPosted: Mon Apr 11, 2016 1:41 pm     Reply with quote

Code:
setup_adc_ports(sAN0|sAN1|sAN2|sAN4|VSS_VREF);

I think problem is from here.
On data sheet you can select vref+ as AN3 or VDD.
If on your schematic this input AN3 do not have perfect stabilizer you may have this variable data.
also must have something how to select this Vref+ (AN3 or VDD).
I hope this it will help you...
Best regards.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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