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 said:
if i define PORTA as input do i have to ground all the pins(PORTA) pins except pin A0?
and if i define PORTA=0x01 and i connect sensor output into pin A0 i think that i dont ground the rest, do i?
If you're not using the rest of the pins (except A0) just set TRISA=0x01 to make A0 an input and the rest of PortA's pins outputs. Then you don't have to ground the unused pins.
 
futz said:
If you're not using the rest of the pins (except A0) just set TRISA=0x01 to make A0 an input and the rest of PortA's pins outputs. Then you don't have to ground the unused pins.
thanx futz
can i ask u what is ur job???
 
futz said:
Heh :D Plumbing and heating (hydronic). Not microcontrollers or computers or programming or web development. Those are hobbies. Well... web work is a part-time job.
u know what!! great!!! iam a computer engineering and my graduation prject is using real time operating system and embeded system to control industrial machine !!
iam happy bcoz u help me thanx man!!!
:D :) ;)
 
futz said:
Heh :D Plumbing and heating (hydronic). Not microcontrollers or computers or programming or web development. Those are hobbies. Well... web work is a part-time job.
dear Futz i have compile ur code on my kit board but i get no think i do the connections as ur code !!!
the second thing when i copile ur code on an LAB-X ( this is a complete bradboard ) i get the result A2D=1.9 volts and when i connect the power to it i get the result A2D=3.17 volts!! i dont know why is that reading!!! and why it doesnt change when iam moving an object toward the sensor !! it remains constant!!!
this is the main code
Code:
#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=1;	// select Fosc/2
	ADCON1=0x0E;	// select left justify result. A/D port configuration 0
	ADON=1;		// turn on the A2D conversion module
}
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
}

char number[] = "                ";

void main(void)
{
  // unsigned char outString[20];
    
    int voltage;
    unsigned char a;
    long fract;
	char fract1,fract2;    
init_a2d();	// initialise the A2D module
	GIE=0;

TRISE = 0;
TRISD = 0;
TRISB = 0;
TRISA  = 0x01;;
ADCON1 = 0x0E;
RE2 = 0;
DelayMs(500);
lcd_init();

while(1)
	{
	
	             lcd_clear() ;
                         lcd_goto(0) ;
		 a = read_a2d(0);
                           voltage = a;
		 voltage*=5;
		 fract=((((long)voltage*100)/255)%100);
		 fract1=(fract/10)%10;
		 fract2=(fract)%10;
		 voltage/=255;
		 sprintf(number,"A2d = %d.%d%d volts",voltage,fract1,fract2);
	     lcd_puts(number);
	     DelayMs(1500);
	}
}
 

Attachments

  • delay.c
    271 bytes · Views: 138
  • delay.h
    543 bytes · Views: 143
  • lcd.c
    1.1 KB · Views: 154
  • lcd.h
    271 bytes · Views: 151
  • lcddemo.c
    1.4 KB · Views: 153
engkhlaif said:
dear Futz i have compile ur code on my kit board but i get no think i do the connections as ur code !!!
the second thing when i copile ur code on an LAB-X ( this is a complete bradboard ) i get the result A2D=1.9 volts and when i connect the power to it i get the result A2D=3.17 volts!! i dont know why is that reading!!! and why it doesnt change when iam moving an object toward the sensor !! it remains constant!!!
We're back to this. There's no way for me to say why your circuit and/or code won't work. My hardware is different. Yours is far, far away and I can't see it. You'll have to go over it extremely carefully and find out why it isn't working. This debugging part is the funnest part of this stuff. It's a challenge! :D

This is the part where, if you didn't already (because you downloaded somebody else's code), you really get to know your code, in detail.

Again, try to break it down into smaller sub-sections. Get it to absolute minimum - just the AD and the LCD displaying only the value in ADRESH in decimal. Or better yet, just the AD and no LCD. Run that in the debugger and see if you're getting the values you expect in ADRESH (0 - 255). Get that working and then add more.

By the way, to save a few lines in your code you can change this
Code:
TRISE = 0;
TRISD = 0;
TRISB = 0;
to this
Code:
TRISB = TRISD = TRISE = 0;
 
Last edited:
futz said:
Again, try to break it down into smaller sub-sections. Get it to absolute minimum - just the AD and the LCD displaying only the value in ADRESH in decimal. Or better yet, just the AD and no LCD. Run that in the debugger and see if you're getting the values you expect in ADRESH (0 - 255). Get that working and then add more.

By the way, to save a few lines in your code you can change this
Code:
TRISE = 0;
TRISD = 0;
TRISB = 0;
to this
Code:
TRISB = TRISD = TRISE = 0;
dear u mean that i have just to use the function that read the adc value and then i have to display this value and if it is between (0-255) iam in the right way :confused:
then i have to complete the code???
 
