View Full Version : Serial LCD problems

01-20-2008, 08:39 AM
Hey guys I'm having trouble with my LCD screen. What I have going on here, is that I have 1 cog run a timer. I hold down a button on pin 21 and I count to see how long I hold it down. I can run it without the WriteLCD method, and it seems to work without a hitch. Any suggestions would be helpful. Thanks. Please note that I am new, and I have looked on the forum for help. Here is the code that I have written:

'' Counts how long the button is pressed for and
'' displays it on the LCD
LCD_Pin = 0 'The LCD is connected to pin A0.
LCD_Baud = 19_200 'Baud
LCD_Lines = 2 'The number of lines on this LCD

Off = 0
On = 1
LCD: "Debug_lcd"
LONG stack[50]

PUB Main
TOG := 0
cognew(Countdown, @stack[0])
cognew(Button, @stack[10])
PUB Countdown
waitcnt(clkfreq*2 + cnt)
outa[1..5] := %111110
waitcnt(clkfreq*2 + cnt)
outa[1..5] := %111100
waitcnt(clkfreq*2 + cnt)
outa[1..5] := %111000
waitcnt(clkfreq*2 + cnt)
outa[1..5] := %110000
waitcnt(clkfreq*2 + cnt)
outa[1..5] := %100000
waitcnt(clkfreq*2 + cnt)
TOG := 1

PUB Button | count1, count2, count
count1 := 0
count2 := 0
dira[21]~ 'Set pin 21 to input
dira[6]~~ 'Set pin 6 to output
repeat while TOG == 0
repeat while ina[21] == 0
count1 := cnt
repeat while ina[21] == 1
count2 := cnt
count := count2-count1

PRI WriteLCD(count)
repeat 10
waitcnt(clkfreq/20 + cnt)

if LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines)'Initialize the LCD object
LCD.Cursor(Off) 'Set cursor off
LCD.Cls 'Clear the LCD screen
LCD.Str(string("Button time")) 'Print string on LCD
LCD.Gotoxy(5, 1)
LCD.DecF(count, 8) 'Print count as a signed decimal

Chris Savage
01-21-2008, 11:52 AM
Can you describe the issue you are having? Exactly what is happening with the LCD?

Chris Savage
Parallax Tech Support

01-21-2008, 06:52 PM
It will run all the way through the light flashing. Then nothing happens. The LCD either stays blank, or shows what was previously on it. I never makes it through the first line of code. I've run another program, Debug_LCD_Test that runs perfectly (taking into account that I have a smaller LCD). It never has a problem, and its almost the exact same code. Also, did I say something to offend people? Nobody seems like they want to help.

Fred Hawkins
01-21-2008, 10:50 PM
"Also, did I say something to offend people? Nobody seems like they want to help."

'my LCD screen' doesn't add enough information for me... What's the device exactly?

01-21-2008, 11:30 PM
In reference to the above comment, its a Parallax 2x16 Serial LCD (non-Backlit). Just the standard one off of the website.

01-22-2008, 12:12 AM
I'm no expert on all this, but from a quick look at the code you might want to declare two different stack variable arrays, and assign a different one for each COGNEW. Not allocating enough memory space can result in weird results at runtime.


01-22-2008, 05:49 AM
Yup, I tried adding more stack space to no avail.

01-22-2008, 09:58 AM
Maybe the attached file will help. I played around a bit with your code and added a couple of things. There was no _clkmode or _xinfreq statement set, and I think the various serial objects require the prop to be running at 80MHz for timing. If you don't explicitly define it the prop defaults to a slower internal clock. Also, I did set up new arrays for each of your cognew statements but I had to increase it to 50 longs before the LCD would update. (Change it back to 20 and try it yourself- the code compiles fine but nothing happens on the LCD.)

I didn't have time to test the button functions code, so I commented most of it out and just set up a counter to pass a number to WriteLCD. At least you know the LCD function is working, so you should be able to debug the rest.

NOTE: I'm testing this on a Spinstamp, so if you run this on a protoboard or demoboard you need to change the timing constants accordingly. I also changed the LCD pin to pin 12 since mine was already connected there.