Metal tracking car based on MSP430F5529

Publisher:万童洁Latest update time:2018-10-17 Source: eefocusKeywords:MSP430F5529 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include

#include"ldc1314.h"

#include"lcd12864cog.h"

//#include"rtc.h"

#include

unsigned int Just_ch0,Just_ch1,Just_ch2,Just_ch3;

unsigned int Now_ch0,Now_ch1,Now_ch2,Now_ch3;

unsigned int PULSE_NUMBER = 0;

unsigned int beep=0;

int FIVE_DIV_TURNS_NUMBER = 0; //One fifth of a circle

float TURNS_NUMBER = 0;

float CIRCUM = 0.2041; //The circumference of the tire, in m

float ONE_PULSE_DISTANCE = 0.2041/390;

float RT_DISTANCE =0; //Real-time distance

unsigned int half_s=0;

int RTC_SEC=0;

#define BEEP_ON P7OUT|=BIT0;

#define BEEP_OFF P7OUT&=~BIT0;

#define BEEP_DIR P7DIR|=BIT0;

#define Right1  Just_ch0-Now_ch0

#define Right2  Just_ch3-Now_ch3

#define Left1   Just_ch1-Now_ch1

#define Left2   Just_ch2-Now_ch2

#define A_PWM_MOTOR       TA0CCR3   //P1.4

#define B_PWM_MOTOR       TA0CCR4   //P1.5

#define MOTOR_PWM_DUTY TA0CCR0 //Cycle length

#define Normal_speed  3500

#define Turn_S_speed  3500

#define Turn_B_speed  2500

void initalRTC(void)

{

    RTCCTL01 = RTCMODE + RTCBCD + RTCHOLD + RTCTEV_1;

    RTCHOUR = 0x04;

    RTCMIN = 0x30;

    RTCSEC = 0x00;

    RTCDAY = 0x01;

    RTCMON = 0x01;

    RTCYEAR = 0x2011;

    RTCCTL01 &= ~RTCHOLD; //Start the real-time clock

    RTCPS1CTL = RT1IP_5 + RT1PSIE;

    RTCPS0CTL = RT0IP_7 + RT0PSIE;

    RTCCTL0 |= RTCRDYIE + RTCTEVIE;

}

void A_MOTOR(A_DIR,A_SPEED)//P1.4

{

    if(A_DIR==1)//Forward

    {

        P4OUT |= BIT1;   P4OUT &= ~BIT2;

        A_PWM_MOTOR  = A_SPEED;

    }

    if(A_DIR==2)//reverse

    {

        P4OUT |= BIT2;  P4OUT &= ~BIT1;

        A_PWM_MOTOR  = A_SPEED;

    }

    if(A_DIR==3)

    {

        P4OUT |= BIT1+BIT2;

    }

}

void B_MOTOR(B_DIR,B_SPEED)//P1.5

{

    if(B_DIR==1)//Forward

    {

        P4OUT |= BIT3;  P3OUT &= ~BIT3;

        B_PWM_MOTOR  = B_SPEED;

    }

    if(B_DIR==2)//reverse

    {

        P3OUT |= BIT3;  P4OUT &= ~BIT3;

        B_PWM_MOTOR = B_SPEED;

    }

    if(B_DIR==3)

    {

        P4OUT |= BIT3; P3OUT |= BIT3;

    }

}

void CAR_WORK_MODE(Status,A_SPEED,B_SPEED)

{

    if(Status==1)//Go forward

    {    A_MOTOR(1,A_SPEED);  B_MOTOR(1,B_SPEED);   }

    if(Status==2)//Back

    {    A_MOTOR(2,A_SPEED);  B_MOTOR(2,B_SPEED);   }

    if(Status==3)//Turn left

    {    A_MOTOR(1,A_SPEED);  B_MOTOR(3,B_SPEED);   }

    if(Status==4)//Turn right

    {    A_MOTOR(3,A_SPEED);  B_MOTOR(1,B_SPEED);   }

    if(Status==5)//Stop

    {    A_MOTOR(3,A_SPEED);  B_MOTOR(3,B_SPEED);   }

}

