Part Number: TM4C123GH6PM
Tool/software: TI C/C++ Compiler
I am writing a program which transmits data to UART0 by reading the status of GPIO pins. I am using default 16MHz clock . Controller goes into Fault ISR whenever I read or write to GPIO DATA Register. When I comment out the UART initialisation, It is able to read and write data to the GPIO Data Register. This is the COLOR WHEEL program of online course Embedded Systems Design on EDX. Please Help. Below is my code
#include <stdint.h>
#include "inc/tm4c123gh6pm.h"
//void GPIO_init(void);
//void UART_init(void);
unsigned char ask_question(void);
void give_answer(unsigned char receive);
void UART_TX(unsigned char tx_data);
unsigned char UART_RX();
int main(void)
{
unsigned char question_num;
unsigned char answer;
uint32_t delay;
///////////Configure GPIO Port F//////////
SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F
delay = SYSCTL_RCGC2_R;
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port F
GPIO_PORTF_CR_R = 0x1F; // allow changes to PF4-0
// only PF0 needs to be unlocked, other bits can't be locked
GPIO_PORTF_AMSEL_R = 0x00; // 3) disable analog on PF
GPIO_PORTF_PCTL_R = 0x00000000; // 4) PCTL GPIO on PF4-0
GPIO_PORTF_DIR_R = 0x0E; // 5) PF4,PF0 in, PF3-1 out
GPIO_PORTF_AFSEL_R = 0x00; // 6) disable alt funct on PF7-0
GPIO_PORTF_PUR_R = 0x11; // enable pull-up on PF0 and PF4
GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital I/O on PF4-0
GPIO_PORTF_DATA_R &= ~(0x04);// PF2 is made low
//////////////////////////////////////////
////////// Configure UART 0/////////////
SYSCTL_RCGCUART_R = 0x01;// Enable clock for UART 0 module
SYSCTL_RCGCGPIO_R = 0x01;// Enable clock for GPIO A module
UART0_CTL_R &= ~(0x01); //Disable UART 0 before configuring baud rate.
////Baud rate set to 115200///
UART0_IBRD_R = 0x08;
UART0_FBRD_R = 0x2C;
//////////////////////////////
UART0_LCRH_R = 0x60; // Word length = 8bits, Stop bits = 1, Parity none and FIFO disabled
UART0_CTL_R |= 0x00000001; // Enable UART and enable its TX and RX functionality.
GPIO_PORTA_DEN_R |= 0x03; // Enable digital functionality for PA0 and PA1.
GPIO_PORTA_AFSEL_R |= 0x03;
GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00) + 0x11;
GPIO_PORTA_AMSEL_R &= ~0x03;
///////////////////////////////////////////
while(1)
{
question_num = ask_question();
UART_TX(question_num);
answer = UART_RX();
give_answer(answer);
}
}
unsigned char ask_question(void) // this function will glow led corresponding to a given question
{
uint32_t status = 0;
uint32_t send_status = 0;
unsigned char question = '0';
do
{
send_status = GPIO_PORTF_DATA_R&0x00000001;// To read status of PF0.
status = GPIO_PORTF_DATA_R&0x00000010;// To read staus of PF4.
if((question == '0')&(status == 0x10))
{
GPIO_PORTF_DATA_R &= ~(0x04); // Turn off Blue LED
GPIO_PORTF_DATA_R |= 0x02; // Glow Red LED for 1st question. Normally if PF4 not pressed it will remain in this state.
}
if(status == 0x00)
{
question++;
if(question == '1')
{
GPIO_PORTF_DATA_R |= 0x0A; // Glow Red and Green LED to make yellow.
}
if(question == '2')
{
GPIO_PORTF_DATA_R &= ~0x02; //Turn off Red LED
GPIO_PORTF_DATA_R |= 0x08; // Glow green LED
}
if(question == '3')
{
GPIO_PORTF_DATA_R |= 0x0C; // Glow Green and Blue LED to make cyan colour
}
if(question == '4')
{
GPIO_PORTF_DATA_R &= ~0x08; // Turn OFF Green LED
GPIO_PORTF_DATA_R |= 0x04; // Glow blue LED;
}
if(question == '5')
{
question = '0'; // If PF4 pressed again then go back to question 0.
}
}
else
{
// do nothing
}
}while(send_status!=0);
return question;
}
void give_answer(unsigned char receive) // this function will glow led corresponding to a given answer
{
uint8_t status = GPIO_PORTF_DATA_R & 0x10;
do
{
if(receive == '5')
{
GPIO_PORTF_DATA_R = 0x00; // Turn off All LED'S
GPIO_PORTF_DATA_R |= 0x06 ; //Turn on red and blue led to make magenta color. Reply Maybe
}
if(receive == '6')
{
GPIO_PORTF_DATA_R = 0x00; // Turn off All LED'S
GPIO_PORTF_DATA_R |= 0x0E; // Turn on ALL Led to make white. Reply Yes
}
if(receive == '7')
{
GPIO_PORTF_DATA_R = 0x00;// Turn off All LED'S. to make black colour
}
}while(status != 0);// Next question will be asked only when PF4 is pressed.
}
void UART_TX(unsigned char tx_data)
{
while((UART0_FR_R&0x20)!=0);
UART0_DR_R = tx_data;
}
unsigned char UART_RX()
{
unsigned char rx_data;
while((UART0_FR_R&0x10)!=0);
rx_data = UART0_DR_R;
return rx_data;
}