Need help with Timer0 / PORTB Input

View: New views
6 Messages — Rating Filter:   Alert me  

Need help with Timer0 / PORTB Input

by Erik Reynolds :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All,

This is my first time posting here.  I have been designing a countdown
timer that uses a PIC16F872 running at 4 Mhz.  It allows the user to
enter in a time in seconds ranging from 00.0 to 99.9, and then press a
button to start a countdown.  The code uses Timer0 with a pre-scaler
of 1:8 set to 133 so that it interrupts every 10 ms.  Every ten
interrupts, it calls a function that 'clocks' a tenth of a second.  In
the meantime, the chip is continuously outputting the current value
for time.  My issue is that while the countdown works flawlessly, I
can't get any button input on PORTB.  However, when I comment out my
timer0 interrupt routine, and the statement where I enable the timer0
interrupt initially, the button input works just fine.

I'm writing this in C using the CCS complier but I could definitely
rewrite in using the PICC-Lite compiler if it would help.

Any help would greatly be appreciated as I'm stumped!

Thanks!
--
-Erik J. Reynolds
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

RE: Need help with Timer0 / PORTB Input

by Michael Rigby-Jones :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



> -----Original Message-----
> From: piclist-bounces@... [mailto:piclist-bounces@...] On
Behalf

> Of Erik Reynolds
> Sent: 06 August 2008 16:37
> To: piclist@...
> Subject: [PIC] Need help with Timer0 / PORTB Input
>
> Hi All,
>
> This is my first time posting here.  I have been designing a countdown
> timer that uses a PIC16F872 running at 4 Mhz.  It allows the user to
> enter in a time in seconds ranging from 00.0 to 99.9, and then press a
> button to start a countdown.  The code uses Timer0 with a pre-scaler
> of 1:8 set to 133 so that it interrupts every 10 ms.  Every ten
> interrupts, it calls a function that 'clocks' a tenth of a second.  In
> the meantime, the chip is continuously outputting the current value
> for time.  My issue is that while the countdown works flawlessly, I
> can't get any button input on PORTB.  However, when I comment out my
> timer0 interrupt routine, and the statement where I enable the timer0
> interrupt initially, the button input works just fine.
>

Are you trying to use the PORTB "Interrupt on change" feature, or are
you just polling the button input?

Regards

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

Re: Need help with Timer0 / PORTB Input

by Erik Reynolds :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm polling PORTB in a neat little debouncing routine I was taught in
class last semester.

On Wed, Aug 6, 2008 at 11:50 AM, Michael Rigby-Jones
<Michael.Rigby-Jones@...> wrote:

>
>
>> -----Original Message-----
>> From: piclist-bounces@... [mailto:piclist-bounces@...] On
> Behalf
>> Of Erik Reynolds
>> Sent: 06 August 2008 16:37
>> To: piclist@...
>> Subject: [PIC] Need help with Timer0 / PORTB Input
>>
>> Hi All,
>>
>> This is my first time posting here.  I have been designing a countdown
>> timer that uses a PIC16F872 running at 4 Mhz.  It allows the user to
>> enter in a time in seconds ranging from 00.0 to 99.9, and then press a
>> button to start a countdown.  The code uses Timer0 with a pre-scaler
>> of 1:8 set to 133 so that it interrupts every 10 ms.  Every ten
>> interrupts, it calls a function that 'clocks' a tenth of a second.  In
>> the meantime, the chip is continuously outputting the current value
>> for time.  My issue is that while the countdown works flawlessly, I
>> can't get any button input on PORTB.  However, when I comment out my
>> timer0 interrupt routine, and the statement where I enable the timer0
>> interrupt initially, the button input works just fine.
>>
>
> Are you trying to use the PORTB "Interrupt on change" feature, or are
> you just polling the button input?
>
> Regards
>
> Mike
>
> =======================================================================
> This e-mail is intended for the person it is addressed to only. The
> information contained in it may be confidential and/or protected by
> law. If you are not the intended recipient of this message, you must
> not make any use of this information, or copy or show it to any
> person. Please contact us immediately to tell us that you have
> received this e-mail, and return the original to us. Any use,
> forwarding, printing or copying of this message is strictly prohibited.
> No part of this message can be considered a request for goods or
> services.
> =======================================================================
>
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>



--
-Erik J. Reynolds
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

Re: Need help with Timer0 / PORTB Input

