• AVR Freaks

Hot!Timer within timer

Author
Ow19m
New Member
  • Total Posts : 23
  • Reward points : 0
  • Joined: 2020/01/10 06:17:03
  • Location: 0
  • Status: offline
2020/02/18 12:39:45 (permalink)
0

Timer within timer

Hi,
So i wanted to implement a timer within a timer through Interrupt.
 
#include<stdlib.h>
#include<xc.h>
#include<stdio.h>
#include"System.h"
#include<libpic30.h>

void Timer_func()
{  
    T1CONbits.TON=0;
    IFS0bits.T1IF=0;
    PR1= 14394;
   T1CONbits.TON=1;
}


int main(int argc,char**argv)

{
    
 //-----Portcongif------
    ADPCFG=0xFFFF;
    TRISB=0X0000;
   
 //-------------------
    
 //----Timer1 Config-----
    
   T1CONbits.TON=0;
   T1CONbits.TCS=0;
   T1CONbits.TCKPS=0b10;
   PR1= 14394;
   IFS0bits.T1IF=0;
   IEC0bits.T1IE=1;
   T1CONbits.TON=1;     
 //------------------------
   
   
   
    while(1)
   {
      
   }
    return(EXIT_SUCCESS);
}

void __attribute__((interrupt(no_auto_psv))) _T1Interrupt(void)
{
    
      
      //IFS0bits.T1IF=0;
       Timer_func();
       LATDbits.LATD0 = 1;
       
     //  while(!IFS0bits.T1IF){}
       
     //  IFS0bits.T1IF = 0; //Clear timer flag for next period
}
       The problem is that I have a 0.5 second timer set up so theoretically i should be getting 1 second after repeating it again ,but i due to function calling the stop watch gives me 2.27  seconds.Should i expect this ??
 
 

Attached Image(s)

#1

