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.
thx wond3erboy i will zip it all up and post it later. the realy annoying thing is i have used the ADC on the 18f4685 without any trouble! one good thing is its given me alot of practice reading datasheets and i understand them slightly more than i did!
 
i wasnt feeling upto looking at this much today, but have had a good look tonight, it deff seems like wond3rboy smacked the nail on the head a goodun! it now seems to work however would someone mind explaing bit masks in relation to 7e and adcon1, i assumed if i set ADCON1 to 7e it would make ANO analogue, i got round it by using ADC_1ANA wich works and peeking at the reg tells me its 126 but how are theese values arrived at? sorry for not framing the question well, but i am strugling with language tonight and indeed have done all day!
btw i havnt tried it in the real circuit yet but it shows differing values in proteus when i use a pot as the input :D
 
Last edited:
forgot prteus file :D

hmm keeps telling me invalid file when i try to upload! i will keep trying
in the mean time this is the code with working ADC again it is very rough,

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, ADC_1ANA );  //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;
				}
	}
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top