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 CCS Technical Support

ADC reading problem with 16 bit processor
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

ADC reading problem with 16 bit processor
PostPosted: Wed Jan 20, 2021 3:02 am     Reply with quote

Hello there
I want it to read the adc channel with the processor I am using. But it says 0 volts on my screen. The test code I wrote is below. Do you see an error?

CCS C 5.100

Code:

#include <33EP512GM710.h>
#device ADC=10
#device ICSP=2
#use delay(clock=120MHz,crystal=16MHz)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled

#define LARGE_LCD
#include <S1D13700.c>
#include <graphics.c>

void main()
{
   float Voltage=0.0;
   unsigned int16 Sum=0;
   char buffer[40];
   glcd_init(ON);

   setup_adc_ports(sAN8, VSS_VREF);
   setup_adc(ADC_CLOCK_DIV_256 | ADC_TAD_MUL_31);

   while(TRUE)
   {
 
     set_adc_channel(8);
     Sum = read_adc();
     delay_ms(10);
     Voltage = ((2.5 / 1023) * Sum);;
     sprintf(buffer ,"%2.1fvolt", Voltage);
     glcd_text57(4, 117, buffer, 1, ON);

   }

}

_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Wed Jan 20, 2021 4:09 am     Reply with quote

OK. Couple of things to try:
Code:

#include <33EP512GM710.h>
#device ADC=10
#device ICSP=2
#use delay(clock=120MHz,crystal=16MHz)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled

#define LARGE_LCD
#include <S1D13700.c>
#include <graphics.c>

void main()
{
   float Voltage=0.0;
   unsigned int16 Sum=0;
   char buffer[40];
   glcd_init(ON);

   setup_adc_ports(sAN8, VSS_VREF);
   //Do you have a 2.5v Vref on the Vref+ pin?. Not going to
   //work unless you do....
   //However Beware. Minimum required Vref for full accuracy is
   //2.7v, not 2.5v.....

   setup_adc(ADC_CLOCK_DIV_8 | ADC_TAD_MUL_31);
   //Don't clock the ADC too slowly. Accuracy declines if you do
   //Max frequency is 8.33Mhz, /8 gives 7.5MHz

   while(TRUE)
   {
 
     set_adc_channel(8, VSS);
     //All the ADC channels on this chip are differential. Needs to be told
     //to select Vss for the second input.
     delay_ms(10);
     //Though you have a programmed Tacq, the ADC needs a minimum
     //of 20uSec after it is turned on by the setup instruction.
     Sum = read_adc();

     Voltage = ((2.5 / 1024) * Sum);
     //Though the ADC gives 1023 'levels', there are 1024 steps for a given
     //voltage. It reads from 0 to Vref-(Vref/1024).

     sprintf(buffer ,"%2.1fvolt", Voltage);
     glcd_text57(4, 117, buffer, 1, ON);

   }

}
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Wed Jan 20, 2021 4:31 am     Reply with quote

Yes I am using LM336 2.5V. But it still doesn't work. Also, will I specify VSS separately in all ADC entries I use?

Code:

setup_adc_ports(sAN11 | sAN12 | sAN13 | sAN14 | sAN15 | sAN17 | sAN18 | sAN19 | sAN24 | sAN28 | sAN29 | sAN30 | sAN31 | sAN35 | sAN36 | sAN37 | sAN38 | sAN39 | sAN40 | sAN41 | sAN42 | sAN43 | sAN44 | sAN49 , VSS);
setup_adc_ports(VSS_VREF);

_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Wed Jan 20, 2021 6:25 am     Reply with quote

That is just wrong.
The second setting _overrides_ the first one. The value VSS_VDD put in as
the first value in the setup, will put zero as the first term, which turns off all
inputs.
You cannot 'sequence' the setups like this, and the first value is the
inputs to use, not the Vref source.

Are you specifying Vss in the set_adc_channel setting as I show?.

If you look at this ADC, all the inputs are differential, so you can specify two
channels. Setting Vss, tells it to use the Vss connection for the -ve input.

You say 5.100. So the brand new version. Try with 5.097.
5.098, introduced some oddities on the PCD chips. 5.099, makes the
ADC's not work correctly on the chips I'm using. Haven't yet cross checked
5.100.
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Thu Jan 21, 2021 3:21 am     Reply with quote

OK. Have taken the time to have a look at the assembler being generated.
This with 5.100, and my posted source code.
Comments in line....
Code:

....................    setup_adc_ports(sAN8, VSS_VREF);
00724:  CLR     E0E            : E0E = 0 //ANSELA = 0
00726:  CLR     E1E            : E1E = 0 //ANSELB = 0
00728:  MOV     #4,W4          : W4 = 4
0072A:  MOV     W4,E2E         : E2E = W4 //ANSELC = 4 C2 correct
0072C:  CLR     E3E            : E3E = 0 //ANSELD = 0
0072E:  CLR     E4E            : E4E = 0 //ANSELE/F (depends on chip) = 0
//OK. All good to here
00730:  MOV     #400,W4        : W4 = 400
00732:  MOV     W4,E5E         : E5E = W4
//Wrong..... E5E/F does not exist... Problem potentially
00734:  CLR     E6E            : E6E = 0 //ANSELG

00736:  MOV     #2000,W4       : W4 = 2000
00738:  MOV     W4,322         : AD1CON2 = W4
//OK this is setting VSS_VREF

0073A:  MOV     #2000,W4       : W4 = 2000
0073C:  MOV     W4,362         : AD2CON2 = W4
//However this is wrong. Data sheet says this setting does not apply
//for ADC2

....................    setup_adc(ADC_CLOCK_DIV_8 | ADC_TAD_MUL_31);
0073E:  MOV     #1F07,W4       : W4 = 1F07
00740:  MOV     W4,324         : AD1CON3 = W4
//OK this sets to clock/8 and TAD_MUL31

