In: Computer Science
Write a Program using MPLAB IDE and XC8 compiler to do
the followings:
1. When the Program Starts it should send your Name and ID# to the
Serial Port
2. Display the result on Tera Term.
3. Read PORTB buttons in an infinite loop (Main Loop which is the
Read Buttons Loop), your program should not block when reading the
Buttons except in De-bouncing..
But you need to de-bounce each button when pressed
3.1 If the user presses PORTB, RB0 button, write Button 0 and
Toggle ALL PORTD buttons.
3.2 If the user presses PORTB, RB1 button, write Button 1 and
Toggle RD1
3.3 If the user presses PORTB, RB2 button, write Button 2 and
Toggle RD2
3.4 If the user presses PORTB, RB3 button, write Button 3 and Enter
a Read serial loop.
Display the bits of the Character that the user Enters on PORTD and
store in a buffer.
If the Number reaches 15 characters or the user enters '*', then
exit the read serial port loop and Display all characters (string)
entered on Ter Term.
Make sure you null terminate the string. But you will be still in
the Main Loop which is the read button Loop
3.5 If the user presses PORTB, RB4 button, terminate the Main Loop
which is the read button loop then print 'Exit' on the Tera term
and go to infinite loop doing nothing which is just while (true)
CLRWDT(); written after the main reading loop.
Pic18f4620
Assembly &c
Code
Ans)
#include <stdio.h>
//Implementing stack using array
//Basic operations - push and pop
int main()
{
int n, choice, stack[100], newelement, popelement, top, i;
choice = 0;
top = 0;
//Getting the stack size
printf("Enter the stack size\n");
scanf("%d", &n);
while(choice != 4) {
//Get the choice of operation in command line
printf("\n\n1.Push 2.Pop 3.Display 4.Exit\nEnter your choice");
scanf("%d", &choice);
switch(choice) {
case 1:
//Push a new element if the stack isnt full
if(top == n) {
printf("\n\nStack size is full");
}
else {
printf("\n\nEnter the element to push into the stack");
scanf("%d", &newelement);
stack[top] = newelement;
top++;
}
break;
case 2:
//Pop out the last element
if(top == 0) {
printf("\n\nStack is empty");
}
else {
top--;
popelement = stack[top];
printf("\n\nPopped Element: %d", popelement);
}
break;
case 3:
//Display the contents of the stack
printf("\n\nDisplaying Stack elements in reverse order of insertion: ");
for(i = top-1; i >= 0; i--) {
printf(" %d ", stack[i]);
}
break;
case 4:
//Exit
break;
default:
//Invalid choice
printf("\n\nEnter a valid choice");
}
}
return 0;
}
Logic explanation:
Stack is a data structure that follows LIFO(Last In First Out). In this program, stack has been implemented for integers using array in C. The basic operations of stack are push and pop. Push operation involves inserting new element into the stack while pop involves removing the last inserted element from the stack. Additionally, there is an option to display the contents of the stack in the reverse order.
--------------------------**********"""""***********------------------------
if your satisfy above answer please give positive rating or?
if any doubts below comment here
please don't downvote.only upvote or comment
Thank you!