0% found this document useful (0 votes)
5 views40 pages

C Da 2 - Removed

The document outlines various experiments conducted in an Embedded C Programming Lab at VIT, focusing on tasks such as finding the largest number in an array, moving zeros to the end, counting vowels in a string, and performing bitwise operations. Each experiment includes algorithms, C code, input/output examples, and results demonstrating successful execution. The document emphasizes hands-on learning and practical applications in embedded systems.
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)
5 views40 pages

C Da 2 - Removed

The document outlines various experiments conducted in an Embedded C Programming Lab at VIT, focusing on tasks such as finding the largest number in an array, moving zeros to the end, counting vowels in a string, and performing bitwise operations. Each experiment includes algorithms, C code, input/output examples, and results demonstrating successful execution. The document emphasizes hands-on learning and practical applications in embedded systems.
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/ 40

SCHOOL OF ELECTRICAL ENGINEERING

VELLORE INSTITUTE OF TECHNOLOGY


VELLORE – 632 014, TAMIL NADU, INDIA

BECE320P – EMBEDDED C PROGRAMMING LAB


WINTER SEMESTER – 2024-2025
SLOT – L19+L20

Digital Assignment
Submitted by
Full Name: Sudarshan Ravindran

Registration No: 22BEE0330


Faculty Name: MARIMUTHU R
EXPERIMENT NO: 2A
Registration No: 22BEE0330
Date: 23/01/25

AIM
1. To find the largest number in an array.
2. To move all zeros to the end.

SOFTWARE
Any C compiler or Code blocks.

ALGORITHM:
1. Open your preferred IDE and start a new project.
2. Create a new C source file.
3. Add the file to your project.
4. Write the C program code.
5. Save your work.
6. Compile or build the project.
7. Debug and resolve any errors.
8. Execute the program.
9. Test it with different inputs.
10. Make modifications if needed.

AIM AND CODES:


1. To find the largest number in an array.

CODE:
#include <stdio.h>

void main() {
int a[10], max, n, i;

printf("enter num of elements");


scanf("%d", &n);

for (i = 0; i < n; i++) {


scanf("%d", &a[i]);
}

max = a[0];

for (i = 1; i < n; i++) {


if (a[i] > max) {
max = a[i];
}
}

printf("The largest element in the array is %d", max);


}

INPUT AND OUTPUT:


Enter num of elements: 6
3 4 5 7 9 10
The largest element in the array is 10

SIMULATION:
2. To move all zeros to the end.
CODE:
#include <stdio.h>

void main() {
int a[10], max, i, n;

printf("Enter the no. of elements: ");


scanf("%d", &n);

printf("Enter the numbers: ");


for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

int b[n], j = 0;

for(i = 0; i < n; i++) {


if(a[i] != 0) {
b[j] = a[i];
j++;
}
}

for(; j < n; j++) {


b[j] = 0;
}

printf("The array with zeros at the tail end: ");


for(i = 0; i < n; i++) {
printf("%d ", b[i]);
}
}

INPUT AND OUTPUT:


Input: Enter number of elements: 5
Enter the numbers:
06208
Output: The array with zeros at the end:
6 2 8 00

SIMULATION:
MANUAL CORRECTION:
RESULT:
The C program using sequential statements was successfully written, compiled, and
executed.
EXPERIMENT
NO: 2B
REG.NO:22BEE03
D ATE: 06.02.2025

AIM:
1. To count the number of vowels in a string
2. To count the number of p in a string
3. To take two strings and update the alternate characters
4. To take a string and copy the string only till M occurs
SOFTWARE
Any C compiler or Code blocks.
ALGORITHM:
1. Start a new project in your preferred IDE.
2. Create a new C source file
3. Add the new file to the project
4. Write your C program code
5. Save the file
6. Build or compile the project
7. Fix any errors
8. Run or execute the program
9. Test the program with various inputs
10. Modify the code if necessary

QUESTIONS AND CODES:


Q1: To count the number of vowels in a string
CODE:
#include <stdio.h>

int main() {
char str[10];
int count = 0, i;
printf("Enter a string: ");
scanf("%s", str);

for (i = 0; str[i] != '\0'; i++) {


if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
count++;
}
}

