Shop OBEX P1 Docs P2 Docs Learn Events
What am I doing wrong.....Probably everything! — Parallax Forums

What am I doing wrong.....Probably everything!

GuidoGuido Posts: 195
edited 2004-10-03 20:39 in BASIC Stamp
I am having problems with converting this program to a LCD Display. The Debug portion of this works perfectly, but the Azimuth will only go around 240 degrees and the look up table acts different. That is why I added the lower lookup table. Ay help would be appreciated....

'{$STAMP BS2}

E CON 0 ' LCD Enable pin (1 = enabled)

RW CON 2 ' LCD Read/Write pin (1 = write)

RS CON 3 ' Register Select (1 = char)

WPIN CON 8 ' Anno Input

ADDATA CON 9 ' A/D data line

ADCLOCK CON 10 ' A/D clock

ADREAD CON 11 ' ADC vout

RESULT VAR BYTE ' Result of conversion

AZIMUTH VAR WORD ' Compass read 360 degrees

M VAR BYTE ' degress divided by 45

A VAR byte ' Lookup First compass indicator

B VAR BYTE ' Lookup Second compass indicator

WIND VAR BYTE

WINDACC VAR WORD ' Accumulation for average

WINDMAX VAR BYTE ' Maximum wind speed

NSMPL VAR WORD ' number of samples

LCDddir VAR DirB ' port direction

LCDout VAR OutB ' 4-bit LCD data out

LCDin VAR InB ' 4-bit LCD data in

ClrLCD CON $01 ' clear the LCD

DDRam CON $80 ' Display Data RAM control

CGRam CON $40 ' Custom character RAM control

LINE1 CON $00

LINE2 CON $40

char VAR Byte ' character sent to LCD

CHAR2 VAR BYTE

addr VAR Byte ' address to write to / read from

addr2 VAR Byte ' address to write to / read from

index VAR Byte ' loop counter

tOut VAR Byte ' test value to write to LCD

tIn VAR Byte ' test value to read from LCD

temp VAR Word ' temp value for numeric display

FLAG1 VAR NIB

FLAG2 VAR BYTE

FLAG3 VAR BYTE

width VAR Nib ' width of number to display

DirL = %11111101 ' setup pins for LCD

GOSUB LCDinit ' initialize LCD for 4-bit mode

MAIN:

GOSUB AD:

WINDACC=0

COUNT WPIN,2250,WIND ' Frequently, e.g. once per 10 seconds

WINDMAX = WIND MIN WINDMAX ' Maintains Maximum Windspeed

WINDACC = WINDACC + WIND ' Infrequently, only when it is time to log data

NSMPL = NSMPL+1 ' One more sample

AZIMUTH = RESULT */360 ' Compass read 360 degrees

M = AZIMUTH +22 // 360 /45 ' degress divided by 45

LOOKUP M,[noparse][[/noparse]"N","N ","S","S","S","E","N "],A ' Lookup First compass indicator

LOOKUP M,[noparse][[/noparse]"O", "W","W","W","O","E"," E"],B ' Lookup Second compass indicator



DEBUG HOME

DEBUG " GUIDO'S MIRAGE",CR

DEBUG SDEC WINDACC," WIND SPEED ",CR

DEBUG SDEC WINDMAX," MAX WIND SPEED ",CR

DEBUG A,B ," WIND DIRECTION ",CR

DEBUG SDEC AZIMUTH," COMPASS WIND INDICATOR ",CR

PAUSE 500 ' pause between readings

char = ClrLCD ' clear the LCD

GOSUB LCDcommand

IF FLAG1=0 THEN BB:

IF FLAG1=1 THEN AA:

BB:

FOR index = 0 TO 55 ' create display

char = ClrLCD ' clear the LCD

pause 50

LOOKUP index,[noparse][[/noparse]"WIND SPEED COMPASS DEG: "],chAR

GOSUB LCDwrite

NEXT

FLAG1=1

ADDR=WINDACC ' create address (0 to 63)

ADDR2=AZIMUTH

GOTO GG:

GG:

ADDR=ADDR

