KEMBAR78
Ee3017 Embedded C Programming LT P C 2 0 2 3 | PDF | Microcontroller | Computer Engineering
0% found this document useful (0 votes)
16 views36 pages

Ee3017 Embedded C Programming LT P C 2 0 2 3

The document outlines the objectives and experiments for the EE3017 Embedded C Programming course, focusing on embedded programming fundamentals and the GNU C Tool Chain. It includes hands-on laboratory exercises with the 8051 microcontroller, covering topics such as I/O port configuration, timer usage, and interfacing with hardware like LEDs and LCDs. The course aims to enhance students' employability skills through practical applications and projects in embedded C programming.

Uploaded by

SELVAEMI M 22EE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views36 pages

Ee3017 Embedded C Programming LT P C 2 0 2 3

The document outlines the objectives and experiments for the EE3017 Embedded C Programming course, focusing on embedded programming fundamentals and the GNU C Tool Chain. It includes hands-on laboratory exercises with the 8051 microcontroller, covering topics such as I/O port configuration, timer usage, and interfacing with hardware like LEDs and LCDs. The course aims to enhance students' employability skills through practical applications and projects in embedded C programming.

Uploaded by

SELVAEMI M 22EE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

EE3017 EMBEDDED C PROGRAMMING LT P C 2 0 2 3

OBJECTIVES:
• To expose the students to the fundamentals of embedded Programming
• To Introduce the GNU C Programming Tool Chain.
• To study the basic concepts of embedded C.
• To teach the basics of 8051 Programming
• To involve Discussions/ Practice/Exercise in revising & familiarizing the concepts acquired
over the 5 Units of the subject for improved employability skills.

LIST OF EXPERIMENTS
1. Laboratory exercise: Use Use 8051 microcontroller/Embedded processor/IDE/open source
platform to give hands-on training on Embedded C- programming
a) Introduction to IDE (like code blocks, vscode ,etc)and Programming Environment (like Keililu vision,
Proteus)
b) Configuring an I/O port using bitwise programming.
c) Configuring timer for generating hardware delay.
d) Flashing an LED using an interrupt
e) Interfacing 16x2 LCD with 8051
f) Configuring timer for generating PWM signal
g) Interfacing a stepper motor with 8051
2. Assignment: Introduction to Arduino IDE, Raspberry Pi
3. Embedded C-Programming -based Mini project.
TOTAL: 30 PERIODS OUTCOMES:
• Deliver insight into embedded C programming and its salient features for embedded Systems
• Illustrate the software and hardware architecture for distributed computing in embedded Systems
• Develop a solution for problems by using the concept learned in programming using the embedded
controllers
• Develop simple applications with 8051 by using its various features and interfacing with various
external hardware.
• Improved Employability and entrepreneurship capacity due to knowledge upgradation on recent
trends in embedded programming skills.

INDEX
S.No Name of Experiment Pg. No. Date of Sign
Completion

1 Introduction to IDE and


Programming Environment in keil
µVision.

2 Configuring an I/O port using


bitwise programming

3 Configuring timer for generating


hardware delay

4 Flashing an LED using an interrupt

5 Interfacing 16x2 LCD with 8051

6 Configuring timer for generating


PWM signal

Ex.No.1 Introduction to IDE and Programming Environment in keil µVision.

Date:

INTRODUCTION
Earlier to Microcontrollers, Microprocessors were greatly used for each and every purpose.
Microprocessors were containing ALU, general purpose register, stack pointer, program counter, clock counter
and so many other features which the today’s Microcontroller also possesses. But the difference between them
exists with respect to the number of instructions, access times, size, reliability, PCB size and so on.
Microprocessor contains large instruction set called as CISC processor whereas Microcontroller contains less
number of instructions and is called as RISC processor. The access time is less in case of microcontrollers
compared to microprocessors and
the PCB size reduces in case of microcontrollers. There are many versions of microcontrollers 8051, 80528751,
AT8951 from Atmel Corporation and many more. In this manual we will study about the 8051 architecture, its
features, programming and interfacing. MCS 8051 is an 8-bit single chip microcontroller with many built-in
functions and is the core for all MCS-51 devices.

The main features of the 8051 core are:


o Operates with single Power Supply +5V.
o 8-bit CPU optimized for control applications.
o 16-bit program counter (PC) and 16-bit data pointer (DPTR).
o 8-bit program status word (PSW).
o 8-bit stack pointer (SP).
o 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).
o 128 bytes of On-Chip Data Memory (Internal RAM):
o Four Register Banks, each containing 8 registers (R0 to R7) [Total 32 reg]
o 16-bytes of bit addressable memory.
o 80 bytes of general-purpose data memory (Scratch Pad Area).
o Special Function Registers (SFR) to configure/operate microcontroller.
o 32 bit bi-directional I/O Lines (4 ports P0 to P3). Two 16-bit timers/counters (T0 and
T1). o Full duplex UART (Universal Asynchronous Receiver/Transmitter).
o On-Chip oscillator and clock circuitry.

