0% found this document useful (0 votes)
61 views

EDM18B010 - Expt4-Embedded System Design Practice

The document describes using a SysTick timer to control a stepper motor by passing control signals at specific time intervals. It explains that the SysTick timer generates interrupts at a fixed time interval that can be set in the reload register. The procedure to initialize GPIO ports and the SysTick timer is provided. Code examples are given to turn the stepper motor 90 degrees clockwise and then 180 degrees clockwise and back to 0 degrees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

EDM18B010 - Expt4-Embedded System Design Practice

The document describes using a SysTick timer to control a stepper motor by passing control signals at specific time intervals. It explains that the SysTick timer generates interrupts at a fixed time interval that can be set in the reload register. The procedure to initialize GPIO ports and the SysTick timer is provided. Code examples are given to turn the stepper motor 90 degrees clockwise and then 180 degrees clockwise and back to 0 degrees.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Embedded System Design Practice

3​rd ​February, 2021


EDM18B010 Chirag C M

Experiment-4: SysTick Timer- Stepper Motor


Control

Aim:
In this experiment we will deal with passing a sequence of control signals with
specific time intervals to control the stepper motor using SysTick timer.

Requirements:
● PC running on windows
● Keil uVision
● TivaWare
● Stellaris ICDI drivers

Theory:
Systick timer is a dedicated hardware-based timer which is built inside the ​ARM ​Cortex M4
CPU and can be used to generate an interrupt at a fixed interval of time. As shown in the
figure below:
The systick timer will generate interrupts after a specified time and time settings can be done
using the Systick control register.
This figure given below depicts the working behavior of a systick timer of ARM cortex M4
microcontroller:

As you know that the system timer of TM4C123G microcontroller is a 24-bit down counter.
We reload the initial value to reload register and counter decrements from reload value to
zero. The value of the counter decrements on every positive edge of the clock cycle. When
counter values reach zero, the system timer generates an interrupt. Also, the counter is
re-initialized with a reload value again. Hence, the process keeps repeating.

Procedure:
To initialize an I/O port for general use:
1. Activate the clock for the port in the Run Mode Clock Gating Control Register 2 (​RCGC2​).
PortF=0x00000020; PortB=0x00000002; PortA=0x00000001

Use “selective high” i.e “|=” to initialize the clocks for ports whenever using multiple
ports as the variable SYSCTRL_ RCGC2 is common for all.
2. Unlock the port (​LOCK​ = ​0x4C4F434B​).
GPIO_PORTF_LOCK_R = 0x4C4F434B; (Default)
3. Disable the analog function of the pin in the Analog Mode Select register (​AMSEL​), because
we want to use the pin for digital I/O. If this pin is connected to the ADC or analog
comparator, its corresponding bit in ​AMSEL​ must be set as ​1​. In our case, this pin is used as
digital I/O, so its corresponding bit must be set as ​0​.
GPIO_PORTF_AMSEL_R = 0x00;
4. Clear bits in the port control register (​PCTL​) to select regular digital functions. Each GPIO
pin needs four bits in its corresponding PCTL register. Not every pin can be configured to
every alternative function.
GPIO_PORTF_PCTL_R = 0x00000000;
5. Set its direction register (​DIR​)-i.e whether the port is output or input. A DIR bit of ​0​ means
input, and ​1​ means output.
GPIO_PORTF_DIR_R = 0x0E; (00001110)(PF 1-3 input, PF0 & PF4 output)
6. Clear bits in the alternate Function Select register (​AFSEL​).
GPIO_PORTF_AFSEL_R = 0x00;
7. Enable digital port in the Digital Enable register (​DEN​).
GPIO_PORTF_DEN_R = 0x1F; (00011111)(Enable PF0 to PF4 registers)

Systick Timer:

1. Initialize/disable SysTick timer STCTRL=0