00742:  MOV     #80E0,W4       : W4 = 80E0
00744:  MOV     W4,320         : AD1CON1 = W4
//Turns on the ADC, integer output, selects autoconvert mode,
//10bit operation.

....................      set_adc_channel(8, VSS);
00746:  MOV     #8,W4          : W4 = 8
00748:  MOV     W4,328         : AD1CHS0 = W4
//selects AN8, and VrefL for -ve input

....................      Sum = read_adc();
00750:  BCLR.B  320.0          : AD1CON1.DONE = 0
//This agrees with the data sheet where this needs to be cleared.

00752:  BSET.B  320.1          : AD1CON1.SAMP = 1
00754:  BTSS.B  320.0          : Skip if AD1CON1.DONE = 1
00756:  BRA     754            : GoTo 754
//Loops waiting for sample, then conversion.

00758:  PUSH    300            : PUSH ADC1BUF0 to TOS
0075A:  POP     1006           : POP TOS to [1006]
//read the result


I've emailed this to CCS as well. It is 99% correct code. However they
are initialising one incorrect register. It is not defined as a register, so
actually surprised if it stops it from working.
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Thu Jan 21, 2021 10:46 am     Reply with quote

So now the problem is with the 5.100 version? So I edited the code I wrote. My opinion is that CCS C is having trouble with 16 bit ADC readings. The same situation is that I control 21 ADC inputs with an 8 bit processor. I could not see a problem.
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Thu Jan 21, 2021 11:23 am     Reply with quote

You said you were using 5.100, hence this is what I looked at.
Historically for me on 5.097 the ADC ran fine.
Then 5.098, stopped the 16bit code from working at all.
5.099 introduced ADC problems with some chips. On the various chips I'm
currently using 2 went wrong and the third was fine.
5.100, works for 90% of chips, but some specific ones, particularly ones
with the more complex ADCs seem to have problems.
In your case though it looks like a database problem for the chip
registers.
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 1:30 am     Reply with quote

OK. Had an answer back.
The ADC2 setting is wrong, but should not stop the ADC from running.
The other line is a data sheet error from Microchip.

CCS have tested the ADC using the settings as I posted, but onto a
different ADC input (the board they are using doesn't allow easy connection
to the pin being used. For them it works.

They have released 5.101, which fixes the ADC2 issue.

Are you sure you are getting a voltage on pin C2?.
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 6:04 am     Reply with quote

Yes, something is showing on my screen now. But there is a lot of tremors.
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
temtronic



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

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 7:21 am     Reply with quote

re: But there is a lot of tremors.

You mean the reading is not 'stable' ?

If so,you need to know that getting any ADC over 8 bits, to read 'stable' is a challenge. You must have GREAT PCB layout, proper bypass/filter caps, precision Vref, proper grounding, shielded analog wires and of course a rock,stable input voltage !
It's NOT an easy task, but can be done, 3 decades ago I was building 32 channel, 16 bit ADC boards....
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 8:09 am     Reply with quote

mr temtronic:
First of all I do 10 bit reading. + Vref is fixed to 3.0 volts. My PCB board is working fine with 21 channel adc reading in the project I did before. I switched to a 16 bit processor to have more pins in the same system. My project on my 8 bit processor is still running healthier and more stable. The problem here is I seem to be having a problem reading 16 bit. There is an understandable reading when I average. But this time the system is running slow. I get an average of 64. My crystal watch is at 140 MHZ. Does this affect the situation?
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 9:17 am     Reply with quote

A processor at 120MHz, will be generating hundreds of times the RF
interference than one at say 4MHz. You need to be super careful with the
decoupling close to the processor.
Look at figure 2-1. Have you followed what it says here _exactly_.
Especially the separate inductor feed for AVdd. The capacitors adjacent
to the chip need to be close, and good ceramic types. Also the ESR of
the Vcap capacitor (and this needs very short tracks).
Welcome to the problems of designing with higher switching speeds.....
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 12:03 pm     Reply with quote

I did all of them according to Figure 2-1. It just said that the L1 coil should be less than 1 ohm and above 10 mA. I don't have the product. I couldnt find it. I threw a thin wire instead. I used 10uf tantalum for the Vcap. I used a 100nf cap in ceramic capacitors.
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 12:50 pm     Reply with quote

Almost any inductor will meet this. However the actual value should be
based on the calculations also in the sheet.
Look at this for example. Rated to support 94* the required current, and
0.65R.

https://uk.rs-online.com/web/p/leaded-inductors/6755295/?cm_mmc=UK-PLA-DS3A-_-google-_-CSS_UK_EN_Provisioned_Stock_2020-11_Whoop_PO4700161482-_-Leaded+Inductors_Whoop-_-6755295&matchtype=&pla-341922064981&gclid=EAIaIQobChMI_cSj-5mw7gIVy4FQBh0WIQGAEAQYASABEgIb-PD_BwE&gclsrc=aw.ds

Or replace your wire with enamel wire and put ten turns round a small
ferrite ring core.
temtronic



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

View user's profile Send private message

PostPosted: Fri Jan 22, 2021 1:26 pm     Reply with quote

OK, I'm confused....
you say this...
The problem here is I seem to be having a problem reading 16 bit.
...

But you say you're in 10 bit ADC, and I downloaded the datasheet and it says either 10 or 12 bits.....
You should post your small program that shows us what you're doing. If floating point numbers are invloved that can be a problem.

when you upgraded the PIC, did you also redo the PCB to take into account eh 'noise'/EMI a faster PIC generates ?

if you simply read one channel of a known fixed voltage and display the RAW data is it stable ? if not what are the readings ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
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