KEMBAR78
Module 3c | PDF | Integer (Computer Science) | Data Type
0% found this document useful (0 votes)
21 views12 pages

Module 3c

The document discusses programming microcontrollers, specifically the 8051, using C language, emphasizing the importance of hex file size due to limited ROM. It covers data types suitable for the 8051, including unsigned char, signed char, unsigned int, and their applications in C programming. Additionally, it provides examples of C programs for various tasks, such as sending values to ports and creating time delays.

Uploaded by

Ms Sushma B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

Module 3c

The document discusses programming microcontrollers, specifically the 8051, using C language, emphasizing the importance of hex file size due to limited ROM. It covers data types suitable for the 8051, including unsigned char, signed char, unsigned int, and their applications in C programming. Additionally, it provides examples of C programs for various tasks, such as sending values to ports and creating time delays.

Uploaded by

Ms Sushma B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

‰ Compilers produce hex files that is

WHY downloaded to ROM of microcontroller


PROGRAM
¾ The size of hex file is the main concern
8051 IN C
ƒ Microcontrollers have limited on-chip ROM
ƒ Code space for 8051 is limited to 64K bytes
‰ C programming is less time consuming,
but has larger hex file size
‰ The reasons for writing programs in C
¾ It is easier and less time consuming to
write in C than Assembly
¾ C is easier to modify and update
¾ You can use code available in function
libraries
¾ C code is portable to other microcontroller
with little of no modification
Department of Computer Science and Information Engineering
HANEL National Cheng Kung University, TAIWAN 2
‰ A good understanding of C data types
DATA TYPES
for 8051 can help programmers to
create smaller hex files
¾ Unsigned char
¾ Signed char
¾ Unsigned int
¾ Signed int
¾ Sbit (single bit)
¾ Bit and sfr

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 3
‰ The character data type is the most
DATA TYPES
natural choice
Unsigned char ¾ 8051 is an 8-bit microcontroller
‰ Unsigned char is an 8-bit data type in
the range of 0 – 255 (00 – FFH)
¾ One of the most widely used data types
for the 8051
ƒ Counter value
ƒ ASCII characters
‰ C compilers use the signed char as the
default if we do not put the keyword
unsigned

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 4
Write an 8051 C program to send values 00 – FF to port P1.
Solution:
DATA TYPES 1. Pay careful attention to
#include <reg51.h> the size of the data
void main(void) 2. Try to use unsigned char
Unsigned char { instead of int if possible
(cont’) unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}

Write an 8051 C program to send hex values for ASCII characters of


0, 1, 2, 3, 4, 5, A, B, C, and D to port P1.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=“012345ABCD”;
unsigned char z;
for (z=0;z<=10;z++)
P1=mynum[z];
}

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 5
DATA TYPES Write an 8051 C program to toggle all the bits of P1 continuously.
Solution:
Unsigned char //Toggle P1 forever
(cont’) #include <reg51.h>
void main(void)
{
for (;;)
{
p1=0x55;
p1=0xAA;
}
}

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 6
‰ The signed char is an 8-bit data type
DATA TYPES ¾ Use the MSB D7 to represent – or +
¾ Give us values from –128 to +127
Signed char ‰ We should stick with the unsigned char
unless the data needs to be
represented as signed numbers
¾ temperature
Write an 8051 C program to send values of –4 to +4 to port P1.
Solution:
//Singed numbers
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=mynum[z];
}
Department of Computer Science and Information Engineering
HANEL National Cheng Kung University, TAIWAN 7
‰ The unsigned int is a 16-bit data type
DATA TYPES
¾ Takes a value in the range of 0 to 65535
Unsigned and (0000 – FFFFH)
Signed int ¾ Define 16-bit variables such as memory
addresses
¾ Set counter values of more than 256
¾ Since registers and memory accesses are
in 8-bit chunks, the misuse of int variables
will result in a larger hex file
‰ Signed int is a 16-bit data type
¾ Use the MSB D15 to represent – or +
¾ We have 15 bits for the magnitude of the
number from –32768 to +32767

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 8
DATA TYPES Write an 8051 C program to toggle bit D0 of the port P1 (P1.0)
50,000 times.
Single Bit Solution:
sbit keyword allows access to the
(cont’) #include <reg51.h> single bits of the SFR registers
sbit MYBIT=P1^0;

void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 9
‰ The bit data type allows access to
DATA TYPES
single bits of bit-addressable memory
Bit and sfr spaces 20 – 2FH
‰ To access the byte-size SFR registers,
we use the sfr data type
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 -32768 to +32767
sbit 1-bit SFR bit-addressable only
bit 1-bit RAM bit-addressable only
sfr 8-bit RAM addresses 80 – FFH only

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 10
‰ There are two way s to create a time
TIME DELAY delay in 8051 C
¾ Using the 8051 timer (Chap. 9)
¾ Using a simple for loop
be mindful of three factors that can affect
the accuracy of the delay
ƒ The 8051 design
– The number of machine cycle
– The number of clock periods per machine
cycle
ƒ The crystal frequency connected to the X1 – X2
input pins
ƒ Compiler choice
– C compiler converts the C statements and
functions to Assembly language instructions
– Different compilers produce different code

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 11
TIME DELAY Write an 8051 C program to toggle bits of P1 continuously forever
(cont’) with some delay.
Solution:
//Toggle P1 forever with some delay in between
//“on” and “off”
#include <reg51.h> We must use the oscilloscope to
void main(void)
measure the exact duration
{
unsigned int x;
for (;;) //repeat forever
{
p1=0x55;
for (x=0;x<40000;x++); //delay size
//unknown
p1=0xAA;
for (x=0;x<40000;x++);
}
}

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 12
TIME DELAY Write an 8051 C program to toggle bits of P1 ports continuously with
(cont’) a 250 ms.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
p1=0x55;
MSDelay(250);
p1=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
Department of Computer Science and Information Engineering
HANEL National Cheng Kung University, TAIWAN 13

You might also like