GOSUB LCDwrite ' move the value to CGRAM

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 13 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 3

GOSUB PutVal

ADDR=ADDR2 ' create address (0 to 63)

GOSUB LCDwrite ' move the value to CGRAM

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + LINE2 + 13 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 3

GOSUB PutVal

GOTO MAIN ' do it again

END

'

PutVal:

FOR index = (width - 1) TO 0 ' display digits left to right

char = (temp DIG index) + 48 ' convert digit to ASCII

GOSUB LCDwrite ' put digit in display

NEXT

RETURN

LCDinit:

PAUSE 50 ' let the LCD settle

LCDout= %0011 ' 8-bit mode

PULSOUT E,1

PAUSE 5

PULSOUT E,1

PULSOUT E,1

LCDout = %0010 ' 4-bit mode

PULSOUT E,1

char=%00101000 ' MULTI LINE

GOSUB LCDcommand

char = %00001100 ' disp on, crsr off, blink off

GOSUB LCDcommand

char = %00000110 ' inc crsr, no disp shift

GOSUB LCDcommand

RETURN



LCDcommand:

LOW RS ' enter command mode

LCDwrite:

LCDout = char.HighNib ' output high nibble

PULSOUT E,1 ' strobe the Enable line

LCDout = char.LowNib ' output low nibble

PULSOUT E,1

HIGH RS ' return to character mode

RETURN



LCDread:

HIGH RS ' data command

HIGH RW ' read

LCDddir= %0000 ' make data lines inputs

HIGH E

char.HighNib = LCDin ' get high nibble

LOW E

HIGH E

char.LowNib = LCDin ' get low nibble

LOW E

LCDddir= %1111 ' return data lines to outputs

LOW RW

RETURN

return

AD:

PAUSE 100

LOW ADREAD

SHIFTIN ADDATA,ADCLOCK,MSBPOST,[noparse][[/noparse]RESULT\9]

HIGH ADREAD

PAUSE 100

RETURN



AA:

FOR index = 0 TO 54 ' create display

char = ClrLCD ' clear the LCD

pause 50

LOOKUP index,[noparse][[/noparse]"WIND DIRCT: COMPASS DEG "],chAR

GOSUB LCDwrite

NEXT

IF A="N" THEN N:

IF A="S" THEN S:

IF A="E" THEN K:

IF A="W" THEN W:

A2:

ADDR2=B

IF B="N" THEN N1:

IF B="S" THEN S1:

IF B="E" THEN K1:

IF B="W" THEN G1:

IF B="O" THEN O1:

FF:

FLAG1=0

FLAG2=0

FLAG3=0

GOSUB LCDinit

GOTO MAIN







HH:

GOSUB LCDwrite ' move the value to CGRAM

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 10 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 3

GOSUB LCDWRITE



N:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 14 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=78

GOSUB LCDWRITE

GOTO A2

PAUSE 500

GOTO MAIN



S:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 14 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=83

GOSUB LCDWRITE

GOTO A2

PAUSE 500

GOTO MAIN



K:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 14 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=69

GOSUB LCDWRITE

GOTO A2

PAUSE 500

GOTO MAIN





W:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 14 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=87

GOSUB LCDWRITE

GOTO A2

PAUSE 500

GOTO MAIN





N1:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 14 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=78

GOSUB LCDWRITE

PAUSE 500

GOTO FF:



S1:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 15 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=83

GOSUB LCDWRITE

PAUSE 500

GOTO FF:



K1:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 15 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=69

GOSUB LCDWRITE

PAUSE 500

GOTO FF:





G1:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 15 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=87

GOSUB LCDWRITE

PAUSE 500

GOTO FF:





O1:

char = CGRam + addr ' set CGRAM pointer

GOSUB LCDcommand

char = DDRam + 15 ' show address at position 13

GOSUB LCDcommand

temp = addr

width = 1

CHAR=79

GOSUB LCDWRITE

PAUSE 500

GOTO FF:





