Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:39 23 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Coding tips

Author Message
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 12:55am 09 Jul 2011
Copy link to clipboard 
Print this post

I just spent ages trying to figure out why a perfectly good bit of code wasn't working.

I'd been cutting and pasting snippets of code, and had an older version of a routine commented out - just in case I needed to revert back to it.

This will be obvious when you see it, but I stared at the code in my editor for ages.

Here's some example code to show what happened:

100 For n = 1 to 100
150 ? n
200 next n

' snippets of code, including...
200 ' a comment

When I copied and pasted the code from my editor into Tera Term, I ended up with:

100 For n = 1 to 100
150 ? n
200 ' a comment

So here's the answer. If you want to comment out code for later reuse, put the single quote at the very beginning of the line, so the interpreter ignores it. For example,

' snippets of code

' 200 ' a comment

Yes, I know it was obvious, but it's taking me a little while to get used to line numbers again. This tip might save some other poor sod from making the same mistake.

And that leads onto another tip. If you're copying from an editor into the Maximite, remember to use the NEW command before you paste it in. Otherwise you'll end up with stray bits of the previous program intermingled with your new code.

James
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
stuarts

Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 199
Posted: 01:10am 09 Jul 2011
Copy link to clipboard 
Print this post

James, I realised the error of my ways the other night with cutting and pasting and very religously do a new before copying it back.

Cost me a whole heap of time.

Stuart
Time is nature's way of keeping everything from happening all at once.
 
VK6MRG

Guru

Joined: 08/06/2011
Location: Australia
Posts: 347
Posted: 02:47pm 09 Jul 2011
Copy link to clipboard 
Print this post

I had the same problem when I started to use the cut and past with HyperTerminal. Like Stuart, it took me some time to figure out what was going on.
Its easier to ask forgiveness than to seek permission!

............VK6MRG.............VK3MGR............
 
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 11:14am 18 Jul 2011
Copy link to clipboard 
Print this post

Another quick tip:
I have LCD.BAS renames as autorun.bas on my SD card. That way I get a pleasant greeting on my LCD screen every time I start the Maximite. The downside is that I have to type NEW before I start a new program.

I inserted a NEW statement into the autorun.bas code, just before the END statement, and it worked. Now I have a greeting on my LCD screen, and the Maximite has no code in memory.

OK, it's trivial, but I liked it, so I thought I'd share it.
[Hmmm...that sounds like the French waiter in "The Meaning of Life".]

James
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
stuarts

Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 199
Posted: 11:27am 18 Jul 2011
Copy link to clipboard 
Print this post

I like that one James. Of course you could always have the lcd startup program run another program instead of the new statement.

Stuart
Time is nature's way of keeping everything from happening all at once.
 
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 12:33am 23 Jul 2011
Copy link to clipboard 
Print this post

I'm having a little bit of trouble identifying a problem. I don't know if it's a code issue, a bug, or a transmission issue.
I've written the code in Notepad, and I copy it across into the Maximite using TeraTerm.

I tried to copy up a screen capture, but it was too small to read.

Here's a cut-and-paste from TeraTerm:


> 10000 ' Start of Code
> 10010 '
> 10020 GOSUB 29000 'Initialise the LCD
> 10030 GOSUB 11000 'Display the LCD Settings Menu
> 10040 END
> 10050 '
> 10060 '
> 11000 ' Display the menu
> 11010 '
> 11020 '
> 11025 CLS
> 11030 Print "This menu lets you try all the LCD setup options."
> 11040 Print "Some activate immediately. Some only show results when you display some text."
> 11050 Print "The ones that activate immediately are in brackets, otherwise they prompt for values."
> 11052 Print "Please enter the number next to the option you want to try"
> 11055 Print
> 11060 Print "[0] Exit"
> 11070 Print "[1] Clear Display (Write '20H' (space) to DDRAM and set DDRAM address to '00H' from Addesss Counter)"
> 11080 Print "[2] Return Home (Sets DDRAM address to '00H' from Address Counter and return cursor to its "
> 11085 Display Cursor and Blinking Cursor)"
> 11110 Print " 5 Cursor or Display Shift (Set cursor moving and display shift control bit and direction"
> 11115 Print " without changing DDRAM data)"
> 11120 Print " 6 Function Set (Sets Interface Data Length (4 or 8 bits) number of display lines (1 or 2)"
> 11125 Print " and display font type (5x11 or 5x8 dots))"
> 11130 Print " 7 Set CG RAM Address (Sets Chs. Also allows AC to be read)"
> 11160 Print " 10 Write DATA to RAM (Writes data into internal RAM (DDRAM / CGRAM)"
> 11170 Print " 11 Read DATA from RAM (Reads data from internal RAM (DDRAM / CGRAM)"
> 11180 Print " 12 Display some data to see how it is affected by the settings"
> 11190 Print
> 11200 INPUT "Please enter your selection", Sel
> 11210 If Sel < 0 OR Sel > 12 then
> 11220 Print "Sorry, that isn't a valid choice"
> 11230 pause 3000
> 11240 goto 11000
> 11250 endif
> 11260'
> 11270 print Sel
> 11280 pause 3000
> 11290 goto 11000
> 11999 Return
>


