0% found this document useful (0 votes)
7 views39 pages

EMbedded C Record

The document contains C code for UART communication and various arithmetic operations on an 8051 microcontroller, including addition, subtraction, multiplication, and division. It also includes circuit diagrams for interfacing with the microcontroller and examples of using a switch for operation selection. Additionally, there are examples for displaying numbers on a seven-segment display and handling user input for arithmetic operations.

Uploaded by

pk4074066
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)
7 views39 pages

EMbedded C Record

The document contains C code for UART communication and various arithmetic operations on an 8051 microcontroller, including addition, subtraction, multiplication, and division. It also includes circuit diagrams for interfacing with the microcontroller and examples of using a switch for operation selection. Additionally, there are examples for displaying numbers on a seven-segment display and handling user input for arithmetic operations.

Uploaded by

pk4074066
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/ 39

KJCSerial.

h
//*Step 1: Create uart.h (Header
File)/^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/
#ifndef UART_H
#define UART_H

// Function Prototypes
void SerialInit(void);
void serial(unsigned char x);
unsigned char Conv2Hex(unsigned char asc);
void SendByte2PC(unsigned char b);
void Printf(unsigned char s[]);
unsigned char ReadBytePC(void);

#endif // UART_H
/*Step 2: Create uart.c (C File with Function Definitions)
Place the following content in uart.c. This file will include
the header file uart.h
to ensure that all declarations match between the files.*/
//_____________________________
#include <reg51.h> // Register definitions for 8051
microcontroller
#include "uart.h"

#define TH1_VALUE 0xFD // TH1 = 226 for 9600 baud rate


with 11.0592 MHz crystal

void SerialInit()
{
TMOD = 0x20; // Timer 1, Mode 2 (8-bit auto-reload)
TH1 = TH1_VALUE; // Set timer reload value for baud
rate 9600
SCON = 0x50; // Configure UART in mode 1 (8-bit
UART)
TR1 = 1; // Start Timer 1
TI = 0; // Indicate transmit ready
RI = 0;
}
void serial(unsigned char x)
{
SBUF = x; // Place value in Buffer
while (TI == 0)
{
} // Wait until Transmitted
TI = 0;
}
unsigned char Conv2Hex(unsigned char asc)
{
switch (asc)
{
case 0x61:
case 0x41:
return (0x0a);
break;

case 0x62:
case 0x42:
return (0x0b);
break;

case 0x63:
case 0x43:
return (0x0c);
break;
case 0x64:
case 0x44:
return (0x0d);
break;

case 0x65:
case 0x45:
return (0x0e);
break;

case 0x66:
case 0x46:
return (0x0f);
break;
}
}
void SendByte2PC(unsigned char b)
{
if (((b & 0xf0) >> 4) > 9)
serial(0x37 + ((b & 0xf0) >> 4));
else
serial(0x30 + ((b & 0xf0) >> 4));

if (((b & 0x0f)) > 9)


serial(0x37 + ((b & 0x0f)));
else
serial(0x30 + (b & 0x0f));
}
void Printf(unsigned char s[])
{
char i;
i = 0;
serial(0x0d);
while (1)
{
if (s[i] == '\0')
break;
serial(s[i]);
i++;
}}
unsigned char ReadBytePC()
{
unsigned char RcvByte;
RcvByte = 0x00;
while (RI == 0)
;
if (SBUF > 0x40)
RcvByte = (Conv2Hex(SBUF) << 4);
else
RcvByte = ((0x0f & SBUF) << 4);
RI = 0;
serial(SBUF);
while (RI == 0)
;
if (SBUF > 0x40)
RcvByte = RcvByte + Conv2Hex(SBUF);
else
RcvByte = RcvByte + (0x0f & SBUF);
RI = 0;
serial(SBUF);
// byte2PC(RcvByte);
return (RcvByte);
}
 Circuit Diagram EX 01 :

U1
OUTPUT
19 39
XTAL1 P0.0/AD0
38
P0.1/AD1
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3
35
P0.4/AD4
34
P0.5/AD5
33
P0.6/AD6
9 32
RST P0.7/AD7
21
P2.0/A8
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 10
P1.0 P3.0/RXD RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0 TXD
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0 RTS
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/WR CTS
8 17
P1.7 P3.7/RD
AT89C51
EX 01 : Embedded C code for Addition
Programme :
void main() {
unsigned char a, b;
SerialInit();
Printf("Enter Num1 : ");
a=ReadBytePC();
Printf("Enter Num2 : ");
b=ReadBytePC();
Printf("Sum : ");
SendByte2PC(add(a, b));

while(1){
}}
 OUTPUT EX 02
