All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Embedded Systems and Microcontrollers

Notices

Embedded Systems and Microcontrollers Discussion forum for projects and working with embedded systems and microcontrollers (FPGAs, PICs, AVRs). Get help with hardware issues and embedded programming.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 04-06-2010, 01:45 PM
andyv andyv is offline
New Member
 
Join Date: Apr 2010
Posts: 5
Default PIC16f690 interfacing with LCD 16x2

Hi all,

I'm just starting out in the world of microcontrollers, and the microchip pic was recommended to me, along with 'PIC in practice' book by D W Smith.

I have pretty much covered everything in the book, at times with difficulty due to my minute budget. I have been using the PICkit 2 that comes with a PIC16F690, and many will confirm has hidden problebs, such as recognising switch inputs.

My latest issue is interfacing with an 16x2 LCD alfanumeric display. I have come up with the following .asm code from sources off the internet and also the PIC in practice book.

I am using a Displaytech display from RS- 532-6442
Code:
 
 
;*********************************************************
; EQUATES SECTION
 #include <p16F690.inc>
 DELAY1  EQU 21h
 DELAY2  EQU 22h  
 DISPLAY  EQU 23h
 COUNT  EQU 34h
 
;**********************************************************************
; Configuration Bits
    __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
org 0
;*********************************************************
;CONFIGURATION SECTION
Start:
  bsf  STATUS,RP0    ;select Register Page 1
  clrf TRISC             ;make make port C outputs
  CLRF TRISA
  bcf  STATUS,RP0    ;back to Register Page 0
  bsf  STATUS,RP1    ;page 2
  clrf ANSEL             ;turn off ADC
  bcf  STATUS,RP1    ;back to page 0
  CLRF PORTA
  CLRF PORTC
 
;Display Configuration
  MOVLW 03h
  MOVWF PORTC  ;8 BIT DATA
  CALL  CLOCK
  CALL  DELAY
  MOVLW 02h
  MOVWF PORTC  ;CHANGE TO 4BIT
  CALL CLOCK
  CALL DELAY
  MOVLW 0h
  MOVWF PORTC  ;REPAET COMMAND
  CALL DELAY
  MOVLW 08h
  MOVWF PORTC  ;2 LINE DISPLAY
  CALL DELAY
  MOVLW 0h
  MOVWF PORTC  ;DISPLAY ON CURSOR OFF
  CALL CLOCK
  MOVLW 0Ch
  MOVWF PORTC
  CALL CLOCK
  CALL DELAY
  MOVLW 0h
  MOVWF PORTC  ;INCREMENT CURSOR
  CALL CLOCK
  MOVLW 6h
  MOVWF PORTC
  CALL CLOCK

  MOVLW .8
  MOVWF DISPLAY
;*********************************************************
;Program starts now.

HELLO_WORLD
  CALL CLRDISP
  CLRF PORTA
  MOVLW 8h   ;CURSOR AT TOP LEFT,80h
  MOVWF PORTC
  CALL CLOCK
  MOVLW 0h
  MOVWF PORTC
  CALL  CLOCK
  BSF  PORTA,0
  CALL  H
  CALL DELAY
  CALL E
  CALL DELAY 
  CALL  L
  CALL DELAY
  CALL L
  CALL DELAY 
  CALL  O
  CALL DELAY
  CALL GAP
  CALL DELAY 
  CALL  W
  CALL DELAY
  CALL O
  CALL DELAY 
  CALL  R
  CALL DELAY
  CALL L
  CALL DELAY 
  CALL  D
  CALL DELAY
  CALL DELAY3S
  GOTO  HELLO_WORLD 

;*********************************************************
;SUBROUTINE SECTION.
;1MS DELAY

DELAY
     DECFSZ    DELAY1,f             ; Waste time.  
     GOTO      DELAY        ; The Inner loop takes 3 instructions per loop * 
                                  256 loopss = 768 instructions
     DECFSZ    DELAY2,f             ; The outer loop takes and additional 3 
                                              instructions per lap * 256 loops
     GOTO      DELAY          ; (768+3) * 256 = 197376 instructions / 1M 
                                       instructions per second = 0.197 sec.
                                      ; call it a two-tenths of a second.
     RETLW  0
 
