Function Newline:
START
Send carriage-return to USART
Send line-feed to USART
END
Function Text_To_Usart
START
Get text from the argument
Send text to USART
END
Main program:
START
Configure USART to 9600 Baud
DO FOREVER
Display “CALCULATOR PROGRAM”
Display “Enter First Number: ”
Read first number
Display “Enter Second Number: ”
Read second number
Display “Operation: ”
Read operation
Perform operation
Display “Result= ”
Display the result
ENDDO
END
Figure 6.55: Project PDL
Project Program
The program listing of the project is shown in Figure 6.56. The program consists of a main program and two functions called Newline and Text_To_Usart. Function Newline sends a carriage return and line feed to the USART to move the cursor to the next line. Function Text_To_Usart sends a text message to the USART.
/*********************************************************************
CALCULATOR WITH PC INTERFACE
==============================
In this project a PC is connected to a PIC18F452 microcontroller. The
project is a simple integer calculator. User enters the numbers using
the PC keyboard. Results are displayed on the PC monitor.
The following operations can be performed:
+ − * /
This program uses the built in USART of the microcontroller. The USART is
configured to operate with 9600 Baud rate.
The serial TX pin is RC6 and the serial RX pin is RC7.
Author: Dogan Ibrahim
Date: July 2007
File: SERIAL1.C
*********************************************************************/
#define Enter 13
#define Plus '+'
#define Minus '−'
#define Multiply '*'
#define Divide '/'
//
// This function sends carriage-return and line-feed to USART
//
void Newline() {
Usart_Write(0x0D); // Send carriage-return
Usart_Write(0x0A); // Send line-feed
}
//
// This function sends a text to USART
//
void Text_To_Usart(unsigned char *m) {
unsigned char i;
i = 0;
while(m[i] != 0) { // Send TEXT to USART
Usart_Write(m[i]);
i++;
}
}
//
// Start of MAIN program
//
void main() {
unsigned char MyKey, i,j,kbd[5],op[12];
unsigned long Calc, Op1, Op2,Key;
unsigned char msg1[] = " CALCULATOR PROGRAM";
unsigned char msg2[] = " Enter First Number: ";
unsigned char msg3[] = "Enter Second Number: ";
unsigned char msg4[] = " Enter Operation: ";
unsigned char msg5[] = " Result = ";
//
// Configure the USART
//
Usart_Init(9600); // Baud=9600
//
// Program loop
//
for(;;) // Endless loop
{
MyKey = 0;
Op1 = 0;
Op2 = 0;
Newline(); // Send newline
Newline(); // Send newline
Text_To_Usart(msg1); // Send TEXT
Newline(); // Send newline
Newline(); // Send newline
//
// Get the first number
//
Text_To_Usart(msg2); // Send TEXT to USART
do // Get first number
{
if (Usart_Data_Ready()) // If a character ready
{
MyKey = Usart_Read(); // Get a character
if (MyKey == Enter) break; // If ENTER key
Usart_Write(MyKey); // Echo the character
Key = MyKey - '0';
Op1 = 10*Op1 + Key; // First number in Op1
}
} while(1);
Newline();
//
// Get the second character
//
Text_To_Usart(msg3); // Send TEXT to USART
do // Get second number
{
if (Usart_Data_Ready()) {
MyKey = Usart_Read(); // Get a character
if (Mykey == Enter) break; // If ENTER key
Usart_Write(MyKey); // Echo the character
Key = MyKey - '0';
Op2 = 10*Op2 + Key; // Second number in Op2
}
} while(1);
Newline();
//
// Get the operation
//
Text_To_Usart(msg4);
do {
if (Usart_Data_Ready()) {
MyKey = Usart_Read(); // Get a character
if (MyKey == Enter) break; // If ENTER key
Usart_Write(MyKey); // Echo the character
Key = MyKey;