PDA

View Full Version : A/D display result on LCD



winsthon
- 28th December 2003, 12:13
Hi Im new to BASIC. I have a problem about PIC16F877 A/D...I want to display the digital output of the analog input signal and convert it as HEX value and display it on the LCD.

I have a code using BASIC but I dont know how to display the HEX equivalent so instead I use # to display the decimal value. can anyone help me how to do this convert the decimal value into its HEX equivalent...Here's my code...

Define ADC_CLOCK = 3 'default value is 3
Define ADC_SAMPLEUS = 10 'default value is 20
Define LCD_BITS = 8 'allowed values are 4 and 8 - the number of data interface lines
Define LCD_DREG = PORTB
Define LCD_DBIT = 0 '0 or 4 for 4-bit interface, ignored for 8-bit interface
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 1
Define LCD_EREG = PORTD
Define LCD_EBIT = 3
Define LCD_RWREG = PORTD 'set to 0 if not used, 0 is default
Define LCD_RWBIT = 2 'set to 0 if not used, 0 is default

Dim an0 As Word
Dim an1 As Word
Dim an2 As Word

TRISA = 0xff 'set all PORTA pins as inputs
ADCON1 = 0 'set all PORTA pins as analog inputs
Lcdinit 'initialize LCD module; cursor is off

loop:
Adcin 0, an0
Adcin 1, an1
Adcin 2, an2
Lcdcmdout LcdClear 'clear LCD display
Lcdout "Hex Value:" 'text for the line 1
Lcdcmdout LcdLine2Home 'set cursor at the beginning of line 2
Lcdout #an0,#an1,#an2 'formatted text for line 2
WaitMs 2000 'larger value should be used in real device
Goto loop 'loop forever


by the way Im using 3 analog inputs...I want to know the HEX value each of the input and display them on the LCD like FF FF FF 2 bytes only per input and display them together on the LCD. I used the PIC simulator IDE 2.9 with built-in BASIC compiler. Whats the difference between this BASIC from PIC simulator IDE and PicBASIC pro.??? Anyone I appreaciate your help.

Melanie
- 28th December 2003, 14:36
OK... this is for PBP... I'm not familiar with your IDE's Basic.

LCDOut $FE,$01 ' Clears your LCD, sets cursor to Line 1 Column 1
LCDOut "Hex Value:" 'text for the line 1
LCDOut $FE,$C0 ' Cursor to Line 2 Column 1
LCDOut HEX2 AN0," ".HEX2 AN1," ",HEX2 AN2 ' Formatted text for line 2
' Note the space between displayed values
Pause 2000

Refer to the PBP manuals LCDOut command to see what I've done.

Melanie

winsthon
- 29th December 2003, 06:39
Originally posted by Melanie
OK... this is for PBP... I'm not familiar with your IDE's Basic.

LCDOut $FE,$01 ' Clears your LCD, sets cursor to Line 1 Column 1
LCDOut "Hex Value:" 'text for the line 1
LCDOut $FE,$C0 ' Cursor to Line 2 Column 1
LCDOut HEX2 AN0," ".HEX2 AN1," ",HEX2 AN2 ' Formatted text for line 2
' Note the space between displayed values
Pause 2000

Refer to the PBP manuals LCDOut command to see what I've done.

Melanie


About the code you gave me...I included it on my code but the compiler PBP cannot understand ADCON1 and LCDINIT. Why can revise it for Im still learning PBP. thanks.

Melanie
- 29th December 2003, 10:34
It's not a case of cutting and pasting somebody's code. Nothing is learnt by doing that. So, I'm going to be a little bit cruel here - but it does aid the learning process... can you find for me the command LCDINIT in section 5 of the PBP manual (to help you the commands are in alphabetical order) - What does it say?

ADCON1 however is valid. The compiler WILL understand ADCON1 once you tell it you're using a pic16F877. By default it looks at a 16F84 which doesn't have an ADCON1 register which is probably why PBP is currently rejecting it.

What version of PBP are you using?

winsthon
- 29th December 2003, 13:35
I found the LCDINIT already thanks for the advice. Im using PicBasic Pro 2.43...

Melanie
- 29th December 2003, 13:46
You gotta tell me what page it's on... *smiles*

winsthon
- 29th December 2003, 13:56
Originally posted by Melanie
You gotta tell me what page it's on... *smiles*