void L298N_IN14_INIT()

{

    P4DIR|=BIT1+BIT2+BIT3; P3DIR|=BIT3;

    P4OUT|=BIT1+BIT2+BIT3;     P3OUT|=BIT3;

}

void MOTOR_PWM_SET(unsigned int PWM_DUTY)

{

        TA0CCTL2 = CM_1 + SCS + CAP + CCIE; // rising edge capture, synchronous capture, capture mode, interrupt enable

        P1DIR&=~BIT3;

        P1SEL|=BIT3; //Rising edge input capture pin

        P1DIR |= BIT4+BIT5;

        P1SEL |= BIT4+BIT5; //Motor PWM output pin

        MOTOR_PWM_DUTY = PWM_DUTY;

        TA0CCTL3 = OUTMOD_7;

        TA0CCTL4 = OUTMOD_7;

        TA0CTL = TASSEL_2 + MC_1 + TACLR; //Increment count mode

}

void MOTOR_ENABLE()

{

    P2DIR|=BIT7;

    P2OUT|=BIT7; //Motor drive enable

}

void LCD_PORT_CONFIGRE()

{

    cs_DIR; //Macro definition in LCD header file

    cd_DIR;

    reset_DIR;

    sclk_DIR;

    sda_DIR;

}

void LDC1314_FIRST_DATA()

{

      LDC1314_INIT();

      _delay_cycles(4000000); //Power-on delay to wait for LDC1314 to initialize and stabilize the first reading value

      LDC1314_Read();

      Just_ch0 = SENSOR_CH[0];

      Just_ch1 = SENSOR_CH[1];

      Just_ch2 = SENSOR_CH[2];

      Just_ch3 = SENSOR_CH[3];

}

void TIMER_INIT(unsigned int TIME)//timing time = TIME/32768

{

       P4DIR|=BIT7; //Timer display, LED on and off

       TA1CCTL0 = CCIE;

       TA1CCR0 =  TIME;

       TA1CTL = TASSEL_1 + MC_1 + TACLR;

}

void FIXED_DISAPPER()

{

    display_string_8x16(1,70,"SES:");//time separator

    display_string_8x16(5,70,"DST:");//time separator

}

int main(void)

{

    WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer

    MOTOR_PWM_SET(10000); //10KHz period

    L298N_IN14_INIT();

    TIMER_INIT(1000); //Timer time = TIME/32768

    LDC1314_FIRST_DATA();

    MOTOR_ENABLE();

    BEEP_DIR;

    BEEP_OFF;

    LCD_PORT_CONFIGRE();

    LCD_init();

    delay(50);

    clear_screen(); //Clear screen on power on

    FIXED_DISAPPER();

    initalRTC();

    __bis_SR_register(GIE); //General interrupt is turned on

  while(1)

  {

      if((Now_ch0>180)||(Now_ch1>180)||(Now_ch2>180)||(Now_ch3>180))

          beep=1;

      if((Now_ch0<170)&&(Now_ch1<170)&&(Now_ch2<170)&&(Now_ch3<170))

          beep=2;

            switch(beep)

            {

            case 1: BEEP_ON;break;

            case 2: BEEP_OFF;break;

            default :break;

            }

      if((Right1==0)&&(Left1==0 )&&( Right2==0)&&(Left2==0))

     {

       CAR_WORK_MODE(1,Normal_speed,Normal_speed);

     }

    //Inner channel-small curve turning mode

    if(((Left1==1)||(Right1==1))&&(Left2==0)&&(Right2==0))

    {

      while((Left1==1)&&(Right1==0)&&(Left2==0)&&(Right2==0))

      {

       CAR_WORK_MODE(4,Turn_S_speed ,Turn_S_speed);

      }

      while((Right1==1)&&(Left1==0)&&(Left2==0)&&(Right2==0))

      {

       CAR_WORK_MODE(3,Turn_S_speed ,Turn_S_speed);

      }

    }

    //Outer channel-large curve turning mode

    if((Left2==1)||(Right2==1)&&(Left1==0)&&(Right1==0))

    {

      while((Left2==1)&&(Right2==0)&&(Left1==0)&&(Right1==0))

      {

       CAR_WORK_MODE(4,Turn_B_speed,Turn_B_speed);

        while(Right1==0)

          ;//Wait for the turn to complete

       }

       while((Right2==1)&&(Left2==0)&&(Left1==0)&&(Right1==0))

      {

       CAR_WORK_MODE(3,Turn_B_speed,Turn_B_speed);

        while(Left1==0)

        ;//Wait for the turn to complete

       }

     }

    //When the coils on the same side are detected at the same time:

    if(((Right1==1)&&(Right2==1))||((Left1==1)&&(Left2==1)))

    {

      while((Right1==0)&&(Right2==0))

         CAR_WORK_MODE(3,Turn_B_speed,Turn_B_speed);

      while((Left1==0)&&(Left2==0))

         CAR_WORK_MODE(4,Turn_B_speed,Turn_B_speed);

    }

  }

}

