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
Sun Jan 25 2015, 08:10 PM
This box is for chat. Post problems in a suitable forum
ExperimenterUK
Sun Jan 25 2015, 08:04 PM
@gaurav.k Use the purple "Post reply" button. Green buttons often present a fresh edit box.
ExperimenterUK
Sun Jan 25 2015, 07:33 PM
@avianash..we have no entry for avianash.. Are you sure that is your login name ?
Fri Jan 23 2015, 04:29 AM
lolokijuh
DJ Bakal tulungan mo ako s IC Tester
Fri Jan 23 2015, 04:29 AM
lolokijuh
sir help about IC Tester
Thu Jan 22 2015, 01:59 PM
apparao majji
could you please help in programming
Thu Jan 22 2015, 10:13 AM
avianash
hi my account is inactive plz help me
gaurav.k
Thu Jan 22 2015, 07:04 AM
not able top post any reply from my login... after pressing "post Reply "it takes me to a blank page
Thu Jan 22 2015, 04:06 AM
frankie
friend, that modification should be done in the program so that led up and down oscillation is one blink, thanks
Wed Jan 21 2015, 05:06 PM
Gargiarya
to get a clear view of seven segment with 8051 visit http://www.mechaterrain.com/seven-segment-fixed-display-using-8051

Downloads

Comments

nawfel
Sat Jan 17 2015, 07:27 AM
Rohi123
Sat Jan 10 2015, 11:00 AM
safakarici
Fri Jan 09 2015, 08:55 PM
Nikita_Pinjare
Thu Jan 08 2015, 03:10 PM
mayank058
Wed Jan 07 2015, 05:43 AM
Overeating
Tue Jan 06 2015, 05:56 AM
trancebreaker11
Sun Jan 04 2015, 07:42 AM
akash474001
Thu Jan 01 2015, 05:49 AM

Online

Guests: 34, Members: 0 ...

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

Members: 37573
Newest member: nawfel
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Jan 25 2015, 08:19 PM
According to this page the output is fairly linear...
mayank058 Avatar
Posted By mayank058
Thu Jan 22 2015, 12:16 PM
dear sir, Now i want to generate a si...
mayank058 Avatar
Posted By mayank058
Tue Jan 20 2015, 06:40 AM
dear sir, Now its working properly. Th...
ajay_bhargav Avatar
Posted By ajay_bhargav
Tue Jan 20 2015, 04:36 AM
ajay_bhargav Avatar
Posted By ajay_bhargav
Tue Jan 20 2015, 04:35 AM
I found another issue with your code:calling PWMIn...
mayank058 Avatar
Posted By mayank058
Mon Jan 19 2015, 05:47 AM
dear sir, Please do it for me..
PsySc0rpi0n Avatar
Posted By PsySc0rpi0n
Sun Jan 18 2015, 08:23 PM
But it's a way to debug to consider to the future ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Sun Jan 18 2015, 06:04 AM
I remember i created small library for lpc23xx ser...
ajay_bhargav Avatar
Posted By ajay_bhargav
Sun Jan 18 2015, 06:00 AM
It's obviously a personal choice. Digging into map...
ajay_bhargav Avatar
Posted By ajay_bhargav
Sun Jan 18 2015, 05:58 AM
@karan123 I suggest you to create a new thread for...