Saturday, October 9, 2010

1.6 Embedded C & its Program Structure

Hello Readers:

I have started this blog to show you Embedded C programming for 8051 family microcontroller using KEIL compiler. The KISS philosophy is used to write this tutorial. What is KISS? Keep It Simple, Stupid!

1.6 Embedded C & its Program Structure

Why program the 89S52 in C?: Compilers produce hex files that we download in to the ROM of the microcontroller. The size of the hex file produced by the compiler is one of the main concerns of microcontroller programmers, for two reasons:

  1. Microcontrollers have limited on-chip ROM.
  2. The code space for the 8051 is limited to 64Kbytes.
How does the choice of programming language affect the compiled program size? While Assembly language produces a hex file that is much smaller than C, programming in Assembly language is tedious and time consuming. C programming, on the other hand, is less time consuming and much easier to write, but the hex file size produced is much larger than if we used Assembly language. The following are some of the major reasons for writing programs in C instead of Assembly:

  1. It is easier and less time consuming to write in C than Assembly.
  2. C is easier to modify and update.
  3. You can use code available in function libraries.
  4. C code is portable to other microcontrollers with little or no modification.
C data types: Since one of the goals of 8051 C programmers is to create smaller hex files, it is worthwhile to re-examine C data types for 8051 C. In other words, a good understanding of C data types for the 8051 can help programmers to create smaller hex files.

Since the 8051 is an 8-bit microcontroller, the character data type is the most natural choice for many applications. The unsigned char is an 8-bit data type that takes a value in the range of 0-255 (00-FFh). It is one of the most widely used data types for the 8051. In many situations, such as setting a counter value, where there is no need for signed data we should use the unsigned char instead of the signed char. Remember that C compilers use the signed char as the default if we do not put the keyword unsigned in front of the char.

Table 1.6.1 shows widely used data types for 8051 C. Note that default data type is signed char.


Table 1.6.1
Data Type
Size in Bits
Data Range/Usage
unsigned char
8-bit
0 to 255
(signed ) char
8-bit
-128 to +127
unsigned int
16-bit
0 to 65535
(signed) int
16-bit
-32,768 to +32,767
sbit
1-bit
SFR bit-addressable only
bit
1-bit
RAM bit-addressable only
sfr
8-bit
RAM addresses 80-FFh only

Program Structure of Embedded C
Generally the program structure of embedded c is as shown in Fig 1.6.2. Note that it is similar to C language. 




The comments are done by putting two forward slashes in front of single-line comment or putting block comments in between /* and */. The first line indicates the program number of the source code.

The include line or line two is necessary as it includes intrinsic functions, declarations to our program. We are including REG51.H header file as we are using 51 series microcontroller chip as our target chip.

The third line declaring the function MSDelay() before main, and is known as function prototyping. It is necessary as MSDelay() function is written after the main and compiler executes the source code from top. The word void indicates function returns nothing.

As C language, embedded C needs main() function and block codes written inside it are executed first. The function code is written in between ‘{‘ and ‘}’

In embedded C also semicolon is used as statement, command terminator. 

The sixth line shows how to declare the variables. The tenth line shows how to call function MSDelay().The thirteenth line is function heading of MSDelay().  

<End of 1.6   Embedded C & its Program Structure >

...till next post bye-bye & take care.

Friday, October 8, 2010

1.5 About KEIL

Hello Readers:

I have started this blog to show you Embedded C programming for 8051 family microcontroller using KEIL compiler. The KISS philosophy is used to write this tutorial. What is KISS? Keep It Simple, Stupid!

KEIL Compiler:  

Keil development tools for the 8051 microcontroller family support every level of developer from the professional applications engineer to the student just learning about embedded software development. The industry-standard Keil C Compilers, Macro Assemblers, Debuggers, Real-time Kernels, and Single-board Computers support ALL 8051-compatible derivatives and help you get your projects completed on schedule.

