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

18F45J11 ADC problems
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
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Fri Dec 02, 2011 12:25 am     Reply with quote

Chip changed, no difference, voltages in norm.

AN0 - int temp
AN1 - ext tmp2
AN2 - ext tmp1
AN5 - mains
AN6 - power
AN7 - battery

tested values on pin:
mains - 3.20V
power - 3.04V
battery - 2.72V
int tmp - 2.98v
ext1 tmp - 2.89V
ext2 tmp - 2.90V


Code:


main.c:

//===================================

#FUSES WDT                       //No Watch Dog Timer
#FUSES WDT2048                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES IOL1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES NOWPCFG               
#FUSES WPEND                 
#FUSES WPDIS                 
#FUSES NOLPT1OSC                //Timer1 configured for higher power operation
#FUSES NOT1DIG               
#FUSES MSSPMSK7             
#FUSES DSWDT2048             
#FUSES NODSWDT               
#FUSES NODSBOR               
#FUSES RTCOSC_INT           
#FUSES DSWDTOSC_INT         
#FUSES WPFP 
#use delay (clock=20000000)

#pin_select U2TX = PIN_C2
#pin_select U2RX = PIN_C1

#use rs232(uart1,baud=19200,xmit=MT_TX,rcv=MT_RX,parity=N,bits=8,stop=1,stream=mtbus,restart_wdt,errors,TIMEOUT=5)
#use rs232(uart2,baud=19200,parity=N,bits=8,stop=1,stream=mbbus,restart_wdt,errors,TIMEOUT=5)
#use i2c(master, sda=I2C_DT, scl=I2C_CLK)

#use fixed_io(A_outputs=PIN_A3,PIN_A5)
#use fixed_io(B_outputs=PIN_B3,PIN_B4,PIN_B5,PIN_B6,PIN_B7)
#use fixed_io(C_outputs=PIN_C2,PIN_C5,PIN_C6)
#use fixed_io(D_outputs=PIN_D0,PIN_D1,PIN_D2,PIN_D3,PIN_D4)
#use fixed_io(E_outputs=)

//===================================

setup_wdt(WDT_ON);            //set watch dog timer
disable_interrupts(GLOBAL);    //clear interrupts

port_b_pullups(false);
delay_us(10);
setup_adc_ports(sAN0 | sAN1 | sAN2 | sAN5  | sAN6 | sAN7 | VSS_VDD);
setup_adc(ADC_CLOCK_DIV_16 | ADC_TAD_MUL_4); //set AD ports
setup_psp(PMP_DISABLED);
set_timer0(0);               //clear timers
set_rtcc(0);               //clear timers
setup_timer_0(T0_INTERNAL | T0_8_BIT | T0_DIV_1); //set timer 0 parameters
setup_rtc(RTC_DISABLE,0);
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
setup_comparator(NC_NC_NC_NC);   //turn off comparators

//===================================

long int tmp_load;

then:

void read_analogue_input(unsigned char adcParameter) { //capture analogue from temperature

   int i;

   disable_interrupts(GLOBAL);       //clear interrupts   
   set_adc_channel(adcParameter);
   tmp_load = read_adc();
   enable_interrupts(GLOBAL);       //clear interrupts

} //void

then:

void mains_test_(void) {
   read_analogue_input(5); // test mains
   
      comms_mt_TX_high();
         fprintf(mtbus,"MAINS %lu%c",tmp_load,0x0D);
      comms_mt_TX_low();

}





As per datasheet:

Voltage on any digital only I/O pin or MCLR with respect to VSS (when VDD > 2.0V) .................................. -0.3V to 6.0V
Voltage on any digital only I/O pin or MCLR with respect to VSS (when VDD < 2.0V)..................... -0.3V to (VDD + 4.0V)
Voltage on any combined digital and analog pin with respect to VSS (except VDD)........................ -0.3V to (VDD + 0.3V)
Voltage on VDDCORE with respect to VSS...................... -0.3V to 2.75V
Voltage on VDD with respect to VSS .............................. -0.3V to 4.0V

so in theory chip should be able to handle close to 4V DC on analogue anyway from my understanding when VCC is 3.3V.

Voltages are static, I do test once a second.

(I was never in the history of my work with this pic able to get analogue readings right)
I have checked voltages on oscilloscope and there is not much movement up and down. At least not that much to get such tests results:

INT TMP 864
EXTTMP1 895
EXTTMP2 391
MAINS 1016
PWD 271
BTT 222

INT TMP 830
EXTTMP1 960
EXTTMP2 444
MAINS 894
PWD 304
BTT 223

INT TMP 864
EXTTMP1 944
EXTTMP2 391
MAINS 992
PWD 279
BTT 223

INT TMP 880
EXTTMP1 903
EXTTMP2 432
MAINS 911
PWD 255
BTT 227

INT TMP 888
EXTTMP1 903
EXTTMP2 391
MAINS 895
PWD 288
BTT 223

INT TMP 862
EXTTMP1 894
EXTTMP2 391
MAINS 903
PWD 255
BTT 223

INT TMP 831
EXTTMP1 960
EXTTMP2 383
MAINS 895
PWD 287
BTT 223

INT TMP 896
EXTTMP1 960
EXTTMP2 416
MAINS 1008
PWD 271
BTT 252

INT TMP 896
EXTTMP1 895
EXTTMP2 382
MAINS 992
PWD 344
BTT 248
_________________
Help "d" others and then you shell receive some help from "d" others.
Ttelmah



