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.

why doesnt this code work?

Status
Not open for further replies.

NewGeek

New Member
OK this is a bit of code, modified from Nigels LCD routine. The sub 'text2a' is where you input the characters to be written to the LCD.
I am trying to substitute the value stored in an address 'MyNum' for a character.
In this case, 'X' and 'Y' are both written to the LCD fine, but the value of 'MyNum' (5) is just blank. Why not? This code should write "5XY" to LCD, correct?

Code:
cblock	0x0C
                MyNum    ;adds variable 'MyNum' to address 0x0C				
endc


Text2a		ADDWF   PCL, f
     call GetNumber
          retlw	MyNum
          retlw	'Y'
          retlw	'Z'
          retlw   0x00	


GetNumber
          movlw	d'5'          ;move 5 to W
          movwf   MyNum          ;Move 5 to 'MyNum'
return
 
RETLW means 'Return with literal in W'. A literal is a constant. 'X' is a constant and 'Y' is a constant so those work ok. MyNum however is a variable, so RETLW MyNum will not put the value (5) of MyNum into W but it's address (0x0C).

Another thing to mind is that you have to send the ASCII code for a certai character to the LCD. so, if you want to print a '5' you should not send value 5 to the lcd but 53, wich is the ASCII code for character '5' ( www.asciitable.com )
 
Ok that makes sense. Thanks.
But then how would you make the value of a variable literal so that it can be written to LCD?
For instance, if Im storing values from a sensor into MyNum, how can I display on the LCD what those values are?

Yes I know about ASCII, I was just using 5 as an example.
 
You just change the 'CALL GetNumber' in Text2a into a 'GOTO GetNumber' and in the routine GetNumber you do MOVLW d'5' followed by a normal return.

The main program calls Text2a, wich goes to GetNumber where 5 is put into W and the return makes it go back to the main program.
 
If Im reading you correctly, I think your code will just put '5' into W and then write it to the LCD...
Thats not what I want to do. Maybe I didnt make it clear.

I want to write the value of MyNum to LCD, but I dont know what that value is, and its always changing. For example, MyNum is a value reported by a temperature sensor. I was just using 5 as an example of a value that MyNum might contain.
Does that make sense?

Your code just puts the literal into W then to LCD. I want to put a variable to LCD. Thanks.
 
You can adjust GetNumber as you want.

Code:
MOVF     MyNum, W
RETURN

will put the value of MyNum into W and then return to the main caller where it is sent to lcd.
 
As you've already been told, RETLW only returns a constant, all you need to do is load the ASCII value you want into W and "CALL LCD_Char", or you can load the numeric value and "CALL LCD_CharD", which converts the numeric value to ASCII for you (by simply adding 0x30 to it).

So, either:
Code:
movf variable, w ;variable must be an ASCII value
call LCD_Char

Or

Code:
movf Variable, w ;variable must be a numeric value from 0-9
call LCD_CharD

Some of the later LCD routines also give a routine to convert a 16 bit number to ASCII and display it - you can use the same routine for 8 bit as well, simply zero the upper byte, and only use the lower three bytes of the conversion. The analogue tutorial is one which uses those routines.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top