engkhlaif said:
u mean that i have just to use the function that read the adc value and then i have to display this value and if it is between (0-255) iam in the right way :confused:
What I mean is that you'll have to learn to debug on your own. Debugging is THE MOST IMPORTANT THING YOU MUST LEARN when working with microcontrollers and electronics. Nobody else can debug for you. It's your circuit and your software. Go through it with a fine tooth comb and find where it's messing up. Make little test programs and simplify it as much as you can so you can see what each section of your code is doing by itself.

Yes, make a tiny little program to read the value and check that it's actually getting what you expect. Do it in the debugger (not simulator) so you don't need all the LCD code. You can just look at ADRESH directly. Stop the program with your hand at different distances from the sensor and look at ADRESH to see if it's changing the way you expect.

If that works then put the LCD code back in and try just displaying that ADRESH value with no voltage conversions. Just the one byte number. See if it changes when you move your hand in front of the sensor.
 
Last edited:
futz said:
What I mean is that you'll have to learn to debug on your own. Debugging is THE MOST IMPORTANT THING YOU MUST LEARN when working with microcontrollers and electronics. Nobody else can debug for you. It's your circuit and your software. Go through it with a fine tooth comb and find where it's messing up. Make little test programs and simplify it as much as you can so you can see what each section of your code is doing by itself.

If that works then put the LCD code back in and try just displaying that ADRESH value with no voltage conversions. Just the one byte number. See if it changes when you move your hand in front of the sensor.

dear,
you mean by one byte number the content of the ADRESH!!
ok il will test it tomoro in the lab and i hope i get the result u know what !! i have a great headache from this point !! my graduation project stops at this point bcoz it it very crtitical !!
thanx again and iam happy to be ur friend !!! :p
 
engkhlaif said:
you mean by one byte number the content of the ADRESH!!
I guess. ADRESH is one byte. It's the MSByte of the ADRESH/ADRESL pair of registers where the ADC results are placed after each conversion. Since you left justified (last I looked) your ADC result, you can pretty much ignore ADRESL and just use ADRESH. Good enough for many things. For more accuracy you'd have to use them both and write different code.

i have a great headache from this point !! my graduation project stops at this point bcoz it it very crtitical !!
You ain't seen nothin yet! :D Wait till you have to debug something really complex!
 
Dear futZZ
i have connect the LCD with My PIC16F877A and i expect to say a string that i try to print it
and here is the code u help me with i dont know why the LCD doens display any thing just a blank i have coonect the pins as shown below:
LCD PIC
1 GND
2 vcc
3 potentiometer
4 RS RD0
5 RW RD1
6 E RD2
and i connect the data as B4-B7 with lcd pins 11-14
i dont know what is the problem
Code:
#include <pic.h>
//#include <htc.h>
#include <stdlib.h>

__CONFIG(XT&WDTDIS&LVPDIS);

void lcd_line1(void);				//function prototypes
void lcd_line2(void);
void lcd_cmd(unsigned char);
void lcd_char(char);
void e_togg(void);
void lcd_init(void);
void lcd_string(char *);
void lcd_busy(void);
unsigned char read_a2d(void);
void lcd_16number(int);
void delay(void);

#define	E		RD2
#define	RS		RD0
#define RW		RD1
#define	busyflag	RB3
#define RW_TrisBit	TRISD1
#define D7_TrisBit	TRISB3

char number[] = "     ";

int main(void)
{
	char x;
	unsigned char a;
	ADCON1 = 0b00001110;
	ADCON0 = 0b10000000;
	ADON = 1;
	TRISB = TRISD = 0;
	RW = 0;					//set R/W low
	E = 0;					//set E low
	lcd_busy();				//wait for LCD to settle
	lcd_init();

	while(1)
	{
		a = read_a2d();
		lcd_line1();
		lcd_16number(a);
		lcd_string(number);
      	lcd_string("dd");	  // i try tp prent any thing here 
      delay();
	}
}

unsigned char read_a2d(void)
{
	ADGO=1;
	while(ADGO){}
	return(ADRESH);
}

void lcd_string(char *senpoint)
{
	while(*senpoint != '\0')
	{
		lcd_char(*senpoint);
		senpoint++;
	}
}	

void lcd_busy(void)
{
	RW_TrisBit = 1;					//make R/W input (read)
	D7_TrisBit = 1;					//make D7 input
	RS = 0;						//set RS low
	RW = 1;						//set R/W high
	E = 1;						//set E high
	while(busyflag);				//wait for busy flag to go low
	E = 0;						//set E low
	RW = 0;						//set R/W low
	TRISB = 0;					//make D7 output
	RW_TrisBit = 0;					//make R/W output (write)
}	
	
void lcd_16number(int num)
{
	number[0]=(char)(abs(num/10000)+0x30);
	num=num-abs(num/10000)*10000;
	number[1]=(char)(abs(num/1000)+0x30);
	num=num-abs(num/1000)*1000;
	number[2]=(char)(abs(num/100)+0x30);
	num=num-abs(num/100)*100;
	number[3]=(char)(abs(num/10)+0x30);
	num=num-abs(num/10)*10;
	number[4]=(char)(num+0x30);
}

