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.

PIC16F877A ADC MSB not returning any value

Status
Not open for further replies.

SilverWingz

New Member
I am a pic beginner working on a 16F877A trainer kit.
I Intended to write an ADC code on MPLab,the MSB of which i intended to feed to the PWM module's CCPR1L register to have manual pwm control.
I managed to get the PWM code running,however,got stuck on the ADC section.

Problem:ADRESH byte seems to send nothing to PortB

Here is my code:
Code:
#include <htc.h>
#ifndef _XTAL_FREQ

 #define _XTAL_FREQ 20000000
#endif

void init(void)
{

	TRISA = 0xFF;			//PortA as Input
	TRISB = 0x00;			//PortB as Outputs
	PORTB = 0x00;			//Initialise

	ADCON1 = 0b00000000;	//left justified,all analog
	ADCON0 = 0b10101001;	//Fosc/16 ; pin5 as analog in ;
	
}


void main(void)
{

	init();

	while(1){
		__delay_ms(2);			//wait
		GODONE=1;		//start conversion
		while(GODONE);	//wait for conversion to complete
		PORTB=ADRESH;	//transfer to portB for dilslay on LED
	}
}

Can anyone please point out if i am making any stupid logical errors?

I hope i got the polling format right...i was just following the PIC datasheet

Thanks in advance
 
*facepalm*
i had this nagging feeling i was doing something silly.
i was getting RA5 and AN5 mixed up.
it was RA5, i.e. AN4 that i wanted.

made the correction:ADCON0 = 0b10100001

thanks a bunch.works just fine now.
 
continuing with the same project...i was hoping that if i kept refreshing the value of the duty cycle:CCPR1L = ADRESH,I would be able to get manual PWM control...
so this is the code that i came up with:
Code:
#include <htc.h>
#ifndef _XTAL_FREQ

 #define _XTAL_FREQ 20000000
#endif

void adc_init(void)
{

	TRISA = 0xFF;			//PortA as Input
	TRISB = 0x00;			//PortB as Outputs
	PORTB = 0x00;			//Initialise


	ADCON1 = 0b00000000;	//left justified,all analog
	ADCON0 = 0b10100001;	//Fosc/16 ; pin5 as analog in ;
	
}


void main(void)
{
	adc_init();
	/////////////////////////setting up the pwm next for ccp1
	TRISC = 0x00;           // set PORTC as output
    PORTC = 0x00;           // clear PORTC
	PR2 = 0b10011011 ;		//period
	T2CON = 0b00000111 ;	//timer2 on,prescaler 16
	CCP1CON = 0b00111100 ;	//pwm enable
	//////////////////////////going into loop to refresh adc value as well as duty cycle
	while(1)
	{
		__delay_ms(2);			//wait
		GODONE=1;		/* enable A2D converter */
		while(GODONE);	//wait for conversion to complete
		CCPR1L=ADRESH;	//update duty cycle from adc value
	}
}

as before,ccp1 pin is providing no o/p
i am not sure whether this code is structurally sound.Is the pwm supposed to work if i keep refreshing the value inside the loop?
my concept of the workings of each module is sketchy atm,which is why im having trouble visualising the dry run of the program.

please point out any obvious flaws if u see them.
i will go through the datasheet again in the meantime.
i'm not thinking straight atm.need some coffee.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top