Software Development Cycle
When you use the Keil µVision, the project development cycle is roughly the same as it is for any other software development project.
  1. Create a project, select the target chip from the device database, and configure the tool settings.
  2. Create source files in C or assembly.
  3. Build your application with the project manager.
  4. Correct errors in source files.
  5. Test the linked application.
The following block diagram illustrates the complete µVision/ARM software development cycle. Each component is described below.


Where to find KEIL and what version is used in this tutorial?
You can get the KEIL software used in this tutorial from this site. I mean click here to get it, which is having code limit of 2KB. The uVision V4.10 version is used throughout this tutorial. You can get latest evaluation version of KEIL from their website. Their official website address is www.keil.com. Installing the KEIL is very straight forward: double click the setup, set the folder path-where you want it to install, click OK.

NOTE: In Embedded C the targeted chip /centre-piece of program is referred as ‘Target’ and our Target chip is AT89S52.

System Requirement for installing KEIL
  • PC with Pentium, Pentium-II or compatible processor,
  • Windows 95, Windows-98, Windows NT 4.0, or higher,
  • 16 MB RAM minimum,
  • 20 MB free disk space.
  •  
For additional help please goto Books tab or Help->uVision Help in KEIL IDE

  To learn how to create project, add source file, configure the compiler, setting debug session and run to see the output please refer KEIL in-built HELP [which comes with IDE]. This HELP is very informative and shows about everything you wanted know with the help of screenshots and examples.   

<End of 1.5   About KEIL>

...till next post bye-bye & take care.

Thursday, October 7, 2010

1.4 Flowcharts & Control Program Structures

Hello Readers:

I have started this blog to show you Embedded C programming for 8051 family microcontroller using KEIL compiler. The KISS philosophy is used to write this tutorial. What is KISS? Keep It Simple, Stupid!

1.4       Flowcharts & Control Program Structures

Flowcharts: Flowcharts use graphic symbols to represent different types of program operations. These symbols are connected together into a flowchart to show the flow of execution of a program. The Fig 1.4.1 shows commonly used flowchart symbols.


Note: Flow charts are not statement to statement translation of code or program. It only shows the algorithm in graphical form.



Some of the program control structures
Structured programming uses three basic types of program control structures: sequence, control and iteration.

The Sequence is simply executing instructions one after another. The Fig 1.4.2 shows it.



The Control programming structures are IF…Then and IF…Else and are shown in Fig 1.4.2


The three iteration programming structures are WHILE, DO…WHILE and FOR and are shown in Fig 1.5.4



<End of 1.4   Flowcharts & Control Program Structures>  


...till next post bye-bye & take care.

Wednesday, October 6, 2010

1.3 About Microcontrollers & 8051 Family

Hello Readers:

I have started this blog to show you Embedded C programming for 8051 family microcontroller using KEIL compiler. The KISS philosophy is used to write this tutorial. What is KISS? Keep It Simple, Stupid!

1.3       About Microcontrollers & 8051 Family

 
How many 8-bit microcontroller families are there?
There are four widely used 8-bit microcontroller chip manufacturers are there: Freescale’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16x from Microchip Technology. Each have unique instruction set and register set hence they are not compatible to each other.


The Intel 8051, 8-bit microcontroller
The Intel 8051 is a Harvard architecture, single chip microcontroller (µC) which was developed by Intel in 1980 for use in embedded systems. The official designation for the 8051 family is MCS 51. Intel's original versions were popular in the 1980s and early 1990s, but has today[update] largely been superseded by a vast range of faster and/or functionally enhanced 8051-compatible devices manufactured by more than 20 independent manufacturers including Atmel, Infineon Technologies (formerly Siemens AG), Maxim Integrated Products (via its Dallas Semiconductor subsidiary), NXP (formerly Philips Semiconductor), Nuvoton (formerly Winbond), ST Microelectronics, Silicon Laboratories (formerly Cygnal), Texas Instruments and Cypress Semiconductor.
Intel's original 8051 family was developed using NMOS technology, but later versions, identified by a letter C in their name (e.g., 80C51) used CMOS technology and were less power-hungry than their NMOS predecessors. This made them more suitable for battery-powered devices.
Important features and applications
It provides many functions (CPU, RAM, ROM, I/O, interrupt logic, timer, etc.) in a single package
A particularly useful feature of the 8051 core is the inclusion of a boolean processing engine which allows bit-level boolean logic operations to be carried out directly and efficiently on internal registers and RAM. This feature helped cement the 8051's popularity in industrial control applications. Another valued feature is that it has four separate register sets, which can be used to greatly reduce interrupt latency compared to the more common method of storing interrupt context on a stack.

