Search This Blog

Sunday, January 24, 2010

16F88 - A Pernicious Feature

    I really hope this helps someone.  I had been working happily with a 16F84A for a while and was getting pretty confident.  I ordered a 16F88 and began to wade my way through all the additional configuration settings.  I was anxious to take advantage of all the freed up pins with the internal oscillator and MCLR pin.  I just couldn't get it to work.  I dropped back to just turning on an led.  That actually worked.  But nothing else would even make a flicker.  The code was compiled with gputils and loaded with picp and a PicStart Plus.  After slowly adding bits to the basic "turn on the led" code and looking at the generated and written hex files, I made an interesting discovery.  I had been setting up the first few lines of the program like this:

;******************************************************************************
;  RESET VECTOR                                                               *
;******************************************************************************
      ORG     0x000             ; processor reset vector
      nop
      goto    main              ; go to beginning of program

;******************************************************************************
;  INTERRUPT VECTOR                                                           *
;******************************************************************************
      ORG     0x004             ; interrupt vector location
        ; standard interupt code here

If I put 3 nops in the reset code in addition to the goto, my programs ran!  I'm still not sure what is going on here.  The generated and written hex code look correct.  The practical conclusion:  Make sure that your ORGs line up with the lines of code you have.  I shudder to think what will happen if I want to force a new page for a section of code to keep it from spanning two pages...  We'll cross that bridge when we get to it.  Hopefully, my bootloader code and accompanying PC code will not have the same issue.  I hope it is just an odd issue with either the compiler or the programming software.  It is also odd that I don't see this with the 16F84A.

Baby Steps To a Bootloader

Linux.... the final frontier... these are my voyages as I try yet another thing that takes more manual effort in Linux.  I am playing with a Microchip mid-range 16F88 MCU.  The current goal is to create a bootloader and program that will load new code onto the chip in circuit from a Linux PC.  There are probably some great tools for this.  I decided to get the information I could and just write my own.  I've started with the tinyBootloader's source code as a template.  To keep the size of the bootloader down, it looks like I'll want to use the built-in USART circuits and pins.  That means building an inverter circuit for the serial cable with a Maxim 3232.
    So far, I'm working on an LCD parallel to serial circuit based on Myke Predko's.  On the PC end, I've grabbed the uspp code as a helper package for serial communications in Python ( authors: Isaac Barona Martínez, Damien Géranton, Douglas Jones, J.Grauheding ).  I've brushed up on my rusty Python to parse an Intel hex file:
def readHex():
    file = open( 'blink.hex', 'r' )
    lines = file.readlines()
    file.close()
    for line in lines:
        ni = line[1:3]
        numInstr = int( ni, 16 ) / 2
        addrH = hex( int( line[3:5], 16 ) / 2 )
        addrL = hex( int( line[5:7], 16 ) / 2 )
        recordType = int(line[7:9],16)
        start = 9
        # Record Type 0: data
        # Record Type 1: end of file
        # Record Type 2 & 3: Extended segment addressing.  throw error for us...
        # Record Type 4: signals true 32 bit addressing.  ignore for us
        # Record Type 5: anther extend address thingy.  throw error
        if recordType == 0: # This is a data line
            print 'numInstr: ' + str(numInstr),
            print 'address: ' + addrH + ' ' + addrL + ' ',
            dataList = []
            for i in range( start, len(line) - 4, 4 ):
                dataLH = line[i:i+2], line[i+2:i+4]
                dataList.append( dataLH )
            checksum = line[len(line)-3:]
            print 'data: ',
            for data in dataList:
                print data[0] + data[1],
            print ' checksum: ' + checksum
        elif recordType == 1:  # end of file
            print '\n Complete.'
        elif recordType == 4:   # print inhx32 warning
            print  'Found an indicator line to use 32 bit addressing.  Compile with inhx8s or inhx8m. Ignoring this line.'
        else:
            raise Exception( 'AAAAAAGGGGHHHH!  Found lines with extended segment addressing.  Why are you putting this on a pic??' )     
       
readHex()

Note that this is just a read and spew.  I found a couple of interesting things.  The first is with my compiler.  I use the gputils package. It defaults to 32 bit style ( inhx32 ).  This puts a record type of 4 on the first line.  I plan to ignore any lines that are not record type 0 ( data ) or 1( end of file ) for the first draft. 
    Baby steps.  First I need to get the LCD interface to work with the 16F88.  Then comes the Python program to send the hex code.  Then I'll work on sending it code to load into data eeprom.  The bootloader code will follow.  In the final game, I'd like to replace the DB9 serial with USB.  Then I'd be able to update my chip's code from my laptop instead of my desktop.  I'm also looking forward to the day when I can't find a computer with a serial port :)

Happy Birthday to Blog

    I have finally bitten the bullet and created a blog.  So, here comes the obligatory "mission" statement.  This blog is intended for my own use as a diary of code snippets and discoveries.  I have benefitted from so many blogs/posts from users on the web.  I depend on the postings I find to do my job, my hobbies, and so many other facets of my life.  With luck, someone will stumble on my posts and find solutions to problems they face.
    I am a software developer, working primarily in Java.  I have recently taken up programming Microchip picmicro MCUs as a hobby.  My posts will focus on discoveries, solutions, and dilemmas in these arenas.  Ocassionally, I may post some random nonsense for the sheer catharsis of it.
   Geeks, wish me luck.  The better my posts, the better your chance they may help you!