2
\$\begingroup\$

I originally asked the this question on Stack Overflow. I think here would have been a better audience.

I'm trying to program a PIC12C508A to do a simple LED learning circuit. I've read some examples, the Microchip Datasheet, pic12c508a.h and pic12c508a.inc. I've tried to set the TRIS register using a C program and an ASM program but it does not take. Using MPLAB X, the XC8 compiler, and the built in simulator to check the SFR registers I can see that the TRIS is not updating even when the WREG holds the correct values. If anyone has experience with this please check out my code and see if I am doing something wrong.

#include <xc.h>

// -- CONFIG
#pragma config MCLRE = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config WDT = OFF        // Turn Watchdog Timer Off.
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
#pragma config OSC = IntRC      // Internal RC Oscillator

// -- Internal Frequency
#define _XTAL_FREQ 400000

int main()
{
    OPTION = 0b11011111; // Added this from feedback on Stack Overflow
             //--0----- Clear T0CS per PIC12C508A datasheet on using GP2 as output

    TRIS = 0b111010;  // 0x3A
           //---0-0 Set GP0 and GP2 as outputs

    GPIO = 0b000100;  // 0x04
           //---1-0 Set GP2 as HIGH and GP0 as LOW

    for(int countdown = 10; countdown > 0; --countdown) {
        __delay_ms(60000); // Delay 1 minute.
    }

    GPIO = 0b000001;  // 0x01
           //---0-1 Set GP2 as LOW and GP0 as HIGH

    while(1)
        NOP();
}

I also tried it in assembly which is pretty much identical to the Gooligum tutorials for baseline PIC models. (Update: I have not tried to set the OPTION register in ASM yet.)

  list  p=12c508a
  #include <p12c508a.inc>

  __CONFIG  _MCLRE_ON & _CP_OFF & _WDT_OFF & _IntRC_OSC

RCCAL CODE    0x0FF   ; Processor Reset Vector
      res     1       ; Hold internal RC cal value, as a movlw k

RESET CODE    0x000   ; RESET VECTOR
      movwf   OSCCAL  ; Factory Calibration

start
      movlw   b'111010' ; Configure GP0/GP2 as outputs
      tris    GPIO      ;
      movlw   b'000100' ; Set GP2 HIGH - GREEN LED
      movwf   GPIO

      goto    $         ; loop forever

      END

This all seems pretty straight forward but when I use breakpoints and examine the SFR registers in the simulator I can see that the GPIO and TRIS registers are never changed even though the WREG will hold the correct values. I've examined the ASM output that the XC8 compiler generates and it is almost identical to the ASM I wrote when it comes to setting the registers.

I've also tried using HEX values and straight integer values and the results are the same.

\$\endgroup\$
3
\$\begingroup\$

The problem of TRIS not updating appears to be a bug in MPLAB X's PIC508 simulator core. I complied your code with MPLAB X and it failed in the simulator. I then loaded the hex file into MPLAB 8.92 and it simulated perfectly.

I recommend using MPLAB 8.92 for low-end chips until (if ever) they get the bugs out of MPLAB X, but if you want to continue using MPLAB X then switch the processor to PIC12F508 (which doesn't have this problem).

\$\endgroup\$
  • \$\begingroup\$ Someone on StackOverflow caught my mistake. I have the _XTAL_FREQ set to 400Khz not 4Mhz. When I made the change and programmed a 12C it worked on the breadboard. The simulator, as you pointed out, still does not work in MPLAB X. Thank you for help. \$\endgroup\$ – Justin Jul 15 '14 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.