The Fig 1.3.1 shows internal block diagram of 8051 microcontroller.



The 8051 UARTs make it simple to use the chip as a serial communications interface. External pins can be configured to connect to internal shift registers in a variety of ways, and the internal timers can also be used, allowing serial communications in a number of modes, both synchronous and asynchronous. Some modes allow communications with no external components. A mode compatible with an RS-485 multi-point communications environment is achievable, but the 8051's real strength is fitting in with existing ad-hoc protocols (e.g., when controlling serial-controlled devices).

Once a UART, and a timer if necessary, have been configured, the programmer needs only to write a simple interrupt routine to refill the send shift register whenever the last bit is shifted out by the UART and/or empty the full receive shift register (copy the data somewhere else). The main program then performs serial reads and writes simply by reading and writing 8-bit data to stacks.

8051 based microcontrollers typically include one or two UARTs, two or three timers, 128 or 256 bytes of internal data RAM (16 bytes of which are bit-addressable), up to 128 bytes of I/O, 512 bytes to 64 kB of internal program memory, and sometimes a quantity of extended data RAM (ERAM) located in the external data space. The original 8051 core ran at 12 clock cycles per machine cycle, with most instructions executing in one or two machine cycles. With a 12 MHz clock frequency, the 8051 could thus execute 1 million one-cycle instructions per second or 500,000 two-cycle instructions per second. Enhanced 8051 cores are now commonly used which run at six, four, two, or even one clock per machine cycle, and have clock frequencies of up to 100 MHz, and are thus capable of an even greater number of instructions per second. All SILabs, some Dallas and a few Atmel devices have single cycle cores.

Common features included in modern 8051 based microcontrollers include built-in reset timers with brown-out detection, on-chip oscillators, self-programmable Flash ROM program memory, bootloader code in ROM, EEPROM non-volatile data storage, I²C, SPI, and USB host interfaces, CAN or LIN bus, PWM generators, analog comparators, A/D and D/A converters, RTCs, extra counters and timers, in-circuit debugging facilities, more interrupt sources, and extra power saving modes.

Memory Architecture
The 8051 has four distinct types of memory - internal RAM, special function registers, program memory, and external data memory.

Internal RAM (IRAM) is located from address 0 to address 0xFF. IRAM from 0x00 to 0x7F can be accessed directly, and the bytes from 0x20 to 0x2F are also bit-addressable. IRAM from 0x80 to 0xFF must be accessed indirectly, using the @R0 or @R1 syntax, with the address to access loaded in R0 or R1.

Special function registers (SFR) are located from address 0x80 to 0xFF, and are accessed directly using the same instructions as for the lower half of IRAM. Some of the SFR's are also bit-addressable.

Program memory (PMEM, though less common in usage than IRAM and XRAM) is located starting at address 0. It may be on- or off-chip, depending on the particular model of chip being used. Program memory is read-only, though some variants of the 8051 use on-chip flash memory and provide a method of re-programming the memory in-system or in-application. Aside from storing code, program memory can also store tables of constants that can be accessed by MOVC A, @DPTR, using the 16-bit special function register DPTR.

External data memory (XRAM) also starts at address 0. It can also be on- or off-chip; what makes it "external" is that it must be accessed using the MOVX (Move eXternal) instruction. Many variants of the 8051 include the standard 256 bytes of IRAM plus a few KB of XRAM on the chip. If more XRAM is required by an application, the internal XRAM can be disabled, and all MOVX instructions will fetch from the external bus.

