Experiment Report - 10
Benedict I
EC23I2004
Theory:
UART (Universal Asynchronous Receiver/Transmi er)
A UART is used to interface the Tiva microcontroller with your PC.
UART Module Introduc on
The TM4C123 microcontrollers has 8 UART modules.
Two devices communica ng with asynchronous serial interfaces operate at
the same frequency (baud rate) but have two separate clocks.
With a UART protocol, the clock signal is not included in the interface cable
between devices.
Two UART devices can communicate with each other as long as the two
clocks have frequencies within ±5% of each other.
There are five mechanisms to synchronize the microcontroller with the I/O
device.
Blind: Fixed delay in so ware
Polling: So ware checks a status flag in the device
Interrupt: Hardware triggers so ware when hardware is ready
Periodic Polling: So ware in a periodic interrupt checks a status flag
Direct Memory Access (DMA): Hardware automa cally transfers data
when hardware is ready
UART Ini aliza on
RCGS1
UART0_CTL_R
UART0_IBRD_R
UART0_FBRD_R
UART0_LCRH_R
UART0_FR_R
UART0_DR_R
Algorithm for UART Module
Include header files
Ini alize Port A for UART
Ini alize Port F for on board LED access
Use polling method to synchronize data exchange
Input character/s
Output character/s
In the main, call all the ini aliza on func ons.
Change colour based on string commands
Example Exercise:
Control on-board LED colour by sending commands from PC using UART module.
Code:
#include "tm4c123gh6pm.h"
#include <stdint.h>
#include "PLL.h"
#include "string.h"
char string[20];
// standard ASCII symbols
#define CR 0x0D // carriage return (return to beginning of current line)
#define LF 0x0A //line feed or newline character '\n'
#define BS 0x08 //backspace
void UART_Init(void){
SYSCTL_RCGC1_R |= 0x01; // ac vate UART0 Clock Ga ng Control
SYSCTL_RCGC2_R |= 0x01; // ac vate port A Clock Ga ng Control
UART0_CTL_R &= ~(0x01); // disable UART
UART0_IBRD_R = 43; // IBRD = int(80,000,000 / (16 * 115,200)) =
int(43.40278)
UART0_FBRD_R = 26; // FBRD = int(0.40278 * 64 + 0.5) = 26
UART0_LCRH_R = 0x70; // 8 bit word length, no parity bits, one stop bit, Enable
FIFOs
UART0_CTL_R |= 0x01; // enable UART
GPIO_PORTA_AFSEL_R |= 0x03; // enable alt funct on PA1-0
GPIO_PORTA_DEN_R |= 0x03; // enable digital I/O on PA1-0
GPIO_PORTA_PCTL_R = 0x00000011; // configure PA1-0 as UART
GPIO_PORTA_AMSEL_R &= ~0x03; // disable analog func onality on PA
}
void UART_OutChar(unsigned char data){
while((UART0_FR_R&0x20) != 0); // UART Transmit FIFO Full
UART0_DR_R = data;
}
void UART_OutString(char *pt){
while(*pt){
UART_OutChar(*pt);
pt++;
}
}
unsigned char UART_InChar(void){
while((UART0_FR_R&0x10) != 0); // UART Receive FIFO Empty
return((unsigned char)(UART0_DR_R&0xFF));
}
void UART_InString(char *bufPt, unsigned short max) {
int length=0;
char character;
character = UART_InChar();
while(character != CR){
if(character == BS){
if(length){
bufPt--;
length--;
UART_OutChar(BS);
}
}
else if(length < max){
*bufPt = character;
bufPt++;
length++;
UART_OutChar(character);
}
character = UART_InChar();
}
*bufPt = 0;
}
void PortF_Init(void)
{
unsigned long delay;
SYSCTL_RCGC2_R |= 0x20;// clock for Port F
delay = SYSCTL_RCGC2_R;// wait 3-5 bus cycles
GPIO_PORTF_LOCK_R = 0x4C4F434B;//unlock GPIOPortF
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 to be unlocked, other bits can't be
GPIO_PORTF_AMSEL_R = 0x00;// disable analog
GPIO_PORTF_PCTL_R = 0x00;// bits for PF4-0
GPIO_PORTF_DIR_R = 0x0E;// PF4,0 in, PF3,1 out
GPIO_PORTF_AFSEL_R = 0x00;//disable alt func
GPIO_PORTF_PUR_R = 0x11;// enable pullup on PF0,4
GPIO_PORTF_DEN_R = 0x1F;// enable digital I/O
}
int main(void){
PLL_Init(); // set system clock to 50 MHz
UART_Init(); // ini alize UART
PortF_Init(); // ini alize portF
while(1){
UART_OutString("Enter colour: "); //output data
UART_InString(string,19); // input data
if( strcmp(string,"red")== 0){ // if input data red
GPIO_PORTF_DATA_R = (0x02); // red led on
UART_OutChar(LF); // new line
UART_OutString("Red LED glowing");} // output data
message
else if( strcmp(string,"blue")== 0){
GPIO_PORTF_DATA_R = (0x04);
UART_OutChar(LF);
UART_OutString("Blue LED glowing");}
else if( strcmp(string,"green")== 0){
GPIO_PORTF_DATA_R = (0x08);
UART_OutChar(LF);
UART_OutString("Green LED glowing");}
else if( strcmp(string,"yellow")== 0){
GPIO_PORTF_DATA_R = (0x0A);
UART_OutChar(LF);
UART_OutString("Yellow LED glowing");}
else if( strcmp(string,"cyan")== 0){
GPIO_PORTF_DATA_R = (0x0C);
UART_OutChar(LF);
UART_OutString("Cyan LED glowing");}
else if( strcmp(string,"pink")== 0){
GPIO_PORTF_DATA_R = (0x06);
UART_OutChar(LF);
UART_OutString("Pink LED glowing");}
else if( strcmp(string,"white")== 0){
GPIO_PORTF_DATA_R = (0x0E);
UART_OutChar(LF);
UART_OutString("White LED glowing");}
else{
GPIO_PORTF_DATA_R = (0x00);
UART_OutChar(LF);
UART_OutString("NO LED glowing");}
UART_OutChar(LF);
}
}
Example Exercise – Red LED turned on for input “red”
Result: Successfully controlled on-board LED colour by sending commands from
PC using UART module.
Exercise 1:
Control speed and direc on of DC motor by sending commands from PC using
UART modules.
Code:
//Include header files
#include "tm4c123gh6pm.h"
#include <stdint.h>
#include "PLL.h"
#include "string.h"
char string[20]; //string array
// standard ASCII symbols
#define CR 0x0D // carriage return (return to beginning of current line)
#define LF 0x0A //line feed or newline character '\n'
#define BS 0x08 //backspace
void UART_Init(void){
SYSCTL_RCGC1_R |= 0x01; // ac vate UART0 Clock Ga ng Control
SYSCTL_RCGC2_R |= 0x01; // ac vate port A Clock Ga ng Control
UART0_CTL_R &= ~(0x01); // disable UART
UART0_IBRD_R = 43;// IBRD = int(80,000,000 / (16 * 115,200)) = int(43.40278)
UART0_FBRD_R = 26; // FBRD = int(0.40278 * 64 + 0.5) = 26
UART0_LCRH_R = 0x70;//8 bit word length, no parity bits,one stop bit,Enable FIFOs
UART0_CTL_R |= 0x01; // enable UART
GPIO_PORTA_AFSEL_R |= 0x03; // enable alt funct on PA1-0
GPIO_PORTA_DEN_R |= 0x03; // enable digital I/O on PA1-0
GPIO_PORTA_PCTL_R = 0x00000011; // configure PA1-0 as UART
GPIO_PORTA_AMSEL_R &= ~0x03; // disable analog func onality on PA
}
void UART_OutChar(unsigned char data){
while((UART0_FR_R&0x20) != 0); // UART Transmit FIFO Full
UART0_DR_R = data;
}
void UART_OutString(char *pt){
while(*pt){
UART_OutChar(*pt);
pt++;
}
}
unsigned char UART_InChar(void){
while((UART0_FR_R&0x10) != 0); // UART Receive FIFO Empty
return((unsigned char)(UART0_DR_R&0xFF));
}
void UART_InString(char *bufPt, unsigned short max) {
int length=0;
char character;
character = UART_InChar();
while(character != CR){
if(character == BS){
if(length){
bufPt--;
length--;
UART_OutChar(BS);
}
}
else if(length < max){
*bufPt = character;
bufPt++;
length++;
UART_OutChar(character);
}
character = UART_InChar();
}
*bufPt = 0;
}
//port D ini aliza on
void PortD_Init(void)
{
vola le unsigned long delay;
SYSCTL_RCGC2_R |= 0x08;// clock for Port D
delay = SYSCTL_RCGC2_R;// wait 3-5 bus cycles
GPIO_PORTD_LOCK_R = 0x4C4F434B;//unlock GPIOPortD
GPIO_PORTD_CR_R = 0x03; // allow changes to PD1-0
// only PF0 to be unlocked, other bits can't be
GPIO_PORTD_AMSEL_R = 0x00;// disable analog
GPIO_PORTD_PCTL_R = 0x00;// bits for PD1-0
GPIO_PORTD_DIR_R = 0x03;// PD-1 PD-0
GPIO_PORTD_AFSEL_R = 0x00;//disable alt func
GPIO_PORTD_PUR_R = 0x03;// enable pullup on PD1-0
GPIO_PORTD_DEN_R = 0x03;// enable digital I/O
}
void SysInit(void) //Defining the SysInit func on
{
NVIC_ST_CTRL_R = 0; //clear this register ini ally
NVIC_ST_CURRENT_R = 0;// any write to current clears it
NVIC_ST_CTRL_R = 0x00000005;// enable with core clock and interrupts
}
void SysLoad(unsigned long period) //Defining the SysLoad func on
{
NVIC_ST_RELOAD_R = period -1;
//Since we are comparing to 0 hence -1 is done to count those many mes
NVIC_ST_CURRENT_R = 0;
// any value wri en to CURRENT clears
while((NVIC_ST_CTRL_R&0x00010000)==0){ // wait for count flag}}
void SysFun(void){
NVIC_ST_CTRL_R = 0;//disable SysTick during setup
NVIC_ST_CTRL_R = 0x00000005; // enable with core clock and interrupts}
int main(void){
PLL_Init(); // set system clock to 50 MHz
UART_Init();// ini alize UART
PortD_Init(); // ini alize portD
SysFun();// ini alize sysfun
SysInit(); // ini alize sys_init
while(1){
UART_OutString("Enter Speed and Direc on: "); //output data
UART_InString(string,19); // input data
if( strcmp(string,"50 clock")== 0)// if input data read{
UART_OutChar(LF); // new line
UART_OutString("Motor roates at 50% DC Clockwise");
while(((UART0_FR_R&0x10) != 0)){
GPIO_PORTD_DATA_R = (0x01); //using pD.0 =1 for 5ms
SysLoad(400000); // wait 5ms
GPIO_PORTD_DATA_R = (0x00); //using pD.0 =0 for 5ms
SysLoad(400000); // wait 5ms}}
else if(strcmp(string,"50 an clock")== 0){
UART_OutChar(LF); // new line
UART_OutString("Motor roates at 50% DC An -clockkwise");
while(((UART0_FR_R&0x10) != 0)){
GPIO_PORTD_DATA_R = (0x02); //using pD.1 =1 for 5ms
SysLoad(400000); // wait 5ms
GPIO_PORTD_DATA_R = (0x00); //using pD.1 =0 for 5ms
SysLoad(400000); // wait 5ms
}}
else if( strcmp(string,"75 clock")== 0){
UART_OutChar(LF); // new line
UART_OutString("Motor roates at 75% DC Clockwise");
// output data message
while(((UART0_FR_R&0x10) != 0)){
GPIO_PORTD_DATA_R = (0x01); //using pD.0 =1 for 7.5ms
SysLoad(600000); // wait 7.5ms
GPIO_PORTD_DATA_R = (0x00); //using pD.0 =0 for 2.5ms
SysLoad(200000); // wait 2.5ms
}}
else if( strcmp(string,"90 clock")== 0)
{
UART_OutChar(LF); // new line
UART_OutString("Motor roates at 90% DC Clockwise");
// output data message
while(((UART0_FR_R&0x10) != 0))
{
GPIO_PORTD_DATA_R = (0x01); //using pD.0 =1 for 9ms
SysLoad(720000); // wait 9ms
GPIO_PORTD_DATA_R = (0x00); //using pD.0 =0 for 1ms
SysLoad(80000); // wait 1ms
// keeping pin D.1 always as zero
}}
else if( strcmp(string,"90 an clock")== 0){
UART_OutChar(LF); // new line
UART_OutString("Motor roates at 90% DC an Clockwise");
while(((UART0_FR_R&0x10) != 0))
{
GPIO_PORTD_DATA_R = (0x02); //using pD.1 =1 for 9ms
SysLoad(720000); // wait 9ms
GPIO_PORTD_DATA_R = (0x00); //using pD.1 =1 for 1ms
SysLoad(80000); // wait 1ms
// keeping pin D.0 always as zero
}}
else{
GPIO_PORTD_DATA_R &= (0x00);//clearing
UART_OutChar(LF); // new line
UART_OutString("No opera on");// output data message}
UART_OutChar(LF); // new line} }
Exercise 1
Motor rotates at 50% speed in An -clockwise direc on for given input
Motor rotates at 50% speed in Clockwise direc on for given input
Result: Successfully controlled speed and direc on of DC motor by sending
commands from PC using UART modules.