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

Using Software-Based Serial Communication

Using Software-Based Serial Communication

The preceding example made use of the microcontroller’s USART and thus its special serial I/O pins. Serial communication can also be handled entirely in software, without using the USART. In this method, any pin of the microcontroller can be used for serial communication.

The calculator program given in Project 10 can be reprogrammed using the mikroC software serial communications library functions known as the Software Uart Library.

The modified program listing is given in Figure 6.58. The circuit diagram of the project is same as in Figure 6.54 (i.e., RC6 and RC7 are used for serial TX and RX respectively), although any other port pins can also be used. At the beginning of the program the serial I/O port is configured by calling function Soft_Uart_Init. The serial port name, the pins used for TX and RX, the baud rate, and the mode are specified. The mode tells the microcontroller whether or not the data is inverted. Setting mode to 1 inverts the data. When a MAX232 chip is used, the data should be noninverted (i.e., mode = 0).

/**********************************************************************
                      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:
+ ? * /
In this program the serial communication is handled in software
and the serial port is configured to operate with 9600 Baud rate.
Port pins RC6 and RC7 are used for serial TX and RX respectively.
Author: Dogan Ibrahim
Date:   July 2007
File:   SERIAL2.C
**********************************************************************/
#define Enter 13
#define Plus '+'
#define Minus '?'
#define Multiply '*'
#define Divide '/'
//
// This function sends carriage-return and line-feed to USART
//
void Newline() {
 Soft_Uart_Write(0x0D); // Send carriage-return
 Soft_Uart_Write(0x0A); // Send line-feed
}
//
// This function sends a text to serial port
//
void Text_To_Usart(unsigned char *m) {
 unsigned char i;
 i = 0;
 while(m[i] != 0) { // Send TEXT to serial port
  Soft_Uart_Write(m[i]);
  i++;
 }
}
//
// Start of MAIN program
//
void main() {
 unsigned char MyKey, i,j,error,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 serial port
 //
 Soft_Uart_Init(PORTC,7,6,2400,0); // TX=RC6, RX=RC7, 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
  do                                // Get first number
  {
   do                               // If a character ready
    MyKey = Soft_Uart_Read(&error); // Get a character
   while (error);
   if (MyKey == Enter) break;       // If ENTER key
   Soft_Uart_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
  do                                // Get second number
  {
   do
    MyKey = Soft_Uart_Read(&error); // Get a character
   while(error);
   if (Mykey == Enter) break;       // If ENTER key
   Soft_Uart_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 {
   do
   MyKey = Soft_Uart_Read(&error); // Get a character
   while(error);
   if (MyKey == Enter) break;       // If ENTER key
   Soft_Uart_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++)Soft_Uart_Write(kbd[i]); // Display result
 }
}


Figure 6.58: Modified program

Serial data is then output using function Soft_Uart_Write. Serial data is input using function Soft_Uart_Read. As the reading is a nonblocking function, it is necessary to check whether or not a data byte is available before attempting to read. This is done using the error argument of the function. The remaining parts of the program are the same.

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


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