Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

C Programming and Interfacing with Intel 80C51 Microcontroller

| 0 comments |
In this tutorial, we illustrate how to write C program that turns on and off a LED connected to the port of a Intel 80C51 microcontroller chip. Such simple project forms the basis of embedded software development with C.

Intel 80C51 is an 8 bit microcontroller used in many areas of embedded systems design. For our purpose here it should be noted that the 80C51 has 4 ports called Port0, Port1, Port2 and Port3. All of these are bidirectional port. Each of the port has 8 pins and each of them can be written to and read from. So there are 4x8=32 I/O ports.

Port 1 is different from others in that it is designed as an I/O port. Other ports additional to input/output function also provides other special functions which will not be discussed here.

Here we will look at how the individual pins of the Port 1 can be controlled. That is how to send a bit, read a bit and how to send a byte and read a byte. This is how you usually begin learning a microcontroller and C programming as well.

We will start by writing 1 bit to the port 1 pins.

Consider the following schematic:


In the above figure, a LED is connected to the Port 1, pin 0. So to turn on the LED we must have logic high at the port pin 0. The C program for doing this is as follows,

#include <reg51.h>

sbit mybit = P1^0;

void main(void)
     {
      mybit = 0;
      }

In the above code, the Port 1 bit 0 is assigned a variable called mybit is declared using the sbit data type. In the main function this variable is set to 0 meaning that port 1 bit 0 is set to logic 0 and therefore doing this will not turn on the LED.

But if we change the above code so that mybit = 1; then now the Port 1 bit 0 has logic 1 high and therefore due to the potential difference created between that pin and the ground current flows into the LED and the LED turns on.

So to turn on the LED we would write the C program as follows:

#include <reg51.h>

sbit mybit = P1^0;

void main(void)
     {
      mybit = 1;
      }

The result is shown,


Notice that the LED is turned into blue.

In the above schematics we did not limit the current through the LED. One should add a resistor to limit the current flowing into the LED so that it does not get excessive current otherwise it will burn and get damaged.

So lets add a resistor before we move on. The LED has 2.2V forward voltage and current limit of 10mA. If the Pin voltage is at 5V then we can calculate the resistor value to limit the current into the resistor to 10mA.

R = (5V-2.2V)/10mA = 380 ohm ~370ohm.

OK, see the diagram below



Above we showed you how to turn off and on a LED at the port 1 pin 0 of the microcontroller.

In next tutorial post we illustrate how to write program with delays to turn on and off the LED.

For learning what is Proteus and how it is useful for embedded system development see the blog post- Embedded Systems Programming with Proteus Software.

Read More..

VHDL Programming tutorial on Priority encoder

| 0 comments |
Priority Encoder is an important class of Encoder. It is different from a ordinary encoder in that it gives priority to the input signal. The signal corresponding to the higher priority is outputted by the Priority encoder.

Here the design of 8 to 3 priority encoder is taken as an example for VHDL programming tutorial.

The truth table of a 8 to 3priority encoder is shown below:

8 to 3 priority encoder

In the truth table, the input data is x7x6x5x4x3x2x1x0 which is 8 bit input. And the 3 bit output is y2y1y0. The z signal indicates a condition that all input bits are 0 which is also a possible combination.

The priority is the signal with input 1XXXXXXX where X indicates dont care which means it does not matter whether it is 0 or 1. The next priority goes to 01XXXXXX and so on up on the table.

Suppose if 10100110 is the input data then the output is 111.

Below is the VHDL code for this 8 to 3 priority encoder:

library ieee;
use ieee.std_logic_1164.all;

entity prio_encoder is
    port(
    x : in std_logic_vector(7 downto 0);
    y : out std_logic_vector(2 downto 0);
    z : out std_logic
    );
end prio_encoder;

architecture model of prio_encoder is
begin
   
    process(x)
    begin
        if (x(7) = 1) then y <= "111";
        elsif (x(6) = 1) then y <= "110";
        elsif (x(5) = 1) then y <= "101";
        elsif (x(4) = 1) then y <= "100";
        elsif (x(3) = 1) then y <= "011";
        elsif (x(2) = 1) then y <= "010";
        elsif (x(1) = 1) then y <= "001";
        else y <= "000";
        end if;
       
        end process;
       
        z <= 0 when x = "00000000" else 1;           