If you look at the original code below, you'll see that line 11085 has lost its Print command, and a couple of the following lines are missing.

Any suggestions on what's happening, or what I can do to resolve it?


10 ' These values are constants and should not be changed by the user
80 '
90 '
100 ' Change these values to match your LCD module
110 '
120 _C_Lines = 4
130 _C_LineLength = 20
135 ' ' You shouldn't have to change the next 4 values
140 _C_L1_Start = 0
150 _C_L2_Start = 64
160 _C_L3_Start = 20
170 _C_L4_Start = 84
200 '
210 DIM _Offset(4)
220 _Offset(1) = _C_L1_Start ' load all the offsets into an array
230 _Offset(2) = _C_L2_Start
240 _Offset(3) = _C_L3_Start
250 _Offset(4) = _C_L4_Start
255 '
260 DIM _Text$(4)
261 '
262 ' Change these values to change the initial display message
263 ' Note: if you have a 20 character display, put spaces on the end to fill
264 ' them out to 20 characters length, otherwise you get strange characters displayed.
265 ' If you have a 16 character display, trim the spaces off the end until they're 16 chars long.
266 '
270 _Text$(1) = "Hello World I'm " ' load initial display values for the screen
280 _Text$(2) = "a Maximite LCD "
290 _Text$(3) = "communicating with a" ' if you have a 2 line display, there's no harm leaving these extra lines here
300 _Text$(4) = "4 line HD44780 LCD. "
310 '
320 '
1000 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
1010 ' V2 LCD driver for standard 16 X 2 and 20 X 4 LCDs
1020 ' James Deakins July 2011,
1024 ' based on the original 16 X 2 code by
1025 ' Geoff Graham, March 2011
1030 '
1040 ' Example 20 X 4: HD44780
1070 '
1080 ' To use:
1090 ' - Set up your text messages and number of lines / characters for your LCD
1100 ' - execute GOSUB 11000 to initialise display
1110 ' - execute GOSUB 12000 to display the text in
1120 ' LCD_line1$, LCD_line2$, (and LCD_line3$ and LCD_line4$)
1130 '
1140 ' Make sure you have the right pinouts (see below). There's a new
1142 ' schematic on Geoff's site, but if you have the old one, USE THIS
1145 ' PINOUT INFORMATION INSTEAD OF THE PINOUTS ON THE SCHEMATIC!!
1147 ' ************************************************************ **
1150 ' Maximite pin 11 is RS, pin 12 is EN. R/W is grounded.
1160 ' >>>pins 13 to 16 are D4 to D7.<<< May be different to schematic!
1165 ' ************************************************************ **
1180 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
1190 '
10000 ' Start of Code
10010 '
10020 GOSUB 29000 'Initialise the LCD
10030 GOSUB 11000 'Display the LCD Settings Menu
10040 END
10050 '
10060 '
11000 ' Display the menu
11010 '
11020 '
11025 CLS
11030 Print "This menu lets you try all the LCD setup options."
11040 Print "Some activate immediately. Some only show results when you display some text."
11050 Print "The ones that activate immediately are in brackets, otherwise they prompt for values."
11052 Print "Please enter the number next to the option you want to try"
11055 Print
11060 Print "[0] Exit"
11070 Print "[1] Clear Display (Write '20H' (space) to DDRAM and set DDRAM address to '00H' from Addesss Counter)"
11080 Print "[2] Return Home (Sets DDRAM address to '00H' from Address Counter and return cursor to its "
11085 Print " original position if shifted. DDRAM contents not changed)"
11090 Print " 3 Entry Mode SET (Assign cursor moving direction and enable shifting of entire display)"
11100 Print " 4 Display ON/OFF Control (Set control bits for Display Cursor and Blinking Cursor)"
11110 Print " 5 Cursor or Display Shift (Set cursor moving and display shift control bit and direction"
11115 Print " without changing DDRAM data)"
11120 Print " 6 Function Set (Sets Interface Data Length (4 or 8 bits) number of display lines (1 or 2)"
11125 Print " and display font type (5x11 or 5x8 dots))"
11130 Print " 7 Set CG RAM Address (Sets Character Generator RAM address in address counter)
11140 Print " 8 Set DD RAM Address (Sets DD RAM address in address counter)
11150 Print " 9 Read 'Busy' flag and address (The Busy flag tells you whether the panel is still performing internal operations. Also allows AC to be read)"
11160 Print " 10 Write DATA to RAM (Writes data into internal RAM (DDRAM / CGRAM)"
11170 Print " 11 Read DATA from RAM (Reads data from internal RAM (DDRAM / CGRAM)"
11180 Print " 12 Display some data to see how it is affected by the settings"
11190 Print
11200 INPUT "Please enter your selection", Sel
11210 If Sel < 0 OR Sel > 12 then
11220 Print "Sorry, that isn't a valid choice"
11230 pause 3000
11240 goto 11000
11250 endif
11260'
11270 print Sel
11280 pause 3000
11290 goto 11000
11999 Return



