Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
i think it will be easier to change the connection rather that change codeblueroomelectronics 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?
Did you mean to say, "How can I buy a programmer for PIC 16F877A?"engkhlaif said:how can i but a programmer for pic 16f877a ???
Hello there,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);
}
}
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'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 LOLblueroomelectronics 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?
first thanx Futzfutz 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 LOL
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)
My code comes nowhere near the max size for 16F877. You're doing something weird.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 !!!!
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.José Carlos Pinto Miranda said:to something like:
sprintf(outString,"A2D = %1.2f volts",voltage);
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.blueroomelectronics said:Isn't the limit 2K on the 16F877A with Hi-Tech PICC lite?
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%)
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.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...
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.
#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);
}
}
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?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!!!
dear Futz, i dont connect the sensor bcoz my time has finished in the Labfutz 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?