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.

CCS/MSP430G2553: Control motor vibration with PWM signal in software

Part Number: MSP430G2553

Tool/software: Code Composer Studio

I'm trying to control a vibration motor using PWM without using the interrupts in the msp430 launchpad. I'm creating a program that takes in the reading from a LIDAR sensor. The motor has to vibrate according to this reading. If the reading is lower than 10 m but greater than 0 then the motor should turn on and the vibration should be strong if the reading is greater the vibration should decrease.

This is what I have on the main.c file so far, but I'm not sure if I'm doing it right and how to make the motor_on = 1,  motor_on = 2 be related to the intensity of motor vibration. Also for the readings coming from the LIDAR I was thinking to make an array and store the reading there and just pull them when I need it for the motor vibration. Any ideas on how to do this

#include <msp430.h>
#include "motor.h"

unsigned int g1msTimeout = 0;
unsigned int g1msTimer = 0;

int distance = 0;  //lidar reading
int count = 0;
int motor_count =0;
int s; 

// Function prototypes
void ConfigureClockModule();
//void ConfigureClockModule(void);
void ManageSoftwareTimers();

void main(void){
 // Stop the watchdog timer, and configure the clock module.
    WDTCTL = WDTPW + WDTHOLD;
    ConfigureClockModule();
 
   While(1){
        if (distance < 5){
            motor_count =1;  //motor intensity
        }
        else if(5 < distance <10){
            motor_count = 2;
        }
        else if(10 < distance < 20){
            motor_count = 3;
        }
        if((motor_count = 1) && (count > 100)){
            TURN_OFF_MOTOR;
        }
        if((motor_count = 2) && (count > 1000)){
            TURN_OFF_MOTOR;
        }
        else if(count > 10000){
            count = 0;
        }
        else{
            TURN_ON_MOTOR;
        }

        if(10 < distance < 15){
            if(count < 50){
                TURN_OFF_MOTOR;
            }
            if(5000 < count < 6000){
                TURN_ON_MOTOR;
            }
            else if(count > 6000){
                count = 0;
            }
      }
}

void ConfigureClockModule(void)
{
    // Configure Digitally Controlled Oscillator (DCO) using factory calibrations.
    DCOCTL  = CALDCO_1MHZ;
    BCSCTL1 = CALBC1_1MHZ;

    //BCSCTL2 |= DIVS_3;
}

void ManageSoftwareTimers(void)
{
    if (g1msTimeout)
    {
        g1msTimeout--;
        g1msTimer++;
    }
}

 

#ifndef MOTOR_H
#define MOTOR_H

#include <msp430.h>

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *                 Motor #1
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 */
#define MOTOR1 1
#define MOTOR_BIT				BIT6
#define MOTOR_PORT				P1OUT
#define MOTOR_DDR				P1DIR
#define SET_MOTOR_AS_AN_OUTPUT	MOTOR_DDR |= MOTOR_BIT
#define TURN_ON_MOTOR			MOTOR_PORT |= MOTOR_BIT
#define TURN_OFF_MOTOR			MOTOR_PORT &= ~MOTOR_BIT
#define TOGGLE_MOTOR			MOTOR_PORT ^= MOTOR_BIT

// Prototypes
void InitializeMotorPortPins(void);

#endif

**Attention** This is a public forum