29000 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
29010 ' Initialise the LCD
29020 '
29030 FOR i = 11 TO 16 : SETPIN i, 9 : NEXT i ' set the pins to Open collector digital output to 5V
29040 _LCD_byte = &B0011 : GOSUB 30100 : PAUSE 5 ' reset
29050 _LCD_byte = &B0011 : GOSUB 30100 : PAUSE 5 ' reset
29060 _LCD_byte = &B0011 : GOSUB 30100 : PAUSE 5 ' reset
29070 _LCD_byte = &B0010 : GOSUB 30100 : PAUSE 2 ' 4 bit mode
29080 _LCD_byte = &B00101100 : GOSUB 30000 ' 4 bit, 2 lines (or 4 lines)
29090 _LCD_byte = &B00001100 : GOSUB 30000 ' display on, no cursor
29100 _LCD_byte = &B00000110 : GOSUB 30000 ' increment on write
29110 _LCD_byte = &B00000001 : GOSUB 30000 ' clear display
29120 pause 2
29130 RETURN
30000 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''
30010 ' Send a byte to the LCD
30020 ' the data to be sent is in _LCD_byte
30030 '
30040 PIN(13) = _LCD_byte AND &B00010000 ' output the 1st 4 bits
30050 PIN(14) = _LCD_byte AND &B00100000
30060 PIN(15) = _LCD_byte AND &B01000000
30070 PIN(16) = _LCD_byte AND &B10000000
30080 PIN(12) = 1 : PIN(12) = 0 ' tell lcd to accept data
30100 ' Entry point to send just 4 bits to the LCD
30110 PIN(13) = _LCD_byte AND &B00000001 ' output the 2nd 4 bits
30120 PIN(14) = _LCD_byte AND &B00000010
30130 PIN(15) = _LCD_byte AND &B00000100
30140 PIN(16) = _LCD_byte AND &B00001000
30150 PIN(12) = 1 : PIN(12) = 0 : PIN(11) = 0 ' tell lcd to accept data
30160 RETURN
30170 '
30200 ' Cursor LOCATE code
30210 '
30220 ' Move cursor to a location on the LCD screen
30240 ' The line is sent in _LCD_Line, the column is sent in _LCD_Col
30250 _Start = _Offset(_LCD_Line) + _LCD_Col -1
30270 _LCD_byte = 128 + _Start : GOSUB 30000 ' move to the requested location
30280 RETURN


Thanks

[puzzled] JamesEdited by James_From_Canb 2011-07-24
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
VK6MRG

Guru

Joined: 08/06/2011
Location: Australia
Posts: 347
Posted: 12:53am 23 Jul 2011
Copy link to clipboard 
Print this post

I'm not sure what is happening, but if you try to cut and paste large amounts of code, it does funny things. I cut and paste about 20 lines at a time to stop this. A pain if you have a 200 line program.
I have tried playing with the baud rates to slow things down and speed things up, but it is a random thing that seems not to be speed related.
I know it's not the answer, but at least I know what it's not.
Edited by VK6MRG 2011-07-24
Its easier to ask forgiveness than to seek permission!

............VK6MRG.............VK3MGR............
 
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 01:36am 23 Jul 2011
Copy link to clipboard 
Print this post

That was a perfect answer. Thanks heaps. You're right - changing Windows Com port settings didn't help - but you put me on the right track.

TeraTerm has a setup option for the serial port allowing a pause between lines being sent. I set it to 50 milliseconds and it worked. It can probably work with less, but I'm happy with 50.