Well we might not be on the same page or same PICbasic manual but when I used LCDINIT on PIC Basic Pro 2.43 it allows to compile and assembled using MPASMWIN. Im using PIC Basic Pro 2.43 on Micro code Studio Plus. I have a question why is it the when I compiled it and assembled it on MPASMwin and run on MPLAB SIM, PBPPIC14.LIB is the one that is being simulated is this how it supposed to work? just curious...I haven't programmed it to my microchip just to make sure...and Oh by the way what PBP you are using and the compiler for ASM? can you give me a link that I can download these tools? Thanks you...

Melanie
- 29th December 2003, 14:36
Firstly, as far as I am aware, there is no command such as LCDINIT, therefore you should not have found it in your manual. However, saying that, the compiler should have flagged that as an error since it's not a reserved word either. I've tried several LCDINxxx words like LCDINA, LCDINITF, LCDINFRED and they all compile happilly too... so a bad mark for PBP in that respect.

That aside, the point I'm trying to make is, that if the word isn't in the PBP manual, or it's not a valid PIC Register name it shouldn't be used. Your original example had LCDCMDOUT, LCDINIT and WAITMS none of which are valid PBP words which you would have discovered if you looked in the PBP manual. This is why I asked if you're using PBP.

As I've just mentioned, there is NO "Initialise LCD" command in PBP. The LCD is initialised on your first instruction to it (which usually is Clear Display - eg LCDOut $FE,$01), however the LCD is a slow device to wake up at Power-On, so you should have a 1 second (or thereabouts - some LCD's need two seconds) delay in your program from power-on to allow for the LCD to settle before you output anything to it.

I don't use Microcode Studio which is why I can't answer questions on it, but I have all the versions of PBP for the last couple of years which allows me to check for errors in older versions too. The only tools I use is MPLAB from Microchip and PBP from MeLabs. Unless I have to, I also prefer to use the MeLabs assembler which comes with PBP. Other than the manual and the PIC's Datasheets, there are no other downloads that I use.

Melanie

winsthon
- 29th December 2003, 14:40
thanks for the info I appreciate your help. God Bless and more power.

winsthon
- 31st December 2003, 01:20
I have another problem. Im going to get the output of the analog signals via RA0, RA1 and RA3 and convert then into Binary and display on the LCD using a button. So far I have this code which I think the button didn't work. Here's the code:

INCLUDE "BS1DEFS.BAS "
Define ADC_CLOCK 3 'default value is 3
DEFINE ADC_BITS 8
Define ADC_SAMPLEUS 10 'default value is 20
Define LCD_BITS 8 'allowed values are 4 and 8 - the number of data interface lines
Define LCD_DREG PORTB
Define LCD_DBIT 0 '0 or 4 for 4-bit interface, ignored for 8-bit interface
Define LCD_RSREG PORTD 'RS port
Define LCD_RSBIT 1 'RS pin
Define LCD_EREG PORTD 'En port
Define LCD_EBIT 3 'En pin
Define LCD_RWREG PORTD 'R/W port
Define LCD_RWBIT 2 'R/W pin
DEFInE LCD_LINES 4 'LCD lines
DEFINE BUTTON_PAUSE 50 'button debounce delay is 50ms

an0 var word 'set variable an0 as word
an1 var word 'set variable an1 as word
B var PORTC.4 'define RC4 as pin input to select RA0 or RA1 as input
btn var byte
ledrc0 var PORTC.0 'set ledrc1 as RC0 indicates RA0 as input
ledrc1 var PORTC.1 'set ledrc2 as RC1 indicates RA1 as input

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = 4 ' Set RA0, RA1 & RA3 as analog input
btn = 0
goto loop 'skip subroutine

ra0: 'ra0 subroutine
high ledrc0 = 1
low ledrc1 = 0
Adcin 0, an0
Lcdout $fe, $01 ' Clears your LCD
Lcdout $fe, $c0 ' Cursor to Line 2 Column 1
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14, " ", "RA0", " ", "VALUE:" ' Formatted text for line 2
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $94 ' cursor to line 3
Lcdout $fe, $14, BIN8 an0 ' Formatted text for line 3
pause 1000
return

ra1: 'ra1 subroutine
high ledrc1
low ledrc0
ADCIN 1, an1
Lcdout $fe, $01 ' Clears your LCD, sets cursor to Line 1 Column 1
Lcdout $fe, $c0 ' Cursor to Line 2 Column 1
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $14, " ", "RA1", " ", "VALUE:" ' Formatted text for line 2
Lcdout $fe, $14 ' move cursor 1 space
Lcdout $fe, $94 ' cursor to line 3
Lcdout $fe, $14, BIN8 an1 ' Formatted text for line 3
pause 1000
return

