1
\$\begingroup\$

Here's a relatively simple question: What is wrong with my code below?

I am trying to blink an LED from GP2 pin, but am getting confused when it comes to setting GPIO pins and registers, and its a bit different too because the 509 doesn't have an ADC. Would really appreciate some help.

Thanks (code below).

#include <xc.h>
#include <pic12f509.h>

#pragma config OSC = IntRC      // Oscillator Selection bits (internal RC oscillator)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
#pragma config CP = OFF         // Code Protection bit (Code protection off)
#pragma config MCLRE = ON       // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)

#define _XTAL_FREQ 4000000
#define LED GPIObits.GP2

int main()
{
    TRISGPIO = 0x00;

    while(1)
    {
        LED = 1;
        __delay_ms(1000);
        LED = 0;
        __delay_ms(1000);
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ There's nothing obviously wrong with your code. Have you wired the LED the right way round and do you have a current limiting resistor? Also, MCLR must be tied high unless you turn if OFF in the config. \$\endgroup\$ Commented Oct 5, 2015 at 7:22
  • \$\begingroup\$ @RogerRowland 1k current ilimitng resistor in use, mclr pulled high with 1n4148 and 4.7k resistor in series, and sample 12f blink hex file that came with the programmer works fine, my guess is I'm missing some register setting or configuration of some sort. \$\endgroup\$
    – ezra_vdj
    Commented Oct 5, 2015 at 7:32
  • \$\begingroup\$ Set T0CS to zero in the OPTION register before setting the TRIS bits - datasheet explains why. \$\endgroup\$ Commented Oct 5, 2015 at 7:50

1 Answer 1

2
\$\begingroup\$

In Section 4.5 of the datasheet for this device, the OPTION register includes bit 5, or T0CS - the Timer0 Clock Source Select bit, which by default is set (1).

In this state, the comment in the datasheet notes:

1 = Transition on T0CKI pin (overrides TRIS on the T0CKI pin)

As GP2 is shared with T0CKI, you need to clear T0CS before clearing the TRIS register bits to enable GP2 as an output.

int main()
{
    OPTION = 0x00;
    TRISGPIO = 0x00;

    while(1)
    {
        LED = 1;
        __delay_ms(1000);
        LED = 0;
        __delay_ms(1000);
    }
}
\$\endgroup\$
7
  • \$\begingroup\$ Set to 0 by OPTION_REGbits.T0CS=0? \$\endgroup\$
    – ezra_vdj
    Commented Oct 5, 2015 at 9:03
  • \$\begingroup\$ @ezra_vdj Correct - see edited answer. \$\endgroup\$ Commented Oct 5, 2015 at 9:07
  • \$\begingroup\$ XC8 does not like the use of OPTION_REGbits.T0CS=0 (error 192). How do you do the other way where you address the register directly? \$\endgroup\$
    – ezra_vdj
    Commented Oct 5, 2015 at 9:10
  • \$\begingroup\$ @ezra_vdj I'd have to fire up MPLAB X and try it, but I'm not at the right PC at the moment. I would think that just T0CS = 0; should be enough. It depends how the register has been declared in the relevant header. I'll update for sure as soon as I can. \$\endgroup\$ Commented Oct 5, 2015 at 9:21
  • \$\begingroup\$ @ezra_vdj See edit - I just browsed the header, and it declares OPTION but doesn't split out the bits. So the code above will mask out T0CS to set it to zero and leave all other bits unchanged. \$\endgroup\$ Commented Oct 5, 2015 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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