EX 02 : Embedded C Code for Subtraction

Programme :
unsigned char sub(unsigned char a, unsigned char b) {
return a - b ;

}
void main() {
unsigned char a, b;
SerialInit();
Printf("Enter Num1 : ");
a=ReadBytePC();
Printf("Enter Num2 : ");
b=ReadBytePC();
Printf("Su : ");
SendByte2PC(sub(a, b));

while(1){
}}
 OUTPUT
EX 03 : Embedded C Code for Multipliction
Programme :

unsigned char mul(unsigned char a, unsigned char b)


{
return a * b ;

}
void main() {
unsigned char a, b;
SerialInit();
Printf("Enter Num1 : ");
a=ReadBytePC();
Printf("Enter Num2 : ");
b=ReadBytePC();
Printf("Mul : ");
SendByte2PC(mul(a, b));

while(1){
}}
 OUTPUT
EX 04 : Embedded C code for Division
Programme :
unsigned char div(unsigned char a, unsigned char b) {
return a / b ;
}
void main() {
unsigned char a, b;
SerialInit();
Printf(“Enter Num1 : “);
a=ReadBytePC();
Printf(“Enter Num2 : “);
b=ReadBytePC();
Printf(“Divide : “);
SendByte2PC(div(a, b));

while(1){
}}
 OUTPUT
EX 05 : Perform addition when a<b and
subtraction when a>b
Programme :
unsigned char add(unsigned char a , unsigned char b){
return a + b;
}
unsigned char sub(unsigned char a, unsigned char b){
return a-b;
}
void main(){
unsigned char a , b ;
SerialInit();
Printf("Enter Num1 : ");
a=ReadBytePC();
Printf("Enter Num2 : ");
b=ReadBytePc();
if(a<b){
Printf("Sum : ");
SendByte2PC(add(a,b));
}
else{
Printf("Subtract : ");
SendByte2PC(sub(a,b));
}}
 Circuit Diagram
U1
19 39
XTAL1 P0.0/AD0
38
P0.1/AD1
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3
35
P0.4/AD4
34
P0.5/AD5
33
P0.6/AD6
9 32
RST P0.7/AD7
21
P2.0/A8
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 10
P1.0 P3.0/RXD RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0 TXD
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0 RTS
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/WR CTS
8 17
P1.7 P3.7/RD
AT89C51

 OUTPUT
EX 06 : Perform Addition When Switch is
Pressed And Subtraction When Switch is
Released
Programme :
sbit SWITCH=P1^0;
void main(){
unsigned char a , b ;
SerialInit();
Printf("Enter Num1 : ");
a=ReadBytePC();
Printf("Enter Num2 : ");
b=ReadBytePc();
if(SWITCH==0){
Printf("Switch Pressed \n ");
Printf("Sum : ");
SendByte2PC(add(a,b));
}
else{
Printf("Switch Released \n");
Printf("Subtract : ");
SendByte2PC(sub(a,b));
}
while(1);
}
 Circuit Diagram
U1
19 39
XTAL1 P0.0/AD0
38
P0.1/AD1
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3
35
P0.4/AD4
34
P0.5/AD5
33
P0.6/AD6
9 32
RST P0.7/AD7
21
P2.0/A8
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 10
P1.0 P3.0/RXD RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0 TXD
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0 RTS
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/WR CTS
8 17
P1.7 P3.7/RD
AT89C51

 OUTPUT
EX 07 : Arithmetic Operation By User
Choice Using VT
Programme :
void main(){
SerialInit();
Printf("Enter First Num : ");
a=ReadBytePC();
Printf("Enter Second NUm : ");
b=ReadBytePC();
Printf(" 01. Addition \n");
Printf(" 02. subtraction \n");
Printf(" 03. Multiplication \n");
Printf(" 04. Division \n");
Printf(" Enter Your Choice ");
choice = ReadBytePC();
switch (choice) {
case 01 :
result = sum(a, b);
Printf("Addition result: ");
SendByte2PC(result);
break;
case 02 :
result = sub(a, b);
Printf("Subtraction result: ");
SendByte2PC(result);
break;
case 03 :
result = mul(a, b);
Printf("Multiplication result: ");
SendByte2PC(result);
break;
case 04 :
result = div(a, b);
Printf("Division result: ");
SendByte2PC(result);
break;
default :
Printf("Enter Valid Choice ");
choice=ReadBytePC();
break;
}
while(1);
{
}
}
 Circuit Diagram :

