EMbedded C Record
EMbedded C Record
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"
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));
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 :
}
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();
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;
}
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
}