8051 PIN DIAGRAM

STEPS TO CREATE AND COMPILE Keil μVision:

μ
1. Double Click on the icon on the desktop. vision keil
2. Close any previous projects that were opened using – Project -> Close.
3. Start Project – New Project, and select the CPU from the device database
(Database-Atmel- ).On clicking ‘OK’, the following option is displayed. Choose ‘No’.
4. Create a source file (using File->New), type in the assembly or C program and save this
(filename.asm/filename.c) and add this source file to the project using either one of the
following two methods.
(i) Project->Manage->Components, Environment Books->add files-> browse to the required
file -> OK “OR”
ii) right click on the Source Group in the Project Window and the Add Files to Group option.
5. Set the Target options using -> Project – Options for Target opens the μ Options for Target
– Target configuration dialog. Set the Xtal (Crystal frequency) frequency as 11.0592 MHz,
and also the Options for Target – Debug – use either Simulator / Keil Monitor- 51 driver.
6. If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the COM
Port to which the board is connected and select the baud rate as 19200 or 9600 (recommended).
Enable Serial Interrupt option if the user application is not using on-chip UART, to stop
program execution.
7. Build the project; using Project - > Build Project. application and links. Any errors in the code are
indicated by – “Target not created” in the Build window, along with the error line. Debug the errors.
After an error free, to build go to Debug mode.
8. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog Or
by clicking in the icon.
9. The program is run using the Debug-Run command & halted using Debug-Stop Running.
Also the (reset, run, halt) icons can be used. Additional icons are (step, step over, and step into,
run till cursor).
10. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status, etc.
If it is a part-A program, the appropriate memory window is opened using View -> memory
window (for data RAM & XRAM locations), Watch window (for timer program), serial
window, etc.

How to create a new μProject?


Step 1: Give a double click on μvision 5 icon on the desk top, it will generate a window as shown below.
Step 2: To create new project go to project select new micro vision project.
Step 3: select a drive where you would like to create your project.
Step 4: Create a new folder and name it with your project name.
Step 5: Open that project folder and give a name of your project
executable file and save it.
Step 6: After saving it will show some window there you select your microcontroller company i.e NXP

fromPhillips.
Step 7: Select your chip as LPC2148
Step 8: After selecting chip click on OK then it will display some window asking to add STARTUP
file. Select YES.

Step 9: A target is created and startup filoe is added to your project target and is shown below.
Step 10:To write your project code select a new file from FILE menu bar.
S

tep 11: It will display some text editor, to save that file select SAVE option from FILE menu bar.
Step 12: By giving a file name lwith extension .C for c files and save
it.
Step 13: Write the code of your project and save it.
Step 14: To add our c file to target give a right click on Source Group, choose “ADD files to Group”

option
Step 15:It will displays some window there select the file you have to add and click on ADD option.
Step 16: The file will be added to our target and it shows in the project window.
Step 17: Now give a right click on target in the project window and
select “Options for Target”.
Step 18: It will shoe some window, in that go to output option and choose
Create Hex file option by selecting that box.
Step 19: In the same window go to Linker option and choose Use Memory Layout from Target Dialog
by selecting the box, and click OK.

Step 20: Now to Compile your project go to Project select Build Target option or press F7.
Step 21: In the build OUT PUT window you can see the errors and warnings if there in your code. And
here Your project Hex file will be created.
Result:
Ex.No.2 CONFIGURING AN I/O PORT USING BITWISE PROGRAMMING
– TOGGLING OF LED
Date:

Aim: Write an Embedded C program to toggle all the bits of P3 continuously.

Software Requirement:
✓ Keil u vision
Theory: -
This experiment aims to toggle all the bits of Port1 continuously using the software keil u vision 5.
Firstly the header files reg51.H is declared for the intended 8051. Then the main function starts. In
the main function an infinite for loop starts where each time 0x55 followed by 0xAA is sent to
Port1 and the loop goes on.
Program: -

#include<reg52.h>
void delay(unsigned int);
void main(void)
{
unsigned char mybyte;
mybyte=P3;
for(;;)
{
P3=0x35;
delay(100);
P3=0x67;
delay(100);
}
}
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++){
for (j=0;j<1976;j++);
}
}
Procedure:-
The procedure for the program of storing a data in accumulator is as follows. File ->new -
>Program(code) -> Save -> Add existing files to source group 1 -> Build -> Start Debug -> Run
- >Peripherals -> Port3 -> Debug (step) -> Debug the process

