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

[Question] ADC multiple reads and PIC16f1826
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 20061

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 11:17 am     Reply with quote

Several reasons.

The 'best' standard voltage regulator, will only give perhaps 1% accuracy _at the regulator_. First problem is that as soon as there are devices drawing current, this supply will droop between the regulator and the chip. Anything better than perhaps 2% at the chip is very unusual. But then the devices drawing current, will vary in how much they draw. The PIC will change literally internal state by internal state as the chip processes, and external circuitry will also be drawing varying currents. Now the regulator always takes time to react to changes, and the lines themselves will also be slightly inductive, so watching the supply at the chip on a high frequency scope, you will find tens of mV of noise at the PIC, even when the whole circuit is well designed. The gate drive to a FET, is nominally a voltage, not a current drive, yet want to change it quickly and instantaneous currents will be many hundreds of mA.
With any flaws in the design, the problems will get larger, and especially if there is any circuitry around drawing significant current, the rails will often see sometimes even hundreds of mV of momentary changes....
People generally underestimate how difficult it is to make a supply like this be really smooth. I often say 'look at a PC motherboard'. This will have possibly as many smoothing capacitors as chips. Separate supply layers for every supply, yet even with this, they will use separate supplies for the audio circuitry, with the main supplies having far too much noise.
The Vref input only has to deliver a few mA (typically less than 2mA). Use a laser trimmed bandgap reference, and be surprised at how much more stable and more accurate the readings from the ADC suddenly become. Using proper power planes to the PIC, with good smoothing, and a separate Vref, I've got units that will sit reading exactly the same value from a stable voltage, for hour after hour. Try with the supply rail instead, any you see several counts of change, even with a supply that a DVM, says is not changing.....
temtronic



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

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 12:42 pm     Reply with quote

You should also consider using rail to rail op amps as simple voltage followers between your sensors and the PIC. When used as a buffer they will tend to protect the PIC.


Jay
murgui



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

PostPosted: Mon Dec 28, 2015 3:21 pm     Reply with quote

I didn't know such events. I will try to avoid such modifications and give it a try if the sensibility isn't good enough I'll try to implement the modifications you guys so kindly wrote.

You're great!!! I don't know how many hours I'd have spent to figure out all the things you told me.

Thank you very much, I will keep an eye in the forum.
murgui



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

PostPosted: Thu Jan 14, 2016 12:36 pm     Reply with quote

Hi! Yesterday I finally finished my exams so I'm ready to dedicate my time to PICs. I followed Temtronic's advice of doing the blinking LED program at 1HZ. I also installed 4.141 compiler version and managed to acquire a PicKit so I'm working on protoboard.

The problem is it blinks 4 times the velocity it should. I found it really difficult to find related posts and the only related talked about the PLL, which is a postscaler for what I saw.
I guess my error is conceptual but I couldn't figure out the solution.

Main.c

Code:
#include <main.h>


void main()
{

while(1) //function oscillation 1hz
{
output_high(PIN_B0);
delay_ms(1000);
output_low(PIN_B0);
delay_ms(1000);
}

}


main.h


Code:
main.h


#include <16F1826.h>
#device ADC=10

#FUSES HS
#FUSES NOWDT                                            //No Watch Dog Timer               
#FUSES NOPUT   //No Power Up Timer
#FUSES MCLR                                             //Master Clear pin enabled
#FUSES NOPROTECT                                        //Code not protected from reading
#FUSES NOCPD                                            //No EE protection
#FUSES NOBROWNOUT                                       //No brownout reset           
#FUSES IESO                //Internal External Switch Over mode enabled
#FUSES FCMEN                                            //Fail-safe clock monitor enabled
#FUSES NOWRT                                            //Program memory not write protected
#FUSES STVREN              //Stack full/underflow will cause reset
#FUSES BORV19               
#FUSES NODEBUG             //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(clock=20000000)
#define I2C_SDA   PIN_B1
#define I2C_SCL   PIN_B4
#use i2c(Master,Fast,sda=PIN_B1,scl=PIN_B4)
//#use RS232(BAUD=9600,BITS=8,PARITY=N,XMIT=PIN_B2,RCV=PIN_B3)



So looking at main.c it should blink 5 times in 10 seconds and it blinks 20 times in 10 seconds.
I have a 20MHz xtal with 2 capacitors of 18pF, I've tried with some other capacitor values 12pF~22pF and the results are the same.

Thank you very much!

Murgui


EDIT: I've seen is a 16f18XX related problem and is just not possible to fix, can somebody confirm that?
temtronic



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

View user's profile Send private message

PostPosted: Thu Jan 14, 2016 2:32 pm     Reply with quote

I don't use that PIC but downloaded the datasheet and did a quick read..

hmm... 'magically' 4 X faster... sounds like the PLL is enabled.
Could be a compiler 'bug' but I'd code the correct #FUSE to NOT have the PLL enabled as a test.
These 'new' PICs have a lot of fuses and perhaps the 'default' is PLL ON ?

It'd be a good idea to dump the LISTING and see how the fuses are being configured.

Jay
edit.
have to ask why not use the internal OSC ? you can set to 8,16,32MHz or ??
might be worth a test.
murgui



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

PostPosted: Thu Jan 14, 2016 5:11 pm     Reply with quote

Hi,

Following your advice I looked to the existing FUSES and unfortunately I didn't see a PLL disable fuse. I've seen NOPLLEN in other PICs but this is not the case and the compiler detects it as an error. I will keep looking for a way to disable it as it really seems to be the PLL wandering with the clock frequency.

