caramoan tour package

caramoan tour package

Author Topic: ADC and 16x2 LCD in Hi-Tech C  (Read 7202 times)

Offline drx2k

  • Size D Battery
  • ******
  • Posts: 326
  • Pogi/Ganda Points: 15
  • Gender: Male
    • Drx2k Network
ADC and 16x2 LCD in Hi-Tech C
« on: August 04, 2009, 08:10:57 PM »
Mga Sir, help naman po sa coding ng ADC at LCD gamit ang Hi-Tech C



eto po yung ginamit kong code galing sa mga tutorial at projects dito

Code: [Select]
#include <pic.h>
#include "delay.c"
#include<stdio.h>
//configuration fuse
__CONFIG(XT & WDTDIS & PWRTDIS & UNPROTECT);


static bit LCD_RS @ ((unsigned)&PORTB*8+4); // Register select
static bit LCD_EN @ ((unsigned)&PORTB*8+5); // Enable


#define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))


void lcd_write(unsigned char c)
{
PORTB = (PORTB & 0xF0) |  (c >> 4);
LCD_STROBE;
PORTB = (PORTB & 0xF0) |  (c & 0x0F);
LCD_STROBE;
DelayUs(40);
}


//Clear and home the LCD
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x01); //0x0C for no blinking cursor
DelayMs(2);
}


//write a string of chars to the LCD
void lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}


//write one character to the LCD
void lcd_putch(char c)
{
LCD_RS = 1; // write characters
PORTB = (PORTB & 0xF0) |  (c >> 4);
LCD_STROBE;
PORTB = (PORTB & 0xF0) |  (c & 0x0F);
LCD_STROBE;
DelayUs(40);
}


// Go to the specified position
void lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}


//initialise the LCD - put into 4 bit mode
void lcd_init(void)
{
LCD_RS = 0; // write control bytes
DelayMs(15); // power on delay
PORTB = 0x03; // attention!
LCD_STROBE;
DelayMs(5);
LCD_STROBE;
DelayUs(100);
LCD_STROBE;
DelayMs(5);
PORTB = 0x02; // set 4 bit mode
LCD_STROBE;
DelayUs(40);
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font (0010 1000)
lcd_write(0x08); // display off (0000 1000)
lcd_write(0x0F); // display on, blink curson on
lcd_write(0x06); // entry mode
}


void init_adc(void)
{


GIE=0; // disable interrupts
ADCS1 = 1; //select 32Fosc
ADCS0 = 0;
ADCON1=0; // A/D port configuration 0
ADFM = 1; //right justified result
ADON=1; // turn on the AD conversion module

}


unsigned int read_adc(unsigned char channel)
{
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
DelayUs(10);
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(((ADRESH&0x03)<<8)+ADRESL); // return the 10-bit result
}


void main()
{

int adc_value;
float converted_value;

TRISB &= ~0xFF;
PORTB &= ~0xFF;

lcd_init(); //initialize LCD, set to 4-bit mode
lcd_clear(); //clear LCD
init_adc();

adc_value = read_adc(0);

converted_value = 5 * adc / 1023;

lcd_puts("---Volt Meter---"); //display text in first line
lcd_goto(0x40); //display text in second line
lcd_puts("Voltage: ");


while(1);

}


paano ko kaya maii-display yung value ng variable na converted_value sa LCD in float?


