Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

18f1320 ADC question

Status
Not open for further replies.

ghostman11

Well-Known Member
this is for logans bench supply fan controller project, we have the switching sorted and some of the other bits are coming alone ok but i decided to run it in proteus as a sim and proteus declared AN0 was configured as digital! i thought we had actualy got the ADC sorted but apparently not.
now i have looked in the datasheetbut it hasnt helped much because logan wants to use the inbuilt libs.
Code:
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_0_TAD,
ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS,
0b1011);
that is how he has it set up and he is readiong it with

Code:
SetChanADC(ADC_CH0);
ConvertADC();
while( BusyADC() ); // Wait for ADC conversion// Wait for ADC conversion
temp1 = ReadADC(); // Read result and put in temp

can anyone see any reason this wont work? the ADC channel we want is ANO the rest digital we are also going to use pwm on cccp1 (if this is poss)
i wont post his full code yet as it needs a good clean up to make sense!!
would we be better setting up ADCON0 - ADCON2 manualy and just use
Code:
SetChanADC(ADC_CH0);
ConvertADC();
while( BusyADC() ); // Wait for ADC conversion// Wait for ADC conversion
temp1 = ReadADC(); // Read result and put in temp
for the reading and conversion?
 
i have just found a doc on adc from microchip looks like there is diff lib versions of ADC so i guess i will start there. i will let you know how it goes :D
 
Have had a look at this it for your chip and C18

Code:
OpenADC
All Other Processors
Function: Configure the A/D convertor.
Include: adc.h
Prototype: void OpenADC(unsigned char config,
unsigned char config2 ,
unsigned char portconfig);
Arguments: config
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.
A/D clock source:
ADC_FOSC_2 FOSC / 2
ADC_FOSC_4 FOSC / 4
ADC_FOSC_8 FOSC / 8
ADC_FOSC_16 FOSC / 16
ADC_FOSC_32 FOSC / 32
ADC_FOSC_64 FOSC / 64
ADC_FOSC_RC Internal RC Oscillator
A/D result justification:
ADC_RIGHT_JUST Result in Least Significant bits
ADC_LEFT_JUST Result in Most Significant bits
A/D acquisition time select:
ADC_0_TAD 0 Tad
ADC_2_TAD 2 Tad
ADC_4_TAD 4 Tad
ADC_6_TAD 6 Tad
ADC_8_TAD 8 Tad
ADC_12_TAD 12 Tad
ADC_16_TAD 16 Tad
ADC_20_TAD 20 Tad
config2
A bitmask that is created by performing a bitwise AND operation (‘&’)
with a value from each of the categories listed below. These values are
defined in the file adc.h.
Channel:
ADC_CH0 Channel 0
ADC_CH1 Channel 1
ADC_CH2 Channel 2
ADC_CH3 Channel 3
ADC_CH4 Channel 4
ADC_CH5 Channel 5
ADC_CH6 Channel 6
ADC_CH7 Channel 7
ADC_CH8 Channel 8
ADC_CH9 Channel 9
ADC_CH10 Channel 10
ADC_CH11 Channel 11
ADC_CH12 Channel 12
ADC_CH13 Channel 13
ADC_CH14 Channel 14
ADC_CH15 Channel 15

Code:
A/D Interrupts:
ADC_INT_ON Interrupts enabled
ADC_INT_OFF Interrupts disabled
A/D voltage configuration:
ADC_VREFPLUS_VDD VREF+ = AVDD
ADC_VREFPLUS_EXT VREF+ = external
ADC_VREFMINUS_VDD VREF- = AVDD
ADC_VREFMINUS_EXT VREF- = external
portconfig
The value of portconfig is any value from 0 to 127 inclusive for the
PIC18F1220/1320 and 0 to 15 inclusive for all other processors. This is
the value of bits 0 through 6 or bits 0 through 3 of the ADCON1 register,
which are the port configuration bits.
Remarks: This function resets the A/D-related registers to the POR state and then
configures the clock, result format, voltage reference, port and channel.
File Name: adcopen.c
Code Example: OpenADC( ADC_FOSC_32 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_CH0 &
ADC_INT_OFF, 15 );
I would start with this
Code:
OpenADC( ADC_FOSC_8 &
ADC_RIGHT_JUST &
ADC_VREFPLUS_VDD & 
ADC_VREFMINUS_VDD &
ADC_12_TAD,
ADC_CH0 &
ADC_INT_OFF, 15 );
 
Last edited:
thx burt thats a great help! i used ADC with the 18f4685 ages ago and didnt have a problem but my memory is shot to bits now so i am having to relearn alot of stuff from scratch :D
 
