PDA

View Full Version : Why doesn't my code for 18f452 work on 18f252?



senojlr
- 22nd December 2005, 02:46
Hello, I developed code for a project to work on a PIC 18f452 and have decided to use a PIC 18f252 instead. It works fine on the 452 chip but will not work on the 252 chip. I have the problem narrowed down to the define commands to initialize the LCD. I can comment them out and it works fine except the LCD . The attached code is not my actual project but some test code that I used to narrow the problem down. It runs fine on the 452 chip but when you run it on the 252 chip the watchdog timer times out. The code sends messages to an LCD and turns an LED attached to portb.6 on and off depending on which message is displayed. I have double checked the hardware connections and they are correct. ANY IDEA'S ????

Thanks

Define OSC 20 ' Set Oscillator Freq to 20 MHZ
Define LCD_DREG PORTC 'Set LCD Data Port
Define LCD_DBit 0 'Set Starting data bit (0 or 4) if 4- bit bus
Define LCD_RSREG PORTA 'Set LCD Register Select port
Define LCD_RSBIT 0 'Set LCD Register select bit
Define LCD_EREG PORTB 'Set LCD Enable port
Define LCD_EBIT 0 'Set LCD Enable bit
Define LCD_BITS 4 'Set LCD BUS size (4 or 8 bits)
Define LCD_LINES2 'Set # of lines on LCD
Define LCD_COMMANDUS 2000 'Set command delay time in us
Define LCD_DATAUS 500 'Set Data delay time in us
LCD1 VAR Word ' LCD Counter LSD
LCD2 VAR Word ' LCD Counter MSD
LCDFlag Var Bit
LCDFlag=0
LCD2 = 600 'Counter to switch between LCD Messages

loop: '********************* Program Loop ****************************
LCD1 = LCD1 + 1 ' COUNTER
If LCD1 => 92 Then
LCD2 = LCD2 + 1 'Counter to switch between LCD Messages
LCD1 = 0
endif
If LCD2 > 500 then 'Switch LCD Messages every 500 counts
if LCDFlag = 0 then
Lcdout $fe, $80 ' Move Cursor to 1st line of LCD Display
Lcdout "Msg1 Line1"
Lcdout $fe, $C0 ' Move Cursor to 2nd lind of LCD display
Lcdout "Msg1 Line2"
LCDFlag=1
low 6 'Turn on LED on PORTB.6
LCD2 = 0 'Reset Counter
Else
Lcdout $fe, $80 ' Move Cursor to 1st line of LCD Display
Lcdout "Msg2 Line1"
Lcdout $fe, $C0 ' Move Cursor to 2nd lind of LCD display
Lcdout "Msg2 Line2"
LCDFlag=0
high 6 'Turn off LED on PORTB.6
LCD2 = 0 'Reset Counter
Endif
endif
goto loop

MikeTamu
- 22nd December 2005, 02:53
Add the following code at the very beginning of yours:

ADCON1.3=0
ADCON1.2=1
ADCON1.1=1

Your pins on port A are most likely in analog mode, this puts them in digital mode.

senojlr
- 22nd December 2005, 03:18
Thanks for the reply mike. I tried it and it still does not work. I am using the same code for the 252 chip as I am the 452 chip and the 452 works fine.

Bruce
- 22nd December 2005, 05:58
As Mike already pointed out, you really do need to disable A/D on PORTA.0 with ADCON1 = 7. These pins are by default A/D inputs until you configure them for digital I/O with ADCON1. It may just be a fluke that it's working on the 452 with RA0 configured as an A/D input.

This (below) isn't part of the problem, but if you want the expected results from both statements then change;

LCD_LINES2

to

LCD_LINES 2 ' < -- need the separation

And

If LCD1 => 92 Then

to

If LCD1 >= 92 Then ' = should always be after > when combined.

Also be sure to set HS VS XT or you may not be providing enough drive for the higher speed crystal.


when you run it on the 252 chip the watchdog timer times out
PBP should automatically handle resetting the WDT for you, but if you suspect the WDT is fouling things up, just disable it before programming the part.

senojlr
- 23rd December 2005, 02:42
Thanks for the replys guys. I tried everything that you suggested but it did not fix the problem. Out of desperation I was just trying different things and moved the the LCD Data Port from PortC to PortB and it works fine now. I don't know what the difference is because the PortC pins on the 252 and 452 chips are suppose to be the same. If anyone knows what the difference may be I would be interested to know. Anyway thanks again guys and have a Merry CHRISTmas.

RJ