Observation:-

Result:
Ex.No.3 Configuring timer for generating hardware delay

Date:

Aim: Write a C program to generate a square wave for 1ms delay.

Software Requirement:
✓ Keil u vision
Theory:
Timer interrupt in 8051 Microcontroller. so, now before going into the details, let me first throw some
light on how we are gonna implement this. Timers count from 0 to 255 in 8 bit mode as in 8 bit 255 is
the maximum value and when timer hits the 255 number then we say that our timer is overflowed. Now
when timer overflows, then it sends us a indication using which we generate our intterupt. In timers,
there are few registers in which they store their value. If we are talking about Timer0 then timer0 stores
its value in TL0 register. Now suppose I want my timer to start counting from 10 instead 0 then I will
store 10 in my TL0 register and it will count from 10 instead 0 and when it reaches 255 it will
overflow. Now when Timer0 will overflow then it will make TF0 bit HIGH. TF0 is another register
value, if its 1 then it means that our timer is full and if its 0 then it means our timer is still counting. So,
that's how we count from our timer and check the pin TF0.
This experiment aims to generate a square wave for 1ms delay using the software keil u vision 5.
There are two ways to create a time delay in 8051 C: 1. Using a simple for loop 2. Using the
8051 timers.

TMOD register (Timer mode register):


TMOD

Timer1 Timer 0

7 6 5 4 3 2 1 0

Gate C/T M1 M0 Gate C/T M1 M0


✓ Gate Control
0 = Timer enabled
1 = Timer enabled if INTx is high
✓ C/T: Counter or Timer Selector
0 = Internal count source (clock/12)
1 = External count source T0/T1(P3.4/P3.5) pin.

✓ M1-M0: Mode Control


00-Mode 0, 13 bit count mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Split Timer mode

TCON (Timer Control register):


TCON is an 8-bit register. Its bits are used for generating interrupts internal or
external. The most important bits of the timer TR and TF are also in it.
TR (timer run) and TF (timer overflow) bits which we use in almost all over
timer applications are in it.

✓ TRx: Timer x run control


0 = Timer not running
1 = Timer running

✓ TFx: Timer x OverFlow flag


0 = Timer has not overflowed/rolled over
1 = Timer has overflowed/rolled over

✓ IT0/IT1: Timer Interrupts. when IT0=1 or IT1=1 It specifies interrupt on falling edge and when
IT0=0 or IT1=0 it specifies interrupt on rising edge.
✓ IE0/IE1: Used for external Interrupts.

Program: Mode 1

Program : Mode 2
Procedure:
The procedure for the program of storing
a data in accumulator is as follows. File new
Program(code) Save Add existing files to source group 1 Write P1 New Set up Analysis Window
Run Start Debug Build Close Run Debug the process

Observation:-
Result:
Ex.No.4 Flashing An LED Using An Interrupt

Date:

Aim: To write a Embedded C program to flash LED in port 3 using AT89S52 development board

Software Requirement:
✓ Keil Keil u vision
✓ Isp Flash Programmer

Hardware Requirements
✓ AT89S52 Development board

Theory
Timer interrupt in 8051 Microcontroller.Timers count from 0 to 255 in 8 bit mode as in 8 bit 255
is the maximum value and when timer hits the 255 number then we say that our timer is
overflowed. Now when timer overflows, then it sends us a indication using which we generate our
intterupt. In
timers, there are few registers in which they store their value. If we are talking about Timer0 then
timer0 stores its value in TL0 register. Now suppose I want my timer to start counting from 10
instead 0 then I will store 10 in my TL0 register and it will count from 10 instead 0 and when it
reaches 255 it will overflow. Now when Timer0 will overflow then it will make TF0 bit HIGH. TF0
is another register value, if its 1 then it means that our timer is full and if its 0 then it means our
timer is still counting

Product Layout of AT89S52 development board

PORT 3

P3.0 RXD/LED1

P3.1 TXD/LED2

P3.2 LED3

P3.3 LED4

P3.4 LED5

P3.5 LED6

P3.6 LED7

P3.7 LED8

Program:

#include<reg52.h>
#define LED P3;
void delay(unsigned int);
void main(void)
{
unsigned char mybyte;
mybyte=P3;
for(;;)
{
P3=0;
delay(250);
P3=1;
delay(250);
}
}
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++){
for (j=0;j<1976;j++);
}

Observation:-

Result:

Ex.No.5 Interfacing 16x2 LCD with 8051

Date:

Aim: To write a embedded c program to interface a 16x2 LCD to display string in 8051 microcontroller.
Hardware Requirement:
✓ AT89S52 Development board