#pragma vector=TIMER1_A0_VECTOR

__interrupt void TIMER1_A0_ISR(void)

{

      P4OUT^=BIT7; //Timing indication

      _delay_cycles(50);

      LDC1314_Read();

      Now_ch0 = SENSOR_CH[0]; //Read

      Now_ch1 = SENSOR_CH[1]; //Right channel

      Now_ch2 = SENSOR_CH[2]; //Right channel

      Now_ch3 = SENSOR_CH[3]; //Read

}

#pragma vector=TIMER0_A1_VECTOR

__interrupt void TIMER0_A1_ISR(void)

{

       PULSE_NUMBER++;

     if(PULSE_NUMBER==195)//1/5 lap Full lap 390

       {

        PULSE_NUMBER = 0; // clear and restart counting

        //FIVE_DIV_TURNS_NUMBER++;

        TURNS_NUMBER++;

        //Real-time display of driving distance:

        RT_DISTANCE = TURNS_NUMBER*CIRCUM;

        display_number_8x16(5,100,RT_DISTANCE,1); //Real-time distance, two effective decimal places

       }

    TA0CCTL2 &=~CCIFG; // Clear interrupt flag

}

#pragma vector=RTC_VECTOR

__interrupt void RTC_ISR(void)

{

    switch (__even_in_range(RTCIV, RTC_RT1PSIFG))

    {

        case RTC_NONE:

            break;

        case RTC_RTCRDYIFG:

         //   __bic_SR_register_on_exit(LPM3_bits);

            break;

        case RTC_RTCTEVIFG:

            break;

        case RTC_RTCAIFG:

            break;

        case RTC_RT0PSIFG:

         //   P4OUT ^= BIT7;

            break;

        case RTC_RT1PSIFG:

        // P1OUT ^= BIT0; //Generate a 0.5s signal and generate a 1s signal by doubling

             half_s++;

            while(half_s==2)

            {

                half_s=0;

                RTC_SEC++;

               // while(RTC_SEC>delay_time) //RTC module is placed before reading the value, the reading delay time needs to be subtracted

                display_number_8x16(1,100,RTC_SEC,-1);

                display_number_8x16(1,10,Now_ch0,-1);

            }

            break;

        default:

            break;

    }

}


Keywords:MSP430F5529 Reference address:Metal tracking car based on MSP430F5529

Previous article:MSP430F5529 LCD SPI communication protocol
Next article:Detailed explanation and selection of pull-up resistors and pull-down resistors for microcontrollers

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号