Why doesn't my code for 18f452 work on 18f252?


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2005
    Posts
    14

    Default Why doesn't my code for 18f452 work on 18f252?

    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

  2. #2
    MikeTamu's Avatar
    MikeTamu Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  3. #3
    Join Date
    Aug 2005
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    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.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Aug 2005
    Posts
    14


    Did you find this post helpful? Yes | No

    Default I Fixed IT

    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

Similar Threads

  1. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 07:25
  2. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  3. Error code [112]/[113]
    By Dj tempo in forum General
    Replies: 3
    Last Post: - 17th June 2007, 16:15
  4. Code: Why is this code greater than 2000 words?
    By DrDreas in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 1st June 2007, 19:51
  5. Replies: 1
    Last Post: - 31st August 2005, 20:00

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts