Êíèãà: Advanced PIC Microcontroller Projects in C

Project Program

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;
  }
 } while(1);
 //
 // Perform the operation
 //
 Newline();
 switch(Key)          // Calculate
 {
 case Plus:
  Calc = Op1 + Op2;   // If ADD
  break;
 case Minus:
  Calc = Op1 - Op2;   // If Subtract
  break;
 case Multiply:
  Calc = Op1 * Op2;   // If Multiply
  break;
 case Divide:
  Calc = Op1 / Op2;   // If Divide
  break;
 }
 LongToStr(Calc, op); // Convert to string
 //
 // Remove leading blanks
 //
 j=0;
 for (i=0;i<=11;i++) {
  if (op[i] != ' ') // If a blank
  {
   kbd[j]=op[i];
   j++;
  }
 }
 Text_To_Usart(msg5);
 for(i=0; i<j; i++)Usart_Write(kbd[i]); // Display result
 }
}


Figure 6.56: Program listing

At the beginning of the program various messages used in the program are defined as msg1 to msg5. The USART is then initialized to 9600 baud using the mikroC library routine Usart_Init. Then the heading “CALCULATOR PROGRAM” is displayed on the PC monitor. The program reads the first number from the keyboard using the library function Usart_Read. Function Usart_Data_Ready checks when a new data byte is ready before reading it. Variable Op1 stores the first number. Similarly, another loop is formed and the second number is read into variable Op2. The program then reads the operation to be performed (+ – * /). The required operation is performed inside a switch statement and the result is stored in variable Calc. The program then converts the result into string format by calling library function LongToStr. Leading blanks are removed from this string, and the final result is stored in character array kbd and sent to the USART to display on the PC keyboard.

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 0.047. Çàïðîñîâ Ê ÁÄ/Cache: 0 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç