Выбрать главу

 //

 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.

CHAPTER 7

Advanced PIC18 Projects — SD Card Projects

In this and the remaining chapters we will look at the design of more complex PIC18 microcontroller–based projects. This chapter discusses the design of Secure Digital (SD) memory card–based projects. The remaining chapters of the book describe the basic theory and design of projects based on the popular USB bus and CAN bus protocols.

7.1 The SD Card

Before going into the design details of SD card–based projects, we should take a look at the basic principles and operation of SD card memory devices. Figure 7.1 shows a typical SD card.

Figure 7.1: A typical SD card

The SD card is a flash memory storage device designed to provide high-capacity, nonvolatile, and rewritable storage in a small size. These devices are frequently used in many electronic consumer goods, such as cameras, computers, GPS systems, mobile phones, and PDAs. The memory capacity of the SD cards is increasing all the time. Currently they are available at capacities from 256MB to 8GB. The SD cards come in three sizes: standard, mini, and micro. Table 7.1 lists the main specifications of the most common standard SD and miniSD cards.

Table 7.1: Standard SD and miniSD cards

  Standard SD miniSD
Dimensions 32 × 24 × 2.1mm 21.5 × 20 × 1.4mm
Card weight 2.0 grams 1.0 grams
Operating voltage 2.7–3.6V 2.7–3.6V
Write protect yes no
Pins 9 11
Interface SD or SPI SD or SPI
Current consumption <75mA (Write) <40mA (Write)