Comments

  • K de JongK de Jong Posts: 154
    edited 2004-10-01 15:51
    Hi Guido,

    What type of LCD are you using? HD44780 or compatible?? You use 3 bit for control but do you use 4 bit for data??

    Your code is so long that I did not really find this. Maybe you can cut out and show the pieces of importance to the problems you have to improve readability a bit.

    Regards,

    Klaus
  • GuidoGuido Posts: 195
    edited 2004-10-01 16:24
    I am trying to learn all of this....some of it is over my head, but still attempt to figure it out. I am using a HD44780. The reason I placed the whole code is that the Debug portion of this works perfectly, but when I try and convert this to lcd, it has problems......such as wind direction...will lock up some times and compass Degrees only seem to function 240 degrees or so.....Not sure if it is 3 or 4 bit....
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-10-01 20:19
    Guido,

    ·· I agree with Klaus, you should probably post the relevant parts of the code...I have converted many DEBUG display routines to LCD, and I can't follow real easy what you're doing in the code.· Mainly because most of your labels are 1 or 2 letters which isn't very descript...I will offer you this though...When converting directly from the DEBUG statements to the LCD, you can run into problems the way you're going about it when you were using DEBUG modifiers.· These won't work exactly the same when you're outputting the data to the LCD via 4-bit parallel mode without seom extra work.· If you were using a serial LCD, or had a serial backpack, it would be much easier, since you could use the modifiers in the SEROUT command as well.· This would keep the proper formatting and display of numeric values the way they worked before.· Using a serial backpack or display will also add a small bit of cost, but it will also save I/O lines on the BS2...Here are 2 I have used:

    http://pic-an-lcd.com/·· (A little pricey)

    http://www.phanderson.com/lcd106/lcd106.html· (Better pricing, but less tech support)

    I use these chips in all my BS2 applications that involve the LCD (Except the mouse trap).



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • GuidoGuido Posts: 195
    edited 2004-10-01 20:54
    First of all I want to thank Klaus and Chris. Heck you think it is confusing to you!!! Again I am a beginner to all this and learn more by example. I did discover one thing that may be creating this problem....I do not think I terminated the DB4-DB7...Do you really need them? It seems they just go to the stamp I/O without any progams on them..I have checked and I am using 4 bit.....what the hell is 3 bit? For my own knowledge.

    Thanks Again
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-10-01 21:12
    Guido, it's not really 3-bit...On an HD44780 LCD controller you need to sent commands 8 bits at a time...In 4-bit mode, you send them 4 bits at a time...The upper nibble (Upper 4 bits) usually only uses bit5 for most commands, but technicially you need them all.· I would surmise that you are sending data to the LCD that it's interpreting as commands, or special characters.· With DEBUG you were using format modifiers to clean up your output, and now you can't use them that way.· For simplicity, I would still use the serial method, but if you want, I can post code for printing to the LCD in 4-bit mode.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • GuidoGuido Posts: 195
    edited 2004-10-01 21:27
    Chris,

    I would really appreciate that. Also in the Stamp Works manual they show terminating the DB4-DB7 to I/O pins.....Why? There is no programming for those pins?

    Again

    Thank You Very much!!!
  • K de JongK de Jong Posts: 154
    edited 2004-10-01 22:17
    Hi Guido,

    But there really SHOULD be a fairly large bunch of code handling DB4-DB7 on a BS2 with the HD44780 LCD controller !!!

    I think Chris will show you that code, if not then I have similar LCD-code here.

    Regards and succes,

    Klaus
  • GuidoGuido Posts: 195
    edited 2004-10-02 15:54
    Hi Klaus,
    First of all Thank you for all your help. Again I have done a lot of reading and maybe I am totally confused as normal. The LCD is HD44780 compatable. I have no terminations on DB4-DB7. If you check my original supplied program I am only using:

    E CON 0 ' LCD Enable pin (1 = enabled)

    RW CON 2 ' LCD Read/Write pin (1 = write)

    RS CON 3 ' Register Select (1 = char)

    I am having no problems displaying Wind Speed, Max Speed. The Azimuth seems to only respond for 240 degrees instead of 360. There is no dead band. I would really appreciate if a sample code or what the heck I might be doing wrong. When I went into the Stamp Work book they show that DB4-DB7 are connect to I/O, But they show now programming for those I/O.

    Again Thank You

    Guido
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-10-03 14:24
    Guido said...
    Chris,
    I would really appreciate that. Also in the Stamp Works manual they show terminating the DB4-DB7 to I/O pins.....Why? There is no programming for those pins?
    Again
    Thank You Very much!!!

    Guido,

    ·· First of all I guess I didn't catch this before, but in 4-bit mode on Hitachi controllers it's the DB4-DB7 pins that you're using in 4-bit mode.· I don't kno which ones you were going to connect, but as a minimum to control the LCD you need at least 6 pins.· DB4-DB7 for data, the RS line, and the Enable line (E).· You don't need the R/W line as you can tie it low (As long as you adhere to timing of writes to the display).· I suppose most people would say to ground DB0-DB3, however, in many 4-bit applications I have seen these simply left floating.· My guess would be that the controller pulls them low, although I can't say without referencing my tech sheet, which is at the shop.· I suppose if you want to be on the safe side, ground them.

    ·· As for writing to the display in 4-bit mode, you must first write the high nibble, then the low nibble.· It would be something like:

    LCD_Out:
    ··· OUTC = data >> 4
    ··· PULSOUT 6, 1
    ··· OUTC = data
    ··· PULSOUT 6, 1
    ··· RETURN

    Where your LCD DB4-DB7 is connected to BS2 pins 8-11, respectively, and the Enable line is connected to pin 6 on the BS2.· To send commands, you would first bring the Register Select Line LOW, call the routine above, then bring the RS line HIGH.· Of course, I do have code for this, but it's at the shop, and I will be there tomorrow and send it.· Hope this helps for now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • GuidoGuido Posts: 195
    edited 2004-10-03 16:00
    Chris,

    Thank you again for your help. I would appreciate you sending me that information. The thing that I am still puzzled about is there is no code, or identification for DB4-DB7...Guess what I am saying, It appears this is the Data area, but how does the Stamp know that? What is actually being done with these pins. I apologize for being a newbee, but I really love the challenge of learning something new and useful, and I am finding out that this forum provides the help regardless of should I say stupidity!!

    I want to apologize to Buck Rodgers about my post. Again being new, I have to learn the process of this forum. I give everyone who has helped a big thank you.....Trust me someday it will make sense.

    Thanks Again Chris

    Guido
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-10-03 17:37
    Guido,

    The Stamp doesn't have to know aobut DB4-DB7 since those are external connections; but you as a programmer know what they are. Chris's example above is a good one in that OUTC becomes DB4-DB7.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-10-03 19:53
    Guido & Jon,

    ·· I think (In retrospect) that I got the code (Or at least it's framework) from either one of Jon's articles, or an older Parallax document.· I say this because I am home, and all I have left on this computer is code-snippets I was testing years ago to port some of my Z80 routines onto the BS2.· At the time if Parallax already had code to do something, I modified it (If needed) to my application and used it (No sense re-inventing the wheel).· However, I was going to suggest downloading the Stampworks manual from Parallax:

    http://www.parallax.com/dl/docs/books/stampworksmanual.pdf

    It has a very similar example for using the LCD in 4-bit mode, and maybe looking at it will help you understand it better.· If not, let us know and we'll try to elaborate.· But generally speaking, using the LCD on the stamp is really quite easy once you've done it a couple of times...Which is why back when I got into the BASIC Stamp, I tried almost all of the experiments in the Stampworks manual before I tried any large projects, and I created a code-snippet library from the Parallax source of things I thought I could use in my routines.· Of course, credit is always give where it is due, and I am almost positive that the LCD code I quoted above came from one of Jon's earlier writings...Even as a programmer of 20+ years, starting with a new CPU/MCU can be tough.

    Anyway, good luck on it!




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs


    Post Edited (Chris Savage) : 10/3/2004 9:06:10 PM GMT
  • K de JongK de Jong Posts: 154
    edited 2004-10-03 20:39
    Yes Chris, it is the right book. The article about programming LCD displays starts at page 75.

    Klaus
Sign In or Register to comment.