Blink an LED explained in easy way for quick understanding (using embedded C language)      

Before we go in detail of how to blink an LED(light emitting diode) with microcontroller, let us have a brief  introduction on GPIO (general purpose I/O (input / Output)).

Intro to GPIO

The GPIO line of any microcontroller can be used mainly to perform two things

1. Generate a digital signal

2. Read a digital signal

The digital signal can be of TTL / CMOS logic depending up on the microcontroller .In general, the microcontrollers are provided with GPIO lines except for some microcontrollers which are very application specific and may not have GPIO lines.

The process of controlling GPIO lines for most of the controllers are similar in nature. There are mainly two configurations for any GPIO

  • GPIO configured as an input
  • GPIO configured as an output

The GPIO which is configured as input can read a digital signal from an external device, the external device can be a battery charge indicator, signal from a sensor or any device   which you intend to use in performing certain functions. Same way  when you wish to generate a digital signal then the GPIO will be configured as an output and the GPIO output can generate a digital signal, which can be fed to an external device.

Let us see an example to understand a practical application of a GPIO.

Example: Design an indicator for a battery charger using GPIO

Now let us assume that there is a circuitry which gives a digital signal to microcontroller when ever the battery is charged. So, microcontroller reads the digital signal from the battery charger  and then generates a digital signal to blink an LED indicator.

GPIO configuration procedures

The configuration of GPIO for various microcontrollers have almost similar procedure, but the procedure is not completely same for all microcontrollers. There may be few more steps to be considered, depending on the microcontroller. In this document the common configuration procedure is explained.

Steps to generate a digital high using GPIO

Step 1: Configure the GPIO bit direction register as output

Step 2: Set the GPIO bit data/port register as high

Steps to read  digital signal using GPIO

Step 1: Configure the GPIO bit direction register as input

Step 2: Read the GPIO bit data/port register

Example program to blink an LED(Code implemented with  microcontroller atmega8535)

#include <io.h>

/* This program is compiled using  WINAVR platform*/

/* Assumptions

PORT B0 pin of the microcontroller atmega8535 is connected to an LED circuit */

int  main()

{

unsigned int i =0;

/* Configure the GPIO B0 bit direction register as output */

DDRB = 0x01;

while(1)

{

for (i=0;i<50000;i++)

{

/* Set the GPIO bit data/port  register as low */

PORTB = 0x00;

}

for (i=0;i<50000;i++)

{

/* Set the GPIO bit data/port  register as HIGH */

PORTB = 0x01;

}

}

return 0;

}

/* End of Program */

Result

  LED blinks continuously as long as the microcontroller is powered ON.

Advertisements

change the baudrate of HC-05 Bluetooth module in an easy way within a short time – complete procedure

If you want to change some of the settings of your HC-05 Bluetooth module, like baud rate, password or the name of the Bluetooth module, you can do it easy and fast with your Arduino.

There are different HC-05 modules on the market, but it should work with all of them. On the picture below you can see a bare HC-05 module, 5V tolerant HC-05 module and 6V tolerant HC-05 module with a button. I prefer the last one, as the button makes the changing of the settings even easier.

IMG_9290

To change any settings of the module, it should be in the so called “AT Mode”. How to put the Bluetooth module in AT Mode: The “Key Pin” of the module should be set to “High”. This means we should apply voltage to this pin at the same time we power the module. On the last module, the button does this job. It should be pressed when powering the module with 5V. On the module without button, we need to supply 3.3V to the “Key Pin” when powering on.

Here is an example how to connect the module to the Arduino Nano v3. Note that there should be an jumper on GND and RST pin. By doing this, the Arduino will only act as a simple FTDI adapter.

IMG_9291

First connect the Arduino to the PC over USB. Then connect the cables in the following way:

  • TX Bluetooth –> TX Arduino
  • RX Bluetooth –> RX Arduino
  • GND Bluetooth –> GND Arduino

… and at last the 5V Bluetooth to 5V Arduino by keeping the button of the Bluetooth pressed. Or on the module without button, connect the Key Pin Bluetooth to 3.3V on Arduino.

If the HC-05 is in AT Mode, the red LED will blink at every 2 seconds.