Last edited:
i couldnt get the bottom code you poted to compile
Code:
OpenADC( ADC_FOSC_8 &
ADC_RIGHT_JUST &
ADC_VREFPLUS_VDD & 
ADC_VREFMINUS_VDD &
ADC_12_TAD,
ADC_CH0 &
ADC_INT_OFF, 15 );

but after head scratching and a cup of coffee i spoted
Code:
ADC_VREFPLUS_VDD & 
ADC_VREFMINUS_VDD &
wich should of course be
Code:
ADC_VREFMINUS_VSS
lol at least i can spot the odd error now! :D
 
i am still not getting this to work! i am pretty sure i am missing something simple tho. this is the full code but be warned its still very messy and unfinished!

Code:
#include <p18cxxx.h>
#include <pwm.h>
#include <timers.h>
#include <delays.h>
#include <adc.h>
#include <usart.h>
#pragma config WDT = OFF, OSC = INTIO2, DEBUG =ON,MCLRE =ON,PWRT = OFF,BOR = OFF

int temp1;
int loop=1;  // variable to count how many times program has looped
void main (void)
{
char DutyCyc = 0xffff;
TRISBbits.TRISB2 =0;
TRISAbits.TRISA0 =1;
OSCCON = 0x72; //8MHz clock

ADCON1=0x7e;		//set RA0 to analogue

Nop();
OpenADC( ADC_FOSC_8 &
ADC_RIGHT_JUST &
ADC_VREFPLUS_VDD & 
ADC_VREFMINUS_VSS &
ADC_12_TAD,
ADC_CH0 &
ADC_INT_OFF, 15 );

//Nop();
TRISBbits.TRISB0=0;
LATBbits.LATB2 =1;
TRISBbits.TRISB3=0;
// Open the USART configured as 8N1, 9600 baud
OpenUSART ( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,25);

LATBbits.LATB2=1;
SetOutputPWM1 (SINGLE_OUT, PWM_MODE_1);
 OpenPWM1(DutyCyc);
while(1)
	{
        putcUSART(", temp1");
		SetChanADC(ADC_CH0);
		Delay10TCYx( 50 );
		ConvertADC(); // Start conversion
		while( BusyADC() ); // Wait for ADC conversion// Wait for ADC conversion
		temp1 = ReadADC(); // Read result and put in temp
	SetDCPWM1(DutyCyc);
		loop=loop++;
	
			if(loop==500)  //800 for led
				{
					LATBbits.LATB0=0; //heart beat led 
Nop();
				}
			if (loop==700) // 1000 for led
				{
					LATBbits.LATB0=1; //heartbeat led
					loop=1;
				}
	}
}

one thing i have noticed tjho is i set
ADCON1=0x7e;
but in the debugger when its stopped i get ADCON1=0x7e; in the watch window. the variabletemp1 always shows a 0 or 1023 so i am assumming that somehow i am not setting AN0 to anologue? anyone got any ideas?
 
I tried your code a a pic18f1220 little bother of the 18f1320 and changed the led to come on using the adc value.

ADC is working the loop is not looping it runs it and stops. I don't know that much C18. Maybe pommie or mike 3v0 atom will have a look. I know this the new mplab has opened a big can of worms for old code.
 
be80be you may well be on to something! i have only just started using the new mplab and i am getting all kinds of problems on code that did work and now isnt! i will keep digging and see what i can come up with, it has also been pointed out that some versions of the ADC lib an error in! thanks for the input it has to be something so obvious i am missing it :D i am going to try and get it working in mplab under proteus that way i can see the watch window 'live' and see whats happening
jason
 
Last edited:
i found this doc **broken link removed**
and according to this version3 is for the 18f1320 and under that for vref it says that for using internal reference the code is ADC_REF_VDD_VSS???? but that seems to contradict some the other docs ive read :S
 
Last edited:
not too sure i trust proteus very much but according to the watch window in isis ADC_REF_VDD_VSS is wrong as it sets VREF to external
 
There setting VCFG<1:0>: Voltage Reference Configuration bits I looked at the asm and it looks right
 
is certainly a strange one!! is 5am here now so i am going to get a few hours sleep! and get back on it later today, this was meant to be a straight forward simple little project!!!:rolleyes: still it gives me something to do lol thanks for the help Burt ;)
 
yr i need c coding for device control through bluetooth from symbian os mobile phone using microcontroller at89c2051...guys plzzzzzzzzzz help me out yr plzzzzz...
 
Last edited:
yr i need c coding for device control through bluetooth from symbian os mobile phone using microcontroller at89c2051...guys plzzzzzzzzzz help me out yr plzzzzz...
:rolleyes::rolleyes: c_viv if you had taken the time to read this thread it might have occured to you that i cant even get the relatively simple ADC to work!!! so how likely do you feel it is that i am capable of doing code for blue tooth and a mobile phone???
Also had you read the whole thread it might have dawned on you that i am using pics AT's!! now i dont mind thread highjackers i have even been known to highjack the odd myself now and then, but what i do have objections to is someone highjacking my thread AND not taking the trouble to read my pointless ramblings!
right now thats off chest i will offer you something that may be of help..........
1. start a new thread