Joined: 11 Mar 2010
Posts: 19249

View user's profile Send private message

PostPosted: Fri Dec 02, 2011 2:54 am     Reply with quote

_Survive_, not 'handle'. Important difference....

I would be wondering about possible HF noise on the Vdd line (since you are using this as Vref), or on the signals themselves.
You have to remember that the analog input on the PIC, has a diode to the Vref (whatever it's source), formed by the FETs in the multiplexer, then a resistor capacitor network (resistance in the multiplexer, then the feed resistance from this to the sampling capacitor), then the internal capacitor from which the readings are taken. Result is that the ADC, tends to act as a small integrator, of any HF signals, and also that any modulation on the Vdd, (if this is used as the REF), can itself be seen as variations in the readings. I've had a PIC integrate local radio signals to give ADC variations....

Get rid of the fixed_io selections for testing at least.
Your set_spi lines are wrong. They are enabling the SPI, but turning off 'slave select'. Syntax to disable an SPI port, is "setup_spi(FALSE);".
Disconnect one of your inputs,, ans substitute a pot between Vss, and Vdd as close to the chip as you can get it. What does the reading from this give?.
Everything says you _have_ a noise problem somewhere.

Best Wishes
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Fri Dec 02, 2011 3:07 am     Reply with quote

"chip should be able to handle close to 4V DC on analogue"

Depends what you mean by "handle". Those limits are absolute maximum values. Those are about the chip *surviving*. Anything outside those and the PIC may be damaged. That doesn't mean the PIC will operate correctly all the way to those limits. The ADC channels are functionally limited to the supply rails. You can't get sensible readings out side the reference volts limits. Exactly what happen between Vref and Vdd is not specified. My expereince strongly suggests that all ADC readings are chaotic if any of the analogue inputs are driven into that range. Most folk here simply set Vdd as Vref, so that isn't a problem. It is a problem if you exceed Vdd or go lower than Vss (0V), EVEN if you keep within the absolute max limits.

Even so, you cannot expect stable, clean readings from the ADC if you use your supply as the ADC reference. There's all sorts of noise, and you don't know what the power voltage really is. Its usable for simple ratiometric measurements, where what you are measuring is a proportion of the supply, but its nearly useless for any absolute measurement. I can't say for sure if the readings you're getting are "right" but they don't look too far off for a using Vdd as a reference, given the likely noise levels. You do say "there is not much movement", but what is "not much"? How much noise is there? How much in the supply? How much exactly? Do you need to filter your ADC readings? What are you usign them for? If its simply checking a few voltages, as some of your readbacks imply then it may well be good enough. It you are trying to get better than 0.1C resolution from precise temperature sensors then think again. Getting even 10 bit precision from the PIC ADCs requires care, and at the very least a decent external reference voltage.

RF Developer
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Fri Dec 02, 2011 3:55 am     Reply with quote

As a reference I use VDD 3.3V from lm394. However by saying that the voltages I measure are coming from different sources within the circuit. PSU is common in the circuit and is stepped down. I think you may be right here and it looks like sinus in the readings and pattern is repeating itself every circle. I have to do more tests on the circuit. My 5V chip is using different elements and 5V or 12V is coming from lm7805/12/15 which keeps voltage much more stable than lm317/350/338. I think I need to change the way I supply the circuit and check again. If you do not see anything wrong in the code then it must be a circuit. Thnx for all help, I have a bit of work now to do to get the circuit right.
Regards
_________________
Help "d" others and then you shell receive some help from "d" others.
Ttelmah



Joined: 11 Mar 2010
Posts: 19249

View user's profile Send private message

PostPosted: Fri Dec 02, 2011 4:52 am     Reply with quote

It is perhaps worth adding a little more.
If you look carefully further into the data sheet, you find that 'analog input voltage', is _limited_ to the range Vrefl to Vrefh. Vrefh, is allowed to go up to Vdd+0.3v. So you potentially could power the chip off 3v (say), and have Vrefh at 3.3v, which would then allow an analog input pin to go up to Vdd+0.3v. It is also worth realising that the whole multiplexer, is an array of FET switches, and if any one is reverse biased, these will work as diodes. So if just one of the analog pins goes higher than the allowed maximum, it _will_ affect the other inputs.

Best Wishes
Linuxbuilders



Joined: 20 Mar 2010
Posts: 193
Location: Auckland NZ

View user's profile Send private message

PostPosted: Fri Dec 02, 2011 5:09 am     Reply with quote

I have just tested the circuit and you are 100% correct, it does affect other pins, chip goes to 3.8V without any problem but starts affecting other readings when it is above the safe limit. I think that data sheet is lowered down to make sure you do not abuse the chip with high voltage. But as I know life it has its tolerance above those limits and will work and not burn, maybe in long term would. It hates 12V there Smile. I know I have tested this too. I have made a test and wired lm7805 into tmp sensor circuit in two different places and guess what? perfect reading each time. So the way to fix it is simple, use LM7812/15 to test mains and battery, lm7805 to supply lm335 to test temperatures or combination of other self regulating chips. This is good excessive and I hope other people will find this useful. I haven't got this problem with 5V chip because all supply was done on 7805/12/15 so it was stable enough to give close to correct reading each time. Thnx Guys.
_________________
Help "d" others and then you shell receive some help from "d" others.
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