printf("Vowel count: %d\n", count);


return 0;
}

INPUT AND OUTPUT:


Input: Enter a string: Sudarshan
Output: Vowel count: 3
SIMULATION:
Q2. To count the number of p in a string
CODE:
#include <stdio.h>

int main() {
char str[10];
int count = 0, i;

printf("Enter a string: ");


scanf("%s", str);

for (i = 0; str[i] != '\0'; i++) {


if (str[i] == 'p') {
count++;
}
}

printf("Number of 'p' in the string: %d\n", count);


return 0;
}

INPUT AND OUTPUT:


Input: Enter a string: Appreciate
Output: Number of 'p' in the string: 2
SIMULATION:
Q3.To take two strings and update the alternate characters

CODE:
#include <stdio.h>
#include <string.h>

void updateAlternateChars(char str1[], char str2[]) {


int len1 = strlen(str1);
int len2 = strlen(str2);
int minLen = (len1 < len2) ? len1 : len2;

for (int i = 0; i < minLen; i += 2) {


str1[i] = str2[i];
}
}

int main() {
char str1[100], str2[100];

printf("Enter first string: ");


scanf("%s", str1);

printf("Enter second string: ");


scanf("%s", str2);

updateAlternateChars(str1, str2);

printf("Updated first string: %s\n", str1);


return 0;
}

INPUT AND OUTPUT:


Input: Enter first string: Sudarshan
Enter second string: yash
output: Updated first string: yusarshan
SIMULATION:
Q4. To take a string and copy the string only till M occurs

CODE:
#include <stdio.h>

int main() {
char str[100], newStr[100];
int i;

printf("Enter a string: ");


scanf("%s", str);

for (i = 0; str[i] != '\0' && str[i] != 'M'; i++) {


newStr[i] = str[i];
}

newStr[i] = '\0'; // Null-terminate the copied string

printf("Copied string: %s\n", newStr);


return 0;
}

INPUT AND OUTPUT:

INPUT :Enter a string: HelloMCoding


OUTPUT: Copied string: Hello
SIMULATION:
MANUAL CODE:
RESULT:
The C program using
control statements was
successfully written,
compiled and executed.
Name: Sudarshan Ravindran
Reg. No: 22BEE0330
Lab Slot: L19 + L20
Faculty: Prof. MARIMUTHU R
Assignment-3(a)
8051 Embedded-C programming
Date: 13-02-2025
Aim:
I. To send values from 0 to 255 to port(P1) serially.
II. To send ASCII values to port(P1).
III. To send signed numbers to port(P1).
IV. To toggle pins alternately between 55 & AA.
V. To toggle pins alternately for 250ms delay & check from logic analyser.
Algorithm:
I. Serially Sending Values from 0 to 255 to Port P1
1. Begin the process.
2. Configure Port P1 as an output port.
3. Iterate through numbers from 0 to 255: o Output each value to Port P1.
4. Complete the loop.
5. Terminate the process. II. Transmitting ASCII Values to Port P1
6. Start execution.
7. Set Port P1 as an output port.
8. Initialize a variable ch with the starting ASCII value (e.g., 32 for space or 65 for 'A').
9. Loop through the required ASCII range: o Send the value of ch to Port P1. o Increment ch to the next
ASCII character.
10. End the loop.
11. Stop execution. III. Sending Signed Numbers to Port P1
12. Begin execution.
13. Configure Port P1 as an output port.
14. Define an array containing signed numbers: {1, -1, 2, -2, 3, -3}.
15. Iterate through each element of the array: o Output the current element to Port P1.
16. End the loop.
17. Terminate the process. IV. Alternating Pattern Transmission to Port P1
18. Start execution.
19. Set Port P1 as an output port.
20. Run an infinite loop (or loop for a required duration): o Send 0x55 (binary 01010101) to Port P1. o
Introduce a brief delay. o Send 0xAA (binary 10101010) to Port P1. o Introduce another brief delay.
21. Repeat continuously.
22. Stop execution when required. V. Generating a 250ms Alternating Pattern on Port P1
23. Begin the process.
24. Configure Port P1 as an output port.
25. Execute a continuous loop (or run for a set duration): o Send 0x55 (binary 01010101) to Port P1. o
Introduce a 250ms delay. o Send 0xAA (binary 10101010) to Port P1. o Introduce another 250ms delay.
26. Continue the cycle.
27. Connect a logic analyzer to Port P1.
28. Verify that the alternating pattern follows the expected 250ms high-low transitions.
29. End the process.
Programs:
Outputs:
Conclusion:
Through these experiments, we effectively controlled Port P1 by transmitting values, toggling
pins, and managing signed numbers. This hands-on approach provided valuable insights into
hardware interactions, timing control, and debugging techniques using a logic analyzer. Such
fundamental concepts are essential for real-time embedded system development, enabling precise
performance optimization and efficient system design.
Assignment-3(b)
8051 Embedded-C programming
Date: 27-02-2025