If anyone else does this, just remember to save the configuration so it's set for the next time.



James
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 11:41am 30 Jul 2011
Copy link to clipboard 
Print this post

The VAL() function only seems to work if you pass in a string representing a decimal value (e.g. VAL("1234") ), but not hex, octal or binary values like VAL("&HF4").

I needed to convert a Hex String to Decimal because the INPUT statement only allows me to accept a string or a decimal value. Now the user can input any numeric data type as a string, and this code converts it. Just make sure the strings representing non decimal numbers start with &H, &O or &B.

Here's an extended version of the VAL() function


21000 ' Replacement code for the VAL() Function.
21010 ' VAL() does not convert Binary, Octal or Hexidecimal strings to Decimal.
21015 ' This routine converts a string representing any decimal, binary, octal or hexidecimal value to Decimal.
21020 ' Usage: Store the string to be converted in a variable called _ExtVal$ before calling this subroutine.
21030 ' The decimal result will be returned in a variable called ExtVal.
21040 '
21050 IF _ExtVal$ = "" THEN
21060 ERROR "Empty string passed to Extended VAL() subroutine"
21070 ENDIF
21080 _Val$ = UCASE$(_ExtVal$) ' We will work with upper case
21090 _Type$ = LEFT$(_Val$,2)
21095 _ValLen = LEN(_Val$)
21100 IF _Type$ = "&H" THEN ' Hex value
21110 GOSUB 21300
21120 ELSEIF _Type$ = "&O" THEN
21130 GOSUB 21600 ' Octal value
21140 ELSEIF _Type$ = "&B" THEN
21150 GOSUB 21900
21160 ELSE
21170 _ExtVal = VAL(_ExtVal$) ' Must be decimal
21180 ENDIF
21190 RETURN

21300 ' A Hex value was passed to the Extended Val subroutine
21310 _ExtVal = 0
21320 _Power = 1
21330 _ValLen = _ValLen - 2 ' &H take up 2 character positions
21340 _Val$ = RIGHT$(_Val$, _ValLen) ' strip off the &H
21350 FOR _X = _ValLen to 1 STEP -1
21360 _Char$ = MID$(_Val$, _X, 1) ' evaluate characters right to left
21370 IF _Char$ >= "0" AND _Char$ <= "9" THEN
21380 _CharVal = VAL(_Char$)
21390 ELSEIF _Char$ >= "A" AND _Char$ <= "F" THEN
21400 _CharVal = ASC(_Char$) - 55
21410 ELSE
21420 PRINT "Invalid character "; _Char$; " found in Hex Value passed to Extended Val subroutine"
21430 ENDIF
21440 _ExtVal = _ExtVal + _CharVal * _Power
21450 _Power = _Power * 16
21460 NEXT _X
21470 RETURN

21600 ' An Octal value was passed to the Extended Val subroutine
21610 _ExtVal = 0
21620 _Power = 1
21630 _ValLen = _ValLen - 2 ' &O take up 2 character positions
21640 _Val$ = RIGHT$(_Val$, _ValLen) ' strip off the &O
21650 FOR _X = _ValLen to 1 STEP -1
21660 _Char$ = MID$(_Val$, _X, 1) ' evaluate characters right to left
21670 IF _Char$ >= "0" AND _Char$ <= "8" THEN
21680 _CharVal = VAL(_Char$)
21690 ELSE
21700 PRINT "Invalid character "; _Char$; " found in Octal Value passed to Extended Val subroutine"
21710 ENDIF
21720 _ExtVal = _ExtVal + _CharVal * _Power
21730 _Power = _Power * 8
21740 NEXT _X
21750 RETURN

21900 ' A Binary value was passed to the Extended Val subroutine
21910 _ExtVal = 0
21920 _Power = 1
21930 _ValLen = _ValLen - 2 ' &B take up 2 character positions
21940 _Val$ = RIGHT$(_Val$, _ValLen) ' strip off the &B
21950 FOR _X = _ValLen to 1 STEP -1
21960 _Char$ = MID$(_Val$, _X, 1) ' evaluate characters right to left
21970 IF _Char$ >= "0" AND _Char$ <= "1" THEN
21980 _CharVal = VAL(_Char$)
21990 ELSE
22000 PRINT "Invalid character "; _Char$; " found in Binary Value passed to Extended Val subroutine"
22010 ENDIF
22020 _ExtVal = _ExtVal + _CharVal * _Power
22030 _Power = _Power * 2
22040 NEXT _X
22050 RETURN



Hope that helps.

James
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024