Register configuration and Program for LED blinking in LPC1768

Topic Progress:

Register configuration for LED blinking in LPC1768

[fusion_builder_container hundred_percent=”no” hundred_percent_height=”no” hundred_percent_height_scroll=”no” hundred_percent_height_center_content=”yes” equal_height_columns=”no” menu_anchor=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” background_color=”” background_image=”” background_position=”center center” background_repeat=”no-repeat” fade=”no” background_parallax=”none” enable_mobile=”no” parallax_speed=”0.3″ video_mp4=”” video_webm=”” video_ogv=”” video_url=”” video_aspect_ratio=”16:9″ video_loop=”yes” video_mute=”yes” video_preview_image=”” border_size=”” border_color=”” border_style=”solid” margin_top=”” margin_bottom=”” padding_top=”” padding_right=”” padding_bottom=”” padding_left=””][fusion_builder_row][fusion_builder_column type=”1_1″ layout=”1_1″ spacing=”” center_content=”no” link=”” target=”_self” min_height=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” background_color=”” background_image=”” background_position=”left top” background_repeat=”no-repeat” hover_type=”none” border_size=”0″ border_color=”” border_style=”solid” border_position=”all” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” dimension_margin=”” animation_type=”” animation_direction=”left” animation_speed=”0.3″ animation_offset=”” last=”no”][fusion_text]The control of the LED circuit is through the GPIO pin. As a programmer, one must know how to configure the registers in order to accomplish this task.

Register configuration can be divided into two parts.

  1. Control Configuration: The programmer must identify the registers that handle the control of the respective peripheral.
    • FIOPIN (Fast Input Output Pin) Register: Each of the pins on the controller has multiple functionalities. These functionalities are multiplexed onto each of the microcontroller pins. The programmer needs to assign the appropriate peripheral functionality to each of these pins. This will be discussed in later topics. For now, all that you must understand is that the default functionality for the pins will be the GPIO functionality.
    • FIODIR (Fast Input Output Direction) Register: The GPIO peripheral can be input or output. The direction of the signal flow must be specified before using the pins.
      Writing 0 to the respective bits on the FIODIR register sets the corresponding pin in the input mode and writing 1 sets it in the output mode.
  1. Data Configuration: Once the control is set, the respective pins are ready to be used for data transmission. Here we have the set of registers that handle the GPIO data transmission.
    • FIOSET (Fast Input Output Set) Register: To set a pin as HIGH, 1 must be written on the respective pin. Writing a 0 has no effect.
    • FIOCLR (Fast Input Output Clear) Register: To clear a pin, 1 must be written on the respective pin. Writing a 0 has no effect.

In the LPC176x library for the Cortex M3 architecture, each of the registers have been defined under different structures. Therefore, to access any registers, you need to point to the location of their definitions under these structures.

Example: The above mentioned registers fall under the GPIO peripheral. And the GPIO pins are categorised under a set of ports. To set the direction of ‘PIN m’ of ‘PORT n’ as OUTPUT, the following operation must be done.

LPC_GPIOn->FIODIR |= (1<<m);               // More about these operations will be discussed in the next section.

[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

Programming the LPC1768 for LED blinking

[fusion_builder_container hundred_percent=”no” equal_height_columns=”no” menu_anchor=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” background_color=”” background_image=”” background_position=”center center” background_repeat=”no-repeat” fade=”no” background_parallax=”none” parallax_speed=”0.3″ video_mp4=”” video_webm=”” video_ogv=”” video_url=”” video_aspect_ratio=”16:9″ video_loop=”yes” video_mute=”yes” overlay_color=”” video_preview_image=”” border_size=”” border_color=”” border_style=”solid” padding_top=”” padding_bottom=”” padding_left=”” padding_right=””][fusion_builder_row][fusion_builder_column type=”1_1″ layout=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” border_position=”all” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” center_content=”no” last=”no” min_height=”” hover_type=”none” link=””][fusion_text]

IMPORTANT TIPS: There is an important point to be remembered while writing data onto a register. Each of the registers will have a maximum of 32 bits and there is a manner in which these registers must be handled.

  1. Keep in mind that, while setting a bit on the register, do not use the assignment operator alone.

Example: Assume that we are setting the direction of pin 0 of port 0 as OUTPUT. (Pin naming starts from 0 and ends at 31)

LPC_GPIO0->FIODIR = 0x00000001;                         // This will set the direction of the desired pin.

BUT, imagine that at a later step, you are required to set the direction of pin 1 of port 0 as OUTPUT keeping pin 0 in the OUTPUT mode itself. In that case, if you perform the same operation as before;

LPC_GPIO0->FIODIR = 0x00000002;

This will effectively set pin 1 of port 0 as OUTPUT, but it will clear the previously stored data on bit 0 of the FIODIR register, configuring it as an INPUT pin.

The reason for this is that when you assign the value 0x00000002, you are assigning bit 0 with the value 0.

To avoid this, use the OR operator along with the assignment operator.

i.e. LPC_GPIO0->FIODIR |= 0x00000001;
LPC_GPIO0->FIODIR |= 0x00000002;

This preserves the previously set values.

  1. In a similar manner, to clear a data from a bit in a register, do not assign a value straight away.

Use the AND operator and the bit-wise negation operator.

Example: To set the pin 1 of port 0 in the input mode (from the above example), use the following method.

LPC_GPIO0->FIODIR &= ~(0x00000002);                                //Work this out.

This preserves the values of the other bits.

  1. Finally, instead of writing the complete 32-bit hex decimal value, you can use the shifting operator.
Example: LPC_GPIO0->FIODIR |= (value << corresponding bit); //Value shifted to the corresponding bit
i.e. Instead of LPC_GPIO0->FIODIR |= 0x00000001;
You can write LPC_GPIO->FIODIR |= (1 << 0);
Similarly, instead of LPC_GPIO0->FIODIR &= ~(0x00000002);
You can write, LPC_GPIO0->FIODIR &= ~(1 << 1);

Code Snippet: Blink an LED using PIN0 of PORT0.

#include
void Delay( int delay )
{
  int i, j;
  for(i=0; i<1024; i++)
    for(j=0; j<delay; j++);
}

int main()
{
  //Set the direction of the pin
  LPC_GPIO0->FIODIR |= (1<<1);
  while(1)
  {
    LPC_GPIO0->FIOSET = (1<<0);
    Delay(50);
    LPC_GPIO0->FIOCLR = (1<<0);
    Delay(50);
  }
}

OBSERVATION: You might be wondering why the OR operator was not used for the FIOSET and FIOCLR register. We had already discussed that writing 0s to either of these registers has no effect. The reason for this emphasizes the main purpose of having two separate registers for data transmission. The OR and AND operators are only important while handling registers that handle both the setting and clearing operations like the FIODIR register.

[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]