/**********************************************************************
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
}
}
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.
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) |
SD card specifications are maintained by the SD Card Association, which has over six hundred members. MiniSD and microSD cards are electrically compatible with the standard SD cards and can be inserted in special adapters and used as standard SD cards in standard card slots.
Читать дальше