使用msp430微控制器生成1 pps

时间:2016-12-15 14:00:13

标签: microcontroller msp430 code-composer

我对这个微控制器领域很陌生,我需要你快速帮助我的新项目。

我正在使用MSP430F5131。 我将解释一下这个项目: 我的主要目标是使用12.8MHz外部振荡器每秒产生1个脉冲。 我已经有一个用于该项目的印刷PCB,这意味着我必须了解之前的设计师所做的工作并将其实施到我的CCS程序中,以便对我的微控制器进行编程。 我仍然是整个微控制器世界的新手,并且渴望了解更多,尽管时间很短。

我已经附上了PCB的电线,以帮助您更多地了解该项目。

对电线的一点解释: 外部拨动开关(来自连接器J6和#34;内部/外部选择")在外部1pps(来自外部GPS)或内部1pps(通过msp430)之间切换。 如果我们选择内部模式然后(输入P2.2)a' 1' (或' 0'如果它是一个低电平有效模式,它是低电平有效模式)从PJ.3(输出)发送到U4,每秒1个脉冲从P2.4输出到连接器J8和J9(相同脉冲的不同连接器)。

我可以使用外部拨动开关(J3连接器的引脚1,2)或分立器(J3连接器的引脚3)将信号切换到ON / BAD / OFF。 如果我将开关切换到开启模式,它会通过P1.6到U5(和门)发送' 1(或' 0'如果它是低电平模式,即低电平有效模式) )并启用脉冲它也发送' 1'通过PJ.0到U6并将脉冲从TTL转换为RS422(差分)。 离散切换选项切换脉冲(ON / OFF)与外部切换开关相同。 这是指PULSE 1,我需要为PULSE 2实现相同的功能。

虽然我仍然不确定RST / NMI / SBWTDIO和TEST / SBWTCK的连接是什么,这些用于将程序下载到msp中吗? 我也不确定PJ.2引脚(FUNCTIONALITY DISABLE)。

我知道很多东西要读,但我对这个项目的时间非常短暂,我没有太多时间研究一切。 所以如果你能阅读代码并帮助我在代码中实现这些功能,我将非常高兴。

这里是我想知道的代码,如果我这样做了:

#include <msp430.h>
#include <intrinsics.h>




volatile unsigned int timerCount = 0; //defines the millisecond counter
volatile unsigned int normalPulse1=0; //defines another flag to indicates when 1 second has passed for normal pulse1
volatile unsigned int badPulse1=0;//defines another flag to indicates when 1 second has passed for bad pulse1
volatile unsigned int normalPulse2=0; //defines another flag to indicates when 1 second has passed for normal pulse2
volatile unsigned int badPulse2=0;//defines another flag to indicates when 1 second has passed for bad pulse2

int main(void) {


    WDTCTL = WDTPW | WDTHOLD;                   // Stop watchdog timer
    P2OUT &= ~(BIT4);                           //preload 1pps to '0'


    // set I/O pins directions
    P1DIR |=BIT6+BIT7;                      //set p1.x to 11000000
    P2DIR |=BIT4;                          // Set P2.4 to output direction
    PJDIR |=BIT0+BIT1+BIT3;                // set pj.x output 0000 1011
    P2SEL |= BIT4;                         //select the option of using TD0.0 in pin 2.4
    P2IES |= BIT4;                         // high -> low is selected with IES.x = 1.
    P2IFG &= ~(BIT4);                      // To prevent an immediate interrupt, clear the flag for
                                          // P1.3 before enabling the interrupt.
    P2IE |= BIT4;                          // Enable interrupts for P2.4

    // Configure XT1 (external oscillator)
    PJSEL |= BIT4+BIT5;                     // port select for xt1
    UCSCTL6 &= ~(XT1OFF);                   //xt1 is on
    UCSCTL3 = 0;                            // FLL REFERENCE CLOCK REFERENCE = XT1

    // configure TD0.0 to output of 1 pulse per second
    TD0CTL0 |=MC_1+ID_3+TDSSEL_0+TDIE+CNTL_0+TDCLR;                //defining timer d TD0.0 (P2.4)
    TD0CCR0=1600-1;                          // setting TAR count up value 1600 (12.8MHz / 8 = 1.6MHz , 1.6MHz / 1600 = 1000 Hz) when 1000 is passed means 1 second has passed as well
    TD0CCTL0 |= CCIE;                        //ENABLES CCIFG INTERUPT ON CCR0


    __enable_interrupt();


    for(;;){                      // main loop (looping forever)



        //   EXTERNAL / INTERNAL SELECTION BY SW4

        if ((P2IN & BIT2)==0){         //  INTERNAL MODE
            PJOUT |=BIT3;              // sends '1' from pj.3 output to the multiplexer U4 (uses the internal 1pps)

            //PULSE 1 : DESCRETE ON/OFF AND SWITCH ON/BAD/OFF

                     if ((P2IN & BIT0)==0 || (P1IN & BIT0)==0) {        //NORMAL SIGNAL OF 1PPS checks if descrete source is on or 1pps sw pulse 1 is on
                         P1OUT |= BIT6;                                     //ENABLES PULSE BY THE 'AND' GATE
                         PJOUT |= BIT0;                                 //ENABLES TTL TO RS232 CONVERTER (FOR DIFF OUTPUT)
                          if(normalPulse1==1){                       //checks if normalPulse1 is on from the ISR
                            normalPulse1 =0;                           // sets normalPulse1 to 0 again so the ISR will generate the pulse 
                            P2OUT ^=BIT4;                            //generates 1pps out of p2.4
                         }

                     }

                     else {
                         P1OUT |= ~(BIT6);                              //DISABLES PULSE BY SENDING A '0' TO THE AND GATE
                     }

                     if ((P1IN & BIT2)==0)  {                           //PULSE 1 BAD SIGNAL checks if the 1pps sw bad pulse is on
                          P1OUT |= BIT6;                                    //ENABLES PULSE BY THE 'AND' GATE
                          PJOUT |= BIT0;                                    //ENABLES TTL TO RS232 CONVERTER (FOR DIFF OUTPUT)
                            if(badPulse1==1){                             //checks if badPulse1 is on from the ISR
                              badPulse1=0;                                // sets badPulse1 to 0 again so the ISR will generate the pulse
                              P2OUT ^=BIT4;                            //generates 1pps out of p2.4

                            }
                     }

                     //PULSE 2 : DESCRETE ON/OFF AND SWITCH ON/BAD/OFF


                     if ((P2IN & BIT1)==0 || (P1IN & BIT0)==0){         //NORMAL SIGNAL OF 1PPS checks if descrete source is on or 1pps sw pulse 2 is on
                         P1OUT |= BIT7;                                     //ENABLES PULSE BY THE 'AND' GATE
                         PJOUT |= BIT1;                                 //ENABLES TTL TO RS232 CONVERTER (FOR DIFF OUTPUT)
                            if(normalPulse2==1){
                                normalPulse2=0;                             // sets normalPulse2 to 0 again so the ISR will generate the pulse
                                P2OUT ^=BIT4;                            //generates 1pps out of p2.4

                            }


                     }



                     else {
                         P1OUT |= ~(BIT7);                                  //DISABLES PULSE BY SENDING A '0' TO THE AND GATE
                     }

                     if ((P1IN & BIT3)==0){                             //PULSE 2 BAD SIGNAL 
                          P1OUT |= BIT6;                                    //ENABLES PULSE BY THE 'AND' GATE
                          PJOUT |= BIT0;                                    //ENABLES TTL TO RS232 CONVERTER (FOR DIFF OUTPUT)
                            if(badPulse2==1){
                                badPulse2=0;                                // sets badPulse2 to 0 again so the ISR will generate the pulse
                                P2OUT ^=BIT4;                            //generates 1pps out of p2.4
                            }
                     }


                     }

        else {                                                              //EXTERNAL MODE
            PJOUT |= ~(BIT3);            //sends '0' from pj.3 output to the multimplexer U4 (uses the external 1pps)
            P1OUT |= BIT6;                  // ENABLES PULSE 1
            P1OUT |= BIT7;              //ENABLES PULSE 2
            PJOUT |= BIT0;              //ENABLES RS422 DIFF OUTPUT FOR PULSE 1
            PJOUT |= BIT1;              // ENABLES RS422 DIFF OUTPUT FOR PULSE 2
                }
        }
}

    return 0;

                    //ISR FOR TIMERD0.0 - NORMAL PULSE 1 AND 2

                    #pragma vector = TIMER0_D0_VECTOR               //GENERATES 1PPS EVERY 1s for normal pulse
                   __interrupt void TIMER0_D0 (void){
                       if (++timerCount > 500) {                        // checks if the incrementation of timerCount reaches 500 (means 1 second has passed)
                                           timerCount = 0;             // resets the millisecond counter to 0
                                           normalPulse1 = 1;             //once it reaches 1000 (1 second) normalPulse1 will be 1 
                                           normalPulse2=1;              //once it reaches 1000 (1 second) normalPulse2 will be 1 
                                           }
                                           P2IFG &= ~(BIT4);           // clears the flAG
                     }


                    //ISR FOR TIMERD0.0 - BAD PULSE 1 AND 2

                    #pragma vector = TIMER0_D0_VECTOR               //GENERATES 1pulse EVERY 2s (0.5Hz) for bad pulse
                   __interrupt void TIMER0_D0 (void){
                       if (++timerCount > 1000) {                           // checks if the incrementation of timerCount reaches 1000 (means 2 second has passed)
                                            timerCount = 0;             // resets the millisecond counter to 0
                                            badPulse1=1;                    // once it reaches 2000( 2 seconds) the badPulse1 will be 1.
                                            badPulse2=1;                    // once it reaches 2000( 2 seconds) the badPulse2 will be 1.
                                           }
                                           P2IFG &= ~(BIT4);           // clears the flAG

0 个答案:

没有答案