Learn Microcontroller Programming online for free

0
16


Download Our App

Introduction – In the microcontroller project the most important thing is programming the microcontroller. So here you are going to learn microcontroller programming online. To make easy we will learn microcontroller programming in c. This language is similar on C programming but it is Embedded C.learn 8051 microcontroller programming in detail.
For writing the program you use any writing software. but to convert it into .hex file you will need Keil software.

Learn Microcontroller Programming online

How to flash .hex file in microcontroller?

Today we will focus on writing 8051 microcontroller program we will learn basic of it. The main aspect of a writing program. as well as common errors come while writing microcontroller program.

Learn 8051 microcontroller programming online

Header in programming

First, we have to configure microcontroller registers with our program. For this, we have to add header in the program for Microcontroller 8051 and we add

#include<regi51.h>

if we have 8052 series microcontroller we use #include.

For some special functions, we can add more header files in these programs.

Advertisement

You can take example of

#include<intrins.h>

which adds instructions likes _nop_() which provides delay of 1 microsecond.

After the header, we start with pin configuration

Pin Naming

The microcontroller has a lot of pins. so some times writing program can be a little confusing process. thus we name pin of the microcontroller which can help us with it. In the case of 8051 microcontroller series, we know there 4 port and all they have 8 pins. So to mention any pin we write it as P1.2 for port 1 pin no. 2. P needs to be capital and counting of pins start from 0 so the first pin is P0.0. Usually don’t use port 0. Because its pull up resistors is weak.

learn microcontroller programming in c

So if we want to name port 1 pin no. 2 as pushbutton we will write it as sbit pushbutton=P1^2;

Sbit Gives a name to pin. But remember semicolon needs to at the end.

Pin Configuration

After naming the pins we need to configure those pins as output pins or input pins. Since we know we need input pins for sensors and output pins for out devices like LCD, Buzzer and LEDs.

So to make any pin as output pin we have made it low. So example if I want to make that pushbutton pin as output pin I will write pushbutton=0;

And if I want to make that pin as input which it is actually I will write pushbutton=1;

Learn Microcontroller Programming online

Now you can use the logic of C++ in this. If you want to use if-else statements you can use them with the name of pins, an example is down below

if(water==1)

{

outwater=1;

}

else { outwater=0;

}

Above example gives output high when water pin input is high, otherwise, it gives low.

But the most important thing in microcontroller programming. is your program has to be in repeating form. so that you don’t need to restart it.

For this, we use an infinite loop in form of while loop.

To do this you have to write the whole main program inside a while loop.

Example:-

while(1) {

Whole program

}

Note – the whole program doesn’t include configuration of pins and naming of pins or header.

Tutorials on microcontroller programming

We can make functions. if you are using one process for so many times those functions are like void function_name().

And these functions are call-in void main() where our while loop is there.

Calling any function is easy as you calling Them by there name

For example

ultrasonic_depth();

This is how we call a function

Actual program

void ultrasonic_depth()

{

send_pulsedepth();

while(!echod);

while(echod);

DPH=TH0;

DPL=TL0;

TH0=TL0=0xff;

if(DPTR<1200)

{

outdepth=1;

}

else

{

outdepth=0;

}

}

So you can understand if want to make a process happen for more times we can do this and save time.

how to add values in registers?

If you want to write any hex number in it you to write it like 0x00;

So it indicates it is a hex number otherwise it take it as a decimal number.

But Keep in mind you can no exceed more than the memory of the microcontroller.

Example of it

TH0=0x00

You can access predefined registers. Like DPTR or timer TH0 or TL0. And You can write any Hex no. In it.

Common mistakes that happens while writing program

Not writing semicolon after the instruction

Change in letters in names or functions can make a big issue for calling any functions

For more such articles and tutorials join our Telegram channel by clicking here

LEAVE A REPLY

Please enter your comment!
Please enter your name here