void lcd_line1(void)
{
	lcd_cmd(0x80);
}

void lcd_line2(void)
{
	lcd_cmd(0xc0);
}		

void lcd_cmd(unsigned char letter)
{
	lcd_busy();
	PORTB = letter;					//put char in PORTB
	PORTB = PORTB >> 4;				//shift over to output high 4 bits
	RS = 0;						//RS low
	e_togg();					//latch the data
	lcd_busy();
	PORTB = letter;
	RS = 0;						//RS low
	e_togg();					//latch it
}

void lcd_char(char letter)
{
	lcd_busy();
	PORTB = letter;					//put char in PORTB
	PORTB = PORTB >> 4;				//shift over to output high 4 bits
	RS = 1;						//RS high
	e_togg();					//latch the data
	lcd_busy();
	PORTB = letter;
	RS = 1;						//RS high
	e_togg();					//latch it
}

void lcd_init(void)
{
	PORTB = 0x03;					//send 3
	e_togg();
	lcd_busy();
	PORTB = 0x03;
	e_togg();
	lcd_busy();
	PORTB = 0x03;
	e_togg();
	lcd_busy();
	PORTB = 0x02;					//send 2 - set 4-bit mode
	e_togg();
	lcd_busy();
	lcd_cmd(0x28);					//set 4-bit mode and 2 lines
	lcd_busy();
	lcd_cmd(0x10);					//cursor move & shift left
	lcd_busy();
	lcd_cmd(0x06);					//entry mode = increment
	lcd_busy();
	lcd_cmd(0x0d);					//display on - cursor blink on
	lcd_busy();
	lcd_cmd(0x01);					//clear display
	lcd_busy();
}

void e_togg(void)
{
	E=1;
	E=0;
}

void delay(void)
{
	int x,y;
	for(x=0;x<1;x++)
	{
		for(y=0;y<10000;y++){}
	}
}
 
also i want to add that is that possible the miss in functionality of the LCD because of crystal ocillator ??? i use 4MHz one
aby one can help in this !!!
 
engkhlaif said:
Dear futZZ
i have connect the LCD with My PIC16F877A and i expect to say a string that i try to print it
and here is the code u help me with i dont know why the LCD doens display any thing just a blank i have coonect the pins as shown below:
LCD PIC
1 GND
2 vcc
3 potentiometer
4 RS RD0
5 RW RD1
6 E RD2
and i connect the data as B4-B7 with lcd pins 11-14
i dont know what is the problem
Well I know exactly what the problem is. It's the same problem I told you about before. You haven't fixed it still. Your lcd_init(), lcd_busy(), lcd_cmd() and lcd_char() functions expect the data lines to be at RB0 to RB3. If you connect the LCD's data lines to RB4 to RB7 instead, then why would you ever expect the LCD to do anything at all??? :confused: :confused: It just isn't gonna happen. The program doesn't magically know that you changed the hardware around.

Computers do exactly what you tell them to do, not what you think you told them to do.

You have to either make some small modifications to the above mentioned routines in the code to put the data in the correct bits of portb or move your data lines to where the code expects them to be.
 
Last edited:
dear futz i have connect them also to RB0 -RB3 with LCD pins (11-14) but i get the same results !!! no thing on the LCD
is that possible to be becoz of crystal oscillator!!!
 
engkhlaif said:
dear futz i have connect them also to RB0 -RB3 with LCD pins (11-14) but i get the same results !!! no thing on the LCD
is that possible to be becoz of crystal oscillator!!!
Check to be sure your osc is running properly (with a blinky test program or something). My code was tested with a 4MHz crystal.
 
i keep getting an error on my sprintf function
sprintf(outString,"A2D = %d.%d volts",voltage);
Error [1098] ../../common/printf.c; 14. conflicting declarations for variable "_sprintf" (C:\Documents and Settings\Owner\Desktop\HI-TECH Software\PICC\PRO\9.60\samples\LCDemo\main.c:70)

what does that mean?
 
could someone please help me?
i got the code to run, i forgot #include<stdio.h> duh!
now im not sure how display 0-5 volts i read over the previous posts and am still unsure i have a picdem 2 plus demo board and have been working with it for a couple of days,
the original math conversion for voltage gives me 71, i tried the futz code conversion and get nothing does anyone have a completed code? please!!!
 
could someone please help me?
i got the code to run, i forgot #include<stdio.h> duh!
now im not sure how display 0-5 volts i read over the previous posts and am still unsure i have a picdem 2 plus demo board and have been working with it for a couple of days,
the original math conversion for voltage gives me 71, i tried the futz code conversion and get nothing does anyone have a completed code? please!!!

How many bits of the adc are you using?

If your max voltage to read is 5v then divide it out to get volts per step.

After your adc conversion you multiply the adc number by the volts per step.

For 8 bits (255) it would be 5v/255 = .0196 v/step.

You can scale the input for larger voltages, simple ohms law and math.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top