EMBEDDED
C TUTORIAL
TIMOTHY ADEGBITE
Welcome
to today’s Trinnex electronics tutorial on Embedded C. Apologies for the long
break. I have been busy with some studies, researches and personal development that
has literally taken most of my time. Thanks to all, that has been following up
with my blog, thanks for all the chats and comments. It shows there are people
who are really passionate about what I am doing. You can always post your
comments here or talk to me privately on anything involving electronics and
embedded Programming.
Remember,
this is a place for startups in electronics and embedded system programmers. If
you think you are not a startup and you need more advance tutorial or support on your projects, you can
send me a mail or chat with me, I will gladly reply you.
Today,
I will continue on the LED BLINK TUTORIAL. I will be flashing two led at a specified
interval using a delay function. This can be accurately achieved with the use
of a timer interrupt, a built-in function of the microcontroller. Using the
interrupt is a subject for another day. The delay function is also accurate,
but the interrupt function is more accurate and precise.
We
will be improving on the program written before, which was flashing an LED. We
will be including one more LED.
This
time, we will be flashing two LED alternatively at the interval of 2 secs. To
achieve this, we make the delay function last for a total of 2 secs. The delay
function can either be in milliseconds (ms) or microseconds (us). To implement
the delay in milliseconds, you use “ __delay_ms( ) ” , to implement the
delay in microseconds, you use “ __delay_us(
) ” . The value in the bracket signifies the duration of the delay.
For
example, if I want a delay of 200ms, I simply type __delay_ms(200). To
implement a delay of 2us, I simply type __delay_us(2).
To
implement a delay of 2secs, what do I do? I simply do a conversion from
seconds to milliseconds. You know that 1000milliseconds makes 1seconds. So
meaning, 2000milliseconds makes 2 seconds.
So, to
implement a delay in seconds, you simply do a conversion from seconds to
milliseconds, then put the value into the bracket. A delay of 2seconds is equal
to 2000milliseconds, to implement this, you simply type __delay_ms(2000).
Below
shows the simply program written to achieve this.
//This is a simple
project, demonstrating the blinking of an LED
//using the pic16f84a microcontroller.
//using the pic16f84a microcontroller.
#include
< xc.h > // header file
that includes all the
//files in the pic16f84a
#define _XTAL_FREQ
4000000 // clocking frequency is 4Mhz
void main(void) // This is the main function. It receives
// no argument and returns
no value.
// It is a void function.
// It is a void function.
{
TRISA=0B00000000; //
equates all PORTA as an output port
TRISB=0B00000000; // equates all PORTB as an output port
PORTA=0; // clears PORTA bits
PORTB=0; // clears PORTB bit
while(1) //
while loop; it iterates continuously
{
PORTBbits.RB0 = 1 ;
// set bit 0 of PORTB
PORTBbits.RB1 = 0 ; // clears bit 1 of PORTB
__delay_ms(2000); // delay for 2000ms
PORTBbits.RB0 = 0 ; // clears bit 0 of PORTB
PORTBbits.RB1 = 1 ; // set bit 1 of PORTB
__delay_ms(2000); //
delay for 2000 ms
}
}
After
compiling this program and running it on Proteus, you will find the two LED
blinking alternatively. One of the
important thing you should learn here is how to implement a delay function and how to assign values to the ports of the
microcontroller, which I have explained in the former tutorials.
In the
next tutorial, I will be working on a LED chaser, but before then, I have a
little task.
ASSIGNMENT:
Try
flashing 3 LEDs alternatively at the interval of 1 sec, post your codes here, to see how efficient your code is and I will give my comment on how it can be improved on. Remember,
there is not short cut to knowing this than constant practice!!!
The next tutorial and the solution to the assignment will be available a week from now.
The next tutorial and the solution to the assignment will be available a week from now.
Below shows the finished
prototype of the design on Proteus.
You can always post your
comments here, for further clarifications and questions.

Comments
One more things, wanna ask you something. I have seen a lot tutorials using PORTB.F0 or PORTB.R01 or TRISB.F0 and you are using PORTBbits.R01, what is the differences?
Thanks alot sir. This is just the beginning.
XC compiler is owed by Microchip which happens be the the manufacturer of the Microchip microcontroller.
Apart from XC compiler, there are other compilers owned by third parties like Micro C compiler, Hi-tech compiler, ccs compiler and lots more.
Each of this compilers has different syntax. To assign a value to the port of the microcontroller using for example bit 1 of PORTB:
XC8 compiler : PORTBbits.RB1 = 1 or RB1 = 1
Micro C : PORTB.F1 = 1
Hi-Tech : RB1 = 1
The Hi-tech syntax tends to be similar to the XC compiler.
you can even develop your own compiler and use your own syntax
hope this answers your question.
So, the choice is yours. I use the XC8 compiler for the tutorials and for my personal projects. its more advance than other compilers and so easy to use. I advice you also get it as soon as possible. you can download it on the Microchip website, www.microchip.com. you can get the free, standard and professional versions there. The Free version is totally free while the other two is at a cost.