2. Clear current register value STCURRENT=0
3. Set reload register to maximum value STRELOAD=0x00ffffff
[max(24 bits)=0x00ffffff]
4. Enable Systick timer
i.e 1. enable clock (1st bit)
2. Enable clock (3rd bit)
STCTRL=0x00000005

5. Initialize reload with the required value


6. Set current value to 0, STCURRENT=0
7. Run until until count flag is raised… i.e [current = reload]

Reload period:

Stepper Motor Working Principle:


Stepper motor is a brushless DC motor that rotates in steps. This is very useful
because it can be precisely positioned without any feedback sensor, which
represents an open-loop controller. The stepper motor consists of a rotor that is
generally a permanent magnet and it is surrounded by the windings of the stator. As
we activate the windings step by step in a particular order and let a current flow
through them they will magnetize the stator and make electromagnetic poles
respectively that will cause propulsion to the motor. So that’ the basic working
principle of the stepper motors.

This is a picture of a stepper motor run in a full step mode. Here 2 coils are active at
any given time providing better torque

Formula:
o
360
S tep angle = Steps per Revolution
S teps per revolution = no. of teeth * no. of poles
Here, the number of teeth is 100 and the motor is a bipolar motor.
Therefore the step angle comes out to be 1.8​o

Code:
Example exercise: Move the stepper @ portD by 90​o​(T=10ms)
#include "PLL.h"
#include "tm4c123gh6pm.h"
#include <stdint.h>

void Port_Init(void); // start sound output


void SysFun(void); //initialize SysTick timer
void SysLoad(unsigned long period); //Load reload value
void SysInit(void);

void Port_Init(void){ unsigned long volatile delay;


SYSCTL_RCGC2_R |= 0x00000008; // activate port D
delay = SYSCTL_RCGC2_R;
GPIO_PORTD_AMSEL_R &= ~0x20; // no analog
GPIO_PORTD_PCTL_R &= ~0x00F00000; // regular function
GPIO_PORTD_DIR_R |= 0x0F; // make PA5 out
//GPIO_PORTA_DR8R_R |= 0x20; // can drive up to 8mA out
GPIO_PORTD_AFSEL_R &= ~0x20; // disable alt function PA5
GPIO_PORTD_DEN_R |= 0x1F; // enable digital I/O on PA5
GPIO_PORTD_PUR_R |= 0x11;
GPIO_PORTD_CR_R |= 0x1F;
}

int main(void){
unsigned long int i=0,k;
PLL_Init();
SysFun();
Port_Init();
SysInit();
while(1){
for (i=1; i<13;i=i+1)
{
GPIO_PORTD_DATA_R=0x05;
SysLoad(800000);
GPIO_PORTD_DATA_R=0x06;
SysLoad(800000);
GPIO_PORTD_DATA_R=0x0A;
SysLoad(800000);
GPIO_PORTD_DATA_R=0x09;
SysLoad(800000);
}

GPIO_PORTD_DATA_R=0x05;
SysLoad(800000);
GPIO_PORTD_DATA_R=0x06;
SysLoad(800000);
}
}

void SysLoad(unsigned long period){


NVIC_ST_RELOAD_R = period -1;
NVIC_ST_CURRENT_R = 0;
while((NVIC_ST_CTRL_R&0x00010000)==0){
}
}
void SysInit(void){

NVIC_ST_CTRL_R = 0;
NVIC_ST_CURRENT_R = 0;//any write to current clears it
NVIC_SYS_PRI3_R=NVIC_SYS_PRI3_R&0x00FFFFFF;//priority 0
NVIC_ST_CTRL_R=0x00000005; //ENABLE WITH CORE CLOCK AND INTERRUPTS
}
void SysFun(void){

NVIC_ST_CTRL_R = 0; //disable SysTick during setup


NVIC_ST_CTRL_R = 0x00000005;

}
Output: port D 0° to 90 °
0° to 90 ° - Continuously giving pulses of 10ms length in a specific sequence(5->6->a->9) so
as to make the motor move in clockwise direction. Repeat this sequence for 13 times to get
90​o​ of step angle.