U1
19 39
XTAL1 P0.0/AD0
38
P0.1/AD1
37
P0.2/AD2
18 36
XTAL2 P0.3/AD3
35
P0.4/AD4
34
P0.5/AD5
33
P0.6/AD6
9 32
RST P0.7/AD7
21
P2.0/A8
22
P2.1/A9
23
P2.2/A10
29 24
PSEN P2.3/A11
30 25
ALE P2.4/A12
31 26
EA P2.5/A13
27
P2.6/A14
28
P2.7/A15
1 10
P1.0 P3.0/RXD
2 11
P1.1 P3.1/TXD
3 12
P1.2 P3.2/INT0
4 13
P1.3 P3.3/INT1
5 14
P1.4 P3.4/T0
6 15
P1.5 P3.5/T1
7 16
P1.6 P3.6/WR
8 17
P1.7 P3.7/RD
AT89C51
EX 08 : Interfacing seven segment
display and Print 0 to 9 and it reverse
Programme:
#include<reg51.h>
void main() {
unsigned char digits[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99,
0x92, 0x82, 0xF8, 0x80, 0x90};
int i, j, k;

while(1) {
for(i = 0; i < 10; i++) {
P1 = digits[i];
for(j = 0; j < 1000; j++) // Outer delay loop
for(k = 0; k < 100; k++);
}
for(i = 9; i >= 0; i--) {
P1 = digits[i];
for(j = 0; j < 1000; j++) // Outer delay loop
for(k = 0; k < 100; k++);
}}}
 Circuit Diagram And OUTPUT
EX 09 : Arithmetic Operation Using VT and
7sd
 Programme :
unsigned char seg_code[] = {0xC0, 0xF9, 0xA4, 0xB0,
0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
unsigned char num1, num2, result,choice;

void addition() {
result = num1 + num2;
P2 = seg_code[result / 10];
P1 = seg_code[result % 10];
}

void subtraction() {
result = num1 - num2;
P2 = seg_code[result / 10];
P1 = seg_code[result % 10];
}

void multiplication() {
result = num1 * num2;
P2 = seg_code[result / 10];
P1 = seg_code[result % 10];
}

void division() {
if (num2 != 0) {
result = num1 / num2;
P2 = seg_code[result / 10];
P1 = seg_code[result % 10];
} else {
Printf("Division by zero error.");
}
}

void main() {
SerialInit();

Printf("Enter 1st num (0-9): ");


num1 = ReadBytePC();
Printf("Enter 2nd num (0-9): ");
num2 = ReadBytePC();

Printf("01. Add\n 02. Subtract\n 03. Multiply\n 04.


Divide\n");
Printf("Enter Your Choice: ");
choice = ReadBytePC();

switch (choice) {
case 01:
addition();
break;
case 02:
subtraction();
break;
case 03:
multiplication();
break;
case 04:
division();
break;
default:
Printf("Enter Valid Choice\n");
break;
}

while (1); // Infinite loop to keep the result displayed


}
 Circuit Diagram And OUTPUT
EX 10 : Print String on LCD
Programme :
#include <reg51.h>

#define LCD_PORT P2 // Assume using Port 2 for data

sbit RS = P1^0; // Register Select pin


sbit RW = P1^1; // Read/Write pin
sbit E = P1^2; // Enable pin

void delay(unsigned int ms);


void lcd_command(unsigned char command);
void lcd_data(unsigned char character);
void lcd_init(void);
void lcd_print(char *str);

void main() {
lcd_init();
lcd_print("Jay Bhavani");
while (1); // Keep the message displayed
}

void lcd_init(void) {
delay(20); // Wait for LCD to power up
lcd_command(0x38); // 8-bit mode, 2 lines, 5x7 matrix
lcd_command(0x0C); // Display on, cursor off
lcd_command(0x01); // Clear display
delay(2); // Wait for clear command
}
void lcd_command(unsigned char command) {
RS = 0; // Command mode
RW = 0; // Write mode
LCD_PORT = command; // Send command
E = 1; // Enable pulse
delay(1);
E = 0; // Disable
}

void lcd_data(unsigned char character) {


RS = 1; // Data mode
RW = 0; // Write mode
LCD_PORT = character; // Send data
E = 1; // Enable pulse
delay(1);
E = 0; // Disable
}

void lcd_print(char *str) {


while (*str) {
lcd_data(*str++); // Send each character to LCD
}
}

void delay(unsigned int ms) {


unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 123; j++); // Adjust loop for
approximate delay
}

You might also like