This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How can i generate a 1 pulse per second ?

Other Parts Discussed in Thread: MSP430F5131

Hello ,

I'm using MSP430F5131 micro-controller

I'm using an external oscillator of 12.8MHz which connected to pins PJ.4 and PJ.5 and defined P2.4 ( with timer : TD0.0) as my 1 pulse per second output.

I would like to generate a 1 pulse per second using this external oscillator through P2.4 . In addition to this 1 PPS i got a toggle switch that toggles between a "normal" 1 pulse per second and a "bad" pulse of 0.5 second.

First of all i would like to know how to write a proper code of 1PPS and then i would like to implement both of the pulses ("normal" and "bad") into the code.

I have written a code of a 1PPS , i'm not sure if i have written it correctly :

   TD0CCR0=49999;                          // setting TAR count up value -1

 

    TD0CCTL0=CCIE;                          //enabling interuption

 

    TD0CTL0 =MC_1|ID_3|IDEX_3|TDSSEL_0|TDCLR; //defining timer d TD0.0 (P2.4)

 

    __enable _interrupt ();

 

    for(;;){

  if ((P1IN & BIT2)==0) { //ACTIVE LOW

 

       #pragma vector = TIMERD0_VECTOR //GENERATES 1PPS EVERY 1s

 

          __interrupt void TD0_ISR (void){

 

          P2OUT ^=BIT4;//TOGGLE THE 1PPS PULSE ('1' - '0' )

 

                     }

}

}

Thanks in advance.

  • Assuming your XT1 initialization is correct, you don't source Timer_D with the external 12.8MHz oscillator (using P3.4/TD0CLK instead which I doubt is connected or initialized). IDEX should also apply to TD0CTL1, not TD0CTL0, and you are counting in up mode to TDCL0 which is not set. Use of the capture/compare registers and interrupts is not necessary for your purposes when the TDIE/TDIFG and ISR should suffice. With ID and IDEX divides of 8 and 4, respectively (IDEX_7 for divided of 8), you will still need to divide your timer's source clock (ACLK or SMCLK) by a factor of 8 in order for a count of 50,000 to properly interrupt every second. Please review the Timer_D code examples and debug your code before providing further questions on the forum.

    Regards,
    Ryan
  • Where can i find examples?

  • How do i use a flag without using capture/compare register? it seems that in the examples they use interupts.
    is there another example for the use of flags only?
  • You can choose to poll an IFG instead of relying on interrupts, this is a common practice among several MSP430 applications. The capture/compare register TDxCCRn holds the value of timer ticks that the TDxR counter register must surpass before the CCIFG flag is set inside of TDxCCTLn, I cannot assist you any further without more detail regarding your desired application.

    Regards,
    Ryan
  • seems like an easier way to poll an IFG instead of those interupts , i have encountered this code :

    #include <io430x11x1.h> // Specific device
    // Pins for LEDs
    #define LED1 BIT3
    #define LED2 BIT4
    void main (void)
    {
    WDTCTL = WDTPW|WDTHOLD; // Stop watchdog timer
    P2OUT = ˜LED1; // Preload LED1 on , LED2 off
    P2DIR = LED1|LED2; // Set pins for LED1 ,2 to output
    TACTL = MC_2|ID_3|TASSEL_2|TACLR; // Set up and start Timer A
    // Continuous up mode , divide clock by 8, clock from SMCLK , clear timer
    for (;;) { // Loop forever
    while (TACTL_bit.TAIFG == 0) { // Wait for overflow
    } // doing nothing
    TACTL_bit.TAIFG = 0; // Clear overflow flag
    P2OUT ˆ= LED1|LED2; // Toggle LEDs
    } // Back around infinite loop
    }

    seems like this code is similar to what i need (without using a ISR), although im not familiar to this function :
    TACTL_bit.TAIFG

    and how could i generate a pulse of 1us within a period of 1 second (Tperiod = 1 second)? How could i use that IFG flag exactly at that time without using the ISR function?

    thanks

  • Use something similar but with your selected TDxCCTLn register instead of TACTL and CCIFG in place of TAIFG.

    Regards,
    Ryan

**Attention** This is a public forum