by Maarten Hofman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Redwood Shores, 6 augustus 2008.

> I'm polling PORTB in a neat little debouncing routine I was taught in
> class last semester.

If your PIC has schmitt trigger inputs you can get away with just using a
0.1uF capacitor and a 100kOhm resistor (resistor to +5V, switch to ground,
capacitor to ground (across switch).

0.1uF*100kOhm=10ms, which is generally the "bounce" time of a typical
switch. I used it succesfully in a number of projects.

Greetings,
Maarten Hofman.
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

Re: Need help with Timer0 / PORTB Input

by M. Adam Davis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't see any interaction between the timer0 and port B.
How fast are you running the micro?
How much processing time is your 10mS timer taking up?
How are you timing the debounce routine?
Does the chip configuration differ between the two examples you've
provided (timer enabled vs disabled)?
Are you properly reseting the timer0 interrupt flag at the end of the
timer0 routine?

You may have to post the code so we can better see how things are interacting.

If I were debuggin this I'd start from the bottom - copy the bit for a
button in portb to another pin set as output and make sure that it's
being read correctly (tests that the pin is an input, and that no
other configuration is consuing the pin).  Then pipe the output of the
debounce routine to the output pin.  Find out whether it's a debounce
problem or a pin configuration problem.

If it misses in debounce, start looking into the code to see where the
signal disappears.  If it happens before debounce double and triple
check the configuration of portb and everything that has anything to
do with it.

The bug has got to be in there _somewhere_...

-Adam

On 8/6/08, Erik Reynolds <ejreynolds@...> wrote:

> I'm polling PORTB in a neat little debouncing routine I was taught in
> class last semester.
>
> On Wed, Aug 6, 2008 at 11:50 AM, Michael Rigby-Jones
> <Michael.Rigby-Jones@...> wrote:
> >
> >
> >> -----Original Message-----
> >> From: piclist-bounces@... [mailto:piclist-bounces@...] On
> > Behalf
> >> Of Erik Reynolds
> >> Sent: 06 August 2008 16:37
> >> To: piclist@...
> >> Subject: [PIC] Need help with Timer0 / PORTB Input
> >>
> >> Hi All,
> >>
> >> This is my first time posting here.  I have been designing a countdown
> >> timer that uses a PIC16F872 running at 4 Mhz.  It allows the user to
> >> enter in a time in seconds ranging from 00.0 to 99.9, and then press a
> >> button to start a countdown.  The code uses Timer0 with a pre-scaler
> >> of 1:8 set to 133 so that it interrupts every 10 ms.  Every ten
> >> interrupts, it calls a function that 'clocks' a tenth of a second.  In
> >> the meantime, the chip is continuously outputting the current value
> >> for time.  My issue is that while the countdown works flawlessly, I
> >> can't get any button input on PORTB.  However, when I comment out my
> >> timer0 interrupt routine, and the statement where I enable the timer0
> >> interrupt initially, the button input works just fine.
> >>
> >
> > Are you trying to use the PORTB "Interrupt on change" feature, or are
> > you just polling the button input?
> >
> > Regards
> >
> > Mike
> >
> > =======================================================================
> > This e-mail is intended for the person it is addressed to only. The
> > information contained in it may be confidential and/or protected by
> > law. If you are not the intended recipient of this message, you must
> > not make any use of this information, or copy or show it to any
> > person. Please contact us immediately to tell us that you have
> > received this e-mail, and return the original to us. Any use,
> > forwarding, printing or copying of this message is strictly prohibited.
> > No part of this message can be considered a request for goods or
> > services.
> > =======================================================================
> >
> > --
> > http://www.piclist.com PIC/SX FAQ & list archive
> > View/change your membership options at
> > http://mailman.mit.edu/mailman/listinfo/piclist
> >
>
>
>
> --
> -Erik J. Reynolds
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>


--
EARTH DAY 2008
Tuesday April 22
Save Money * Save Oil * Save Lives * Save the Planet
http://www.driveslowly.org
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

Re: Need help with Timer0 / PORTB Input

by Erik Reynolds :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Adam,

micro is running at 4 mhz.  Timer interrupt is triggering approx every
10 ms, and every 100ms running a short routine to decrement a few
registers that hold the current seconds, tenth of a second etc.  I've
included the code per your request.  Oh also, between those two cases
I provided, no chip configuration differed, the only thing I changed
was I literally put a comment block around the ISR at the end, and
commented out the line for enable_interrupt(INT_TIMER0).

I apologize if using the CCS PIC Compilier is unusual, I can rewrite
it in C using PICC-Lite if it would be helpful.

Thanks for all the responses thus far guys!

-Erik

// Includes
#include "C:\Documents and Settings\audioHack\My Documents\Timer
Circuit\Code\TimerCircuitV1.h"

// Global Variables
// Timing Registers
unsigned char numInt = 0;
unsigned char blinkCount = 0;
short finishedCountdown = 0;

// Counting Registers
unsigned char numTenths = 0;
unsigned char numSeconds = 0;
unsigned char numTensSeconds = 0;

// Key Press Stuff
unsigned char keyState;
unsigned char keyCode;

// Initialization Function
void initial () {
        port_b_pullups(TRUE);
        setup_adc_ports(NO_ANALOGS);
        setup_adc(ADC_OFF);
        setup_spi(FALSE);
        setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
        setup_timer_1(T1_DISABLED);
        setup_timer_2(T2_DISABLED,0,1);
        set_timer0(133);

        enable_interrupts(INT_TIMER0);

        // Set up the ports for I/O
        set_tris_a(0);
        set_tris_b(0b11100000); // 0xFC
        set_tris_c(0x00);

        enable_interrupts(GLOBAL);
}

// Called when 100 timer0 interrupts have occured, meaning 0.1seconds
have elapsed
void clockTenthSecond () {
        if (numTenths == 0 && numSeconds == 0 && numTensSeconds == 0) {
                // Finished!
                finishedCountdown = 1;
                return;
        } else if (numTenths == 0 && numSeconds != 0) {
                numSeconds--;
                numTenths = 9;
                return;
        } else if (numTenths == 0 && numTensSeconds != 0) {
                numTensSeconds--;
                numSeconds = 9;
                numTenths = 9;
                return;
        } else {
                numTenths--;
                return;
        }
}

// Sees if any key was pressed
char anyKey () {
        // tempSet the tristate of b
        char tempTris;

        tempTris = get_tris_b();
        set_tris_b(0xFF);

        if ((input_b() & 0b11110000) != 0b11110000) {
                // fix tristate of b
                set_tris_b(tempTris);
                return 1;
        } else {
                // fix tristate of b
                set_tris_b(tempTris);
                return 0;
        }
}

// Figures out which key was pressed
char getKey () {
        char key = 0;

        // tempSet tristate of b
        char tempTris;
        tempTris = get_tris_b();
        set_tris_b(0xFF);

        if (input(PIN_B4) == 0) {
                key = 4;
        } else if (input(PIN_B5) == 0) {
                key = 5;
        } else if (input(PIN_B6) == 0) {
                key = 6;
        } else if (input(PIN_B7) == 0) {
                key = 7;
        }

        // fix the tristate of b
        set_tris_b(tempTris);

        return key;
}

// Take up some time
void junkDebounce () {
        char i;
        for (i = 15; i=0; i--);
}

void handleKeyCode (char kc) {
        output_toggle(PIN_B0);
        clockTenthSecond();
}

void keyDebounce () {
        switch (keyState) {
                case 0:
                        if (anyKey())
                                keyState = 1;
                        break;

                case 1:
                        keyCode = getKey();
                        if (keyCode == 0)
                                keyState = 1;
                        else
                                keyState = 3;
                        break;

                case 2:
                        junkDebounce();
                        keyState = 3;
                        break;

                case 3:
                        if (!anyKey())
                                keyState = 4;
                        break;

                case 4:
                        if (!anyKey()) {
                                handleKeyCode(keyCode);
                                keyState = 0;
                        }
                        break;
        }
}
                               
void main() {
   // Call the initialization routine
   initial();

   // Clear the ports
   output_a(0b000000);
   output_b(0x00);
   output_c(0x00);

        // Set some initial values
        numTenths = 0;
        numSeconds = 0;
        numTensSeconds = 1;

        for (;;) {
                // Check for a button press
                keyDebounce();
               
                // Output the tenths
                output_c(0b00111000);
                output_a(numTenths);
       
                delay_ms(10);

                // Output the seconds
                output_c(0b01011000);
                output_a(numSeconds);

                delay_ms(10);

                // Output the tens of seconds
                output_c(0b10011000);
                output_a(numTensSeconds);

                if (finishedCountdown) {
                        output_high(PIN_B0);
                        disable_interrupts(GLOBAL);
                        disable_interrupts(INT_TIMER0);
                }
        }
}

#int_TIMER0
void TIMER0_isr () {
        set_timer0(133);
        numInt++;

        if (numInt == 100) {
                // Clock a tenth of a second!
                clockTenthSecond();
                numInt = 0;
        }

        clear_interrupt(INT_TIMER0);
}

On Wed, Aug 6, 2008 at 12:29 PM, M. Adam Davis <stienman@...> wrote:

> I don't see any interaction between the timer0 and port B.
> How fast are you running the micro?
> How much processing time is your 10mS timer taking up?
> How are you timing the debounce routine?
> Does the chip configuration differ between the two examples you've
> provided (timer enabled vs disabled)?
> Are you properly reseting the timer0 interrupt flag at the end of the
> timer0 routine?
>
> You may have to post the code so we can better see how things are interacting.
>
> If I were debuggin this I'd start from the bottom - copy the bit for a
> button in portb to another pin set as output and make sure that it's
> being read correctly (tests that the pin is an input, and that no
> other configuration is consuing the pin).  Then pipe the output of the
> debounce routine to the output pin.  Find out whether it's a debounce
> problem or a pin configuration problem.
>
> If it misses in debounce, start looking into the code to see where the
> signal disappears.  If it happens before debounce double and triple
> check the configuration of portb and everything that has anything to
> do with it.
>
> The bug has got to be in there _somewhere_...
>
> -Adam
>
> On 8/6/08, Erik Reynolds <ejreynolds@...> wrote:
>> I'm polling PORTB in a neat little debouncing routine I was taught in
>> class last semester.
>>
>> On Wed, Aug 6, 2008 at 11:50 AM, Michael Rigby-Jones
>> <Michael.Rigby-Jones@...> wrote:
>> >
>> >
>> >> -----Original Message-----
>> >> From: piclist-bounces@... [mailto:piclist-bounces@...] On
>> > Behalf
>> >> Of Erik Reynolds
>> >> Sent: 06 August 2008 16:37
>> >> To: piclist@...
>> >> Subject: [PIC] Need help with Timer0 / PORTB Input
>> >>
>> >> Hi All,
>> >>
>> >> This is my first time posting here.  I have been designing a countdown
>> >> timer that uses a PIC16F872 running at 4 Mhz.  It allows the user to
>> >> enter in a time in seconds ranging from 00.0 to 99.9, and then press a
>> >> button to start a countdown.  The code uses Timer0 with a pre-scaler
>> >> of 1:8 set to 133 so that it interrupts every 10 ms.  Every ten
>> >> interrupts, it calls a function that 'clocks' a tenth of a second.  In
>> >> the meantime, the chip is continuously outputting the current value
>> >> for time.  My issue is that while the countdown works flawlessly, I
>> >> can't get any button input on PORTB.  However, when I comment out my
>> >> timer0 interrupt routine, and the statement where I enable the timer0
>> >> interrupt initially, the button input works just fine.
>> >>
>> >
>> > Are you trying to use the PORTB "Interrupt on change" feature, or are
>> > you just polling the button input?
>> >
>> > Regards
>> >
>> > Mike
>> >
>> > =======================================================================
>> > This e-mail is intended for the person it is addressed to only. The
>> > information contained in it may be confidential and/or protected by
>> > law. If you are not the intended recipient of this message, you must
>> > not make any use of this information, or copy or show it to any
>> > person. Please contact us immediately to tell us that you have
>> > received this e-mail, and return the original to us. Any use,
>> > forwarding, printing or copying of this message is strictly prohibited.
>> > No part of this message can be considered a request for goods or
>> > services.
>> > =======================================================================
>> >
>> > --
>> > http://www.piclist.com PIC/SX FAQ & list archive
>> > View/change your membership options at
>> > http://mailman.mit.edu/mailman/listinfo/piclist
>> >
>>
>>
>>
>> --
>> -Erik J. Reynolds
>> --
>> http://www.piclist.com PIC/SX FAQ & list archive
>> View/change your membership options at
>> http://mailman.mit.edu/mailman/listinfo/piclist
>>
>
>
> --
> EARTH DAY 2008
> Tuesday April 22
> Save Money * Save Oil * Save Lives * Save the Planet
> http://www.driveslowly.org
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>



--
-Erik J. Reynolds
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
LightInTheBox - Buy quality products at wholesale price!