loop: 'main
Button B,1,10,5,btn,0,loop
if btn = 1 then
Gosub ra1
else
gosub ra0
endif

Goto loop 'loop forever
END

Anyone I need help with buttons as to which pin I will connect it and how to declare them correctly...Thank you...

winsthon
- 3rd January 2004, 06:16
hi seems that no one wants to reply...please anyone I need your help or at least give me an idea and also can I ask what does this mean:

whats the difference between these two?
ADCON1 = %00001011
ADCON1 = 4

option_reg = $7f
OPTION_REG.7 = 0 'Enable PORTB pull-ups

TRISB = %11110000

what does $7f do, anyone who can show me something like the one above.

Melanie
- 3rd January 2004, 10:25
Patience. Posting a question in the middle of international holidays, or weekends, or in the festive season and expecting immediate replies is being a little optimistic - especially as some of your questions require a little more than a quick two-minute answer!!! I will get around to answering your previous question with time.

>whats the difference between these two?
ADCON1 = %00001011
ADCON1 = 4

I assume you're using a 16F877... please download the datasheet and look in section "11.0 Analogue to Digital Converter Module" - it's right at the start of that section.

>option_reg = $7f
OPTION_REG.7 = 0 'Enable PORTB pull-up
what does $7f do, anyone who can show me something like the one above.

Again look at the OPTION_REG in the PIC's Datasheet. You will find it in the "Memory Organisation" section 2.2.2.2... Adobe provided you with a search facility within the Acrobat Reader if your Datasheet differs from mine.... However $7F is in binary %01111111, bit 7 is zero, all the other bits are one. Cross-check the setting of those bits against the table in the Datasheet detaling each bit of OPTION_REG.

Melanie

winsthon
- 5th January 2004, 01:25
thanks mel for the information. About also to my previous question regarding dealing with buttons. Until now I cant get it, I hope you can give me an idea on how to deal with buttons.

I want to get the corresponding digital output of the 3 analog inputs namely RA0, RA1 and RA3 one at a time by using a buttons such that when I press the first button it will enable RA0, if button2 enable RA1 disable RA0 and RA3 and lastly if button3 is pressed it will enable RA3 and disable RA0 and RA1. Maybe buttons will be connected to PORTC because PORTB and PORTD are being used already for the LCD.

Melanie
- 5th January 2004, 15:01
With regard to a Button selecting a input... your original example used PortC.4 for the button, so we'll carry on using that...

Button is connected between PortC.4 and 0v (Vss). A pull-up Resistor (10K) is connected between PortC.4 and +5v (Vdd).

PushButton var PortC.4

When button is pressed, the status of PushButton will be zero (Low), when button is released, the status will be 1 (High).

We'll also use a variable...

ButtonCounter var Byte

We will increment this variable each time button is pressed, and depending on the state of the variable, we will display one of your three inputs...

ButtonCounter=0
LCDOut $FE,1
' initially clear screen ready for display
Loop:
If PushButton=0 then
' Button has been pressed
LCDOut $FE,1
' Clear screen indicating to user that Button has been pressed
While PushButton=0:Wend
' Wait for button to be released
ButtonCounter=ButtonCounter+1
If ButtonCounter=>3 then ButtonCounter=0
' ButtonCounter only counts 0,1 or 2 then starts over again
endif
If ButtonCounter=0 then Gosub DisplayFirstADC
If ButtonCounter=1 then Gosub DisplaySecondADC
If ButtonCounter=2 then Gosub DisplayThirdADC
goto Loop

Have fun...

Melanie

winsthon
- 9th January 2004, 04:36
thanks mel, with that it helps me a lot to gather data from the ADC. Now that I have the data I just want to ask about SELECT CASE, you see I have a program that uses if...then...else..enif and my code was so long I need to shortened it using SELECT CASE. on the if..then.. command I can have two arguments like

if x<=100 and x<=200 then
.
.
.

I tried select but it doesnt have AND/OR. Is there any other way to shortened my program to make my program execute faster?

Melanie
- 10th January 2004, 11:09
Do read the PBP manual... Section 4.19 on Logical Operators, see the table therein and the example provided underneath.