MPLAB Debug Express Lesson 5: Using Timer0

Thread Starter

NGinuity

Joined Jun 23, 2012
9
Hey everybody. I am going through the lessons for Pickit 3's Debug Express and I am a little stuck on Lesson 5, which introduces the use of Timer0. I have Pickit3 connected to the Development Board, and I am using USB to power the target. I took the lesson code directly from the install, built it, no errors, program the chip, no errors. The problem is that the output does not work as expected. It's supposed to shift through the LED's, incrementing or decrementing when Timer0 expires and resets, and shift direction when the input button is pressed. Unfortunately, it doesn't do anything. This is the first issue I have had with it and all other lessons have worked great!

Here's the Lesson 5 C code. Can anyone see a glaring error that's causing it to hiccup? I'm a decent C coder but admittedly, I don't know the hardware that well yet.

Thanks in advance for any help provided.

-Eric
Rich (BB code):
/** C O N F I G U R A T I O N   B I T S ******************************/

#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                      // CONFIG1H
#pragma config PWRT = OFF, BOREN = OFF, BORV = 30                           // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                   // CONFIG2H
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF                                                  // CONFIG7H


/** I N C L U D E S **************************************************/
#include "p18f45k20.h"
//#include "delays.h"  // no longer being used.
#include "05 Timer.h"  // header file

/** V A R I A B L E S *************************************************/
#pragma udata   // declare statically allocated uinitialized variables
unsigned char LED_Display;  // 8-bit variable

/** D E C L A R A T I O N S *******************************************/
#pragma code    // declare executable instructions

void main (void)
{
    LEDDirections Direction = LEFT2RIGHT;
    BOOL SwitchPressed = FALSE;

    LED_Display = 1;            // initialize

    // Init I/O
    TRISD = 0b00000000;         // PORTD bits 7:0 are all outputs (0)

    INTCON2bits.RBPU = 0;        // enable PORTB internal pullups
    WPUBbits.WPUB0 = 1;            // enable pull up on RB0
    ANSELH = 0x00;              // AN8-12 are digital inputs (AN12 on RB0)

    TRISBbits.TRISB0 = 1;       // PORTB bit 0 (connected to switch) is input (1)

    // Init Timer
    INTCONbits.TMR0IF = 0;          // clear roll-over interrupt flag
    T0CON = 0b00001000;             // no prescale - increments every instruction clock
    //T0CON = 0b00000001;             // prescale 1:4 - four times the delay.
    TMR0H = 0;                      // clear timer - always write upper byte first
    TMR0L = 0;
    T0CONbits.TMR0ON = 1;           // start timer

    while (1)
    {

        if (Direction == LEFT2RIGHT)
        {
            LED_Display <<= 1;          // rotate display by 1 from 0 to 7
            if (LED_Display == 0)
                LED_Display = 1;        // rotated bit out, so set bit 0
        }
        if (Direction == RIGHT2LEFT)
        {
            LED_Display >>= 1;          // rotate display by 1 from 7 to 0
            if (LED_Display == 0)
                LED_Display = 0x80;     // rotated bit out, so set bit 7
       }

        LATD = LED_Display;         // output LED_Display value to PORTD LEDs

        do
        { // poll the switch while waiting for the timer to roll over.
            if (Switch_Pin == 1)
            { // look for switch released.
                SwitchPressed = FALSE;
            }
            else if (SwitchPressed == FALSE) // && (Switch_Pin == 0) due to if-else
            { // switch was just pressed
                SwitchPressed = TRUE;
                // change  direction
                if (Direction == LEFT2RIGHT)
                    Direction = RIGHT2LEFT;
                else
                    Direction = LEFT2RIGHT;
            }

        } while (INTCONbits.TMR0IF == 0);

        // Timer expired
        INTCONbits.TMR0IF = 0;          // Reset Timer flag

    }
    
}
 

ErnieM

Joined Apr 24, 2011
8,377
Well you started off the right way with a set of "known good" items (board, programmer, and code) then... oops.

Did it build correctly? Does the program verify? Does the last lesson you ran still work?
 

Thread Starter

NGinuity

Joined Jun 23, 2012
9
Well you started off the right way with a set of "known good" items (board, programmer, and code) then... oops.

Did it build correctly? Does the program verify? Does the last lesson you ran still work?
Hi Ernie,

Yes, it builds and verifies correctly, and every lesson I ran still works. When this one failed, the first thing I did to rule out hardware failure was I went back and reprogrammed it with Lesson 4 (which increments the LED when an input switch is pressed). It worked just fine.
 

ErnieM