8 Replies Related Threads

    dan1138
    Super Member
    • Total Posts : 3382
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Timer within timer 2020/02/18 12:54:02 (permalink)
    1 (1)
    Ow19m
    The problem is that I have a 0.5 second timer set up so theoretically I should be getting 1 second after repeating it again, but I due to function calling the stop watch gives me 2.27  seconds.
     
    Should i expect this ??

    Yes you should expect this given that you do not seem to have read any of the posts on this forum about how to ask questions.
     
    A good post:
    1. Describes the specific controller you are using
    2. Shows how the configuration words are initialized
    3. Contains a complete example program that builds
    4. Identifies the IDE and versions of the tools you used
    In general it is too much trouble to try you read your code fragments to guess at what you had in mind when you created them.
     
    #2
    NorthGuy
    Super Member
    • Total Posts : 5956
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Timer within timer 2020/02/18 13:02:41 (permalink)
    0
    Your Timer_func() does nothing except for stopping the timer for a very short period and then starting this again. The PR1 register is already set so there's no reason to set it to the same value. You can replace it with:
     
    void Timer_func()
    {  
      IFS0bits.T1IF=0;
    }

     
    There's no code to clear LATD0. There's no mention of the clock frequency. Thus it's impossible to figure out what you're measuring and why your measurements differ from what you expected.
     
     
    #3
    ric
    Super Member
    • Total Posts : 26058
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Timer within timer 2020/02/18 13:45:19 (permalink)
    5 (1)
    This post comes from https://www.microchip.com/forums/m1128675.aspx
    Why did the OP start a new one? Just to annoy everyone?
    They still don't seem to have understood what was being discussed in their first thread.
     

    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!
    #4
    dan1138
    Super Member
    • Total Posts : 3382
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Timer within timer 2020/02/18 15:18:14 (permalink)
    0
    @ric,
     
    What the Original Poster seems to understand is that sometimes they will get working code as a reply to a lame enough post.
     
    For someone with experience with MPLABX and the dsPIC orPIC24 controllers is should take less than 2 hours to create and test in the simulator complete example code that configures a dsPIC30F2010 to execute an event from the TIMER1 interrupt service routine every half a second.
     
    I suspect that the OP would like someone to write and post this code here.
     
    Now I have written this code to find out how much time I would take, but I doubt I'll post it.
     
    I've PM you the project to prove it.
    #5
    ric
    Super Member
    • Total Posts : 26058
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Timer within timer 2020/02/18 15:20:47 (permalink)
    5 (1)
    I also suspect they still don't understand how to set the simulator to correctly use the stopwatch for measuring time intervals.
     

    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
    Ow19m
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: Timer within timer 2020/02/19 06:20:10 (permalink)
    0
    Dan1138,
    First of all I dont understand the ego people have here ,thinking there's Gods of Microcontrollers.Secondly there's nothing that needs "Extra" addition to the code, so there's nothing to write in it except pinpoint a minor mistake,dont have to insult others for no reason if you think you cant answer someone.Im sure ill reach your level one day but i wont degrade someone like this every now and then.  
    #7
    Ow19m
    New Member
    • Total Posts : 23
    • Reward points : 0
    • Joined: 2020/01/10 06:17:03
    • Location: 0
    • Status: offline
    Re: Timer within timer 2020/02/19 06:25:45 (permalink)
    0
    Yes Im absolutely apologize for not giving the details and i knew it would be difficult for the viewer to understand what was happening before for a while ,So i posted  the link where i was talking something related to it.
    #8
    dan1138
    Super Member
    • Total Posts : 3382
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: Timer within timer 2020/02/19 23:13:14 (permalink)
    5 (1)
    Ow19m
    ... there's nothing that needs "Extra" addition to the code, so there's nothing to write in it except pinpoint a minor mistake, ...

    Opinions vary ...
     
    This is the code you posted with my observation:
    #include<stdlib.h>                  //<--- bad style, standard C library not used
    #include<xc.h>
    #include<stdio.h>                   //<--- bad style, standard C I/O not used
    #include"System.h"                  //<--- bad style, include file source not part of posted code. This kind of file often has controller specific definitions.
    #include<libpic30.h>                //<--- bad style, Microchip library not used

    void Timer_func()
    {
        T1CONbits.TON=0;                //<--- error, it is wrong to turn off a timer used to count time
        IFS0bits.T1IF=0;                //<--- mistake, statement should be in ISR not a function called from the ISR
        PR1= 14394;                     //<--- bad style, no need to write same value to period register
        T1CONbits.TON=1;                //<--- mistake, not needed when timer is not turned off
    }

    int main(int argc,char**argv)       //<--- mistake, XC16 compiler main function does not support invocation argument list for embedded code
    {
     //-----Port Congif------
        ADPCFG=0xFFFF;                  //<--- bad style, magic number needs a comment
        TRISB=0X0000;                   //<--- bad style, magic number needs a comment
     //-------------------

     //----Timer1 Config-----
        T1CONbits.TON=0;                //<--- bad style, magic number needs a comment
        T1CONbits.TCS=0;                //<--- bad style, magic number needs a comment
        T1CONbits.TCKPS=0b10;           //<--- bad style, magic number needs a comment
        PR1= 14394;                     //<--- bad style, magic number needs a comment
        IFS0bits.T1IF=0;                //<--- bad style, magic number needs a comment
        IEC0bits.T1IE=1;                //<--- bad style, magic number needs a comment
        T1CONbits.TON=1;                //<--- bad style, magic number needs a comment
     //------------------------

        while(1)
        {

        }
        return(EXIT_SUCCESS);           //<--- bad style, embedded code does not exit main, needs a comment
    }

    void __attribute__((interrupt(no_auto_psv))) _T1Interrupt(void)
    {
    //  IFS0bits.T1IF=0;                //<--- bad style, this statement should be here and not commented out
        Timer_func();
        LATDbits.LATD0 = 1;             //<--- error, PORTD never set for digital output

    //  while(!IFS0bits.T1IF){}         //<--- bad style, never loop on flags in an ISR

    //  IFS0bits.T1IF = 0; //Clear timer flag for next period    //<--- bad style, obvious confusion on when to clear the timer interrupt request flag
    }

    Other posts in this thread have made similar comments on your posted code.

    The process of creating a clear, small, complete file that implements the logic that is troubling you is one of the best ways to find solutions for your problems. This is regardless of anyone else seeing your code. This is really an exercise to get your mind focused on only one aspect of a problem. This will let you know if this is the problem you are looking for.

    Learning to do this is a very important tool for embedded code development.

    I'm not going to preach any particular style for coding in the C language.

    You can get an idea of my preferences by looking at my other posts in this forum.

    There's a saying: "With any three coders you can have four arguments on style."

    What I will say about coding style is to pick one that works for you and helps you write good code and be willing to change when you discover somthing that works better.
    post edited by dan1138 - 2020/02/20 19:12:23
    #9
    Jump to:
    © 2020 APG vNext Commercial Version 4.5