Then start the PuTTY program configure the right COM Port (Check it in the Device Manger) and set the Baud Rate to 38400. This is the baud rate for AT Mode. Then click “Open”.

HC-05.over.PuTTY

In the newly opened black window type “AT”, press Enter and you should get “OK” as a result. Note that you want see what you type in the windows… Here some of the commands to change the settings you wish:

Change baud rate to 57600, 1 stop bit, 0 parity AT+UART=57600,1,0
Change module name AT+NAME=YOURNAME
Change pairing code AT+PSWD=0000

Note: You can use other similar terminal applications such as teraterm, Hyperterminal, Docklight these all were supported by windows. For ubuntu users you can use gtkterm terminal.

What is a patch?

A patch is a structured file that consists of a list of differences between one set of files and another. All code changes, additions, or deletions to Drupal core and contributed modules/themes between developers are done through patches.

Patches make development easier, because instead of supplying a replacement file, possibly consisting of thousands of lines of code, the patch includes only the exact changes that were made. In effect, a patch is a list of all the changes made to a file, which can be used to re-create those changes on another copy of that file.

Difference between the const and volatile qualifier in C

Const keyword in C programming language

Constants in C can be defined using “const” qualifier. What it means is that memory location whose value cannot be changed during its life time. Lets see an example

int main ()
{
const int x = 10;
printf (“X is %d\n”, x);
x = 5;
printf (“X is %d\n”, x);
return 0;
}

You can see that the above program cannot compile. Because you are assigning value to a const more than once. As I said, its value cannot be changed. You can read the value of a constant as many times as you want, but you can assign its value only once.

Volatile keyword in C programming language

Lets move to “volatile”. To understand the use of volatile qualifier you would have to know multi-threading and some knowledge of Computer Architecture.
Let us first see, how a compiler optimizes a variable. Let us take the help of code below:

int main ()
{
int x;
x = 10;
x++;
return 0;
}

We know that a variable represents a memory location. Hence, x would have some memory location, let it be 1000.

  • The control is first at the Line 1, when compiler will allocate some memory to x, starting at location 1000.
  • Then we get to Line 2. To assign, a value to x. CPU will copy “10” to the memory location to 1000.
  • Similarly, for Line 3, CPU will add 1 to that memory location.

All above operation includes CPU operating on the memory. The problem with this approach is, it is slow, as this would include a lot of Bus communication between CPU and Processor. So, what compiler do is it do some optimizations. These optimizations includes using Registers of a CPU, operating on them and then copying the value from Register to memory when required. Registers are actually the fastest way of communication with a CPU.
So, lets see how compiler will perform with above code using registers.

  • As usual, compiler will first allocate some memory to x, starting at location 1000.
  • When, it encounters Line 2. Compiler thinks that, why use the CPU-Memory communicaton? Why not use the fastest method using Registers? So, it will actually store value 10 into a Register, say A.
  • At Line 3, as value was stored in Register A not in the memory, then it will increment the value inside the Register not memory.
  • If it wants to it can copy the value of Register A back into the memory location of x.

Now lets enter into world of MultiCore CPUs and Multithreading. In a MultiCore CPU, there are more than one core. Let us assume there are 2 Cores. Each Core has its own set of Registers. A thread is an independent unit of execution inside a process. With multithreading you can perform more than one tasks simultaneously using more than one threads.
Let us have a program.

int x;
void thread_func ()
{
for (int i = 0; i < 1000; i++)
{
x += 2;
}
}

int main ()
{
pthread_t *th1, *th2;
x = 0;
th1 = pthread_create (thread_func);
th2 = pthread_create (thread_func);
}

Pthread is a POSIX Library to create a thread. Here two threads are created. Both of them runs the same function “thread_func”. In thread_func, x is accessed here x is now global variable. Let two threads are running on two different cores. Let us assume that compiler has done the same optimizations and will use the fast method of Registers.
For Thread 1, th1:

  • At Line 1, of thread_func value of x has been assigned. This value is stored in Register A of Core 1.
  • Then the loop runs and in each iteration of loop, 2 is added to Register A.
  • At the end, 2000 will be added to x through Thread 1.

