Atelier 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Atelier 1: 3 led

Description: This is a LED blinker using the on-board RGB LED and switches in the

// Tiva C Launchpad board. When no switches are pressed, the blue LED stays on. When SW1

// is pressed, the blue and green LEDs will blink. When SW2 is pressed, the red and blue

// LEDs will blink.

#include "stdint.h"

#include "C:/Keil_v5/tm4c123gh6pm.h"

//define easy to read names for registers

#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, SW2; // input from PF0 and PF4

// Function Prototypes

void PortF_Init(void);

void Delay(void);

// 3. Subroutines Section

int main(void){

PortF_Init(); // Call initialization of port PF0-PF4


while(1){

SW1 = GPIO_PORTF_DATA_R&0x10; // read PF4 into SW1

SW2 = GPIO_PORTF_DATA_R&0x01; // read PF0 into SW2

if(SW1 == 0x00){ // zero means SW1 is pressed

GPIO_PORTF_DATA_R = 0x08; // LED is green

else if(SW2 == 0x00){ // 0x10 means SW1 is not pressed

GPIO_PORTF_DATA_R = 0x02; // LED is red

Delay(); // wait 0.1 sec

GPIO_PORTF_DATA_R = 0x04; // LED is blue

Delay(); // wait 0.1 sec

// Subroutine to initialize port F pins for input and output

// PF4 and PF0 are input SW1 and SW2 respectively

// PF3,PF2,PF1 are outputs to the LED

void PortF_Init(void){

volatile unsigned long delay;

SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock

delay = SYSCTL_RCGC2_R; // reading register adds a 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 = 0x0E; // 5) PF4,PF0 input, PF3,PF2,PF1 output

GPIO_PORTF_AFSEL_R = 0x00; // 6) no alternate function

GPIO_PORTF_PUR_R = 0x11; // enable pullup resistors on PF4,PF0

GPIO_PORTF_DEN_R = 0x1F; // 7) enable digital pins PF4-PF0

}
void Delay(void){unsigned long volatile time;

time = 727240*200/91; // 0.1sec

while(time){

time--;

Atelier 2 : Led blink


One red led blink on and off

#include "stdint.h"

#include "C:/Keil_v5/tm4c123gh6pm.h"

#include "TM4C123.h"

void delay_ms(uint32_t ms) {

// Simple delay function

uint32_t i, j;

for (i = 0; i < ms; i++)

for (j = 0; j < 20000; j++) {}

int main(void) {

// Enable the GPIOF peripheral clock

SYSCTL->RCGCGPIO |= (1U << 5); // Enable clock to GPIOF

// Wait until the GPIOF peripheral is ready

while (!(SYSCTL->PRGPIO & (1U << 5))) {}

// Configure PF1 (LED) as output

GPIOF->DIR |= (1U << 1); // Set PF1 as output


GPIOF->DEN |= (1U << 1); // Enable digital function for PF1

while (1) {

// Toggle PF1 (LED) on and off

GPIOF->DATA ^= (1U << 1);

delay_ms(1000); // Delay for 1 second

Atelier 3: one switch one led


One switch control one led on and off

#include "stdint.h"

#include "C:/Keil_v5/tm4c123gh6pm.h"

#include "TM4C123.h"

void delay_ms(uint32_t ms) {

// Simple delay function

uint32_t i, j;

for (i = 0; i < ms; i++)

for (j = 0; j < 20000; j++) {}

int main(void) {

// Enable the GPIOF peripheral clock

SYSCTL->RCGCGPIO |= (1U << 5); // Enable clock to GPIOF

// Wait until the GPIOF peripheral is ready

while (!(SYSCTL->PRGPIO & (1U << 5))) {}

// Configure PF0 (Switch) as input


GPIOF->DIR &= ~(1U << 0); // Set PF0 as input

GPIOF->DEN |= (1U << 0); // Enable digital function for PF0

GPIOF->PUR |= (1U << 0); // Enable pull-up resistor for PF0

// Configure PF1 (LED) as output

GPIOF->DIR |= (1U << 1); // Set PF1 as output

GPIOF->DEN |= (1U << 1); // Enable digital function for PF1

while (1) {

// Check the state of PF0 (Switch)

if ((GPIOF->DATA & (1U << 0)) == 0) {

// Switch is pressed, turn on PF1 (LED)

GPIOF->DATA |= (1U << 1);

} else {

// Switch is not pressed, turn off PF1 (LED)

GPIOF->DATA &= ~(1U << 1);

delay_ms(100); // Delay for 100 milliseconds

//red blue #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 PF4 (*((volatile unsigned long *)0x40025040))

#define PF3 (*((volatile unsigned long *)0x40025020))

#define PF2 (*((volatile unsigned long *)0x40025010))

#define PF1 (*((volatile unsigned long *)0x40025008))

#define PF0 (*((volatile unsigned long *)0x40025004))

#define GPIO_PORTF_DR2R_R (*((volatile unsigned long *)0x40025500))

#define GPIO_PORTF_DR4R_R (*((volatile unsigned long *)0x40025504))

#define GPIO_PORTF_DR8R_R (*((volatile unsigned long *)0x40025508))

#define GPIO_LOCK_KEY 0x4C4F434B // Unlocks the GPIO_CR register

#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))

#include <stdint.h>

#include <C:/Keil_v5/inc/tm4c123gh6pm.h>

void PortF_Init(void){ volatile unsigned long delay;

SYSCTL_RCGC2_R |= 0x00000020; // 1) activate clock for Port F

delay = SYSCTL_RCGC2_R; // allow time for clock to start

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

unsigned long Led;

void Delay(void){unsigned long volatile time;

time = 145448; // 0.1sec

while(time){

time--;
}

int main(void){

PortF_Init(); // make PF1 out (PF1 built-in LED)

while(1){

GPIO_PORTF_DATA_R = 0x02; // LED is red

Delay();

Led = GPIO_PORTF_DATA_R; // read previous

Led = Led^0x02; // toggle red LED, PF1

GPIO_PORTF_DATA_R = Led; // output

Delay();

GPIO_PORTF_DATA_R = 0x04; // LED is blue

Delay();

GPIO_PORTF_DATA_R = 0x00; // LED is off

Delay();

GPIO_PORTF_DATA_R = 0x08; // LED is green

Delay();

GPIO_PORTF_DATA_R = 0x00; // LED is off

Delay();

// Color LED(s) PortF

// dark --- 0

// red R-- 0x02

// blue --B 0x04

// green -G- 0x08

// yellow RG- 0x0A

// sky blue -GB 0x0C

// white RGB 0x0E

// pink R-B 0x06


#include <stdint.h>

#include "TM4C123GH6PM.h"

#include <math.h>

#define PI 3.14159265358979323846

// Define GPIO pins

#define SW1_PIN (1U << 4) // PF4


#define SW2_PIN (1U << 0) // PF0

#define RED_LED_PIN (1U << 1) // PF1

#define BLUE_LED_PIN (1U << 2) // PF2

#define GREEN_LED_PIN (1U << 3) // PF3

static uint32_t gpiodata;

int main() {

GPIOF->LOCK = 0x4C4F434B; // Unlock GPIOCR register

GPIOF->PUR = SW1_PIN | SW2_PIN; // Enable Pull Up resistor for PF0 and PF4

GPIOF->DIR = BLUE_LED_PIN | RED_LED_PIN | GREEN_LED_PIN; // Set PF1, PF2, PF3 as output pins

GPIOF->DEN = SW1_PIN | SW2_PIN | RED_LED_PIN | BLUE_LED_PIN | GREEN_LED_PIN; // Digital


enable for PF0, PF1, PF2, PF3, PF4

GPIOF->CR = 0x1F; // Allow changes to PF4-0

SYSCTL->RCGCGPIO |= 0x20; // Enable clock to GPIOF

double time = 0.0; // Temps pour simuler une variation sinusoïdale

double frequency = 0.1; // Fréquence de la sinusoïde

while (1) {

// Simulation d'une variation sinusoïdale

gpiodata = (uint32_t)(sin(2 * PI * frequency * time) * 0xFFFF);

// Incrémentez le temps

time += 0.01;

if ((GPIOF->DATA & (SW1_PIN | SW2_PIN)) == SW2_PIN) // Check if SW2 is pressed

GPIOF->DATA = RED_LED_PIN; // Red LED on

} else if ((GPIOF->DATA & (SW1_PIN | SW2_PIN)) == SW1_PIN) // Check if SW1 is pressed

GPIOF->DATA = BLUE_LED_PIN; // Blue LED on

} else {
GPIOF->DATA = GREEN_LED_PIN; // Green LED on

gpiodata = GPIOF->DATA;

You might also like