sinubukan ko kasi yung ganito para mai-display sa lcd yung value pero di gumana at may warning sa compilation :-[


Code: [Select]
printf(lcd_puts,"Voltage: %f",converted_value);

yung  converted_value, hindi sya nagbibigay ng value in floating point, ang binibigay lang nya ay "F" kapag ginamit sa

Code: [Select]
printf("%f", converted_value)



Philippine Electronics Forum

ADC and 16x2 LCD in Hi-Tech C
« on: August 04, 2009, 08:10:57 PM »

Offline 0b00000111

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 6129
  • Pogi/Ganda Points: 398
  • There is no delight in owning anything unshared.
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #1 on: August 04, 2009, 11:04:30 PM »
much better po sir check nyo yung manual ng compiler. may nakita ako dating compiler na di supported ang display ng floating-point sa printf... (di ko na maalala kung anong compiler yun)
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Philippine Electronics Forum

Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #1 on: August 04, 2009, 11:04:30 PM »

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #2 on: August 04, 2009, 11:06:13 PM »
try mo nga to... i'm not sure also, how hitech c cast value

Code: [Select]
lcd_puts("Voltage: %f",(float)converted_value);
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Philippine Electronics Forum

Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #2 on: August 04, 2009, 11:06:13 PM »

Offline 0b00000111

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 6129
  • Pogi/Ganda Points: 398
  • There is no delight in owning anything unshared.
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #3 on: August 04, 2009, 11:10:28 PM »
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Philippine Electronics Forum

Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #3 on: August 04, 2009, 11:10:28 PM »

Offline drx2k

  • Size D Battery
  • ******
  • Posts: 326
  • Pogi/Ganda Points: 15
  • Gender: Male
    • Drx2k Network
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #4 on: August 04, 2009, 11:18:24 PM »
try mo nga to... i'm not sure also, how hitech c cast value

Code: [Select]
lcd_puts("Voltage: %f",(float)converted_value);

may error pa din po

Code: [Select]
Error   [186] C:\Documents and Settings\Mark\Desktop\PIC16F877A with 16x2 LCD using 1 PORT, ADC\Program1\Main.c; 130.46 too many function arguments

Philippine Electronics Forum

Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #4 on: August 04, 2009, 11:18:24 PM »

Offline drx2k

  • Size D Battery
  • ******
  • Posts: 326
  • Pogi/Ganda Points: 15
  • Gender: Male
    • Drx2k Network
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #5 on: August 04, 2009, 11:21:25 PM »

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #6 on: August 04, 2009, 11:25:11 PM »
Code: [Select]
void main()
{

int adc_value;
float converted_value;

TRISB &= ~0xFF;
PORTB &= ~0xFF;

lcd_init(); //initialize LCD, set to 4-bit mode
lcd_clear(); //clear LCD
init_adc();

adc_value = read_adc(0);

converted_value = 5 * adc_value / 1023;   // <<<<<<<<<<<<CORRECTION LANG

lcd_puts("---Volt Meter---"); //display text in first line
lcd_goto(0x40); //display text in second line
lcd_puts("Voltage: %f",converted_value);


while(1);

}
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #7 on: August 05, 2009, 08:23:25 AM »
dry2k,

use sprintf() to convert the float value to ascii before displaying to LCD. The LCD will only accept character (ascii) data.

Also, enable the use of float in the project build configuration
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #8 on: August 05, 2009, 10:43:21 AM »
dry2k,

use sprintf() to convert the float value to ascii before displaying to LCD. The LCD will only accept character (ascii) data.

Also, enable the use of float in the project build configuration

i see...

sanay pa ako sa CCS ah. sa ccs kasi, printf() to LCD is allowed.

or is it the LCD driver that is working???
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #9 on: August 20, 2009, 05:11:59 PM »
parang may problem dun sa main loop ???

'di ba dapat ganito:
Code: [Select]
main()
{
   //
   < initialize >
   //
   while(1)
   {
        < read adc >
        < display adc reading >
   }
}
* yung read_adc() saka lcd_puts sa 2nd line ng lcd eh dapat nasa loob ng while(1) loop
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline marcelino

  • Moderator
  • Solar Power Satellite
  • *****
  • Posts: 6016
  • Pogi/Ganda Points: 258
  • ...keep moving forward! - Robinson's
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #10 on: August 20, 2009, 06:49:08 PM »
parang may problem dun sa main loop ???

'di ba dapat ganito:
Code: [Select]
main()
{
   //
   < initialize >
   //
   while(1)
   {
        < read adc >
        < display adc reading >
   }
}
* yung read_adc() saka lcd_puts sa 2nd line ng lcd eh dapat nasa loob ng while(1) loop

ay oo nga... one shot lang yung gawa ko... di nagrerefresh! heheh ;D
"Don't take life seriously. After all, no one has ever come out of it alive. -Bugs Bunny"

Offline clarkent

  • Lead Acid Battery
  • *******
  • Posts: 505
  • Pogi/Ganda Points: 20
  • Gender: Male
  • iehs
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #11 on: August 23, 2009, 12:28:32 PM »
gumagana po ba ang lcd sample code ng hitech c? nung ginamit ko po kasi yung library di naman gumagana.
if you think you can or think you can't, you're probably right -h.ford

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #12 on: August 23, 2009, 01:16:08 PM »
gumagana po ba ang lcd sample code ng hitech c? nung ginamit ko po kasi yung library di naman gumagana.

yup it works fine.

pero kailangan nasa same project folder ang mga files na ito:
- lcd.h
- lcd.c
-delay.h
-delay.c

together with your main.c program
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Offline clarkent

  • Lead Acid Battery
  • *******
  • Posts: 505
  • Pogi/Ganda Points: 20
  • Gender: Male
  • iehs
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #13 on: August 23, 2009, 01:31:11 PM »
nung ginamit ko po kasi sya sir paranz wala namang lumalabas sa lcd. i even tried first the main.c that was in it. yung may ipprint na 1234567 yet wala namang lumalabas sa lcd.
if you think you can or think you can't, you're probably right -h.ford

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #14 on: August 23, 2009, 01:41:29 PM »
^ tinest mo sa actual hardware? or simulation sa proteus?
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Offline clarkent

  • Lead Acid Battery
  • *******
  • Posts: 505
  • Pogi/Ganda Points: 20
  • Gender: Male
  • iehs
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #15 on: August 23, 2009, 01:58:33 PM »
sa proteus lang po sir paranz.

pad ccsc po kasi ang gamit ko gumagana yung 12x2 eh.(sa proteus ulit) ;D
if you think you can or think you can't, you're probably right -h.ford

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #16 on: August 23, 2009, 02:03:10 PM »
check mo ang freq ng pic sa simulation, tapos yun din ang gamitin mo sa delay.h. Just in case na walang output sa lcd due to timing problem
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Offline clarkent

  • Lead Acid Battery
  • *******
  • Posts: 505
  • Pogi/Ganda Points: 20
  • Gender: Male
  • iehs
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #17 on: August 23, 2009, 02:17:15 PM »
so kailangan ko pong palitan yung freq na nakalagay sa delay.h sir? ang usual ko nmn pong ginagamit is 4Mhz sir.
if you think you can or think you can't, you're probably right -h.ford

Offline paranz

  • Moderator
  • Nuclear Reactor
  • *****
  • Posts: 4525
  • Pogi/Ganda Points: 177
  • Gender: Male
  • 1/4W resistor specialist
    • RapidSignal Electronics
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #18 on: August 23, 2009, 02:27:25 PM »
so kailangan ko pong palitan yung freq na nakalagay sa delay.h sir? ang usual ko nmn pong ginagamit is 4Mhz sir.

yup ganun nga. 4mhz sa PIC tapos 4 mhz sa delay.h

and make sure na tama ang connections, between PIC and LCD. By default, ito ang connections ng sample LCD code sa hitech

  • RB0 - D4
  • RB1 - D5
  • RB2 - D6
  • RB3 - D7
  • RA2 - RS
  • RA3 - EN

Pwede naman kahit anong output pin ang gamitin mo for the LCD EN and RS control pins, you can change that by modifying the code

Code: [Select]
static bit LCD_RS @ ((unsigned)&PORTA*8+2); // Register select
static bit LCD_EN @ ((unsigned)&PORTA*8+3); // Enable

sa loob ng lcd.c

Tapos make sure na initialized ang mga PIC pins above as output sa main.c
PIC16 Programming Tutorial using MPLAB and Hi-Tech C
www.rapidsignalph.com/tutorials/pic16-tutorials

Arduino & gizDuino Tutorials
www.rapidsignalph.com/tutorials/arduino-tutorials

Offline clarkent

  • Lead Acid Battery
  • *******
  • Posts: 505
  • Pogi/Ganda Points: 20
  • Gender: Male
  • iehs
Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #19 on: August 23, 2009, 03:03:56 PM »
ok po sir, subukan ko po ulit.
if you think you can or think you can't, you're probably right -h.ford

Philippine Electronics Forum

Re: ADC and 16x2 LCD in Hi-Tech C
« Reply #19 on: August 23, 2009, 03:03:56 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com