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.
ok i will change my connections -RB0RB3
realy iam very happy for ur help
thank u very much
i will test it in the university on monday
i want to ask u
how can i but a programmer for pic 16f877a ???
 
blueroomelectronics said:
Wouldn't it be easier just to fix the code to work with your setup?

Do you mean build a programmer for the 16F877A?
i think it will be easier to change the connection rather that change code
about the programmer i mean that i want to buy some one or if not how can i build my own one!!!!
 
engkhlaif said:
how can i but a programmer for pic 16f877a ???
Did you mean to say, "How can I buy a programmer for PIC 16F877A?"

Get a **broken link removed** or a PICkit2 clone, like a Blueroom Electronics' Junebug. They're cheap and very, very good programmer/debuggers. They're extremely well supported by Microchip and work with pretty much all the PICs (the Junebug won't do 3.3V PICs though). You can buy direct from Microchip too. There may be a Microchip Direct dealer in your country. If not, any place where you can buy a PIC chip should be able to order you a PICkit2.

Don't bother building a crappy JDM programmer. You'll spend all your time fighting with it to try to make it work.
 
Last edited:
engkhlaif said:
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);

}

}
Hello there,
No mather what the compiler is, or what microcontroller you use (and even what language you use), when you perform (x/255.0)*(5.0) since variavel x is an unsigned char and since it comes in first place, when you divide it by a float, the result still comes in an unsigned char and so it will always be zero (unless x=255) since it has no decimal part. If you want to perform such a calculation you should start with a float:
voltage=x;
voltage/=255.0;
voltage*=5.0;

(why diviging by 255 and multiplying by 5 though??? why not simply dividing by 51?)
Hope this helps...
 
engkhlaif said:
i think it will be easier to change the connection rather that change code
about the programmer i mean that i want to buy some one or if not how can i build my own one!!!!

That means you don't understand the code. Letting Futz write your code won't help you if you don't know what he's doing. I'm learning C too and I can see where to make the changes required to move the pin assignments. You should take the time to understand what is happening in your program, what's the point of submitting something that's not your work?
 
blueroomelectronics said:
That means you don't understand the code. Letting Futz write your code won't help you if you don't know what he's doing. I'm learning C too and I can see where to make the changes required to move the pin assignments. You should take the time to understand what is happening in your program, what's the point of submitting something that's not your work?
That's VERY simple code. In each of lcd_char and lcd_cmd you have to delete one line and add one line. And in lcd_init there are three lines to change. Very simple stuff. That's all the hint you're getting from me :p LOL
 
futz said:
That's VERY simple code. In each of lcd_char and lcd_cmd you have to delete one line and add one line. And in lcd_init there are three lines to change. Very simple stuff. That's all the hint you're getting from me :p LOL
first thanx Futz
i get it what i want to change
i have to change PORTB to modify it to output to RB4-RB7 and so the rest
thanx again!!!
yaaa!!! when i compile ur code i get the message from the programmer that the size of the HEX exceed the size of PIC (8kb) so i copy some of ur code and paste it in mine !!!
is there any way to skip this issue !!!!
 
By the way...
I don't know the compiler you use, but usually in C language when You print a float one uses %f instead of %d. However I can't be sure about your compiler since it is a version microcontroller aplicable...
Anyway, I would try to change the line:
sprintf(outString,"A2D = %d.%d volts",voltage);

to something like:
sprintf(outString,"A2D = %1.2f volts",voltage);

(since you divide by 51.0 maximum integer part would be 5 that corresponds to 1 digit, and as for the decimal part... who cares about milivolts on a proximity sensor where there is always a significant reading error)
 
José Carlos Pinto Miranda said:
By the way...
I don't know the compiler you use, but usually in C language when You print a float one uses %f instead of %d. However I can't be sure about your compiler since it is a version microcontroller aplicable...
Anyway, I would try to change the line:
sprintf(outString,"A2D = %d.%d volts",voltage);

to something like:
sprintf(outString,"A2D = %1.2f volts",voltage);

(since you divide by 51.0 maximum integer part would be 5 that corresponds to 1 digit, and as for the decimal part... who cares about milivolts on a proximity sensor where there is always a significant reading error)

dear,what is that mean when u type %1.2f and why u multilpy by this factor 1.2!!!!
some of my friends here advice me to compile a posted one pls see page 3 from my post !!!!
 
