Sei sulla pagina 1di 22

➢ The C programming language is a general-purpose high-level programming

language that o€ers ecient and compact code

➢ Many control and monitoring-based applications


can be solved more eciently with C

➢ C was originally available on mainframe computers, mini-


computers, and personal computers

➢ The C programming language is now available on most microcontrollers and


microprocessors.

➢ the industry standard C51 optimizing C


compiler is used throughout. This compiler has been developed by Keil
Elektronik GmbH.

➢ C51 is available on both MS-DOS and Windows-based


operating systems

➢ 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.2 signed char/unsigned char


➢ These data types are as in standard C language and are used to declare signed
and unsigned character variables. Each character variable is 1 byte long
(8 bits). Signed character variables range from À128 to ‡127; unsigned
character variables range from 0 to 255.

➢ 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.2 signed char/unsigned char


➢ These data types are as in standard C language and are used to declare signed
and unsigned character variables. Each character variable is 1 byte long
(8 bits). Signed character variables range from À128 to ‡127; unsigned
character variables range from 0 to 255.
➢ Example:
unsigned char var1,var2; /* declare var1 and var2 as unsigned char */
var1 ˆ 0xA4; /* assign hexadecimal A4 to variable var1 */
\var2 ˆ var1; /* assign var1 to var2 */

➢ 2.1.3 signed short/unsigned short


➢ These data types are as in standard C language and are used to declare signed
and unsigned short variables. Each short variable is 2 bytes long (16 bits).
Signed short variables range from À32 768 to ‡32 767 and unsigned short
variables range from 0 to 65 535.
➢ Example:
unsigned short temp; /* declare temp as unsigned short */
unsigned short wind; /* declare wind as unsigned short */
temp ˆ 0x0C200; /* assign hexadecimal C200 to variable temp */
wind ˆ temp; /* assign variable temp to wind */

➢ 2.1.4 signed int/unsigned int


➢ As in the standard C language, these data types are used to declare signed and unsigned
integer variables. Integer variables are 2 bytes long (16 bits). Signed integers range from
À32 768 to ‡32 767 and unsigned integers range from 0 to 65 535.
➢ Example:
unsigned int n1,n2; /* declare n1 and n2 as unsigned integers */
n1 ˆ 10; /* assign 10 to n1 */
n2 ˆ 2*n1; /* multiply n1 by 2 and assign to n2 */
➢ 2.1.5 signed long/unsigned long
➢ These data types are as in standard C language and are used to declare signed
and unsigned long integer variables. Each long integer variable is 4 bytes long
(32 bits).
➢ Example:
unsigned long temp; /* declare temp as long integer variable */
temp ˆ 250 000; /* assign 250 000 to variable temp */

̄
➢ 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 */

➢ 2.2 Memory Models


➢ 8051 architecture supports both program (or code) and data memory areas.
Program memory is read-only and it cannot be written. The program memory can be
increased by connecting additional external memory to the basic microcontroller.
There may be up to 64 Kbytes of program memory.

➢ Depending upon the type of processor used di€erent 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 ecient 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
ecient.
➢ 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):

Void timer1() interrupt 3


➢ {
➢ interrupt service code goes in here
➢ }
➢ Similarly, the ISR for timer 0 (interrupt number 1) is declared as:
➢ void timer0() interrupt 1
➢ {
➢ interrupt service code goes in here
➢ }
➢ Note that we can specify the register bank to be used for the ISR with the using
➢ function attribute:
➢ void timer0() interrupt 1 using 2
➢ {
➢ interrupt service code goes in here
➢ }

➢ 2.4 Structure of a Microcontroller-based C Program


➢ The structure of a C program developed for a microcontroller is basically the
same as the structure of a standard C program, with a few minor changes.
➢ It is always advisable to describe the project at the beginning of a program
➢ The project name, ®lename, date, and the target processor type should also be included in
this part of the program.
➢ The register de®nition f®le should then be included for the type of target processor used.

➢ /
**************************************************************************
**************************
➢ 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>

#pragma config OSC = XT // 4MHz Crystal, (XT oscillator)


#pragma config PBADEN = OFF // PORTB<4:0> pins are configured as digital I/O on
Reset)

void delay_ms(unsigned int duration);

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
}
}

void delay_ms(unsigned int duration) // delay in miliseconds for 4.0MHZ crystal


{
unsigned int i;
for(;duration!=0;duration--)
{
for(i=0;i<=50;i++)
{
_asm
nop
nop
nop
_endasm
}
_asm
nop
nop
_endasm
}
}

➢ PROGRAM FOR 8 LEDS ON/OFF AFTER SOME DELAY

org 0000h; start


ajmp loop
org 0080h; to 0030h office address to avoid sensitive 00-30
loop:
mov p1, # 0ffh; turn off all lights
clr p1.0; point lighting p1.0
lcall delay; delay period of time
clr p1.1; point lighting p1.1
lcall delay
clr p1.2; point lighting p1.2
lcall delay
clr p1.3; point lighting p1.3
lcall delay
clr p1.4; point lighting p1.4
lcall delay
clr p1.5; point lighting p1.5
lcall delay
clr p1.6; point lighting p1.6
lcall delay
clr p1.7; point lighting p1.7
lcall delay
AJMP LOOP; to the best start to re-run loop Department
delay: mov r5, # 20; delay.
d1: mov r6, # 40
d2: mov r7, # 248
djnz r7, $
djnz r6, d2
djnz r5, d1
ret
end
4 / 14

Potrebbero piacerti anche