MPLAB Debug Express Lesson 5: Using Timer0

Discussion in 'Embedded Systems and Microcontrollers' started by NGinuity, Jun 23, 2012.

  1. NGinuity

    Thread Starter New Member

    Jun 23, 2012
    9
    0
    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
    Code ( (Unknown Language)):
    1.  
    2. /** C O N F I G U R A T I O N   B I T S ******************************/
    3.  
    4. #pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                      // CONFIG1H
    5. #pragma config PWRT = OFF, BOREN = OFF, BORV = 30                           // CONFIG2L
    6. #pragma config WDTEN = OFF, WDTPS = 32768                                   // CONFIG2H
    7. #pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
    8. #pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
    9. #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
    10. #pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
    11. #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
    12. #pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
    13. #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
    14. #pragma config EBTRB = OFF                                                  // CONFIG7H
    15.  
    16.  
    17. /** I N C L U D E S **************************************************/
    18. #include "p18f45k20.h"
    19. //#include "delays.h"  // no longer being used.
    20. #include "05 Timer.h"  // header file
    21.  
    22. /** V A R I A B L E S *************************************************/
    23. #pragma udata   // declare statically allocated uinitialized variables
    24. unsigned char LED_Display;  // 8-bit variable
    25.  
    26. /** D E C L A R A T I O N S *******************************************/
    27. #pragma code    // declare executable instructions
    28.  
    29. void main (void)
    30. {
    31.     LEDDirections Direction = LEFT2RIGHT;
    32.     BOOL SwitchPressed = FALSE;
    33.  
    34.     LED_Display = 1;            // initialize
    35.  
    36.     // Init I/O
    37.     TRISD = 0b00000000;         // PORTD bits 7:0 are all outputs (0)
    38.  
    39.     INTCON2bits.RBPU = 0;        // enable PORTB internal pullups
    40.     WPUBbits.WPUB0 = 1;            // enable pull up on RB0
    41.     ANSELH = 0x00;              // AN8-12 are digital inputs (AN12 on RB0)
    42.  
    43.     TRISBbits.TRISB0 = 1;       // PORTB bit 0 (connected to switch) is input (1)
    44.  
    45.     // Init Timer
    46.     INTCONbits.TMR0IF = 0;          // clear roll-over interrupt flag
    47.     T0CON = 0b00001000;             // no prescale - increments every instruction clock
    48.     //T0CON = 0b00000001;             // prescale 1:4 - four times the delay.
    49.     TMR0H = 0;                      // clear timer - always write upper byte first
    50.     TMR0L = 0;
    51.     T0CONbits.TMR0ON = 1;           // start timer
    52.  
    53.     while (1)
    54.     {
    55.  
    56.         if (Direction == LEFT2RIGHT)
    57.         {
    58.             LED_Display <<= 1;          // rotate display by 1 from 0 to 7
    59.             if (LED_Display == 0)
    60.                 LED_Display = 1;        // rotated bit out, so set bit 0
    61.         }
    62.         if (Direction == RIGHT2LEFT)
    63.         {
    64.             LED_Display >>= 1;          // rotate display by 1 from 7 to 0
    65.             if (LED_Display == 0)
    66.                 LED_Display = 0x80;     // rotated bit out, so set bit 7
    67.        }
    68.  
    69.         LATD = LED_Display;         // output LED_Display value to PORTD LEDs
    70.  
    71.         do
    72.         { // poll the switch while waiting for the timer to roll over.
    73.             if (Switch_Pin == 1)
    74.             { // look for switch released.
    75.                 SwitchPressed = FALSE;
    76.             }
    77.             else if (SwitchPressed == FALSE) // && (Switch_Pin == 0) due to if-else
    78.             { // switch was just pressed
    79.                 SwitchPressed = TRUE;
    80.                 // change  direction
    81.                 if (Direction == LEFT2RIGHT)
    82.                     Direction = RIGHT2LEFT;
    83.                 else
    84.                     Direction = LEFT2RIGHT;
    85.             }
    86.  
    87.         } while (INTCONbits.TMR0IF == 0);
    88.  
    89.         // Timer expired
    90.         INTCONbits.TMR0IF = 0;          // Reset Timer flag
    91.  
    92.     }
    93.    
    94. }
    95.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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?
     
  3. NGinuity

    Thread Starter New Member

    Jun 23, 2012
    9
    0
    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.
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Don;t you have to turn TMR0 ON in the 18F45k20??

    I think one bit in the T0CON register turns the timer on.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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.
     
  6. NGinuity

    Thread Starter New Member

    Jun 23, 2012
    9
    0
    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)

    Code ( (Unknown Language)):
    1.     // Init Timer
    2.     INTCONbits.TMR0IF = 0;          // clear roll-over interrupt flag
    3.     T0CON = 0b00001000;             // no prescale - increments every instruction clock
    4.     //T0CON = 0b00000001;             // prescale 1:4 - four times the delay.
    5.     TMR0H = 0;                      // clear timer - always write upper byte first
    6.     TMR0L = 0;
    7.     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.
     
  7. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  8. NGinuity

    Thread Starter New Member

    Jun 23, 2012
    9
    0
    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.
     
  9. NGinuity

    Thread Starter New Member

    Jun 23, 2012
    9
    0
    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?
     
  10. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    what's the setting of the /DEBUG configuration bit? Is this off?
     
  11. NGinuity

    Thread Starter New Member

    Jun 23, 2012
    9
    0
    I don't even think it's set. Here's what my configuration block looks like:

    Code ( (Unknown Language)):
    1. #pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                      // CONFIG1H
    2. #pragma config PWRT = OFF, BOREN = OFF, BORV = 30                           // CONFIG2L
    3. #pragma config WDTEN = OFF, WDTPS = 32768                                   // CONFIG2H
    4. #pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
    5. #pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
    6. #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
    7. #pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
    8. #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
    9. #pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
    10. #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
    11. #pragma config EBTRB = OFF                                                  // CONFIG7H
    Is there somewhere else I should look for it? (Sorry, I'm neeeeew :D)
     
  12. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  13. NGinuity

    Thread Starter New Member

    Jun 23, 2012
    9
    0
    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:

     
  14. lampi

    New Member

    Jul 29, 2012
    1
    0
    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.
     
Loading...