Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
➢ There are many other high-level language compilers available for micro-
controllers, including PASCAL, BASIC, and other C compilers.
➢ some companies supply free limited capability compilers, mainly for evaluation purposes
➢ These compilers can be used for learning the features of a speci®c product and in some
cases small projects can be developed with such compilers.
➢ The C51 compiler has been developed for the 8051 family of microcontrollers.
➢ This is one of the most commonly used industry standard C compilers for the
8051 family, and can generate machine code for most of the 20-pin and 40-pin
8051 devices
➢ 2.1.1 bit
These data types may be used to declare 1-bit variables.
➢ Example:
̄
bit my_ ag; ̄ as a bit variable */
/* declare my_ ag
̄ 1;
my_ ag ̄ to 1 */
/* set my_ ag
̄
➢ 2.1.6 float
➢ This data type is used to declare a ̄oating point variable.
➢ Example:
̄ t1,t2;
float /* declare t1 and t2 as ̄oating point variables */
t1 25.4 /* assign 25.4 to t1 */
t2 sqrt(t1); /* assign the square-root of t1 to t2 */
➢ 2.1.7 sbit
➢ This data type is provided for the 8051 family and is used to declare an
individual bit within the SFR of the 8051 family. For example, using this data
type one can access individual bits of an I/O port.
➢ Example:
sbit switch P1^3; /* variable switch is assigned to bit 3 of port 1 */
switch 0; /* clear bit 3 of port 1 */
➢ 2.1.8 sfr
➢ This data type is similar to sbit but is used to declare 8-bit variables.
➢ Example:
sfr P1 0x90; /* Port 1 address 0x90 assigned to P1 */
sfr P2 0xA0; /* Port 2 address 0xA0 assigned to P2 */
unsigned char my_data; /* declare my_data as unsigned character */
my_data P1; /* read 8 bit data from port 1 and assign to my_data */
P2 my_data++; /* increment my_data and send to port 2 */
➢ 2.1.9 sfr16
➢ This data type is similar to sfr but is used to declare 16-bit variables. When
using this data type, the low byte should precede the high byte.
➢ Example:
Timer 2 of the 8052 microcontroller uses addresses 0xCC and 0xCD for the low
and high bytes. We can declare variable T2 to access both timer locations.
sfr16 T2 0xCC; /* Timer 2, T2L=CC and T2H=CD */
T2 0xAE01; /* load Timer 2 with hexadecimal value AE01 */
➢ Depending upon the type of processor used dierent amounts of internal program memory
are available.
➢ Data memory resides within the 8051 CPU and can be read from and written
into. Up to 256 bytes of data memory are available depending upon the type of
microcontroller used.
➢ The memory model determines what type of program memory is to be used for
a given application.
➢ There are three memory models, known as SMALL, COMPACT, and LARGE, and the
required model is speci®ed using the compiler directives.
➢ The SMALL memory model is used if all the variables reside in the internal data memory of
the 8051. This memory model generates the fastest and the most efficient code and should
be used whenever possible.
➢ In the COMPACT memory model, all variables reside in one page of external data memory.
A maximum of 256 bytes of variables can be used. This memory model is not as ecient as
the SMALL model.
➢ In the LARGE memory model, all variables reside in external data memory. A maximum of
64 Kbytes of data can be used.
➢ The LARGE model generates more code than the other two models and thus it is not very
ecient.
➢ Compiling in the SMALL memory model always generates the fastest and the smallest code
possible since accessing the internal memory is always faster than accessing any external
memory.
➢ 2.3 Interrupts
➢ The C51 compiler allows us to declare interrupt service routines (ISRs) in our C code and
then the program automatically jumps to this code when an interrupt occurs. The compiler
automatically generates the interrupt vectors and entry and exit code for interrupt routines.
➢ An ISR is declared similar to a function declaration but the interrupt number is speci®ed as
part of the function declaration.
➢ For example, the following is a declaration of the ISR for timer 1 interrupts (interrupt
number 3):
➢ /
**************************************************************************
**************************
➢ Project: Give project name
➢ File: Give ®lename
➢ Date: Date program was developed
➢ Processor: Give target processor type
̄
➢ This is the program header. Describe your program here brie y.
➢ **************************************************************************
**************************/
➢ #include <AT892051.h>
➢ #de®ne ...... /* include your de®ne statements here */
➢ sbit ...... /* include your bit de®nitions here */
➢ int ......
➢ char ...... /* include your global declarations here */
➢ void func1() /* include you functions here */
➢ {
➢ }
➢ main()
➢ /* main code */
➢ {
➢ /* include comments here */
➢ }
➢
//
// Designed by www.MCUExamples.com
// rasika0612@gmail.com
//
#include <p18f2520.h>
void main()
{
TRISC=0;
while (1)
{
LATC=0xff; //Set all pins of portC to logic high
delay_ms(500); // delay 500mS
LATC=0; //Set all pins of portC to logic low
delay_ms(500); // delay 500mS
}
}