Adsense

Write the simple switch and LED interfacing code in Embedded C on KL25Z FRDM board.

// In this experiment single switch taken from the 16 switches of 4X4 matrix keypad

//4 columns are connected to the 4-7 as I/P and 4 rows are connected to the 0-3 as O/P pins of the //PORTC

// blue LED connected on the PORTD pin1, will lit whenever configured switch is pressed

#include <MKL25Z4.H>

//user define function to provide a delay in micro second
void delayUs(int n);

//function to detect the switch
char Switch_detect(void);

int main(void)
{
    SIM->SCGC5 |= 0x1800;       /* enable clock to Port C and D  */
        
        //initialization switch 1 from matrix keypad
        // PTC0 and PTC4
        PORTC->PCR[0] = 0x103;      /* make PTC0 pin as GPIO and enable pullup*/
        PORTC->PCR[4] = 0x103;      /* make PTC4 pin as GPIO and enable pullup*/
        PTC->PDDR = 0x01;         /* make PTC0 as output pin and PTC4 input pin*/
    
        //PortD pin 1 configure for blue LED
        PORTD->PCR[1] = 0x100;      /* make PTD1 pin as GPIO */
        PTD->PDDR |= 0x02;          /* make PTD1 as output pin */

    while(1)
    {
        if (Switch_detect() != 0)    /* if a key is pressed? */
            PTD->PDOR &= ~0x02;          /* turn on blue LED */
        else
            PTD->PDOR |= 0x02;          /* turn off blue LED */
    }
}

char Switch_detect(void)
{
    int col;

    PTC->PDDR |= 0x01;          /* enable row1 */
    PTC->PDOR &= ~0x01;            /* we make sure it must clear before switch detection  process*/
    delayUs(2);                 /* wait for signal return */
    col = PTC->PDIR & 0x10;     /* read column1 */
    PTC->PDDR = 0;              /* disable row1*/
    if (col == 0x10)
        return 0;               /* no key pressed */
    else
        return 1;               /* a key is pressed */
}

void delayUs(int n)
{
    int i; int j;
    for(i = 0 ; i < n; i++) {
        for(j = 0; j < 8; j++) ;
    }
}

Steps to realize basic embedded C code to blink green LED with some finite delay.

Free-scale Memory Map

Input output registers ranges from 0x400FF000 to 0x400FFFFF - we need to memory map every time when we perform simulation without kit. While flashing code into the kit memory mapping is necessary.


Memory Map of GPIO

Each port registers description is given below please refer figures for the same.

 


 

GPIO Programming and Interfacing

Port Data Output Register (GPIOx_PDOR): In the case of Freescale ARM chip, each of the Direction register needs to be a 0 to configure the port pin as input and a 1 as output.

 

 

The GPIO Clock enable for the I/O ports


 

 

Alternate pin functions and the simple GPIO

 

Portx_PCR of each port will be used for the configuration of the alternate functions bit 8-10 decides mux value and it will decide the function mentioned below.  

 

 

 Alternate functions and their descriptions.

 

 

 

LED connection in Freescale FRDM board

 

 

(1) Enable the clock to PORTB by setting 1 to SCGC5 register bit no 10, since access is denied to the port registers until the clock is enabled,

(2) Configure PortB_PCR19 (Pin Control Register) to select GPIO function for PTB19, by setting mux field (bit no 8 to 10) of the PCR19 register to 001.

(3) Set the Direction register bit 19 of PTB as output. by setting values 1 to bit 19 on PDDR register.

(4) Write HIGH to PTB19 in data register, to turn off the LED.

(5) Call a delay function,

(6) Write LOW to PTB19 in data register, to turn on LED.

(7) Call a delay function,

(8) Repeat steps 4 to 7.

 

 

Write a ARM cortex M0+ assembly language code based on arithmatc and logical instructions.

 Problem 1: Implement following code conversions to convert binary no 0x89ABCDEF(32-bits) into a) BCD (64-bits)   b) Gray (32-bits) Problem...