Pic16f887

Thread Starter

benz_0074

Joined Jun 17, 2011
11
Im quite new to Microcontrollers, and I need some help getting the LCD display system working for PIC16f887, 2x16 display system, using MPlab IDE v8.10. The code that I have at the moment is:

Rich (BB code):
LIST p = 16F887 ;Simply sets the processor type being used
#INCLUDE <P16F887.INC> ;file for processor, adds predifined labels
 
__CONFIG _CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _LVP_OFF
; Sets many parameters
org 0x00 ; Start the code at location zero
 
 
LCD_PORT Equ PORTB 
 
LCD_TRIS Equ TRISB
LCD_RS Equ 0x04 ;LCD handshake lines
LCD_E Equ 0x05
 
 
CBLOCK 0x20 
count ; Counter used when switch pressed has stopped 
count1 ; 160us Counter variable 
counta ; variables for delay timers 
countb ; variables for delay timers 
LCDTemp ; 4 bit for LCD 
ENDC ; 
 
org 0x000 ; 
goto Init ; 
 
HEX_Table 
addwf PCL, f 
retlw 0x30 
retlw 0x31 
retlw 0x32 
retlw 0x33 
retlw 0x34 
retlw 0x35 
retlw 0x36 
retlw 0x37 
retlw 0x38 
retlw 0x39 
retlw 0x41 
retlw 0x42 
retlw 0x43 
retlw 0x44 
retlw 0x45 
retlw 0x46 
 
Text 
addwf PCL, f 
retlw 'W' 
retlw 'A' 
retlw 'M' 
retlw 'B' 
retlw 'U' 
retlw 'L' 
retlw 'U' 
retlw 'L' 
retlw 'U' 
retlw ' ' 
retlw ' ' 
retlw ' ' 
retlw 0x00 
 
Text2 
addwf PCL, f 
retlw 'B' 
retlw 'D' 
retlw 'N' 
retlw 'S' 
retlw 'O' 
retlw 'N'
retlw ' ' 
retlw ' ' 
retlw ' ' 
 
 
 
retlw 0x00 
 
; Initialize the PIC and the LCD 
Init ; 
bsf STATUS,5 ;set memory bank 3
bsf STATUS,6
clrf ANSEL ;cofigure pins as digital
clrf ANSELH
 
bcf STATUS,5 ;set memory bank 2
bcf CM1CON0,7 ;disable comparators
bcf CM2CON1,7
 
bcf STATUS,6 ;set memory bank 1
bsf STATUS,5
movlw 0XC0
movwf TRISA ;set port A as output except output 6 and 7
clrf TRISB ;set port B as output
clrf TRISC ;set port C as output
clrf TRISD ;set port D as output
bcf STATUS,5 ;set memory bank 0
 
 
call LCDInit ; Initialize the LCD Display
 
 
; Main program... 
Message 
movf count, w ; put counter value in W 
goto NextMessage ; display next message if finished 
xorlw 0x00 ; is it a zero? 
btfsc STATUS, Z 
call text; get a character from the text table 
call LCD_Char 
incf count, f 
goto Message 
 
NextMessage 
call LCD_L2 ;move to 2nd row, first column 
clrf count ;set counter register to zero 
 
Message2 
movf count, w ;put counter value in W 
call Text2 ;get a character from the text table 
xorlw 0x00 ;is it a zero? 
btfsc STATUS, Z 
goto EndMessage 
call LCD_Char 
incf count, f 
goto Message2 
 
EndMessage 
 
; Infinate loop 
Stop 
goto Stop ;endless loop 
 
; LCD routines and subs 
LCDInit ; 4 Bit Initialization... 
call Del05 ; Wait 15 msecs 
call Del05 ; 
call Del05 ; 
movlw 0x03 ; Send the Reset Instruction 
movwf LCD_PORT ; 
call Pulse_e ; Pulse LCD_E 
call Del05 ; Delay 5ms 
call Pulse_e ; Pulse LCD_E 
call D160us ; Delay of 160us 
call Pulse_e ; Pulse LCD_E 
call D160us ; Delay of 160us 
movlw 0x02 ; Send the Data Length Specification 
movwf LCD_PORT ; 
call Pulse_e ; Pulse LCD_E 
call D160us ; Delay of 160us 
movlw 0x028 ; Set Interface Length 
call LCDIns ; 
movlw 0x010 ; Turn Off Display 
call LCDIns ; 
movlw 0x001 ; Clear Display RAM 
call LCDIns ; 
movlw 0x006 ; Set Cursor Movement 
call LCDIns ; 
movlw 0x00C ; Turn on Display/Cursor 
call LCDIns ; 
call LCD_Clr ; Clear the LCD 
return ; 
 