Joined Apr 24, 2011
8,377
I don't have anything good to add. I tried building the project (I have the files not the hardware from the Debug Express) and it simulates fine for me. I went as far as to replace their code with your code and it still builds and simulates, at least I could see how the LEDs would turn on in step.

Check the include path and lib paths are correct. I also deleted the linker script inside the project as the standard script applies. I included my project with has your code but the hex file may work better.
 

Attachments

Thread Starter

NGinuity

Joined Jun 23, 2012
9
Don;t you have to turn TMR0 ON in the 18F45k20??

I think one bit in the T0CON register turns the timer on.
Hi THE_RB, I am using this code to set up the timer for the TOCON register... if there's something more to set up TMR0, I don't know about it, but as I understand it, the value I am using for TOCON sets up the timer to increment once a cycle (no prescaler)

Rich (BB code):
    // Init Timer
    INTCONbits.TMR0IF = 0;          // clear roll-over interrupt flag
    T0CON = 0b00001000;             // no prescale - increments every instruction clock
    //T0CON = 0b00000001;             // prescale 1:4 - four times the delay.
    TMR0H = 0;                      // clear timer - always write upper byte first
    TMR0L = 0;
    T0CONbits.TMR0ON = 1;           // start timer
Ernie,

I'm not totally sure what is going on, but I went on to lesson 7, which is basically the same thing as 5, except it adds the potentiometer to control the speed of the increment using ADC. It worked fine, didn't touch the code one bit. Came back to it today, reprogrammed the chip and it isn't working like 5 now.
 

takao21203

Joined Apr 28, 2012
3,702
what I do in cases like that is to attach a LED, then try to blink it, if this also fails, I try just simply to switch it on somewhere.

If I can switch it on, I know at least the PIC is running.

If there are dynamic signals emitted, I attach a piezo speaker from which I get noise, if the signal is present.

I solved almost all problems with the help of LEDs and piezo speaker.
 

Thread Starter

NGinuity

Joined Jun 23, 2012
9
what I do in cases like that is to attach a LED, then try to blink it, if this also fails, I try just simply to switch it on somewhere.

If I can switch it on, I know at least the PIC is running.

If there are dynamic signals emitted, I attach a piezo speaker from which I get noise, if the signal is present.

I solved almost all problems with the help of LEDs and piezo speaker.
Hi takao,

This is the Debug Express development board so I've got LED's mounted by default on all of these outputs. The program just goes screwy for some reason. I can do anything up to introducing anything from TMR0 and it works just fine, same file locations for standard header and linker files and I don't see anything with the configuration bits either. It's really weird.
 

Thread Starter

NGinuity

Joined Jun 23, 2012
9
Ok, I managed to get it to work, but I am still a little baffled as to why I am incurring some behavior. Whenever I invoke anything from TMR0, and program the target board (or have it interact with MPLAB at all, including verify or read), for some reason it doesn't work properly until I do a hard reset on the development board. I suspect it has something to do with the timer being interacted on strangely by the programmer, but can anyone tell me why this is the case?
 

Thread Starter

NGinuity

Joined Jun 23, 2012
9
what's the setting of the /DEBUG configuration bit? Is this off?
I don't even think it's set. Here's what my configuration block looks like:

Rich (BB code):
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                      // CONFIG1H
#pragma config PWRT = OFF, BOREN = OFF, BORV = 30                           // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                   // CONFIG2H
#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF                                                  // CONFIG7H
Is there somewhere else I should look for it? (Sorry, I'm neeeeew :D)
 

takao21203

Joined Apr 28, 2012
3,702
yes there is a menu topic to see the actual configuration bits.

Sorry I can't look for legacy MPLAB, have MPLAB X installed here.

It's ON by default for some PICs I think.
 

Thread Starter

NGinuity

Joined Jun 23, 2012
9
I found it. The menu option for configuration bits has "Configuration Bits set in code" checked, it's not doing any overrides from the looks of it.

I looked at the datasheet for the PIC18F4520 and Debug falls under CONFIG4L in the 7th bit. It looks like it is set to 1 by default and the datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf Page 255) says:

bit 7 DEBUG: Background Debugger Enable bit
1 = Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
 

lampi

Joined Jul 29, 2012
1
The source code for lesson 05 is wrong. It has:

#pragma config MCLRE = ON

It should be:

#pragma config MCLRE = OFF

In order to make the program work as originally written, you have to disconnect the Pickit 3 from the eval board after programming and apply a different power supply to the PIC. That is because, with the MCLRE = ON, the Master Clear input is enabled and the Pickit 3 keeps the PIC in reset. So turn it off, MCLRE = OFF, to run lesson 05.

For lesson 06 you'll need to turn it back on, MCLRE = ON, to enable debugging.
 
Top