end model;

In the above code, we used if elsif then statement of VHDL to implement the priority encoder. Since the if statement is a sequential state it is written inside the Process statement with sensitivity signal as x. The first statement is checked first and then next statement inside a process since the statements are sequential. If the first statement is true then the output is send to the output and the process exits. If not then the second statement is checked and if it evaluates true then it corresponding output is outputted and so on. So this code implements priority.The last else statement is needed so that the synthesizer does not create a latch. That is all combination of the input must be included. Finally the z output when all inputs are 0s is implemented outside the process statement.

The simulated waveform is shown below:

8 to 3 priority encoder simulated waveform

Encoders are important part of a digital system. There are other application of encoders such as code conversion encoders and includes designs that needs to be encoded to reduce output signals.
Read More..

download C programming for Microcontrollers by Joe Pardue free

| 0 comments |
Download here C Programming for Microcontrollers Featuring ATMELs AVR Butterfly and the free WinAVR Compiler by Joe Pardue for free. This is a good book on learning how to program AVR microcontroller using C program recommended by many. The book teaches basics of programming AVR, connecting the microcontroller to the PC for programming, using free Compilers, downloading the program into the microcontrollers. Then it teaches how to create program for AVR microcontroller to control input/ output ports with example of making LEDs connected to the port turn on and off. It also teaches how to program AVR microcontroller to communicate with PC using serial communication via the USART interface. Other C programming tutorials includes programming AVR in C for the analog to digital conversion, digital to analog conversion, LCD display control and others.

C Programming for Microcontrollers Featuring ATMELs AVR Butterfly and the free WinAVR Compiler by Joe Pardue
 Download Link for C programming for Microcontrollers pdf ebook by Joe Pardue

http://www.filefactory.com/file/si1nbwgcphn/JoeC.pdf


The Book not only provides tutorials and information on C programming and ATMEL microcontrollers but also teaches soldering, making physical connections etc required in the designs. Good example illustration of using AVR microcontroller for controlling mechanical machines such as motors with pictures are provided so that reader can understand what is being done. See one such picture screenshot from the book-




Read More..

embedded systems programming with proteus

| 0 comments |
Proteus can be very helpful tool for learning embedded system programming. The process involved is quite easy.

You start you embedded system design project by creating a new project. During the project creation you will an option whether you want a firmware development project. At this point you have two options. The first one is that you can say yes and use the either the Proteus build in compiler or assembler and second one is that you can select No to the option. If you selected the second option then you would create the program for the microprocessors and/or microcontrollers in the separate compiler or assembler tool such as Keil or PICC and then you would copy the source code .asm or .hex or .exe created from those compilers/assemblers into the Project project folder.

In case you select the first option, that is, you choose that you wanted to created a firmware project during the project creation you have the option to select the compiler/assembler for the microcontroller/microprocessor you are working with.

Below is a screenshot of this step:

embedded systems design and programming with proteus

In the above screenshot if you had selected the 8051 microcontroller family for the project then you would see a list of compiler available to you(and those not installed but could be used). The example here uses the Keil compiler.

After you have created your firmware project then you would see the source code editor program with the default header for the microcontroller and some intial C code as shown below.

embedded systems programming with proteus

By default the file name is main.c but you could change that. The default code provided by proteus is to make the programming quicker.

If you switch to the ISIS schematic editor you will see that the chosen microconroller is already placed on the schematic.

embedded systems design with proteus

Now from this point on you can start your embedded system design project. Following shows a simple control application in which a switch is connected to the Port1 0 pin and a sound buzzer is connected to the same port but pin 7. The switch connected to the pin 0 is checked and if open outputs sound from the buzzer at port 1 pin 7.

The following is the schematic for this small project.

embedded systems development with proteus

 Then you could simply start writing your C program for your embedded system design. The program code is simply written into the aforementioned proteus keil compiler and editor as shown.

embedded systems programming with proteus

Once you have written your program you should compile and build the project.

Once this is also done you would go back to your schematic and run the simulation. You could interactively switch on and off the switch in the schematic.

So this tutorial illustrated how proteus could be used for embedded system programming and development
Read More..