LCDIns ; Send the Instruction to the LCD
movwf LCDTemp ; Save the Value 
swapf LCDTemp, 1
movf LCDTemp, 0
andlw 0x0F ; Most Significant Nibble first 
movwf LCD_PORT ; 
bcf LCD_PORT, LCD_RS ; 
call Pulse_e ; 
swapf LCDTemp, w ; Least Significant Nibble Second 
andlw 0x0F ; 
movwf LCD_PORT ; 
bcf LCD_PORT, LCD_RS ; 
call Pulse_e ; 
call Del01 ; wait 1 ms 
movf LCDTemp, w ; 
andlw 0xFC ; Have to Delay 5 msecs? 
btfsc STATUS, Z ; 
call Del01 ; 1ms 
return ; 
 
LCD_CharD 
addlw 0x30 ; add 0x30 to convert to ASCII 
LCD_Char ; Send the Character to the LCD 
movwf LCDTemp ; Save the Value 
swapf LCDTemp, 1
MovF LCDTemp, 0
andlw 0x0F ; Most Significant Nibble first 
movwf LCD_PORT ; 
bsf LCD_PORT, LCD_RS ; 
call Pulse_e ; 
swapf LCDTemp, w ; Least Significant Nibble Second 
andlw 0x0F ; 
movwf LCD_PORT ; 
bsf LCD_PORT, LCD_RS ; 
call Pulse_e ; 
call Del05 ; 
nop ; 
return ; 
 
 
LCD_L2: movlw 0xc0 ; move to 2nd row, first column 
call LCDIns ; 
retlw 0x00 ; 
 
LCD_Clr movlw 0x01 ; Clear display 
call LCDIns ; 
retlw 0x00 ; 
 
Pulse_e ; 
bsf LCD_PORT, LCD_E ; LCD Enable pulse to write data from PORTB 
nop ; into LCD module. 
nop
bcf LCD_PORT, LCD_E ; 
nop ;
nop 
retlw 0x00 ; 
 
 
; Delay routines... 
D160us 
clrf count1 ; 
bsf count1, 5 ; Delay 160 usecs 
bsf count1, 4 ; 
decfsz count1, f ; 
goto $ - 1 ; 
return ; 
 
Del255 movlw 0xff ; delay 255 mS 
goto d0 ; 
Del200 movlw d'255' ; delay 200mS 
goto d0 ; 
Del100 movlw d'200' ; delay 100mS 
goto d0 ; 
Del50 movlw d'100' ; delay 50mS 
goto d0 ; 
Del20 movlw d'40' ; delay 20mS 
goto d0 ; 
Del05 movlw 0x10 ; delay 5.000 ms (4 MHz clock) 
goto d0 ; 
Del01 movlw 0x05 ; delay 1.000 ms (4 MHz clock) 
d0 movwf count1 ; 
d1 movlw 0xC7 ; delay 1mS 
movwf counta ; 
movlw 0x04 ; 
movwf countb ; 
Del_0 decfsz counta,f ; 
goto $+2 ; 
decfsz countb,f ; 
goto Del_0 ; 
decfsz count1,f ; 
goto d1 ; 
retlw 0x00 ; 
 