DELAY1S
  MOVLW .10
  MOVWF COUNT
LOOP 
  CALL DELAY
  DECFSZ COUNT
  GOTO LOOP
  RETLW 0

DELAY3S
  MOVLW .30
  MOVWF COUNT
LOOP1 
  CALL DELAY
  DECFSZ COUNT
  GOTO LOOP1
  RETLW 0
 
CLOCK 
  BSF  PORTA,2
  NOP
  BCF  PORTA,2
  NOP
  RETLW 0
 
;***********************************************************************
;
;HELLO WORLD
  
A  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 3h  ;First part of byte (31h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 1h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
H  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 3h  ;First part of byte (38h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 8h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
  
E  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 3h  ;First part of byte (35h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 5h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
L  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 3h  ;First part of byte (3Ch)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 0Ch
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
O  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 3h  ;First part of byte (3Fh)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 0Fh
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
WW  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 4h  ;First part of byte (47h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 7h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
RR  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 4h  ;First part of byte (42h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 2h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
DD  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 3h  ;First part of byte (32h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 4h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
GAP  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 1h  ;First part of byte (10h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 0h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0
 
CLRDISP CLRF PORTA
  MOVLW .1  ;Enables Display
  MOVWF PORTA
  MOVLW 0h  ;First part of byte (01h)
  MOVWF PORTC
  CALL CLOCK
  MOVLW 1h
  MOVWF PORTC
  CALL CLOCK
  RETLW 0

   END
Select All
If you could either go through my code, or possibly suggest code for me that I can then play with, and learn from, that would be a great help

Thanks all,

AndyV

Last edited by andyv; 04-06-2010 at 02:12 PM. Reason: .asm code, put in code window.
Reply With Quote
  #2  
Old 04-06-2010, 02:18 PM
t06afre's Avatar
t06afre t06afre is offline
Senior Member
 
Join Date: May 2009
Posts: 5,939
Default

Welcome to the forum I am sure we can help you. What is problem Can you write a few words about that also. And perhaps post a schematic that show us how the LCD is connected. It would be very helpful for us.
By the way. Then you post code it is more convenient to use the code option explained here http://forum.allaboutcircuits.com/mi...do=bbcode#code You can edit your first posting to see the difference
Reply With Quote
  #3  
Old 04-06-2010, 02:36 PM
andyv andyv is offline
New Member
 
Join Date: Apr 2010
Posts: 5
Default

Cheers t06afre,

I'm not getting anything displayed on my screen, just all white squares surrounded by black lines (if that makes sense I'll be amazed!)

I have connect the PIC to the LCD by RA0-RS RA2-E and RC0-RC7 - DB0-DB7 for the command lines.

I have grounded R/W as I don't need to read back the values.

When i check the o/p of the pic with an LED there is definatly a supply to the screen on the command lines, RS, and E. So There must be something wrong in the initialising, but i can't figure out what it is.

Thanks again,

Andy.
Reply With Quote
  #4  
Old 04-07-2010, 01:47 AM
MMcLaren's Avatar
MMcLaren MMcLaren is offline
Senior Member
 
Join Date: Feb 2010
Location: Michigan, USA
Posts: 636
Default

Hi Andy,

You may be running into a few problems;

(1) You need to clear the ANSEL and ANSELH registers in bank 2 to disable the ADC and enable the I/O pins for "digital" I/O. <added> Please excuse me -- I just saw that you cleared ANSEL.

(2) You've connected the LCD using an 8-bit data interface but the software you posted is for 4-bit interface mode. If you want to use 4-bit mode then you should ground the LCD D3 through D0 pins.

(3) There's a reasonably precise procedure for initializing '44780 displays in either 8-bit or 4-bit interface mode. Both procedures are outlined pretty well here; Brief overview about Hitachi HD44780 LCD controller

I attached an example program that I just threw together and tested on a 16F690 using 4-MHz INTOSC with LCD D4..D7 on RC0..RC3, LCD RS on RC4, and LCD E on RC7. Please note in the LCD init' procedure where you need to send single 4-bit nybbles instead of bytes during the first part of the procedure until the controller is switched into 4-bit mode. After that you can start sending 8-bit bytes as two nybbles.

Best luck. Regards, Mike
Attached Images
File Type: jpg K8LH LCD Photo 1.JPG (36.5 KB, 216 views)
Attached Files
File Type: txt AAC 690+LCD.ASM.TXT (10.2 KB, 264 views)

Last edited by MMcLaren; 04-07-2010 at 04:24 AM.
Reply With Quote
  #5  
Old 04-08-2010, 08:57 AM
andyv andyv is offline
New Member
 
Join Date: Apr 2010
Posts: 5
Default

Thanks guys, you've helped loads.

Theres some language in your example Mike that I've never seen before! Theres still a long road ahead, and look forward to going down it.

Cheers again, Andy.
Reply With Quote
  #6  
Old 04-08-2010, 11:19 AM
MMcLaren's Avatar
MMcLaren MMcLaren is offline
Senior Member
 
Join Date: Feb 2010
Location: Michigan, USA
Posts: 636
Default

Hi Andy,

You're probably talking about those assembler pseudo opcodes; skpc (skip if carry), skpnc (skip no carry), etc. They're described in the MPLAB "MPASM Assembler" help file. I use them because they're more intuitive (to me) then the 'btfss' (bit test file skip if set) or 'btfsc' (bit test file skip if clear) instructions they replace. I apologize for any confusion they may have caused you Sir.

Good luck on your project.
Reply With Quote
  #7  
Old 04-09-2010, 06:54 AM
andyv andyv is offline
New Member
 
Join Date: Apr 2010
Posts: 5
Default

No, not at all, i kinda got the hang of it once I had read it through a few times. Its nice to see more language used to open my eyes, and hopefully applications.

Thanks.
Reply With Quote
  #8  
Old 04-09-2010, 01:19 PM
MMcLaren's Avatar
MMcLaren MMcLaren is offline
Senior Member
 
Join Date: Feb 2010
Location: Michigan, USA
Posts: 636
Default

Hi Andy,

Did you get your LCD working yet or would you like an example program setup for your 8-bit interface using RC7..RC0 (D7..D0) + RA2 (E) + RA0 (RS)?

Mike
Reply With Quote
  #9  
Old 04-19-2010, 12:47 PM
andyv andyv is offline
New Member
 
Join Date: Apr 2010
Posts: 5
Default

Sorry to resurrect this, but I think I may have found the reason why I'm being unsuccessful with the interfacing.

I have run the program that you've suggested Mike, and it still didn't run, so I went back to basics, read through the links you've suggested, and still didn't run, so I ran the MPLAB SIM and watched the registers, it writes the info for an 8 bit interface, but only seems to write the lower 4 bits.

Is there something obvious I'm missing?!?!

Cheers again, Andy.
Reply With Quote
  #10  
Old 04-19-2010, 01:26 PM
MMcLaren's Avatar
MMcLaren MMcLaren is offline
Senior Member
 
Join Date: Feb 2010
Location: Michigan, USA
Posts: 636
Default

Hi Andy,

The "working" example program I posted is for 4-bit interface mode and is wired like this;

Attached Images
File Type: png LCD-690.PNG (21.3 KB, 322 views)
Reply With Quote
Reply   Post New Thread

Tags
, , ,


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interfacing CSTN LCD with PC varunvats69 Embedded Systems and Microcontrollers 2 12-23-2009 11:05 AM
Lcd Interfacing To 8051 Karandeep Singh Embedded Systems and Microcontrollers 6 10-12-2009 12:36 AM
Interfacing Nokia 7110 LCD on PIC12F629 mystamo Embedded Systems and Microcontrollers 4 06-12-2009 04:26 AM
LCD and real clock timer apiz88 Programmer's Corner 3 10-03-2008 05:39 AM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:42 AM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin
Copyright ©2000 - 2014, vBulletin Solutions, Inc.