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.5. AUTONOMOUS MAZE NAVIGATING ROBOT 357
//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
ADC12CTL0 |= ADC12ENC; //Enable conversions
ADC12CTL0 |= ADC12SC; //Start conversion-software trigger
while(ADC12IFG == 0x0000); //Wait for conversion complete flag
}
//***********************************************************************
void determine_robot_action(void)
{
//Implement robot action truth table
if((lt_sensor<threshold)&&(ct_sensor<threshold)&&(rt_sensor<threshold))
robot_action = straight_ahead;
else if((lt_sensor<threshold)&&(ct_sensor<threshold)
&&(rt_sensor>=threshold))
robot_action = straight_ahead;
else if((lt_sensor<threshold)&&(ct_sensor>=threshold)
&&(rt_sensor<threshold))
robot_action = turn_right;
else if((lt_sensor<threshold)&&(ct_sensor>=threshold)
&&(rt_sensor>=threshold))
robot_action = turn_left;
358 10. SYSTEM LEVEL DESIGN
else if((lt_sensor>=threshold)&&(ct_sensor<threshold)
&&(rt_sensor<threshold ))
robot_action = straight_ahead;
else if((lt_sensor>=threshold)&&(ct_sensor<threshold)
&&(rt_sensor>=threshold))
robot_action = straight_ahead;
else if ((lt_sensor>=threshold)&&(ct_sensor>=threshold)
&&(rt_sensor<threshold))
robot_action = turn_right;
else if((lt_sensor>=threshold)&&(ct_sensor>=threshold)
&&(rt_sensor>=threshold ))
robot_action = reverse;
else
robot_action = straight_ahead;
}
//***********************************************************************
void perform_robot_action(void)
{
TBCCR0 = 512-1; //PWM period
TBCCTL0 = OUTMOD_7; //CCR0 reset/set
TBCCTL1 = OUTMOD_7; //CCR1 reset/set
if(robot_action==turn_left)
{
TBCCR0 = 0; //CCR0 PWM duty cycle - stop
TBCCR1 = 255; //CCR1 PWM duty cycle - half speed
}
else if(robot_action==turn_right)
{
TBCCR0 = 255; //CCR0 PWM duty cycle - half speed
TBCCR1 = 0; //CCR1 PWM duty cycle - stop
}

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