end
----------------------------------------------------------------------
Debug build of project `C:\Users\samuel\Desktop\comp245\lab2\lab2.mcp' started.
Preprocessor symbol `__DEBUG' is defined.
Fri Jun 17 21:34:01 2011
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p18F452 "lab2.asm" /l"lab2.lst" /e"lab2.err" /d__DEBUG=1
Warning[205] C:\USERS\SAMUEL\DESKTOP\COMP245\LAB2\LAB2.ASM 1 : Found directive in column 1. (LIST)
Warning[215] C:\USERS\SAMUEL\DESKTOP\COMP245\LAB2\LAB2.ASM 1 : Processor superseded by command line. Verify processor symbol.
Message[301] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\P16F887.INC 33 : MESSAGE: (Processor-header file mismatch. Verify selected processor.)
Warning[230] C:\USERS\SAMUEL\DESKTOP\COMP245\LAB2\LAB2.ASM 4 : __CONFIG has been deprecated for PIC18 devices. Use directive CONFIG.
Error[126] C:\USERS\SAMUEL\DESKTOP\COMP245\LAB2\LAB2.ASM 4 : Argument out of range (not a valid config register address)
Error[113] C:\USERS\SAMUEL\DESKTOP\COMP245\LAB2\LAB2.ASM 108 : Symbol not previously defined (text)
Warning[226] C:\USERS\SAMUEL\DESKTOP\COMP245\LAB2\LAB2.ASM 227 : Destination address must be word aligned
Halting build on first failure as requested.
----------------------------------------------------------------------
Debug build of project `C:\Users\samuel\Desktop\comp245\lab2\lab2.mcp' failed.
Preprocessor symbol `__DEBUG' is defined.
Fri Jun 17 21:34:02 2011
----------------------------------------------------------------------
BUILD FAILED
Please any help before Tuesday June 21st would help
 

Attachments

Last edited by a moderator:

AlexR

Joined Jan 16, 2008
732
You probably did not set the device type in MPLAB (Configure > Select Device ...) and MPLAB is trying to assemble your code for a PIC18F452 instead of the PIC16F887 that you wrote the code for.
 

debjit625

Joined Apr 17, 2010
790
No AlexR is correct, you didn’t set the device correctly, look at the status bar in MPLAB you will see 18F452 rather 16F887.

Another thing, their is a call you made to "text" routine but their is no "text" routine their is only "Text" routine, remember assembly is a case sensitive language.

Error
Rich (BB code):
Rich (BB code):
Rich (BB code):
call text; get a character from the text table 

It should be
Rich (BB code):
Rich (BB code):
Rich (BB code):
call Text; get a character from the text table 


I am attaching the MPLAB project....

Good Luck
 

Attachments

AlexR

Joined Jan 16, 2008
732
Not according to the MPLAB assembler its not!
See the assembler message near the bottom of the code in your first post;
Rich (BB code):
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p18F425 "lab2.asm" /l"lab2.lst" /e"lab2.err" /d__DEBUG=1
That clearly states that the assembler is assembling code for a PIC18F425 not for a PIC16F887. The only way that will happen is if you don't set up the right device type in MPLAB.
 

Thread Starter

benz_0074

Joined Jun 17, 2011
11
Not according to the MPLAB assembler its not!
See the assembler message near the bottom of the code in your first post;
Rich (BB code):
Executing: "C:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p18F425 "lab2.asm" /l"lab2.lst" /e"lab2.err" /d__DEBUG=1
That clearly states that the assembler is assembling code for a PIC18F425 not for a PIC16F887. The only way that will happen is if you don't set up the right device type in MPLAB.
I did check the select device and it is set for pic16f887.
 

Thread Starter

benz_0074

Joined Jun 17, 2011
11
Hey guys thank you for your help. Everything works now expect Wambululu does not get outputted on the Display but bdnson does. My hunch might be telling me there is something wrong with my "call Text" line. I have a picture of the output in the attachment same as the code that works now it only shows BDNSON.
 

Attachments

Last edited:

pancikok

Joined Oct 31, 2009
26
i think you have put the goto NextMessage and call Text in wrong positions so instead of this:
Rich (BB code):
Message 
movf count, w ; put counter value in W 
goto NextMessage ; display next message if finished 
xorlw 0x00 ; is it a zero? 
btfsc STATUS, Z 
call text; get a character from the text table 
call LCD_Char 
incf count, f 
goto Message
it should be like this:
Rich (BB code):
Message 
movf count, w ; put counter value in W 
call Text; get a character from the text table 
xorlw 0x00 ; is it a zero? 
btfsc STATUS, Z 
goto NextMessage ; display next message if finished 
call LCD_Char 
incf count, f 
goto Message
well, i guess though! )
 
Top