0
\$\begingroup\$

So I have a PIC16F1827 on a breadboard and was trying to get a simple demo working where I touch a wire to turn on an LED using the Capacitive Sensing Module. I was primarily going off this example I found, cross-referencing section 27 of the datasheet to make any corrections. However, despite my best efforts it wont do anything. From what I can gather the Timer1 isn't incrementing at all, just sitting at 0. Can someone help me figure out what I've screwed up here?

int count = 0;
int thresh = 0;

void main(void)
{
    // initialize the device
    SYSTEM_Initialize();
    SWDTEN = 0;

    TRISA1 = 0;
    RA1 = 0;
    // Set B5 as analog input
    TRISB5 = 1;
    ANSB5 = 1;
    // Set CPS module as Timer1 clock source
    T1CON = 0b11000100;
    T1GCON = 0;
    // Set CPS range to high and channel to 8(B5)
    CPSCON0 = 0b10001100;
    CPSCON1 = 0b1000;

    TMR1ON = 1;
    TMR1 = 0;
    __delay_ms(16);
    TMR1ON = 0;
    thresh = TMR1 * 8 / 10;;
    TMR1 = 0;
    TMR1ON = 1;
    //SWDTEN = 1;
    //SLEEP();
    __delay_ms(16);

    while (1)
    {
       TMR1ON = 0;
       count = TMR1;
       RA1 = count < thresh;
       TMR1 = 0;
       TMR1ON = 1;
       //SWDTEN = 1;
       //SLEEP();
       __delay_ms(16);
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ When I have problems with microcontroller peripherals that I just can't resolve, I try backing up and doing something really simple. In this case, given the description of the CPS module in your link, I think I would make code that just turns the light on any time the timer number goes above half it's range, and don't reset the value. This should give you an LED that blinks at a rate roughly proportional to the CPS oscillator's rate, only divided by 2^16 (or whatever the timer size is). So if it blinks, and blinks different when you touch it, you've learned a LOT. \$\endgroup\$ – TimWescott Oct 20 '18 at 17:07
  • \$\begingroup\$ I've tried a few variations of that and similar ideas. That's how I confirmed the timer isn't counting. I set RA1 = count == 0;. The LED stayed solidly lit. \$\endgroup\$ – Austin Leydecker Oct 20 '18 at 21:42
  • \$\begingroup\$ So, I poked at the datasheet a little bit. I strongly suspect that there's some "screw the processor bit" that you haven't set correctly yet. The fact that you're not getting and indication of oscillation at all tells me that either you don't, in fact, have the capacitive sensing oscillator going, or that you've not correctly configured its output to the timer, that you haven't connected it to the expected pin, or that something is dreadfully wrong electronically. Sorry for the vague answer -- I haven't actually used this part, so I can't help with specifics. \$\endgroup\$ – TimWescott Oct 21 '18 at 1:37
  • 1
    \$\begingroup\$ So I did some more poking and prodding and was able to determine that the issue is definitely something to do with the capacitive module or its connection to Timer1, as changing the Timer1 clock source to the system click sends it oscillating exactly as expected. \$\endgroup\$ – Austin Leydecker Oct 21 '18 at 4:51
0
\$\begingroup\$

I figured it out. Turns out I had the channel set wrong, pin B5 is channel 7, not 8. Everything seems to be working as expected now!

\$\endgroup\$
  • \$\begingroup\$ Oh, that is so typical. Don't think that this stuff will go away as you get more experience -- I've been doing this stuff professionally since 1988 and I still get things like that wrong from time to time. Glad to hear you got it working. \$\endgroup\$ – TimWescott Oct 21 '18 at 15:53

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.