Commit 34b191de authored by Stefan Tauner's avatar Stefan Tauner
Browse files

Make both examples do the same thing in the same lines

New objective:
 - Toggle D1 with 2 Hz
 - Turn off D2 if USR_SW1 is pressed
 
Also, refine comments and explain the timing better.
parent 52ff2276
/*
* Toggles LED on pin 1 on port N
* Turns off LED on pin 0 on port N if button (USR_SW1 on port J pin 0) is pressed
*
* Toggles led on pin 1 on port N
*
* Already set build options (no need to change anything):
* Already set build options of this project (no need to change anything):
* Stack size set to 1024 byte
* Heap size disabled - No malloc()
* No code optimization
* Strict floating point interrupt behaviour
* Heap size disabled - No malloc() available
* No code optimization - Easier debugging
* Strict floating point interrupt behavior
* Hardware floating point unit activated
*
*/
#define TARGET_IS_TM4C129_RA2
#include <stdbool.h> /*driverlib header requires stdbool.h to be included a first header file before any driverlib header*/
#include <stdint.h>
#include <driverlib/gpio.h>
#include <driverlib/pin_map.h>/*supplies GPIO_PIN_x*/
#include <driverlib/sysctl.h>
#include <driverlib/rom.h>
#include <inc/hw_memmap.h>/*supplies GPIO_PORTx_BASE*/
#define TARGET_IS_TM4C129_RA2 /* Tells rom.h the version of the silicon */
#include <stdint.h> /* C99 header for uint*_t types */
#include <stdbool.h> /* Driverlib headers require stdbool.h to be included first */
#include <driverlib/gpio.h> /* Supplies GPIO* functions and GPIO_PIN_x */
#include <driverlib/sysctl.h> /* Supplies SysCtl* functions and SYSCTL_* macros */
#include <driverlib/rom.h> /* Supplies ROM_* variations of functions */
#include <inc/hw_memmap.h> /* Supplies GPIO_PORTx_BASE */
/*Controller is initially clocked with 16MHz*/
/* !! Changing this define does not change clock speed !! */
#define F_CPU 16000000
void main (void) {
/* Controller is initially clocked with 16 MHz (via PIOSC) */
/* !!! Changing this macro does not change clock speed !!! */
#define F_CPU (16000000)
int main (void)
{
uint32_t ui32Strength;
uint32_t ui32PinType;
uint8_t ui8val = 1;
uint32_t ui32Loop = 0;
uint8_t ui8button;
uint8_t ui8toggle = (1<<1);
/*activate gpio port n*/
/* Activate GPIO ports */
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
/* Set pin 0 of GPIO port J to digital input */
GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_0);
/* Enable pull-up for button on pin 0 of port J
* Note the API difference between the *Get and *Set functions. */
GPIOPadConfigGet(GPIO_PORTJ_BASE, 0, &ui32Strength, &ui32PinType);
GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0, ui32Strength, GPIO_PIN_TYPE_STD_WPU);
/* Set pins 0 & 1 of GPIO port N to digital output */
GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1 | GPIO_PIN_0);
while(1) {
/* Toggle bit 1 in variable ui8toggle */
ui8toggle = ui8toggle ^ (1<<1);
/* Write value of ui8toggle to pin 1 of port N */
GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, ui8toggle);
/*set pin 1 of gpio port n to output*/
GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE,GPIO_PIN_1);
/* Read value of pin 0 on port j into variable */
ui8button = GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_0);
/* Set pin 0 of port N accordingly */
GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, ui8button);
/*configure GPIO_PIN_1 on port N as std pin*/
GPIOPadConfigGet(GPIO_PORTN_BASE, 1/*api requires pin number [0-7] instead of bit packed representation (e.g., GPIO_PIN_1 )*/, &ui32Strength, &ui32PinType);
GPIOPadConfigSet(GPIO_PORTN_BASE, GPIO_PIN_1,ui32Strength,GPIO_PIN_TYPE_STD);
/* Delay for *approximately* 0.5 s.
* 16M CPU cycles per second / 2 / 3 (SysCtlDelay factor) = 2666667
* The actual delay can be longer due to interrupts! This is not suitable for exact timing. */
ROM_SysCtlDelay(F_CPU/2/3);
for(;;) {
ui8val = ui8val ^ 2;/*toggle bit 1 in variable val*/
/*write value of val to pin 1 of port n*/
GPIOPinWrite (GPIO_PORTN_BASE, GPIO_PIN_1, ui8val);
// for(ui32Loop = 0; ui32Loop < 400000; ui32Loop++){}
ROM_SysCtlDelay(F_CPU/2/3); //approx 0,5 s
}
}
/*
* Toggles LED on pin 1 on port N
* Turns off LED on pin 0 on port N if button (USR_SW1 on port J pin 0) is pressed
*
* Toggles led on pin 1 on port N
*
* Already set build options (no need to change anything):
* Already set build options of this project (no need to change anything):
* Stack size set to 256 byte
* Heap size disabled - No malloc()
* No code optimization
* Strict floating point interrupt behaviour
* Heap size disabled - No malloc() available
* No code optimization - Easier debugging
* Strict floating point interrupt behavior
* Hardware floating point unit activated
*
*/
#include <stdint.h>
#include <inc/tm4c1294ncpdt.h>
int main(void)
#include <stdint.h> /* C99 header for uint*_t types */
#include <stdbool.h> /* Driverlib headers require stdbool.h to be included first */
#include <inc/tm4c1294ncpdt.h> /* Supplies SYSCTL_RCGCGPIO*, GPIO_PORT* macros */
#include <inc/hw_types.h> /* Supplies HWREG macro */
/* Controller is initially clocked with 16 MHz (via PIOSC) */
/* !!! Changing this macro does not change clock speed !!! */
#define F_CPU (16000000)
int main (void)
{
uint32_t ui32Loop;
uint8_t ui8button;
uint8_t ui8toggle = (1<<1);
/* Activate GPIO ports */
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R12 | SYSCTL_RCGCGPIO_R8;
/* Set pin 0 of GPIO port J to digital input */
GPIO_PORTJ_AHB_DEN_R |= (1<<0);
GPIO_PORTJ_AHB_DIR_R &= ~(1<<0);
// Enable the GPIO port that is used for the on-board LED.
SYSCTL_RCGCGPIO_R = SYSCTL_RCGCGPIO_R12|SYSCTL_RCGCGPIO_R8;
/* Enable pull-up for button on pin 0 of port J */
GPIO_PORTJ_AHB_PUR_R |= (1<<0);
// Enable the GPIO pin for the LED (PN0). Set the direction as output, and
// Enable the GPIO pin for digital function.
GPIO_PORTN_DIR_R = 0x01;
GPIO_PORTN_DEN_R = 0x01;
//Digital Enable for PortJ USR Switch1
GPIO_PORTJ_AHB_DEN_R = 0x01;
//Enable Pull Up for PortJ USR Switch1
GPIO_PORTJ_AHB_PUR_R = 0x01;
// Loop forever.
//
while(1)
{
//
// Toggle LED if USR1 is Pressed.
//
GPIO_PORTN_DATA_R ^= ~(GPIO_PORTJ_AHB_DATA_R) & 0x01;
/* Set pins 0 & 1 of GPIO port N to digital output */
GPIO_PORTN_DEN_R |= (1<<1) | (1<<0);
GPIO_PORTN_DIR_R |= (1<<1) | (1<<0);
//
// Delay for a bit.
//
for(ui32Loop = 0; ui32Loop < 400000; ui32Loop++);
while(1) {
/* Toggle bit 1 in variable ui8toggle */
ui8toggle = ui8toggle ^ (1<<1);
/* Write value of ui8toggle to pin 1 of port N */
HWREG(GPIO_PORTN_DATA_BITS_R + (1<<1)) = ui8toggle;
}
/* Read value of pin 0 on port j into variable */
ui8button = HWREG(GPIO_PORTJ_AHB_DATA_BITS_R + (1<<0));
/* Set pin 0 of port N accordingly */
HWREG(GPIO_PORTN_DATA_BITS_R + (1<<0)) = ui8button;
/* Delay for *approximately* 0.5 s by counting to 615385:
* 615385 = 16M cycles per second / 2 / approximately(!) 13 CPU cycles per loop iteration on average.
* The CPU cycles per iteration vary widely, e.g., due to compiler and hardware optimizations! */
for(ui32Loop = 0; ui32Loop < 615385; ui32Loop++)
__nop(); /* "No operation", prohibits possible loop elimination by the compiler. */
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment