Skip to content

Instantly share code, notes, and snippets.

@robsbots
Created July 8, 2014 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsbots/fe8dd16bbb59ed02a704 to your computer and use it in GitHub Desktop.
Save robsbots/fe8dd16bbb59ed02a704 to your computer and use it in GitHub Desktop.
/**
* SOS.c by Iliyan Stankov
* Runs on TM4C123 LaunchPad
* Input from PF4(SW1),PF0(SW2), output to PF3 (Green LED)
* Pressing SW1 starts SOS (Green LED flashes SOS).
* Pressing SW2 stops SOS
*/
// Constant declarations to access port registers using
// symbolic names instead of addresses
// ##########################################################################
// # Variable to hold register addresses
// # These registers control how the peripherals on the chip are set up
// # These include I/O ports, timers etc.
// # (registers, their use and memory locations are listed in data sheet)
// ##########################################################################
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
// Global Variables
unsigned long SW1; // input from PF4
unsigned long SW2; // input from PF0
// Function Prototypes
void PortF_Init(void);
void Flash_SOS(void);
void delay(unsigned int halfsecs);
/**
* MAIN: Controls the order of operations
*/
int main(void)
{
PortF_Init(); // Init port PF4 PF2 PF0
while(1)
{
do
{
SW1 = GPIO_PORTF_DATA_R&0x10; // PF4 into SW1
}
while (SW1 == 0x10);
do
{
FlashSOS();
SW2 = GPIO_PORTF_DATA_R&0x01; // PF0 into SW2
}
while(SW2 == 0x01);
}
}
/**
* Subroutine to initialize port F pins for input and output
* PF4 is input SW1 and PF2 is output Blue LED
*/
void PortF_Init(void)
{
volatile unsigned long delay;
// ###########################################
// # Here we set up the peripherals on the chip.
// #
// # First we set the clock speed.
// # Here we do “current register value” = “current register value” OR hexadecimal 20
// # This sets a bit in the RCGC settings in the “Run-Mode Clock
// # Configuration (RCC)” register which chooses the
// # “Precision internal oscillator / 4” as the clock signal for the chip
// ###########################################
SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock
// # Not sure what this variable is for.....
// # The variable is being set to a register address
delay = SYSCTL_RCGC2_R; // delay
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock PortF PF0
GPIO_PORTF_CR_R |= 0x1F; // allow changes to PF4-0
GPIO_PORTF_AMSEL_R &= 0x00; // 3) disable analog function
GPIO_PORTF_PCTL_R &= 0x00000000; // 4) GPIO clear bit PCTL
GPIO_PORTF_DIR_R &= ~0x11; // 5.1) PF4,PF0 input,
GPIO_PORTF_DIR_R |= 0x08; // 5.2) PF3 output
GPIO_PORTF_AFSEL_R &= ~0x1F; // 6) no alternate functions
GPIO_PORTF_PUR_R |= 0x11; // enable pullup resistors on PF4,PF0
GPIO_PORTF_DEN_R |= 0x1F; // 7) enable digital pins PF4-PF0
}
/**
* Subroutine to Flash a green LED SOS once
*/
void Flash_SOS(void)
{
// S
// # Turn bit on, delay.....
GPIO_PORTF_DATA_R |= 0x08; delay(1);
// # Turn bit off, delay....
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
GPIO_PORTF_DATA_R |= 0x08; delay(1);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
GPIO_PORTF_DATA_R |= 0x08; delay(1);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
// O
GPIO_PORTF_DATA_R |= 0x08; delay(4);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
GPIO_PORTF_DATA_R |= 0x08; delay(4);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
GPIO_PORTF_DATA_R |= 0x08; delay(4);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
// S
GPIO_PORTF_DATA_R |= 0x08; delay(1);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
GPIO_PORTF_DATA_R |= 0x08; delay(1);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
GPIO_PORTF_DATA_R |= 0x08; delay(1);
GPIO_PORTF_DATA_R &= ~0x08; delay(1);
delay(10); // Delay for 5 secs in between flashes
}
/**
* Subroutine to delay in units of half seconds
* Inputs: Number of half seconds to delay
*/
void delay(unsigned int halfsecs)
{
unsigned long count;
while (halfsecs > 0 ) // repeat while still halfsecs to delay
{
count = 4000000;
while (count > 0) // Takes approximately 0.5 secs
{
count--;
}
halfsecs--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment