// PointerRobot.c // Runs on LM3S811 // Use a pointer implementation of a Mealy finite state machine to control // a robot. // Daniel Valvano // June 15, 2011 /* This example accompanies the book "Embedded Systems: Real Time Interfacing to the Arm Cortex M3", ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2011 Example 3.2, Program 3.4 Copyright 2011 by Jonathan W. Valvano, valvano@mail.utexas.edu You may use, edit, run or distribute this file as long as the above copyright notice remains THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. For more information about my classes, my research, and my books, see http://users.ece.utexas.edu/~valvano/ */ // robot mood sensors connected to PB1-0 (00=fine, 01=tired, 10=curious, 11=anxious) // pulse PD3 to sit down while standing // pulse PD2 to stand up while sitting // pulse PD1 to lie down while sitting // pulse PD0 to sit up while sleeping #define GPIO_PORTB_DATA_R (*((volatile unsigned long *)0x400053FC)) #define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400)) #define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C)) #define GPIO_PORTD_DATA_R (*((volatile unsigned long *)0x400073FC)) #define GPIO_PORTD_DIR_R (*((volatile unsigned long *)0x40007400)) #define GPIO_PORTD_DEN_R (*((volatile unsigned long *)0x4000751C)) #define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108)) #define SYSCTL_RCGC2_GPIOD 0x00000008 // port D Clock Gating Control #define SYSCTL_RCGC2_GPIOB 0x00000002 // port B Clock Gating Control struct State{ unsigned long Out[4]; // 4-bit output const struct State *Next[4]; // next }; typedef const struct State StateType; #define Stand &FSM[0] #define Sit &FSM[1] #define Sleep &FSM[2] #define None 0x00 #define SitDown 0x08 // pulse on PD3 #define StandUp 0x04 // pulse on PD2 #define LieDown 0x02 // pulse on PD1 #define SitUp 0x01 // pulse on PD0 StateType FSM[3]={ {{None,SitDown,None,None}, //Standing {Stand,Sit,Stand,Stand}}, {{None,LieDown,None,StandUp},//Sitting {Sit,Sleep,Sit,Stand }}, {{None,None,SitUp,SitUp}, //Sleeping {Sleep,Sleep,Sit,Sit}} }; int main(void){ StateType *Pt; // current state unsigned long Input; // activate port D and port B SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOD+SYSCTL_RCGC2_GPIOB; Pt = Stand; // initial state GPIO_PORTB_DIR_R &= ~0x03;// make PB1-0 input from mood sensor GPIO_PORTB_DEN_R |= 0x03; // enable digital I/O on PB1-0 GPIO_PORTD_DIR_R |= 0x0F; // make PD3-0 output to robot control GPIO_PORTD_DEN_R |= 0x0F; // enable digital I/O on PD3-0 while(1){ Input = GPIO_PORTB_DATA_R&0x03; // input=0-3 GPIO_PORTD_DATA_R |= Pt->Out[Input]; // pulse GPIO_PORTD_DATA_R &= ~0x0F; Pt = Pt->Next[Input]; // next state } }