• AVR Freaks

AnsweredHot!PIC16F877A Timer simulation using MPLAB X IDE

Page: 12 > Showing page 1 of 2
Author
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
2020/09/09 07:43:13 (permalink)
0

PIC16F877A Timer simulation using MPLAB X IDE

Hi 
I am having trouble to simulate code using MPLAB xC8 
I want to toggle PIN RC7 at every overflows
 

/*
 * File:   timer.c
 * Author: Embedded System
 *
 * Created on September 9, 2020, 6:35 AM
 */

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include <xc.h>


void  initialize (void)
{
   //Make all PORTD pins low
   PORTA = 0;
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;
   PORTE = 0;
 
// Configured PORT pins as Output
   TRISA = 0b00000000;
   TRISB = 0b00000000;
   TRISC = 0b00000000;  //RC7 pin as out
   TRISD = 0b00000000;
   TRISE = 0b00000000;
}

void main(void)
{
   
    initialize ();  //initialize port pins 
  
    OPTION_REG = 0b00000101; // Timer0 with internal instruction clock  and 64 as prescalar
   
    TMR0 = 0;    // Load the time value
   
    TMR0IF = 0;                 // clear timer flag
    TMR0IE = 1;                 //Enable timer  bit
   
    while(1)
    {
         if(TMR0IF==1)
         {
            RC7 =~ RC7 ;
            TMR0 += 0;     //Load the timer Value
            TMR0IF=0;       // Clear timer interrupt flag          
         }
   
    }
  
}
 

 
 
How to get exact time taken by timer 0 ? Where to set break points ?

Attached Image(s)

#1
Mysil
Super Member
  • Total Posts : 3809
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 12:07:05 (permalink)
+1 (1)
Hi,
Have run your program in MPLAB X simulator,
and it work as it is written, for me.
 
In the simulator there is instruction cycle counting available,
You may set a breakpoint on any of the 3 statements inside the block testing Timer0 Interrupt Flag,
Then in MPLAB X menu, click 'Window', select 'Debugging', and then 'Stopwatch'.
 
For me this give 17221 simulation cycles. This is a little more than theory, which should be 16384 instruction cycles.
 
    Mysil
 
Edit: Note that if you are going to run this on real hardware PIC16,
then Watchdog timer should be disabled, as long as there is no software present to service the watchdog timer.
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT disabled).
 
    Mysil
post edited by Mysil - 2020/09/09 12:14:56
#2
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 12:45:20 (permalink)
0
Mysil
Hi,
Have run your program in MPLAB X simulator,
and it work as it is written, for me.

I am not working on hardware. I am just trying to learn debugging  
I click on debug -->  debug main project 
 
How to debug code ?

Attached Image(s)

#3
Mysil
Super Member
  • Total Posts : 3809
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 13:09:00 (permalink)
0
Hi,
Click in the left margin to insert or remove a breakpoint.
Then click on 'Continue' icon in toolbar: Round Green image with white triangle pointing to right.
This will make program run until breakpoint is encountered.
 
Note: I am running in MPLAB X v5.25
Image in message #3 indicate that program is running MPLAB X v5.40, with breakpoint on line 59 having no effect.
 
    Mysil
#4
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 19:36:15 (permalink)
0
Mysil
Hi,
Click in the left margin to insert or remove a breakpoint.
Then click on 'Continue' icon in toolbar: Round Green image with white triangle pointing to right.
This will make program run until breakpoint is encountered.
 
Note: I am running in MPLAB X v5.25
Image in message #3 indicate that program is running MPLAB X v5.40, with breakpoint on line 59 having no effect.
 
    Mysil


When I click on continuous button below message shows
 
Break point at line 59 in file E:/PIC16f877A/Timer.X/timer.c cannot be resolved to a valid program memory address
No source code lines were found at current PC 0x7f2. Open program memory view to see instruction code disassembly.
 
 

Attached Image(s)

