Blinky program learning notes

I googled a couple of Cotrex Mx blinky programs and read them quickly.  I found most of them unfriendly to newbies.  I googled on and at last found one tutorial which looks friendly to beginners.  So I cut and pasted some paragraphs here.

The example uses LCP1343, but I think the general idea should be applicable to LPC1114.

LPC1343 Description

http://www.keil.com/dd/chip/4919.htm





ARM Cortex TUTORIALS What is ARM microcontroler and How to start

http://armcortex.net/?page_id=190

Basic explanation C code and LPC1343

Considering that you can find a lot of great websites, books and tutorials about C code we will not focus on that. You will get here a few basic commands to understand our tutorials.

Comments

Advice for all beginners are Comments. When you will start writing code and the whole file will have 10 lines, there is no problem to understand why something was written. But you will see how fast your knowledge and projects will growing and code will rise. In our tutorials you will find a lot of comments for easy and better understanding. Here you can see two examples how to comment in code:

LPC_GPIO0->DATA &= ~(1<<8);   //This is comment and we use it for one line

From // nothing written in this line will be compiled

/*

while (1) {            /* Endless loop*/

LPC_GPIO0->DATA &= ~(1<<8);   /* &= (IN)  Insert 0 in 8 bit of registerDATA  turn 
                              on LED 2 */ 
   for (i=0;i<1000000;i++); 
LPC_GPIO0->DATA |=  (1<<8);   /* |= (OR)  Insert 1 in 8 bit of register DATA  turn 
                              off LED2 */ 
   for (i=0;i<1000000;i++); 
}

*/

Nothing between /**/ will be compiled and it is used for comment more lines at the same time

Basic operations which can be expected from our microcontroller are sending and receiving data on certain programmed pins. If you would like to have deep knowledge how microcotroller works and you are not lazy, take your time and read manuals. For LPC1343  you can find  manuals HERE

Microcontroller LPC 1343 has 48 pins

Pins are allocated in four general input-output (I/O) units, named PIO0, PIO1, PIO2, PIO3  and others. Each I/O unit have different number of pins:

PIO0 have 12 pins labeled from 0-11: POI0_0 to PIO0_11.

PIO1  have 12 pins labeled from 0-11: POI1_0 to PIO1_11.

PIO2 have 12 pins labeled from od 0-11: POI2_0 do PIO2_11.

PIO3 have 4 pins labeled with 0-3: POI3_0 do PIO3_3.

4 are intendent for main power – 2x Vdd (+3,3V), 2x Vss (0V),

2 pins are intendent for crystal

2 pins are intendent for USB signals DM in DP

Each pin can perform various functions. All functions can be found  in manuals. Now we would like to achieve that microcontroller sends some data to one pin belong to some unit. We have to set unit appropriate. 

Operation of individual units can be controlled by registers named GPIO0 – GPIO3

For individual I/O unit we can use more GPIO registers used for different functions for single pin. Through individual GPIO register pin we can set how single pin acts.

For GPIO we can use GPIO0DATA or GPIO0DIR and others which are not interesting for our tutorials.

GPIO0DATA – this is a register that provides which information is entered on individual pin PIO0 I/O unit(1 or 0). When an individual pin is set for input or some other function value in this register shall not affect him.

GPIO0DIR – this is a register that provides  if individual pin PIO0 I/O unit is input or output. 0 is input, 1 is output.

Example how to set individual bit:

LPC_GPIO0->DATA |=  (1<<8);

This command set bit to 1 on PIO0 pin 8

How this works:

(1<<8) mean that the lowest bit in temporary register will be set to 1 and offset for 8 places to the left

We get:

0000 0000 0000 0001 -> enter 1 at the lowest bit register

0000 0001 0000 0000 -> offset for 8 places to the left

|= means that the value obtained in the temporary register is added to the existing value in the registry, which is addressed (GPIO0DATA). »| » is character for binary OR

If the values in  GPIO0DATA are:
0000 0000 0000 0000
0000 0001 0000 0000 | (or)
————————–
0000 0001 0000 0000 -> result in register GPIO0DATA

The process described above has changed only one bit in the register GPIO0DATA.


How do you delete specified bit?

The process is similar, only a few letters are added:


LPC_GPIO0->DATA &= ~(1<<8)

sign ~ means that the value is inverted by the data

0000 0001 0000 0000 get
1111 1110 1111 1111

So we get, if the GPIO0DATA is:

1110 1001 0110 0000
1111 1110 1111 1111 & (and)
————————-
1110 1000 0110 0000 -> result in register GPIO0DATA

The process described above has changed only one bit in the register GPIO0DATA.

If we set data in register we can’t see any visual effects thus here is a little code which can be compiled in our first project.

view source

#include "LPC13xx.h"      /* LPC13xx definitions       */

int main (void) 
  {         /* main loop */
  LPC_GPIO0->DIR |= (1<<8);   /* |= (OR) Enter 1  in 8 bit of register DIR 
  (LED=P0.8) Setting the direction for Port P0(GPIO0) P0=output   */

  while (1) 
    {            /* endless loop*/
    LPC_GPIO0->DATA &= ~(1<<8);   /* &= (AND)  Enter 0 in 8 bit of register DATA   
    turn on LED 2 */
    }
}

I hope that it was not too much for the beginning and that everything was easy to understand. Now we can start with our first project, how to turn LED on.

.END

No comments:

Post a Comment