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.

urgent help in Code!!!!

Status
Not open for further replies.

engkhlaif

New Member
i want to write a c code that configure and gp2d12 IR sensor that will be used to detect the distance of the moving object and then convert the analog voltage into digital and then into distance
iam using the MPLAB and C language
when i use the folowing code the reading always gives
voltage=0;
i dont know why
pls ny one can help me

#include <pic.h>
#include "lcd.h"
#include "delay.h"
#include<stdio.h>

//setup the configuration word for use with ICD2

__CONFIG(DEBUGEN & WDTDIS & LVPDIS ) ;


void init_a2d(void){
ADCON0=0; // select Fosc/2
ADCON1=0; // select left justify result. A/D port configuration 0
ADON=1; // turn on the A2D conversion module
}

/* Return an 8 bit result */
unsigned char read_a2d(unsigned char channel){
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); // return 8 MSB of the result
}


void main (void)
{

unsigned char outString[20];
float voltage;
unsigned char x;
init_a2d(); // initialise the A2D module
GIE=0;
TRISE = 0 ;
TRISD = 0 ;
TRISB = 0 ;
TRISA = 0 ;
ADCON1 = 7 ;
RE2 = 0 ;
RA0=1;
DelayMs(500) ;
lcd_init() ;

while(1)
{
lcd_clear() ;
lcd_goto(0) ;
lcd_puts("welcome " ) ;
x=read_a2d(1);
voltage=(x/255.0)*(5.0);
sprintf(outString,"A2D = %d.%d volts",voltage);
lcd_puts(outString);
//lcd_puts("Real time ") ;
DelayMs(1500);

}

}
 
Please post code with Code tags. Simply click on # in the Advanced Posting menu before pasting your code.

Like this:
Code:
#include <pic.h>
#include "lcd.h"
#include "delay.h"
#include <stdio.h>

//setup the configuration word for use with ICD2

__CONFIG(DEBUGEN & WDTDIS & LVPDIS ) ;

void init_a2d(void)
{
	ADCON0=0;			// select Fosc/2
	ADCON1=0;			// select left justify result. A/D port configuration 0
	ADON=1;				// turn on the A2D conversion module
}

/* Return an 8 bit result */
unsigned char read_a2d(unsigned char channel)
{
	channel&=0x07;			// truncate channel to 3 bits
	ADCON0&=0xC5;			// clear current channel select
	ADCON0|=(channel<<3);		// apply the new channel select
	ADGO=1;				// initiate conversion on the selected channel
	while(ADGO)continue;
	return(ADRESH);			// return 8 MSB of the result
}

void main (void)
{
	unsigned char outString[20];
	float voltage;
	unsigned char x;
	init_a2d();			// initialise the A2D module
	GIE=0;
	TRISA = TRISB = TRISD = TRISE = 0;
	ADCON1 = 7;
	RE2 = 0;
	RA0=1;
	DelayMs(500);
	lcd_init();

	while(1)
	{
		lcd_clear();
		lcd_goto(0);
		lcd_puts("welcome ");
		x=read_a2d(1);
		voltage=(x/255.0)*(5.0);
		sprintf(outString,"A2D = %d.%d volts",voltage);
		lcd_puts(outString);
		//lcd_puts("Real time ");
		DelayMs(1500);
	}
}

Unfortunately with C code the 8-space tabs used by this site will usually mess up your 4-space (or 2 or 3 or whatever you use) C tabs somewhat. But it's better than no indentation at all. :D
 
Last edited:
Unfortunately with C code the 8-space tabs used by this site will usually mess up your 4-space (or 2 or 3 or whatever you use) C tabs somewhat. But it's better than no indentation at all. :D[/QUOTE]

i dont understand ur openion!!!!!!!!!!!!
 
engkhlaif said:
i dont understand ur openion!!!!!!!!!!!!
Scroll up and look at your original post and then the indented C code in mine. Think about it for a bit. It'll come to you. :p

I have a GP2D12 here and was thinking of writing some code for it anyway. In C even. But that doesn't help you right now. I may have it working by tonight. Maybe...
 
futz said:
Scroll up and look at your original post and then the indented C code in mine. Think about it for a bit. It'll come to you. :p

I have a GP2D12 here and was thinking of writing some code for it anyway. In C even. But that doesn't help you right now. I may have it working by tonight. Maybe...[/QUOT
first of all thanx for every thing
then what i want to say is that i have connect the sensor to a pic as you know: sensor output to A0 of PIC16f877a and the other pins one for ground and the other to voltage source
and then i want to display the recored voltage into an LCD and when i configure and installed the HEX file into the PIC it hust prints
"Wellcome volts =0" iam crazy with it
i dont know what to do i have to submit it to my instructor by monday
 
i have attached all thee needed file to compile my project
iam using MPLAB v7.31 with c language
 

Attachments

  • ADC.c
    1.4 KB · Views: 296
  • delay.c
    266 bytes · Views: 180
  • delay.h
    543 bytes · Views: 207
  • lcd.c
    1.6 KB · Views: 208
  • lcd.h
    309 bytes · Views: 197
futz said:
Well you've procrastinated maybe a bit too much. :D

u know what i have worked for it since 3 days or more and i cant took the result i need
so pls if u can help me !!!!!! i will appreciate that for u man
 
engkhlaif said:
PIC16F877A
The only C I have is for 18F and 24F/30F. The only one I've actually used at all is C30 for 24F/30F. I do all my 16F stuff in assembler. So I'll work at that and see if I can come up with something you can sort of port to 16F. No promises. I might get interrupted in an hour or two and nothing more will happen today.
 
futz said:
The only C I have is for 18F and 24F/30F. The only one I've actually used at all is C30 for 24F/30F. I do all my 16F stuff in assembler. So I'll work at that and see if I can come up with something you can sort of port to 16F. No promises. I might get interrupted in an hour or two and nothing more will happen today.

thanx for every thing man
 
Somebody was already thinking of you

**broken link removed**
 
You chose a bad title for your thread. Some people get funny about it. Personally, your question was valid and I have had a quick look at your code.

The thing that sticks out is ADCON1=7; this sets all analogue pins to digital.

Mike.
 
Pommie said:
You chose a bad title for your thread. Some people get funny about it. Personally, your question was valid and I have had a quick look at your code.

The thing that sticks out is ADCON1=7; this sets all analogue pins to digital.

Mike.

I'm one of those "funny" people, I find "urgent" = school assignment. The OP should spend a moment figuring out how to use the debugger, it's a powerful tool when used properly.
 
blueroomelectronics said:
I'm one of those "funny" people, I find "urgent" = school assignment. The OP should spend a moment figuring out how to use the debugger, it's a powerful tool when used properly.

But, the guy has written code and asked for help debugging it. What is wrong with that? As I said, he picked the wrong title, the content of his post was fine.

I'm starting to see less and less tolerance of newbies and this is not good. Everyone started somewhere. The guys that post the "PLLLEEEEEZZZZZ" posts never get anywhere, but it now seems to be spreading to what I consider reasonable posts for a newbie. Lighten up guys.

Mike.
 
Last edited:
Pommie said:
You chose a bad title for your thread. Some people get funny about it. Personally, your question was valid and I have had a quick look at your code.

The thing that sticks out is ADCON1=7; this sets all analogue pins to digital.

Mike.
first i want to thank u for ur hint about the tittle
u mean that i have to change ADCON1=7 to ADCON1=0 or what
u know i dont know what to do !!!
i have headache from this problem and tomoro i have to submit it to my instructor
 
Status
Not open for further replies.

Latest threads

Back
Top