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

346 10. SYSTEM LEVEL DESIGN
//putcommand: to send command a special command character (either 0xFE or
//0x7C) must precede command.
A command may then be sent.
//***********************************************************************
void putcommand(unsigned char command)
{
UART_TX_A0(0xFE);
UART_TX_A0(command);
}
//***********************************************************************
//determine_desired_motor_speed
// 0 VDC: 50% duty cycle, 750 RPM
// 3.3 VDC: 100% duty cycle, 1500 RPM
//***********************************************************************
void determine_desired_motor_speed(void)
{
readADC12(0x04); //Read potentiometer P7.4
desired_speed = ADC12MEM4;
desired_speed=(unsigned int)((((float)(desired_speed)/4096.0)
*750.0)+750.0);
}
//***********************************************************************
//start_motor: ramp up motor from stop to desired speed by incrementing
//duty cycle.
//***********************************************************************
void start_motor(void)
{
unsigned int i;
unsigned int desired_duty_cycle;
desired_duty_cycle = (unsigned int)((float)(desired_speed)*0.341);
TBCCR0 = 512-1; //PWM period
TBCCTL1 = OUTMOD_7; //CCR1 reset/set
10.4. CIRCUIT DIAGRAM 347
TBCCR1 = 0; //CCR1 PWM duty cycle - 0%
TBCTL=TBSSEL_2+MC_1+TBCLR; //SMCLK, upmode, clear TBR
for(i=0; i<=desired_duty_cycle; i++)
{
delay();
TBCCR1 = i;
}
}
//***********************************************************************
void adjust_motor_speed(void)
{
TBCCR0 = 512-1; //PWM period
TBCCTL1 = OUTMOD_7; //CCR1 reset/set
if((desired_motor_speed > actual_motor_speed)&&(TBCCR1<=0xFF))
{
TBCCR1 = TBCCR1 + 1; //increment duty cycle - speed up motor
}
else if((desired_motor_speed < actual_motor_speed)&&(TBCCR1>=0x00))
{
TBCCR1 = TBCCR1 - 1; //increment duty cycle - slow down motor
}
else
{
TBCCR1 = TBCCR1; //maintain current motor speed
}
TBCTL=TBSSEL_2+MC_1+TBCLR; //SMCLK, upmode, clear TBR
}
//***********************************************************************
void display_motor_speed(void)
{
unsigned int thousands, hundreds, tens, ones, dutyCycle;
thousands = desired_motor_speed/1000;
hundreds = (desired_motor_speed - 1000*thousands)/100;
tens = (desired_motor_speed - 1000*thousands - 100*hundreds)/10;
348 10. SYSTEM LEVEL DESIGN
ones = (desired_motor_speed - 1000*thousands - 100*hundreds - 10*tens);
putcommand(0x01); //clear display
putChar((unsigned char)(thousands)+48);
putChar((unsigned char)(hundreds)+48);
putChar((unsigned char)(tens)+48);
putChar((unsigned char)(ones)+48);
putcommand(0x14); //move cursor right
putChar(’R’);
putChar(’P’);
putChar(’M’);
putcommand(0x14); //move cursor right
thousands = actual_motor_speed/1000;
hundreds = (actual_motor_speed - 1000*thousands)/100;
tens = (actual_motor_speed - 1000*thousands - 100*hundreds)/10;
ones = (actual_motor_speed - 1000*thousands - 100*hundreds - 10*tens);
putcommand(0x14); //move cursor right
putChar((unsigned char)(thousands)+48);
putChar((unsigned char)(hundreds)+48);
putChar((unsigned char)(tens)+48);
putChar((unsigned char)(ones)+48);
putcommand(0x14); //move cursor right
putChar(’R’);
putChar(’P’);
putChar(’M’);
putChar(’ ’);
putChar(’ ’);
dutyCycle = TBCCR1*100/255;
hundreds = (dutyCycle)/100;
tens = (dutyCycle - 100*hundreds)/10;
ones = (dutyCycle - 100*hundreds - 10*tens);
if(hundreds > 0)
{
10.4. CIRCUIT DIAGRAM 349
putChar((unsigned char)(hundreds)+48);
}
else
{
putChar(’ ’);
}
putChar((unsigned char)(tens)+48);
putChar((unsigned char)(ones)+48);
putChar(’%’);
}
//***********************************************************************
void determine_motor_speed(void)
{
unsigned int new_cap=0;
unsigned int old_cap=0;
unsigned int cap_diff=0;
//Port P8
P8DIR = P8DIR & 0xDF; //configure port P8 (0:input, 1:output)
//P8.5 as input other pins unchanged
P8SEL = 0x20; //P8 select (0:digital I/O, 1:alternate func)
//P8.5 timer input capture
P8OUT = 0x00; //P8 out - when input selects resistor
// (0:pulldown, 1: pullup)
P8REN = 0x00; //P8REN - resistor enable
// (0: disable resistor, 1: enable resistor)
TA1CCTL0=CM_1+SCS+CCIS_1+CAP+CCIE;
//Rising edge + CCI0B (P8.5)
//+ Capture Mode + Interrupt
TA1CTL = TASSEL_2 + MC_2; //SMCLK + Continuous Mode
_BIS_SR(GIE); //Enable global interrupts
}
#pragma vector=TIMERA1_VECTOR

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