Introduction

The use of C language to program microcontrollers is becoming too common. And most of the time its not easy to buld an application in assembly which instead you can make easily in C. So Its important that you know C language for microcontroller which is commonly known as Embedded C. As we are going to use Keil C51 Compiler, hence we also call it Keil C.

Keywords

Keil C51 compiler adds few more keywords to the scope C Language:

_at_ far sbit
alien idata sfr
bdata interrupt sfr16
bit large small
code pdata _task_
compact _priority_ using
data reentrant xdata
data/idata

Description: The variable will be stored in internal data memory of controller. example:

unsigned char data x;
//or
unsigned char idata y;
		
bdata

Description: The variable will be stored in bit addressable memory of controller. example:

unsigned char bdata x;
//each bit of the variable x can be accessed as follows
x ^ 1 = 1; //1st bit of variable x is set
x ^ 0 = 0; //0th bit of variable x is cleared
		
xdata

Description: The variable will be stored in external RAM memory of controller. example:

unsigned char xdata x;
		
code

Description: This keyword is used to store a constant variable in code memory. Lets say you have a big string which is not going to change anywhere in program. Wasting ram for such string will be foolish thing. So instead we will make use of the keyword "code" as shown in example below. example:

unsigned char code str="this is a constant string";
		
pdata

Description: This keyword will store the variable in paged data memory. This keyword is used occasionally. example:

unsigned char pdata x;
		
_at_

Description: This keyword is used to store a variable on a defined location in ram. example:

unsigned char idata x _at_ 0x30;
// variable x will be stored at location 0x30
// in internal data memory
		
sbit

Description: This keyword is used to define a special bit from SFR (special function register) memory. example:

sbit Port0_0 = 0x80;
// Special bit with name Port0_0 is defined at address 0x80
		
sfr

Description: sfr is used to define an 8-bit special function register from sfr memory. example:

sfr Port1 = 0x90;
// Special function register with name Port1 defined at addrress 0x90
		
sfr16

Description: This keyword is used to define a two sequential 8-bit registers in SFR memory. example:

sfr16 DPTR = 0x82;
// 16-bit special function register starting at 0x82
// DPL at 0x82, DPH at 0x83
		
using

Description: This keyword is used to define register bank for a function. User can specify register bank 0 to 3. example:

void function () using 2
{
	// code
}
// Funtion named "function" uses register bank 2 while executing its code
		
interrupt

Description: This keyword will tells the compiler that function described is an interrupt service routine. C51 compiler supports interrupt functions for 32 interrupts (0-31). Use the interrupt vector address in the following table to determine the interrupt number.

Interrupt vector
example:

void External_Int0() interrupt 0
{
	//code
}
		

Memory Models

There are three kind of memory models available for the user:

Small
All variables in internal data memory.
Compact
Variables in one page, maximum 256 variables (limited due to addressing scheme, memory accessed indirectly using r0 and r1 registers)
large
All variables in external ram. variables are accessed using DPTR.
Depending on our hardware configuration we can specify the momory models as shown below:

//For Small Memory model
#pragma small
//For Compact memory model
#pragma compact
//For large memory model
#pragma large
		

Conversation

ExperimenterUK
Thu Nov 20 2014, 06:00 PM
@Mehboob.. I don't think you can do that.
Sat Nov 15 2014, 06:41 AM
Mehboob
Pls can you send me code in c of PWM generation using UART/serial port in 8051.
Fri Nov 14 2014, 02:51 PM
mohammad aqil
NEED A BOOK ON MICROPROCESSOR AND INTERFACING BY D.V.HALL. RIGHT NOW, I SHALL BE VERY THANK FULL TO U....
Thu Nov 13 2014, 02:15 PM
m
please code hex project avr blood pressure monitor
Thu Nov 13 2014, 05:56 AM
Lambo
hi mate, whats the ajay-bhargav? tell us more pls
ExperimenterUK
Wed Nov 12 2014, 12:56 AM
@tmtm000.. have you been able to join ?
ExperimenterUK
Wed Nov 12 2014, 12:54 AM
@Alice..become a member,then send a PM to ajay_bhargav the site owner.
Tue Nov 11 2014, 03:58 AM
Abhishek Bansal
Thanks a lot for your informative tutorials!
Mon Nov 10 2014, 09:03 AM
Alice
Hello, This is Alice from a PCB rototype manufacturer. Our company is interested in your website contents, and wonder if we can advertise our company with payment on your website. There are two ways to advertise: first, if we an get a instant quote section on your main site columns; second, whether we can get a advertising section if you have any avaliable one. If any of above ways avaliable, please contact me with your detailed scheme and advertising charge. Thanks. Alice
Sat Nov 08 2014, 11:58 AM
Mohamed_123sd
Signals and Systems, 2nd Edition by Alan V. Oppenheim Alan S.willsky with S.Hamid Nawab?

Downloads

Comments

VikiPeter
Tue Nov 18 2014, 09:13 AM
gentlboy
Sun Nov 16 2014, 11:13 PM
philipeastbourne
Wed Nov 12 2014, 05:32 AM
astra91
Mon Nov 10 2014, 08:11 AM
francise
Thu Nov 06 2014, 02:55 AM
ashbir
Tue Nov 04 2014, 11:13 PM
shruthig
Tue Nov 04 2014, 10:50 AM
sk8boardboy
Mon Nov 03 2014, 11:20 AM

Online

Guests: 51, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37547
Newest member: VikiPeter
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Nov 22 2014, 06:15 AM
@ExperimenterUKI make small table to observe Outpu...
Max.otto Avatar
Posted By Max.otto
Fri Nov 21 2014, 02:31 PM
@ExperimeterUKForgot my last question.As i told be...
Max.otto Avatar
Posted By Max.otto
Fri Nov 21 2014, 02:05 PM
@ExperimenterUKI make small table to observe Outpu...
Max.otto Avatar
Posted By Max.otto
Fri Nov 21 2014, 01:12 PM
@ExperimenterUKThank u. I do it. Ohne Period it's ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Nov 20 2014, 05:56 PM
You set the on/off times here as 47%float PWMcom...
vijay.parmar1992 Avatar
Posted By vijay.parmar1992
Thu Nov 20 2014, 04:06 AM
Right now i am studying Bootloader.I want to writ...
Max.otto Avatar
Posted By Max.otto
Wed Nov 19 2014, 03:11 PM
How can i get PWM on-off time..?pls check the atta...
Max.otto Avatar
Posted By Max.otto
Wed Nov 19 2014, 03:05 PM
I am using PSoC 5LP.pls check the attached design ...
ankitbajaj01 Avatar
Posted By ankitbajaj01
Wed Nov 19 2014, 01:49 PM
Thanks for the prompt response, I will try it out ...
engr1931 Avatar
Posted By engr1931
Wed Nov 19 2014, 11:45 AM
this one sir! i attached the whole project! ...