Well I haven't used the internal just because the people suppoused to teach me said that the external is a much better clock source and I just didn't put time to check is that was true. I will look for some comparative and I could maybe finish using the internal oscillator. Do you recommend any lecture in that subject?

Thank you very much!
Murgui
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 14, 2016 7:35 pm     Reply with quote

Look more closely. What possible method could likely be used to turn
off the PLL ? See the fuse listed in bold below:
Quote:

//////// Fuses: LP,XT,HS,RC,INTRC_IO,ECL,ECM,ECH,NOWDT,WDT_SW,WDT_NOSL,WDT
//////// Fuses: PUT,NOPUT,NOMCLR,MCLR,PROTECT,NOPROTECT,CPD,NOCPD,NOBROWNOUT
//////// Fuses: BROWNOUT_SW,BROWNOUT_NOSL,BROWNOUT,CLKOUT,NOCLKOUT,NOIESO
//////// Fuses: IESO,NOFCMEN,FCMEN,WRT,WRT_400,WRT_200,NOWRT,PLL_SW,PLL
//////// Fuses: NOSTVREN,STVREN,BORV25,BORV19,DEBUG,NODEBUG,NOLVP,LVP
//
murgui



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

PostPosted: Fri Jan 15, 2016 3:16 am     Reply with quote

Thank you PCM PROGRAMMER, some heroes don't wear capes.

Murgui
murgui



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

PostPosted: Wed Jan 20, 2016 11:10 am     Reply with quote

PROBLEM SOLVED

For anyone interested, I forgot the ADC configuration(setup_adc and setup_adc_ports)

Thank you as well!


Hello everyone!

So I keep trying to get multiple ADC data. I think I have followed everyone of your advices: right now I'm sending the data with a TTL<->USB. I read the information with hercules, a very simple serial terminal software.

At the moment I have the initial problem: I can only read and send using 1 ADC channel, when I use a for loop in order to work with all the channels(in this case 8) it just sends wrong information.

My code:

Main.c


Code:

#include <main.h>


void main()
{
int8 i,byte1[10],byte2[10];
int16 mesura[10],T[10];
//---------FASE 2---------------------ADC LOGGING
while(TRUE)
   {
   i=0;
   
   for (i=0;i<8;i++)
      {
      set_adc_channel(i);
      mesura[i]=0;
      byte1[i]=00000000;
      byte2[i]=00000000;
      delay_ms(1);
      mesura[i]=read_adc();
      delay_ms(1);
      //T[i]=(int8)(((((mesura[i])/1024.0)*5-227.89)/-1.9958)*100);
      byte1[i] = make8(mesura[i],0);
      byte2[i] = make8(mesura[i],1);
      delay_ms(100);
 
      }
   if ((mesura[0]<1025) & (mesura[0]>-1))
   {
      output_high(PIN_B0);
      delay_ms(1000);
      output_low(PIN_B0);
      delay_ms(1000);
   }
    printf("Hello");
   
    for (i=0;i<8;i++)//writing rs232
   {
    printf("%8u", byte1[i]);
    printf("%8u", byte2[i]);
    delay_ms(1);
    }
    printf("see you");
    delay_ms(5000);
}
}



main.h



Code:
#include <16F1826.h>
#device ADC=10

//#FUSES HS
#FUSES INTRC_IO       //INTERNAL OSCILLATOR en delay clock hay que escoger una frecuencia de 16MHz, 500KHz y 31KHz
#FUSES NOWDT          //No Watch Dog Timer               
#FUSES NOPUT          //No Power Up Timer
#FUSES MCLR                                             //Master Clear pin enabled
#FUSES NOPROTECT                                        //Code not protected from reading
#FUSES NOCPD   
#FUSES PLL_SW                                     
#FUSES NOBROWNOUT                                       //No brownout reset           
#FUSES IESO                //Internal External Switch Over mode enabled
#FUSES FCMEN                                            //Fail-safe clock monitor enabled
#FUSES NOWRT                                            //Program memory not write protected
#FUSES STVREN              //Stack full/underflow will cause reset
#FUSES BORV19               
#FUSES NODEBUG             //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(clock=16000000)
#define I2C_SDA   PIN_B1
#define I2C_SCL   PIN_B4
#use i2c(Master,Fast,sda=PIN_B1,scl=PIN_B4)
#use RS232(BAUD=9600,PARITY=N,XMIT=PIN_B2,RCV=PIN_B3,errors)





The result oh this code is hello and goodbye with 8 pairs of bytes between them, byte1=245 and byte2=61, always.

Before that I used only 1 channel with great results playing with resistors in series and a multimeter to check that the ADC value was correct.

Thank you very much,

Murgui


Last edited by murgui on Wed Jan 20, 2016 11:32 am; edited 2 times in total
jeremiah



Joined: 20 Jul 2010
Posts: 1411

View user's profile Send private message

PostPosted: Wed Jan 20, 2016 11:32 am     Reply with quote

Where is your call to setup the ADC module and enable the channels (enabling is different than selecting)?
murgui



Joined: 23 Dec 2015
Posts: 37

View user's profile Send private message

PostPosted: Wed Jan 20, 2016 12:26 pm     Reply with quote

I didn't enable the ADC but it works Confused
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 Previous  1, 2
Page 2 of 2

 
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