0% found this document useful (0 votes)
47 views10 pages

Cam 4

Uploaded by

tenilsojan
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)
47 views10 pages

Cam 4

Uploaded by

tenilsojan
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/ 10

CAM3

CAM3 1
In Embedded C, a constant is a value that does not change during the program's execution.

Types of Constants:
1. Literal Constants:
These are values directly written in the code, such as:

Numbers: 10 , 100 , 3.14

Characters: 'A' , 'B'

2. Symbolic Constants:
These are names used for constant values, which are defined using:

#define directive:

#define PI 3.14

const keyword:

const int MAX_SPEED = 120;

Example:

#include <stdio.h>

#define MAX_TEMP 100 // Symbolic constant using #define


const float GRAVITY = 9.8; // Symbolic constant using const

int main() {
printf("Maximum temperature: %d\n", MAX_TEMP);
printf("Gravity: %.2f\n", GRAVITY);
return 0;
}

Why Use Constants?


Readability: Makes code easier to understand.

Safety: Prevents accidental changes to important values.

Maintainability: Allows easy updates to values in one place.

CAM3 2
CAM3 3
CAM3 4
To interface the 8051 microcontroller with an LCD device and display the characters 'H', 'E', 'L', 'L', 'O'
continuously with delays, you need to connect the LCD to the 8051 and use appropriate control and data pins.
Typically, the LCD is connected in 4-bit or 8-bit mode, but we will assume it's in 8-bit mode for simplicity.

LCD Pin Connections:


RS (Register Select): Selects command (RS = 0) or data (RS = 1)

RW (Read/Write): Controls read or write operation (RW = 0 for write)

EN (Enable): Enables the LCD to read data or commands

D0 to D7 (Data pins): 8-bit data pins for sending data/commands

VSS: Ground

VDD: Power (+5V)

VO (Contrast): Used to control the contrast (can be connected to a potentiometer)

Embedded C Code:

#include <reg51.h> // Include 8051 specific header file

// LCD control pins


#define RS P2^0 // Register Select pin connected to P2.0
#define RW P2^1 // Read/Write pin connected to P2.1
#define EN P2^2 // Enable pin connected to P2.2

// LCD data pins (connected to P1.0 to P1.7 for 8-bit mode)


#define LCD_DATA P1

// Function to send command to the LCD


void LCD_Command(unsigned char cmd) {
RS = 0; // Select command register
RW = 0; // Write mode
LCD_DATA = cmd; // Send command
EN = 1; // Enable LCD
delay(5); // Delay
EN = 0; // Disable LCD
}

// Function to send data to the LCD


void LCD_Data(unsigned char data) {
RS = 1; // Select data register
RW = 0; // Write mode

CAM3 5
LCD_DATA = data; // Send data
EN = 1; // Enable LCD
delay(5); // Delay
EN = 0; // Disable LCD
}

// Function to initialize the LCD


void LCD_Init() {
LCD_Command(0x38); // Initialize in 8-bit mode
LCD_Command(0x0C); // Display ON, Cursor OFF
LCD_Command(0x06); // Entry mode: Move cursor right
LCD_Command(0x01); // Clear display
delay(10); // Delay
}

// Delay function
void delay(unsigned int ms) {
unsigned int i, j;
for(i = 0; i < ms; i++) {
for(j = 0; j < 120; j++);
}
}

// Main function
void main() {
LCD_Init(); // Initialize the LCD

while(1) {
// Display 'H', 'E', 'L', 'L', 'O' continuously
LCD_Data('H');
delay(500); // Delay between characters
LCD_Data('E');
delay(500);
LCD_Data('L');
delay(500);
LCD_Data('L');
delay(500);
LCD_Data('O');
delay(500);
}
}

Explanation:
1. LCD_Command(): Sends a command to the LCD (e.g., clear screen, set cursor position).

2. LCD_Data(): Sends data (characters) to the LCD to display.

3. LCD_Init(): Initializes the LCD by sending a sequence of commands.

4. delay(): Provides a delay function to control the timing between characters.

5. Main Loop: In the main() function, the characters 'H', 'E', 'L', 'L', 'O' are displayed continuously with a delay
between each character.

Pin Configuration:
The RS , RW , and EN pins are connected to P2.0, P2.1, and P2.2, respectively.

CAM3 6
The data pins (D0-D7) are connected to the 8051's Port 1 (P1.0 to P1.7).

This code continuously displays the characters 'H', 'E', 'L', 'L', 'O' with a delay of 500 ms between each character
on the LCD connected to the 8051 microcontroller.
Here’s a simpler version of the 8051 assembly program to toggle all the bits of Port 0 every 1 ms using a
subroutine.

Simple Assembly Program:

ORG 0H ; Origin address

START:
MOV P0, #0FFH ; Set all bits of Port 0 to 1

TOGGLE:
CPL P0 ; Toggle all bits of Port 0
ACALL DELAY ; Call the delay subroutine
SJMP TOGGLE ; Repeat the toggle

; Delay subroutine to generate 1 ms delay


DELAY:
MOV R0, #250 ; Outer loop count
DELAY_LOOP:
MOV R1, #250 ; Inner loop count
INNER_LOOP:
NOP ; No operation (1 machine cycle)
NOP ; No operation (1 machine cycle)
DJNZ R1, INNER_LOOP ; Decrement R1 and loop
DJNZ R0, DELAY_LOOP ; Decrement R0 and loop
RET ; Return from subroutine

END

Explanation:
1. Main Program:

MOV P0, #0FFH : Initializes Port 0 with all bits set to 1.

CPL P0 : Toggles all bits of Port 0.

ACALL DELAY : Calls the delay subroutine to create a 1 ms delay.

SJMP TOGGLE : Repeats the toggle and delay indefinitely.

2. Delay Subroutine:

We use nested loops ( DELAY_LOOP and INNER_LOOP ) with NOP (No Operation) instructions to create a delay.

Each NOP takes 1 machine cycle (1 µs), and we create a delay of 1 ms by looping 250 times in each of the
two loops.

This program will toggle all bits of Port 0 every 1 ms continuously.

CAM3 7
Here are five common data types used in Embedded C:

1. char :

Used to store single characters or small integer values (usually 1 byte).

It can store values in the range of -128 to 127 (signed) or 0 to 255 (unsigned).

Example:

CAM3 8
char letter = 'A'; // stores a character
unsigned char count = 255; // stores an unsigned 8-bit value

2. int :

Used for storing integer values (usually 2 or 4 bytes depending on the system architecture).

The signed version can store values from -32,768 to 32,767 (for 2 bytes), or from -2,147,483,648 to
2,147,483,647 (for 4 bytes).

Example:

int number = 100; // stores a signed integer

3. float :

Used for storing floating-point numbers, i.e., numbers with decimal places (typically 4 bytes).

Example:

float temperature = 36.5; // stores a floating-point value

4. unsigned int :

Used for storing positive integer values without a sign (usually 2 or 4 bytes depending on the system).

Example:

unsigned int counter = 1000; // stores an unsigned integer

5. long :

Used for storing large integer values, typically 4 bytes.

It can hold a larger range of values compared to the regular int data type.

Example:

long largeValue = 123456789L; // stores a large integer value

These data types are essential for defining variables in embedded systems programming, where memory and
processing power are often limited. They allow precise control over how data is stored and manipulated in the
system.

CAM3 9
CAM3 10

You might also like