#5
ric
Super Member
  • Total Posts : 28677
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 19:40:36 (permalink)
0
The compiler has sensibly observed that TMR0 += 0; does nothing, so doesn't generate any assembly output for that instruction, so it is impossible to set a breakpoint on an instruction that does not exist.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 12019
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 20:12:08 (permalink)
+1 (1)
ric
The compiler has sensibly observed that TMR0 += 0; does nothing, so doesn't generate any assembly output for that instruction

 
That shouldn't be the case with a volatile register.
#7
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 21:12:11 (permalink)
0
Please take a look at attached screenshot
stop watch gives 17.221 ms. does it means timer overflow at every 17.221ms ?
How to check status of port pins ?

Attached Image(s)

#8
ric
Super Member
  • Total Posts : 28677
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 21:29:04 (permalink)
+1 (1)
Only if your oscillator is 4MHz.
Do note that the simulator does NOT observe the _XTAL_FREQ definition.
You control the simulated speed in the project properties under the simulator settings.
It is saying 17221 instruction cycles. That is 17.221ms if the instruction rate is set to the default 1MHz.
 
I suspect from what you have shown us that it is 17221 instruction cycles from when the PIC started until the FIRST hit on the breakpoint.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#9
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 22:03:29 (permalink)
0
ric
I suspect from what you have shown us that it is 17221 instruction cycles from when the PIC started until the FIRST hit on the breakpoint.

Could you please tell me further steps How to know timer time using simulator ?
#10
1and0
Access is Denied
  • Total Posts : 11340
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 22:13:03 (permalink)
+2 (2)
Your code breaks at initialize() with 17221 cycles. This looks like you did not turn off the Watchdog Timer as someone told you to earlier. So turn the WDT off in your config bit!
 
#11
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/09 23:18:02 (permalink)
0
1and0
Your code breaks at initialize() with 17221 cycles. This looks like you did not turn off the Watchdog Timer as someone told you to earlier. So turn the WDT off in your config bit!

No, I have disabled Watchdog Timer in code 
#12
Mysil
Super Member
  • Total Posts : 3809
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/10 01:53:46 (permalink)
0
No, I have disabled Watchdog Timer in code

Wrong, the Watchdog timer cannot be disabled in code, in this device type.
Clearing the PSA bit in Option_Reg, doesn't disable the watchdog counter,
it will select the shortest watchdog timeout interval.
 
    Mysil
#13
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/10 02:46:33 (permalink)
0
I don't have idea How to disabled watchdog timer
#14
ric
Super Member
  • Total Posts : 28677
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/10 03:00:13 (permalink)
+1 (1)
Even after you were told exactly what to do in post #2 ?
If that is what you have done, show us the new code, as your description of what you have done is too vague.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#15
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/21 04:01:05 (permalink)
0
ric
Even after you were told exactly what to do in post #2 ?
If that is what you have done, show us the new code, as your description of what you have done is too vague.
 

I have disabled watchdog timer in the code. My confusions are  whether I  have to disable watchdog timer anywhere else   

 
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
 
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
 

#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include <xc.h>
 

void  initialize (void)
{
   //Make all PORTD pins low
   PORTA = 0;
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;
   PORTE = 0;
 
// Configured PORT pins as Output
   TRISA = 0b00000000;
   TRISB = 0b00000000;
   TRISC = 0b00000000;  //RC7 pin as out
   TRISD = 0b00000000;
   TRISE = 0b00000000;
}
 
void main(void)
{
   
    initialize ();  //initialize port pins 
  
    OPTION_REG = 0b00000101; // Timer0 with internal instruction clock  and 64 as prescalar
   
    TMR0 = 0;    // Load the time value
   
    TMR0IF = 0;                 // clear timer flag
    TMR0IE = 1;                 //Enable timer  bit
   
    while(1)
    {
         if(TMR0IF==1)
         {
            RC7 =~ RC7 ;
            TMR0 += 0;     //Load the timer Value
            TMR0IF=0;       // Clear timer interrupt flag          
         }
   
    }
  
}
 

post edited by Djsarkar - 2020/09/21 04:02:14
#16
ric
Super Member
  • Total Posts : 28677
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/21 04:34:36 (permalink)
+2 (2)
Djsarkar
I have disabled watchdog timer in the code. My confusions are  whether I  have to disable watchdog timer anywhere else   

No, you have NOT disabled the WDT in your code.
As Mysil pointed out, all writing to the OPTION_REG allows you to do is control if the prescaler is assigned to the WDT. You can't "disable" the WDT that way.
The only way is how you were told to do it in in post#2.
i.e.
change this line
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)

