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.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?
thanx futzfutz 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.
Hehengkhlaif said:thanx futz
can i ask u what is ur 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 !!futz said:HehPlumbing 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 !!!futz said:HehPlumbing and heating (hydronic). Not microcontrollers or computers or programming or web development. Those are hobbies. Well... web work is a part-time job.
#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);
}
}
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!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!!!
TRISE = 0;
TRISD = 0;
TRISB = 0;
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 wayfutz 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
to thisCode:TRISE = 0; TRISD = 0; TRISB = 0;
Code:TRISB = TRISD = TRISE = 0;
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.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
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.
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.engkhlaif said:you mean by one byte number the content of the ADRESH!!
You ain't seen nothin yet!i have a great headache from this point !! my graduation project stops at this point bcoz it it very crtitical !!
#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++){}
}
}
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???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
Check to be sure your osc is running properly (with a blinky test program or something). My code was tested with a 4MHz crystal.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!!!
It means you should start your own thread.
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!!!
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?