engkhlaif said:
first thanx Futz
i get it what i want to change
i have to change PORTB to modify it to output to RB4-RB7 and so the rest
thanx again!!!
yaaa!!! when i compile ur code i get the message from the programmer that the size of the HEX exceed the size of PIC (8kb) so i copy some of ur code and paste it in mine !!!
is there any way to skip this issue !!!!
My code comes nowhere near the max size for 16F877. You're doing something weird.
 
José Carlos Pinto Miranda said:
to something like:
sprintf(outString,"A2D = %1.2f volts",voltage);
It's not possible to printf or sprintf floats with Hi-Tech PICC Lite demo/student edition anyway. The libraries aren't included because they would push code size over the max demo limit every time. That's why we went around it with Pommie's very clever method in my code.
 
Last edited:
blueroomelectronics said:
Isn't the limit 2K on the 16F877A with Hi-Tech PICC lite?
Yes, that's it. I'm at a different machine, but my foggy memory tells me that hex wasn't near the limit... Oh, OK! I'll get up and go look. :p

The hex is 6.42K, or 6576 bytes. What it comes to on the chip I don't know, but I had no problems running it. Maybe I was real close to the limit and/or maybe engkhlaif added something, like more code or another library.

EDIT: I just recompiled it to see. The compiler spits out this:
Code:
Memory Summary:
    Program space        used   48Ah (  1162) of   800h words   ( 56.7%)
    Data space           used    3Dh (    61) of    B0h bytes   ( 34.7%)
    EEPROM space         used     0h (     0) of   100h bytes   (  0.0%)
    Configuration bits   used     1h (     1) of     1h word    (100.0%)
    ID Location space    used     0h (     0) of     4h bytes   (  0.0%)
So there's lots of room left. Engkhlaif has done something weird. :p
 
Last edited:
Sorry, I didn't see, before answering to your original post, that there where already that many answers been given...
I did only reply because it was an easy question as C is concerned.
The %1.2f means that when constructing the reply string which is to be placed in the outString, it has to use onde INTEGER digit and two DECIMAL ones separated by a simple decimal point. Since you are using a "float" variable it seems to be the correct way to display the wanted result. It is not a factor but a simple String format modifier.
When you write "%d.%d" and then only put one variable, you're messing all string by trying to build two INTEGERS using only one variable... Usually this could crash a normal C program, or at least mess the display result. Note that %d aplies to integer variable and so to a space in memory using 2 bytes (usually or sometimes 4). By passing a float variable, using %d, it should always display strange results because float is memory mapped on a diferent way (an so uninterpretable as an integer).
By browsing some of the previous pages it seams that someone has already proposed that you use the correct %f specifier instead of %d. The %1.2f is only to avoid unwanted precision (what use would it be to you to get a display information in the form of say 0.21378E+1, with a %f, instead of a clean 2.13 with a %1.2f)
However if Futz says it is not suported by your compiler, then forget the %f. But bear in mind DON'T use %d.%d if on the list you only put ONE variable, that I'm sure is bound to fail.
 
Last edited:
José Carlos Pinto Miranda said:
Sorry, I didn't see, before answering to your original post, that there where already that many answers been given...
Ya, there's a lot to read to find out the context of this thread. My first thought was of course to just print it out as a float. And of course as soon as I tried it the compiler crapped out and the LCD printed "f" instead, because of the missing lib problem.
 
futz said:
. And of course as soon as I tried it the compiler crapped out
and the LCD printed "f" instead, because of the missing lib problem.

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
}
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,fract
,fract2);
	  lcd_puts(number);
	   DelayMs(3000);
	}



}
i have changed ur code Futz so that i can just take the function that convert the reading of ADC and then i compile it and test it
i get the reading
A2D =3.171 volts
this reading appear just when i connect the power to the PIC and befor i connect the sensor to the PIC!!!
 
engkhlaif said:
i have changed ur code Futz so that i can just take the function that convert the reading of ADC and then i compile it and test it
i get the reading
A2D =3.171 volts
this reading appear just when i connect the power to the PIC and befor i connect the sensor to the PIC!!!
Ya, that's probably pretty normal. With nothing connected to that pin at all, mine reads 70 (decimal), which would be around 1.37 volts. Yours has a circuit connected to it and may be different. What is important is this: does it work when you connect the sensor?
 
Last edited:
futz said:
Ya, that's probably pretty normal. With nothing connected to that pin at all mine reads 70 (decimal), which would be around 1.37 volts. Yours has a circuit connected to it and may be different. What is important is this: does it work when you connect the sensor?
dear Futz, i dont connect the sensor bcoz my time has finished in the Lab
tomoro i will try to connect it
also i want ask u?
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?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top