2. drop the plzzzzzzzzzzzzzzzzzzz's (it just annoys the people you want help from.

3. offer as much detail about your project as you can. This includes saying exactly what you are trying to do. if you have a diagram post it, if you have any code at all then post that as well using CODE TAGS.

4. If for some strange reason you find people dont want to do all your work for you free of charge (i can see no reason why they wouldnt, can you?) then as a last resort goto one of the programmers for hire forum and ask them nicely to do it for you :D

5. GET THE HELL OUT OF MY THREAD!! :D:D
 
hmm read this again, sorry c_viv i must have been grumpy this morning!! i blame the pill's ;) good luck with the project sorry i cant help
 
ok i am going back to square 1 and stripping out the code. i will probally have a go without using the libs and ive seen alot of conflicting stuff about them so probaly easier to just do it manually then worry about reading the results after!
 
i think i am going to admit defeat on this!! i will probally leave pics alone untill i can afford a half decent compiler! i am becoming less and less of a fan of c18 :D i cant think what else i could of tried i have tried using the lib as per the sheets and just writeing directly to the registers as per datasheet i removed the temp sensor and used a voltage divider and still no luck :S
 
Try Swordfish BASIC. I recall issue #2 of JPUG (on my site) has a 4 channel A/D datalogger with serial out. The real bonus is the free SE version supports 256bytes of RAM which just happens to be the total in a 18F1320.
It's worth a try and the IDE is excellent (setup for one button <F12> compile & programming is detailed the Junebug manual)
 
ok thx bill i will have a look at it, the one i tried and liked best was hitide with hitech c but dosnt look like you can download hitide now :( maybe time to switch to basic :D at least for logans project, my incubator projects use bigger chips and more mem tho
 
i am still not getting this to work! i am pretty sure i am missing something simple tho. this is the full code but be warned its still very messy and unfinished!

Code:
#include <p18cxxx.h>
#include <pwm.h>
#include <timers.h>
#include <delays.h>
#include <adc.h>
#include <usart.h>
#pragma config WDT = OFF, OSC = INTIO2, DEBUG =ON,MCLRE =ON,PWRT = OFF,BOR = OFF

int temp1;
int loop=1;  // variable to count how many times program has looped
void main (void)
{
char DutyCyc = 0xffff;
TRISBbits.TRISB2 =0;
TRISAbits.TRISA0 =1;
OSCCON = 0x72; //8MHz clock

ADCON1=0x7e;		//set RA0 to analogue

Nop();
OpenADC( ADC_FOSC_8 &
ADC_RIGHT_JUST &
ADC_VREFPLUS_VDD & 
ADC_VREFMINUS_VSS &
ADC_12_TAD,
ADC_CH0 &
ADC_INT_OFF, [COLOR="Red"]7e[/COLOR] );  //This is a bit mask for the value   ADCON1, this was setting AN0 as digital.

//Nop();
TRISBbits.TRISB0=0;
LATBbits.LATB2 =1;
TRISBbits.TRISB3=0;
// Open the USART configured as 8N1, 9600 baud
OpenUSART ( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,25);

LATBbits.LATB2=1;
SetOutputPWM1 (SINGLE_OUT, PWM_MODE_1);
 OpenPWM1(DutyCyc);
while(1)
	{
        putcUSART(", temp1");
		SetChanADC(ADC_CH0);
		Delay10TCYx( 50 );
		ConvertADC(); // Start conversion
		while( BusyADC() ); // Wait for ADC conversion// Wait for ADC conversion
		temp1 = ReadADC(); // Read result and put in temp
	SetDCPWM1(DutyCyc);
		loop=loop++;
	
			if(loop==500)  //800 for led
				{
					LATBbits.LATB0=0; //heart beat led 
Nop();
				}
			if (loop==700) // 1000 for led
				{
					LATBbits.LATB0=1; //heartbeat led
					loop=1;
				}
	}
}

one thing i have noticed tjho is i set
ADCON1=0x7e;
but in the debugger when its stopped i get ADCON1=0x7e; in the watch window. the variabletemp1 always shows a 0 or 1023 so i am assumming that somehow i am not setting AN0 to anologue? anyone got any ideas?

Hi Bill, Burt, Ghostman(glad you are hale and hearty)..this is what i think is the problem. You were reconfiguring ADCON1. If it doesnt work out still, post the proteus file and i can work on that as well.
 
Status
Not open for further replies.

Latest threads

Back
Top