O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.4. CIRCUIT DIAGRAM 341
motor speed
control
optical
SSR
MOSFET
optical
tachometer
motor
interface
data
for disp
LCD
LCD
initialize
LCD
display
PWM
initialize
adj motor
speed
actual
speed
determine
speed adj
desired
speed
PWM
interrupt
Figure 10.12: Structure chart for the motor speed control project.
10.4.3 UML ACTIVITY DIAGRAMS
The UML activity diagrams for the motor speed control project are shown in Figure 10.13.
10.4.4 MICROCONTROLLER CODE
The code for the motor speed control project follows.
//**********************************************************************
//Description: This program provides a PWM signal via Timer B CCR1 to
//stabilize the speed of a DC motor.
The motor speed is set by a
//potentiometer connected to P7.4. The microcontroller receives feedback
//from an optical tachometer that triggers the input capture interrupt on
//Timer A1 CCR0. The desired speed is maintained when the motor is
//loaded.
//***********************************************************************
//include file***********************************************************
#include "msp430x54x.h"
//function prototypes****************************************************
void hold_watchdog(void);
void initialize_ports(void);
void readADC12(unsigned char);
void putChar(unsigned char);
342 10. SYSTEM LEVEL DESIGN
include files
global variables
function prototypes
initialize ports
initialize ADC12
initialize LCD
initialize PWM
initialize interrupts
while(1)
determine desired
RPM
interrupt ISR
get elapsed time
since last interrupt
reset timer
calculate
actual RPM
adjust speed as necessary
display data
Figure 10.13: UML activity diagrams for the motor speed control project.
void putcommand(unsigned char);
void InitUART_A0(void);
void UART_TX_A0(unsigned char data);
void delay_5ms(void);
void determine_desired_motor_speed(void);
10.4. CIRCUIT DIAGRAM 343
void start_motor(void);
void determine_motor_speed(void);
void display_motor_speed(void);
void adjust_motor_speed(void);
//Global Variables******************************************************
unsigned int desired_speed, actual_motor_speed;
unsigned int old_cap, new_cap, cap_diff;
float SMCLK_rate; //need to determine value for actual
//MSP430 configuration
void main(void)
{
hold_watchdog();
initialize_ports();
InitUART_A0();
determine_desired_motor_speed(); //determine motor speed setting
start_motor(); //ramp up to desired motor speed
while(1)
{
determine_desired_motor_speed(); //determine motor speed setting
determine_motor_speed(); //determine current motor speed
display_motor_speed(); //display current motor speed
adjust_motor_speed(); //adjust RPM for desired speed
}
}
//***********************************************************************
void hold_watchdog(void)
{
WDTCTL = WDTPW + WDTHOLD; //hold Watchdog Timer
}
//***********************************************************************
void initialize_ports()
{
344 10. SYSTEM LEVEL DESIGN
//Port P4
P4DIR = 0xFF; //configure port P4 (0:input, 1:output)
P4SEL = 0x02; //P4 select (0:digital I/O, 1:alternate func)
P4OUT = 0x00; //P4 out - when input selects resistor
// (0:pulldown, 1: pullup)
P4REN = 0x00; //P4REN - resistor enable
// (0: disable resistor, 1: enable resistor)
//Port P7 - ADC
P7DIR = P7DIR & 0xEF; //configure port P7 (0:input, 1:output)
//P7.4 analog inputs, other pins unchanged
P7SEL = 0x10; //P4 select (0:digital I/O, 1:alternate func)
//P7.4 analog input
P7OUT = 0x00; //P4 out - when input selects resistor
// (0:pulldown, 1: pullup)
P7REN = 0x00; //P4REN - resistor enable
// (0: disable resistor, 1: enable resistor)
}
//***********************************************************************
//readADC12(unsigned char channel) This function provides a single
//ADC conversion on the specified channel.
//
//An external reference is used. The MSP430F5438 Experimenter Board is
//configured with AVcc, pin 11 connected to 3.3 VDC and the AVss, pin 12
//connected to ground. We set VR+ and VR- to these two values using the
//ADC12SREFx bits in the ADC12MCTLx register.
//
//A single conversion is performed on the specified channel passed into
//the function. The result is placed in the corresponding ADC12MEMx
//register.
//***********************************************************************
unsigned int readADC(unsigned char channel)
{
ADC12CTL0 = ADC12ON+ADC12SHT0_2; //Turn on ADC12, set sampling time
ADC12CTL1 = ADC12SHP; //Use sampling timer
ADC12MCTL0 = ADC12SREF_0+channel;//Vr+ = AVcc and Vr-=AVss and
//Input channel select
10.4. CIRCUIT DIAGRAM 345
ADC12CTL0 |= ADC12ENC; //Enable conversions
ADC12CTL0 |= ADC12SC; //Start conversion-software trigger
while(ADC12IFG == 0x0000); //Wait for conversion complete flag
}
//***********************************************************************
void InitUART_A0(void)
{
P3SEL = 0x10; //P3.4 USCI_A0 TXD
P3DIR = 0x10; //P3.4 output
UCA0CTL1 |= UCSWRST; //Put USCI state machine in reset during
//configuration
UCA0CTL1 |= UCSSEL_1; //Clock source select: CLK = ACLK
UCA0BR0 = 0x03; //Set Baud rate: 32kHz/9600=3.41
UCA0BR1 = 0x00;
UCA0CTL0 = 0x00; //Configure UART:
// -parity disabled, LSB TX first
// -8 data bits, 1 stop bit
// -UART mode, async
UCA0CTL1 &= ˜UCSWRST; //Initialize USCI state machine
}
//***********************************************************************
void UART_TX_A0(unsigned char data)
{
while (!(UCA0IFG & UCTXIFG)); //USCI_A0 TX buffer ready?
UCA0TXBUF = data; // TX data
}
//***********************************************************************
void putChar(unsigned char character)
{
UART_TX_A0(character);
}
//***********************************************************************

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required