to
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT disabled)

 
That is the ONLY way you can disable it in your PIC.
 
post edited by ric - 2020/09/21 04:36:01

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#17
Djsarkar
Senior Member
  • Total Posts : 156
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: online
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/21 07:34:17 (permalink)
0
 


// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF        // Watchdog Timer disable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include <xc.h>

void  initialize (void)
{
   //Make all PORTD pins low
   PORTA = 0;
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;
   PORTE = 0;
 
// Configured PORT pins as Output
   TRISA = 0b00000000;
   TRISB = 0b00000000;
   TRISC = 0b00000000;  //RC7 pin as out
   TRISD = 0b00000000;
   TRISE = 0b00000000;
}

void main(void)
{
   
    initialize ();  //initialize port pins 
  
    OPTION_REG = 0b00000101; // Timer0 with internal instruction clock  and 64 as prescalar
   
    TMR0 = 0;    // Load the time value
   
    TMR0IF = 0;                 // clear timer flag
    TMR0IE = 1;                 //Enable timer  bit
   
    while(1)
    {
         if(TMR0IF==1)
         {
            RC7 =~ RC7 ;
            TMR0 += 0;     //Load thae timer Value
            TMR0IF=0;       // Clear timer interrupt flag          
         }
   
    }
  
}
 


 
Whats the 16.382ms ? How much time timer take to enable timer flag ?

Attached Image(s)

#18
dan1138
Super Member
  • Total Posts : 3918
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/21 13:18:27 (permalink) ☼ Best Answerby Djsarkar 2020/09/24 03:20:02
0
@Djsarkar,
 
This code does what you appear to want:
/*
 * File:   main.c
 * Author: dan1138
 *
 * Created on September 21, 2020, 12:11 PM
 *
 * Notes:
 *
 * This code depends in the free version of
 * the XC8 compiler being set to use optimization
 * level 2 in the MPLABX project properties.
 *
 * This code is to run in the MPLABX simulator
 * with the system oscillator at 20MHz. You will
 * need to set the Instruction Frequency(Fcyc) to
 * 5 MHz in the simulator project properties for
 * the stopwatch tool to report the correct time.
 */

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ

void  initialize (void)
{
   //Make all PORTD pins low
   PORTA = 0;
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;
   PORTE = 0;
 
// Configured PORT pins as Output
   TRISA = 0b00000000;
   TRISB = 0b00000000;
   TRISC = 0b00000000;  //RC7 pin as out
   TRISD = 0b00000000;
   TRISE = 0b00000000;
   
   CMCON = 0x07;        /* disable comparators so GPIO pins will work as digital I/O */
   ADCON1 = 0x06;       /* disable ADC so GPIO pins will work as digital I/O */
}
 
void main(void)
{

    initialize ();  //initialize port pins  
   
    OPTION_REG = 0b00000101; // Timer0 with internal instruction clock  and 64 as prescalar
    
    TMR0 = 0;    // Load the time value
    
    TMR0IF = 0;                 // clear timer flag
//    TMR0IE = 1;                 //Enable timer  bit <== Bad code, do not do this without an interrupt handler function
    TMR0IE = 0;
    
    while(1)
    {
         if(TMR0IF)
         {
            RC7    = ~RC7;
            //TMR0  += 0;     // Load the timer Value <== this statement does not do what you expect
            TMR0   = TMR0 + 0; // Load the timer Value <== this statement does do what you expect
            TMR0IF = 0;     // Clear timer interrupt flag           
         }
    }
}

Read the comments I added in the code for clues to what I think you have wrong.
#19
1and0
Access is Denied
  • Total Posts : 11340
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: PIC16F877A Timer simulation using MPLAB X IDE 2020/09/21 13:54:09 (permalink)
0
Djsarkar
 

    OPTION_REG = 0b00000101; // Timer0 with internal instruction clock  and 64 as prescalar

 
Whats the 16.382ms ? How much time timer take to enable timer flag ?

Your Timer0 should have an interval of 16,384 instruction cycles. At Fosc = 4 MHz, that would be 16.384 ms. I may be wrong, but that two cycles difference could be a simulator bug. :(
 
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5