5 -> 6 -> a -> 9 sequence can be observed. This pattern repeats for 13 times in order to turn 90​o

Code:
Exercise: turn the stepper @ portE to 180​o ​and then back to 0​o​ stopping it for one second in
between.

#include "PLL.h"
#include "tm4c123gh6pm.h"
#include <stdint.h>

void Port_Init(void); // start sound output


void SysFun(void); //initialize SysTick timer
void SysLoad(unsigned long period); //Load reload value
void SysInit(void);

void Port_Init(void){ unsigned long volatile delay;


SYSCTL_RCGC2_R |= 0x00000010; // activate port D
delay = SYSCTL_RCGC2_R;
GPIO_PORTE_AMSEL_R &= ~0x20; // no analog
GPIO_PORTE_PCTL_R &= ~0x00F00000; // regular function
GPIO_PORTE_DIR_R |= 0x0F; // make PA5 out
//GPIO_PORTA_DR8R_R |= 0x20; // can drive up to 8mA out
GPIO_PORTE_AFSEL_R &= ~0x20; // disable alt funct on PA5
GPIO_PORTE_DEN_R |= 0x1F; // enable digital I/O on PA5
GPIO_PORTE_PUR_R |= 0x11;
GPIO_PORTE_CR_R |= 0x1F;
}

int main(void){
unsigned long int i=0,k;
PLL_Init();
SysFun();
Port_Init();
SysInit();
while(1){
for (i=1; i<25;i=i+1)
{
GPIO_PORTE_DATA_R=0x05;
SysLoad(1600000);
GPIO_PORTE_DATA_R=0x06;
SysLoad(1600000);
GPIO_PORTE_DATA_R=0x0A;
SysLoad(1600000);
GPIO_PORTE_DATA_R=0x09;
SysLoad(1600000);
}
GPIO_PORTE_DATA_R=0x00;
for (i=0; i<10;i=i+1)
SysLoad(1600000);

for (i=1; i<25;i=i+1)


{
GPIO_PORTE_DATA_R=0x09;
SysLoad(1600000);
GPIO_PORTE_DATA_R=0x0A;
SysLoad(1600000);
GPIO_PORTE_DATA_R=0x06;
SysLoad(1600000);
GPIO_PORTE_DATA_R=0x05;
SysLoad(1600000);
}

}
}

void SysLoad(unsigned long period){


NVIC_ST_RELOAD_R = period -1;
NVIC_ST_CURRENT_R = 0;
while((NVIC_ST_CTRL_R&0x00010000)==0){
}
}
void SysInit(void){

NVIC_ST_CTRL_R = 0;
NVIC_ST_CURRENT_R = 0;//any write to current clears it
NVIC_SYS_PRI3_R=NVIC_SYS_PRI3_R&0x00FFFFFF;//priority 0
NVIC_ST_CTRL_R=0x00000005; //ENABLE WITH CORE CLOCK AND INTERRUPTS
}
void SysFun(void){

NVIC_ST_CTRL_R = 0; //disable SysTick during setup


NVIC_ST_CTRL_R = 0x00000005;
}

Output:
0 to 180​o ​ (T=20ms) clockwise

1second pause
180​o ​to 0​o​ sequence reversed 9 -> a -> 6 -> 5, implies direction changed

Inference:
- ​SysTick timer cannot store reload values above 0x00ffffff [24bit]
(8 bits reserved )
- SysTick timer can be used to obtain higher frequencies upto 80Mhz
- SysTickCan be used to operate switches very accurately to turn the
Stepper motor to a specific angle

Result:
● SysTick timer was implemented. Specific time delay and pulse
generator was programmed to provide pulses to turn Stepper
motor to a specific angle
● Stepper motor open loop control was performed and the switch
interface was programmed in such a way that the Stepper can be
turned to a specific angle by operating the switches.

You might also like