Thursday 30 April 2015

[Freescale K20D50M MCU Programming] #4 External LED controlled by a GPIO Switch

#4 External LED  controlled by a GPIO Switch

We tried blinking of an LED. Now we will create a switch using one of the GPIO Pins.  The Reset Button in FRDM-K20D50 does not have an alternate function. Hence I wanted to try out a Input switch.

Take a look at the [Freescale K20D50M MCU Programming] #1 Helloworld on the Console using Processor Expert  for quickstart 

Lets get started.


First we need 2 things. 


  • Identify the PIN for the external LED and a ground pin
  • then another PIN to act as a Switch Input and a ground pin

I identified PTC0  and PTB2 respectively.






to see the schematic to identify the PINS.  There is a good youtube video which gives an example on K25Z board.


Next we will be adding the BITIO_LDD for the LDD configured to the PTC0 PIN.




Then a BITIO_LDD for the switch on PTB2



Setting up the BITIO_LDD isn't enough for the acting with in PTB2.  We need to initialize the GPIO for the PIN. We will add a init_GPIO component and configure it to PTB




Generate the Pex code, and then add then lets add the below logic to toggle the led based on whether a wire between PTB2 and the GRND is connected or not..


The Pin's are connected to cathode, so 0 means high and 1 means low.

Code in main.c 

  for(;;)
  {
  if(SW1_GetVal(NULL)==0)   // CONNECTED 
  {
  led_SetVal(led_DeviceData);
  }
  else
  {
  led_ClrVal(led_DeviceData);
  }
  WAIT1_Waitms(1000);
  }


Now build the code and lets test it. 

Here is how the pins are connected on the board




Now lets see what happens with ehe pins at PTB0 is connected, the red wires on the top side is connected.. and you see the LED glowing


Now lets see what happens when one of the wire on top is pulled out..

Do you see only 1 wire on top is connected, and the LED is off.

This is an example of how you use a GPIO pin as a switch to control through software.



Wednesday 22 April 2015

[Freescale K20D50M MCU Programming] #3 Blinky GPIO - External LED

#3  Blinky External LED 


In [Freescale K20D50M MCU Programming]  #2 Blinky Red LED  onboard, We tried blinky with onboard LED. Lets try out blinking the external LED connected to a GPIO pin.. We are going to use Codewarrior and processorexpert with FRDM-K20D50M board

Take a look at the [Freescale K20D50M MCU Programming] #1 Helloworld on the Console using Processor Expert  for quickstart  

Lets get started.

First we need to see the schematic to identify the GPIO pin for our external LED.  There is a good youtube video which gives an example on K25Z board.






and the pin connectivity as below





Now Lets create a new project as mentioned in [Freescale K20D50M MCU Programming]  #2 Blinky Red LED  onboard we add a BITIO_LDD Component







And setting the properties to PTD0




I have added a WAIT custom component from processor expert to show a delay


And add this code in main.c

  gpio_led_NegVal(NULL);
  WAIT1_Waitms(1000);

  gpio_led_NegVal(NULL);

And you see the external LED Blinking.





Wednesday 15 April 2015

[Freescale K20D50M MCU Programming] #2 Blinky Red LED onboard

#2  Blinky Red LED onboard


Lets try out blinking the RED onboard LED. We are going to use Codewarrior and processorexpert with FRDM-K20D50M board

Take a look at the [Freescale K20D50M MCU Programming] #1 Helloworld on the Console using Processor Expert  for quickstart 

Lets get started.


First we need to see the schematic to identify the onboard LED.  There is a good youtube video which gives an example on K25Z board.




LED  Schematic :





Red LED connected to D6.
Green LED connected to D3
Blue LED connected to D9



Identify the Ports and Pins for the LED's :



Red, look for D6





Green, Look for D3



Blue, Look for D9








The FRDM-K20D50 CPU uses the Blue LED. So, this has to be disabled to get this on-board LED to work.




Then Lets add BITIO_LDD component, 1 for each LED to control them.

Now lets setup the Bit_IO pins for the 3 LED's.  LED Cathode is connected to the microcontroller pin. i.e., Low Level (0) turns it on, and (1) turns it off.   By default we need to set all the 3 LED's to (1) to turn it off.


WE add 3 BitIO_LDD Components, for RED, Green and Blue. 




Green LED -> Switch off by Default. Cathode connected. So 1 is Off


Blue LED -> Switch off by Default. Cathode connected. So 1 is Off. But this pin is used by CPU, so enable PIN Sharing.




Red LED -> Switch ON by Default. Cathode connected. So 0 is On.



Now build and Run the Main.c  you will see the RED LED glowing by the code is flashed.







Now lets make a blinky. :-)


We need to enable the NegVal() method of the BitIO_LDD






And add this code in the main.c or processorexpert.c

int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  for(;;)
  {
 int i =0;
 for(i=0;i<=1000000;i++)
 {
 }
 RED_LED_NegVal(NULL);  // Blink the RED Led
  }
  

Here is the blinky.



CodeWarrior, PEX Project :   Download  onboard_blinky.zip

Saturday 4 April 2015

[Freescale K20D50M MCU Programming] #1 Helloworld on the Console using Processor Expert.

Just starting exploring the world of MCU programming.  I was referred a freescale board K20D50M




Being a hardcore java/j2ee developer, Looking at this was little scary.   Followed the steps in QuickStart package in a OpenSDA Mode.  Now comes the time to write some code. I was told that Processor Expert in Codewarrior can do wonders. You do not need to be a hardcore Embedded developer who knows Bits/Registers/MUXing. ;-)

As a beginner. Codewarrior is the best place to start with. Code, test the device from one place. Later you can jump into the Processor Expert Driver suite to write code, while to test you need to use IAR workbench for Kinetics or a Kinetics Design Studio.


Tools you need 

1. FRDM-K20D50M board
2. Codewarrior or other similar tools
3. Realterm - Terminal viewer tool


Assuming, you got your openSDA configuration done on the bootloader mode.  
Lets get into action. 


Create a Bareboard Project -



Name the project


Select the device - K20D50 - MK20DX128:





Select Connections :




Build and Debug Selection:



RAD :



FINISH


CodeWarrior Perspective :


This is what you will see in your codewarrior. You can read more about the perspectivies, look for videos in Youtube if you are not  used to the eclipse environment.




Now comes the real usecase to print Helloworld  by writing code, run it on the freescale board, and see it on the terminal.

Add the ConsoleIO component to the project:



Component (Advanced) Properties:




Serial_LDD  Changes :

You need to change the Serial to UART0.  Lets check the schematic pdf for their pins




Now lets fix the errors in the properties by setting the in and out appropriately, and the baud rate error will be gone.


Locate the main.c:




Add your Printf :




Lets build :




Run it : 




If device/board is not connected to your USB.



Connect, Refresh and Retry:



Console Output: 



Realterm Configuration:



Realterm Output Configuration:




Realterm Output when you run the program. Press the reset button, you will see the program restarting.


You can add the printf in a for loop.
there you go! The 1st MCU Hello world is done.