Similarly for Thread 2, 2000 will be added.
So, at the end we should get the value 4000 (2000 + 2000) isn’t it?
But we will not.
The reason being, x is a global variable and its is accessed by two Threads on two different cores. Each Core will have its own copy of x in the Register. There is no way that Core 1 could know what is the value of x in the Register of Core 2 and vice-versa. To, solve this issue “volatile” qualifier is used.

If Compiler detects a volatile variable (variable declared with “volatile” qualifier), then it will not perform any kind of optimizations over it. Every time, a Core wants to operate on x then it will have to take the value from the memory, then operate on it and then store that value back into the memory. This is the work of “volatile” keyword to tell the compiler not to do optimizations over this variable.

PS:- To get the 4000 result, you would also have to use Thread Synchronization Techniques. Volatile is mostly used with Thread Synchronization.

Difference between firmware and software

Firmware

Firmware usually refers to fixed data as part of a hardware device, Firmware is software that is programmed into chips and usually perform basic instructions, like BIOS, for various components, Network cards, computer BIOS, etc.

Firmware is also a software that’s embedded in hardware for the purpose of controlling the hardware. An example of firmware is the software embedded in a microwave oven.

Software

Software is meant to be used for interaction, productivity and activity like word processing, video editing, listening to music, or video conferencing.

Software is a program that is is designed to run on a computer’s operating system.

Difference between a router and a modem

Routers and modems are two of the most common computer peripherals, yet many people don’t know the function of each one. While the two devices may look similar, they each serve a difference purpose. Fortunately, the functions of the two devices are pretty easy to understand.

A router is a small box that allows multiple computers to join the same network (see below).

Netgear Router

While early routers provided several Ethernet ports for wired connections, most modern routers offer wireless connectivity as well. These “wireless routers” often have one or two moveable antennas on the sides, though some models house the antennas inside the enclosure. Wireless routers allow multiple computers and other devices, such as smartphones and tablets, to join the same network.

While connecting to a router provides access to a local network (LAN), it does not necessarily provide access to the Internet. In order for devices on the network to connect to the Internet, the router must be connected to a modem. Therefore, most routers have a specific Ethernet port that is designed to connect to the Ethernet port of a cable or DSL modem.

A modem is a device that provides access to the Internet (see below).

Cisco Cable Modem

The modem connects to your ISP, which typically provides either cable or DSL Internet service. Cable modems have a coaxial (or “coax”) connection, which is the same type of connector found on a TV or cable box. This connects to a cable port on the wall. DSL modems have a telephone connector, also called an RJ-11 jack, which connects to a telephone socket on the wall.

By connecting your modem to your router (instead of directly to a computer), all devices connected to the router can access the modem, and therefore, the Internet. The router provides a local IP address to each connected device, but they will all have the same external IP address, which is assigned by your ISP.

To summarize, the device connection order is outlined below:

  1. PC or wireless device
  2. Router
  3. Modem
  4. ISP
  5. Internet

While the router and modem are usually separate entities, in some cases, the modem and router may be combined into a single device. This type of hybrid device is sometimes offered by ISPs to simplify the setup process.

Difference between malloc and calloc

When calloc is used to allocate a block of memory, the allocated region is initialized to zeroes. In contrast, malloc does not touch the contents of the allocated block of memory, which means it contains garbage values. This could potentially be a security risk because the contents of memory are unpredictable and programming errors may result in a leak of these contents.

Function calloc allocates a region of memory large enough to hold “n elements” of “size” bytes each. Also initializes contents of memory to zeroes. malloc allocates “size” bytes of memory.
Number of arguments calloc has 2 arguments malloc has 1 argument
Syntax void *calloc (number_of_blocks, size_of_each_block_in_bytes); void *malloc (size_in_bytes);
Contents of allocated memory Calloc allocated region is initialized to zero. Malloc contents of allocated memory are not changed. i.e., the memory contains unpredictable or garbage values. This presents a risk.
Return value void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned. If the allocation of memory fails, a NULL pointer is returned. void pointer (void *). If the allocation succeeds, a pointer to the block of memory is returned. If the allocation of memory fails, a NULL pointer is returned.