Akash Kollipara
Published © GPL3+

Project 2 - Flashing all onboard-LED colour combinations

TM4C123GXL LED Color change sequential algorithm

BeginnerFull instructions provided3,399
Project 2 - Flashing all onboard-LED colour combinations

Things used in this project

Story

Read more

Code

Header file for "BIT"

C Header File
/*
 * Library for extracting bits
 * Author: Akash Kollipara
 * Date: 19-Dec-2017
 */
 
#ifndef BIT
#define BIT

#define bit0(x) (x & 0x01)
#define bit1(x) (x & 0x02)
#define bit2(x) (x & 0x04)
#define bit3(x) (x & 0x08)
#define bit4(x) (x & 0x10)
#define bit5(x) (x & 0x20)
#define bit6(x) (x & 0x40)
#define bit7(x) (x & 0x80)
#define bit8(x) (x & 0x0100)
#define bit9(x) (x & 0x0200)
#define bit10(x) (x & 0x0400)
#define bit11(x) (x & 0x0800)
#define bit12(x) (x & 0x1000)
#define bit13(x) (x & 0x2000)
#define bit14(x) (x & 0x4000)
#define bit15(x) (x & 0x8000)

#endif

Source Code: TIVA - L2 - LED COLOR FLASH

C/C++
/*
 * main.c
 *
 * Author: Akash Kollipara
 * Date: 18-Dec-2017
 *
 * Note: Flash all combinations of colors using on board LED
 */

#include <stdint.h>     //library file for datatypes
#include "bit.h"

//---SYSTEM CONTROL REGISTERS---//
#define SYS_CTRL_RCGC2  (*((volatile unsigned long *)0x400FE108))   //offset of RCGC2 register is 0x108
#define CLK_GPIOF   0x20

//---GPIO-F REGISTER---//
#define PORTF_DATA  (*((volatile unsigned long *)0x40025038))   //offset of DATA register for PF1, PF2, PF3 is 0x38 [PF7:PF0::9:2]
#define PORTF_DIR   (*((volatile unsigned long *)0x40025400))   //offset of DIR register is 0x400
#define PORTF_DEN   (*((volatile unsigned long *)0x4002551C))   //offset of DEN register is 0x51C

//---PORT-F I/O---//
#define PF1 0x02
#define PF2 0x04
#define PF3 0x08

//---FUNCTION PROTOTYPE---//
void delay(unsigned long);

void main(void)
{
    uint8_t i = 1;
    SYS_CTRL_RCGC2 |= CLK_GPIOF;
    PORTF_DIR |= 0x0000000E;    //set PF1, PF2, PF3 as output
    PORTF_DEN |= 0x0000000E;    //enable PF1, PF2, PF3
    PORTF_DATA = 0;
    while(1)
    {
        PORTF_DATA = (i << 1);
        //i = (i > 7) ? 1 : (i + 1);     //2 because PF1 has 2^1 value
        //i = (bit0(~i) | bit1(~i)) << 1 | bit2(~i) >> 2; //r,y,g,i,b,v
        i = (bit2(~i) >> 2) | (((bit2(~i) >> 1) & bit1(i) | (bit2(i) >> 1) & (bit0(~i) << 1))) | ((bit2(i) & (bit1(~i) << 1) | bit2(~i) & (bit0(i) << 2))); //flash all combinations

        delay(500000);
    }
}

void delay(unsigned long count)
{
    unsigned long i=0;
    for(i=0; i<count; i++);
}

Credits

Akash Kollipara

Akash Kollipara

10 projects • 20 followers
Seeker • Engineer • Thinker I am Electronics and Embedded Systems enthusiast driven towards making things simple and best!

Comments