Programming
There are various high-level programming language compilers for the 8051. Several C compilers are available for the 8051, most of which feature extensions that allow the programmer to specify where each variable should be stored in its six types of memory, and provide access to 8051 specific hardware features such as the multiple register banks and bit manipulation instructions. There are many commercial C compilers. SDCC is a popular open source C compiler. Other high level languages such as Forth, BASIC, Pascal/Object Pascal, PL/M and Modula-2 are available for the 8051, but they are less widely used than C and assembly.

Because IRAM, XRAM, and PMEM all have an address 0, C compilers for the 8051 architecture provide compiler-specific pragmas or other extensions to indicate where a particular piece of data should be stored (i.e. constants in PMEM or variables needing fast access in IRAM). Since data could be in one of three memory spaces, a mechanism is usually provided to allow determining to which memory a pointer refers, either by constraining the pointer type to include the memory space, or by storing metadata with the pointer.

Instruction Set
The 8051 instruction set offers several addressing modes, including
  • direct register, using ACC (the accumulator) and R0-R7
  • direct memory, which access the internal RAM or the SFR's, depending on the address
  • indirect memory, using R0, R1, or DPTR to hold the memory address. The instruction used may vary to access internal RAM, external RAM, or program memory.
  • individual bits of a range of IRAM and some of the SFR's
Many of the operations allow any addressing mode for the source or the destination, for example, MOV 020h, 03fh will copy the value in memory location 0x3f in the internal RAM to the memory location 0x20, also in internal RAM.
Because the 8051 is an accumulator-based architecture, all arithmetic operations must use the accumulator, e.g. ADD A, 020h will add the value in memory location 0x20 in the internal RAM to the accumulator.

One does not need to master these instructions to program the 8051. With the availability of good quality C compilers, including open source SDCC, virtually all programs can be written with high-level language.

Related Processors
The 8051's predecessor, the 8048, was used in the keyboard of the first IBM PC, where it converted keypresses into the serial data stream which is sent to the main unit of the computer. The 8048 and derivatives are still used today[update] for basic model keyboards.

The 8031 was a cut down version of the original Intel 8051 that did not contain any internal program memory (ROM). To use this chip, external ROM had to be added containing the program that the 8031 would fetch and execute.

The 8052 was an enhanced version of the original 8051 that featured 256 bytes of internal RAM instead of 128 bytes, 8 kiB of ROM instead of 4 kiB, and a third 16-bit timer. The 8032 had these same features except for the internal ROM program memory. The 8052 and 8032 are largely considered to be obsolete because these features and more are included in nearly all modern 8051 based microcontrollers.

Intel discontinued its MCS 51 product line in March 2007, however there are plenty of enhanced 8051 products or silicon IP added regularly from other vendors.


About 8051 Family
In 1981 Intel Corporation introduced 8-bit Microcontroller and referred it as MCS-51.Its CPU works only on 8-bits of data, at a time. Two other members of 8051 family microcontrollers are 8031 and 8051. The Table 1.3.1 shows comparison of 8051 family members.

 
How many 8051 chip manufacturers are there?
The most widely appreciated 8051 family chip manufacturers are Intel, Atmel, Philips/NXP, Infineon, Dallas/Maxim.


Criteria for choosing a Microcontroller:
  • It must meet the task at hand efficiently and cost effectively
8-bit or 16-bit or 32-bit?
Speed Support
Packaging: 40pin DIP [Dual-In line Package] or QFP [Quad Flat Package] as space and assembling of prototyping of the end product is considered.
Power Consumption: as critical for battery-powered products.
Amount of RAM/ROM on chip.
Upgrade facility to high to low power version chip.
Cost per unit.
  • How easy it is to develop products around it. Availability of an assembler, debugger, compiler, emulator, technical support [in-house & out house expertise], 3rd party vendor support.
  • Its ready availability in need quantities both now and in the future.
  <End of 1.3 About Microcontrollers & 8051 Family>

...till next post bye-bye and take care.