• PicŪ Basic


  • Getting started with 24 series PICs

    This article details my journey from 18F chips to the 24H series.

    I was one of the many forum users who encouraged Les to produce a compiler for the 24 bit chips. I was delighted when the first version came out, and I just sat and watched as very few of us bit the bullet and got stuck in.

    I bought a Firewing board and what a nice piece of kit it is. I even managed to get the LED flashing at different rates, but I didn't feel able to go much further. I assumed it was going to be terribly complicated and I would get stuck at every turn. So I carried on with my favourite chip: 18F26K22. But then a project came up that needed a massive amount of text and a fairly small piece of code to handle the text (The Turing Test for The National Museum Of Computing at Bletchley Park).

    The project was going well and I had used around 25% of the 64K bytes memory of the 18F. I guessed that one day I would reach the end of the 64K so I looked at bigger 18F series chips. But then a little voice in my head said: "Use one of the 24 bit devices".

    So I bought a 24HJ128GP502 which has 128K of flash memory. I read the data sheet closely with regard to decoupling and power supplies and got my little strip-board with the 24 on it going.

    I copied some bits from Les's examples (thanks Les) and was absolutely amazed when nearly everything I tried worked! I thought this was supposed to be a difficult thing, not so far.

    Which is why I am writing this, if I could have found a simple code example which flashed a LED, ran a timer and handled simple interrupts I would have taken the plunge much sooner. Although the 24 series is a lot more complicated than the 8 bit chips, things seem much more logical.

    Interrupts for example: each interrupt has its own handler, you simply write one handler for each interrupting device without having to work out which interrupt it is. Timers are more logical: you specify the count going upwards that will trigger an interrupt rather than working out when the counter would underflow. I even managed to work out the preset value by hand without needing a plug-in to do it (which was very useful before).

    So here is the code I started with (using a PicKit2 to program the chip):


    [Taken from examples by Les]


    Device = 24HJ128GP502
    Declare Xtal = 79.23
    PLL_Setup(43, 2, 2, $0300) 'set ppl for 79.23Megs

    Symbol led PORTB.5

    While
    Toggle led
    DelayMS 100
    Wend


    Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
    Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
    Config FGS = GWRP_OFF, GCP_OFF
    Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
    Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
    Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
    Config FPOR = FPWRT_PWR128, ALTI2C_OFF
    Config FICD = ICS_PGD1, JTAGEN_OFF

    (I copied the configs at the end blindly without trying to work out what was going on)


    I compiled it, loaded it into the chip, ticked the box on the PicKit2 which turns on the 3.3V Vdd and voila! A flashing led. Could it be any simpler?

    Now I needed a timer. The first timer in this chip is Timer1. A 16 bit timer.
    The interrupt handler for it looks like this:

    Isr- T1Interrupt
    timer_flag = 1 ' tell my main code that timer has completed
    IFS0bits_T1IF = 0 ' Reset the Timer1 interrupt flag
    EndIsr- ' exit the interrupt

    So let's add that to our code together with setting the timer up etc:


    Device = 24HJ128GP502
    Declare Xtal = 79.23
    PLL_Setup(43, 2, 2, $0300) 'set ppl for 79.23Megs

    Symbol led PORTB.5
    Dim timer_flag As Bit


    IFS0bits_T1IF = 0 ' clear Timer1 interrupt flag
    IPC0bits_T1IP0 = 0 ' set priority
    IEC0bits_T1IE = 1 ' Enable the Timer1 interrupt
    TMR1 = 0 ' clear the count
    PR1 = 65535 ' load Timer1 period
    T1CON = %1000000000110000 ' pre-scale 256. bit 15 is "start"

    While
    If timer_flag = 1 Then ' timer has expired, toggle the led
    timer_flag = 0 ' clear our marker
    Toggle led
    EndIf
    Wend


    Isr- T1Interrupt ' int handler for T1
    timer_flag = 1 ' tell main code that timer has completed
    IFS0bits_T1IF = 0 ' clear Timer1 interrupt flag
    EndIsr- ' exit the interrupt


    Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
    Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
    Config FGS = GWRP_OFF, GCP_OFF
    Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
    Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
    Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
    Config FPOR = FPWRT_PWR128, ALTI2C_OFF
    Config FICD = ICS_PGD1, JTAGEN_OFF

    How often will the LED flash?

    The clock is 80Megs (I know it's 79.23 but 80 is easier to work with)
    The prescale (bits 4,5 in T1CON) is set at 256 (both bits set). And the timer is fed from the system clock/2
    So 40,000,000/256 is 156,250 per second (into the timer)
    And the timer is dividing this by 65535 (max value) giving 2.38 per second
    So the LED will be toggled 2.4 times a second, which means it will be on for about half a second and off for the same.

    I think that this is the slowest you can get a single 16 bit timer to run with an 80meg clock. The datasheet shows that you can link two timers to form a 32 bit unit.

    Don't forget that these babies do one instruction every two clocks, that's 40 million instructions a second. The first machine I ever worked on took 288uS to do a single simple instruction. And 80 meg clocks are pretty slow these days!

    So copy and paste the above into your IDE and off you go! You are started on the 24 road. It's not difficult. And thanks to Les who has worked wonders on the compiler.

    Good luck.
    Charlie

    PS If you want to know how the re-assign pins easily, hadv215 has written a very good short article called:

    16-bit devices: Peripheral Functions, Pins and PPS. It's in the same section of the wiki as this article.
  • Recent Activity

    normnet-324

    UltraEdit adaption.

    Thread Starter: johngb

    I didn't know where to post this thread but as I guess this section is one of the most viewed I am posting here. After Les mentioned UltraEdit on...

    normnet Today, 18:22 Go to last post
    joesaliba-353

    Correct use of bit names

    Thread Starter: joesaliba

    So I have the following: - Symbol RBIF = INTCON.0 ' Port B Interrupt-On-Change (IOCx) Interrupt Flag bit Symbol INT0IF = INTCON.1 ' INT0...

    joesaliba Today, 17:20 Go to last post