Software Requirement:
✓ Keil Keil u vision
✓ Isp Flash Programmer

Theory:

Circuit diagram for LCD interfacing with 8051 microcontroller is shown in the above figure.

Program:
// Program for LCD Interfacing with 8051 Microcontroller (AT89S52)
#include<reg51.h>
#define display_port P0 //Data pins connected to port 0 on microcontroller
sbit rs = P1^0; //RS pin connected to pin 0 of port 1
sbit rw = P1^1; // RW pin connected to pin 1 of port 1
sbit e = P1^2; //E pin connected to pin 2 of port 1
void msdelay(unsigned int time) // Function for creating delay in
milliseconds. {
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command) //Function to send command instruction to LCD {
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1
msdelay(10);
}
void main()
{
unsigned char a[15]="CIRCUIT DIGEST"; //string of 14 characters with a null terminator. int
l=0;
lcd_init();
while(a[l] != '\0') // searching the null terminator in the sentence {
lcd_data(a[l]); l++;
msdelay(50);
}

Observation:-

Result:
Ex.No.6 Configuring Timer for Generating PWM Signal

Date:

Aim: Write an Embedded C Program to create square wave using PWM


Software Requirement:
Keil u vision 5
Theory: -
PWM, as the name suggests, is simply a pulse width modulation. We take a pulse and then we modulate its
width and make it small or big. Another term important while studying PWM is named duty cycle. The duty
cycle shows the duration for which the PWM pulse remains HIGH. Now if the pulse remains high for 50%
and LOW for 50% then we say that PWM pulse has a duty cycle of 50%. Similarly, if the pulse is HIGH for
70% and Low for 30% then it has a duty cycle of 70%.
Most of the microcontrollers have special pins assigned for PWM as in Arduino UNO .It has 6 PWM pins on
it. Similarly, PIC Microcontrollers also have PWM pins but unfortunately, the 8051 Microcontroller doesn't
have this luxury means there are no special PWM pins available in 8051 Microcontroller. But PWM is
necessary so we are going to manually generate the PWM pulse using Timer0 interrupt.

Program:
#include<reg52.h>
// PWM_Pin
sbit PWM_Pin = P2^0; // Pin P2.0 is named as PWM_Pin
// Function declarations
void cct_init(void);
void InitTimer0(void);
void InitPWM(void);
// Global variables
unsigned char PWM = 0; // It can have a value from 0 (0% duty cycle) to 255 (100% duty cycle) unsigned
int temp = 0; // Used inside Timer0 ISR
// PWM frequency selector
/* PWM_Freq_Num can have values in between 1 to 257 only
* When PWM_Freq_Num is equal to 1, then it means highest PWM frequency
* which is approximately 1000000/(1*255) = 3.9kHz
* When PWM_Freq_Num is equal to 257, then it means lowest PWM frequency
* which is approximately 1000000/(257*255) = 15Hz
*So, in general you can calculate PWM frequency by using the formula
*PWM Frequency = 1000000/(PWM_Freq_Num*255) */
#define PWM_Freq_Num 1 // Highest possible PWM Frequency
// Main Function
int main(void)
{
cct_init(); // Make all ports zero
InitPWM(); // Start PWM
PWM = 127; // Make 50% duty cycle of PWM
while(1) // Rest is done in Timer0 interrupt
{}
}
// Init CCT function
void cct_init(void)
{
P0 = 0x00;
P1 = 0x00;
P2 = 0x00;
P3 = 0x00;
}
// Timer0 initialize
void InitTimer0(void)
{
TMOD &= 0xF0; // Clear 4bit field for timer0
TMOD |= 0x01; // Set timer0 in mode 1 = 16bit mode
TH0 = 0x00; // First time value
TL0 = 0x00; // Set arbitrarily zero
ET0 = 1; // Enable Timer0 interrupts
EA = 1; // Global interrupt enable
TR0 = 1; // Start Timer 0
}
// PWM initialize
void InitPWM(void)
{
PWM = 0; // Initialize with 0% duty cycle
InitTimer0(); // Initialize timer0 to start generating interrupts
}
// Timer0 ISR // PWM generation code is written inside the Timer0 ISR void
Timer0_ISR (void) interrupt 1
{
TR0 = 0; // Stop Timer 0
if(PWM_Pin) // if PWM_Pin is high
{
PWM_Pin = 0;
temp = (255-PWM) *PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
}
else // if PWM_Pin is low
{
PWM_Pin = 1;
temp = PWM*PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
}
TF0 = 0; // Clear the interrupt flag
TR0 = 1; // Start Timer 0
}

Observation:-
Result:

You might also like