Aim:
I. To get the status of bit P1.0, save it in bit addressable area, and send it to P2.7
continuously.
II. To perform basic logical operations (AND, OR, XOR, NOT, left shift, right shift).
III. To monitor the door sensor, and when it opens, sound the buzzer. You can sound the
buzzer by sending a square wave of a few hundred Hz.1 The door sensor is connected
to P1.1 pin and a buzzer is connected to P1.7 pin.
IV. To convert unpacked BCD to packed BCD & display the bytes on P1.
V. To calculate the checksum byte for the given data 0x25, 0x3C, 0x45 & 0x98.
Algorithm:
I. Reading a Bit, Storing, and Transferring to Another Bit
1. Start execution.
2. Configure P1.0 as an input pin.
3. Set P2.7 as an output pin.
4. Declare a bit variable in the bit-addressable area.
5. Enter an infinite loop: o Read the state of P1.0. o Store the read value in the bit variable.
o Assign the stored value to P2.7.
6. Repeat continuously.
7. Stop execution. II. Performing Basic Logic Operations
8. Start the process.
9. Declare two variables, A and B, with predefined values.
10.Execute the following operations: o AND: P0 = A & B o OR: P1 = A | B o XOR: P2 =
A^ B o NOT: P0 = ~A and P0 = ~B o Left Shift: P1 = A << n (Shift A left by n bits) o
Right Shift: P2 = A >> n (Shift A right by n bits)
11.Store or display the results.
12.Stop execution. III. Monitoring a Door Sensor and Activating a Buzzer
13.Begin execution.
14.Configure P1.1 as an input (Door Sensor) and P1.7 as an output (Buzzer).
15.Continuously check P1.1: o If P1.1 is HIGH (Door Open): ▪ Generate a square wave on
P1.7 by toggling it HIGH and LOW at a few hundred Hz with a small delay. o If P1.1 is
LOW (Door Closed): ▪ Keep P1.7 LOW (Buzzer OFF).
16.Repeat the process indefinitely.
17.Stop execution when required. IV. Converting Unpacked BCD to Packed BCD
18.Start execution.
19.Configure Port P1 as an output.
20.Input two unpacked BCD digits, x and y (each in 0x0X format).
21.Convert the unpacked BCD to packed BCD: o Left shift x by 4 bits: x = x << 4. o
Combine x and y using the OR operation: u = x | y.
22.Output the packed BCD value to P1.
23.Stop execution. V. Computing a Checksum Byte
24.Start execution.
25.Configure Port P1 as an output.
26.Initialize four data bytes: o A = 0x25, B = 0x3C, C = 0x45, D = 0x98.
27.Calculate the sum: Sum = A + B + C + D.
28.Compute the 2’s complement checksum: o Invert all bits: Checksum = ~Sum. o Add 1 to
the result: Checksum = Checksum + 1.
29.Output the checksum value to P1.
30.Stop execution.
Program:
Output:
Conclusion:
By conducting these experiments, we gained deeper insights into bitwise operations, sensor
interfacing, data formatting, and error detection. These fundamental concepts are crucial for
efficient data processing and real-time embedded system applications, enhancing our ability
to design and optimize reliable embedded solutions.

You might also like