Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C Programming for microcontrollers (Joe Pardue, 2005).pdf
Скачиваний:
260
Добавлен:
12.08.2013
Размер:
4.55 Mб
Скачать

Chapter 6: C Functions and Program Structures

Projects

Is anybody out there? Communicating with a PC

Most microcontrollers are buried deep in some device where they run in merry isolation from the rest of the world. Their programs are burned into them and never change. But there are many instances when we might want to communicate with a microcontroller. The Butterfly uses a joystick and an LCD, which is fine for its built-in applications. For anything more complex, like changing the microcontroller software, nothing beats using the PC’s RS232 serial communications port to communicate with the microcontroller through its Universal Synchronous Asynchronous Receiver Transmitter, USART, peripheral. The microcontroller and the PC must agree on the transmission speed in data bits per second, Baud rate, the number of bits per data unit, Data Bits, the parity of the data, Parity, the number of stop bits, Stop Bits, and Flow Control. (Refer to Constructing Your Development System section of Chapter 2 for the required settings) All this information is somewhat arcane and is legacy from even before Teletype machines. Fortunately the USART takes care of most of this stuff for you, so you don’t need to understand it. If you are really interested, get Jan Axelson’s Serial Port Complete (www.lvr.com).

What we need is a method to send commands and data from the PC and receive responses from the Butterfly. In this section we will develop a generic command interpreter skeleton that we will reuse in later programs. In this project we will use this skeleton to build a demonstration that let’s the PC send a command name and a number to the Butterfly. The Butterfly will respond with text.

We will put this software in four files:

PC_Comm.h

PC_Comm.c

Demonstrator.h

Demonstrator.C

The PC_Comm files have many things in them that are well beyond our C training at this point, so just copy them and don’t think too hard about it yet. We will revisit each function later as we increase our knowledge. You should have no

98

Chapter 6: C Functions and Program Structures

trouble understanding anything in the Demonstrator files. If you do, review. In future projects we will only need to make changes to Demonstrator.h and Demonstrator.c.

Demonstrator

Create a new PC Comm directory and in Programmer’s Notepad open a new C/C++ file and write:

// Demonstrator.h CommDemo version

void initializer(void);

void parseInput(char *);

void Comm1(char *); void Comm2(char *); void Comm3(char *); void Comm4(char *);

void responder(char *, char );

Save this file as Demonstrator.h.

In Programmer’s Notepad open a new C/C++ file and write:

// Demonstrator.c PC Comm version

#include "PC_Comm.h"

void initializer()

{

//Calibrate the oscillator: OSCCAL_calibration();

//Initialize the USART USARTinit();

//say hello

sendString("\rPC_Comm.c ready to communicate.\r"); // identify yourself specifically

sendString("\rYou are talking to the PC_Comm demo.\r");

}

void parseInput(char s[])

99

Chapter 6: C Functions and Program Structures

{

// parse first character switch (s[0])

{

case 'c':

if( (s[1] == 'o') && (s[2] == 'm') && (s[3] == 'm') ) switch (s[4]) // parse the fifth character

{

case 'a': Comm1(s); break;

case 'b': Comm2(s); break; case 'c': Comm3(s); break;

case 'd': Comm4(s); break; default:

sendString("\rYou sent: '"); sendChar(s[0]);

sendString("' - I don't understand.\r");

}

break; case 'd':

if( (s[1] == 'e') && (s[2] == 'm') && (s[3] == 'o') && (s[4] == '?') ) sendString("You are talking to the PC_Comm demo.\r");

break; case 'h':

if( (s[1] == 'e') && (s[2] == 'l') && (s[3] == 'l') && (s[4] == 'o') ) sendString("Hello yourself\r");

break;

default:

sendString("\rYou sent: '"); sendChar(s[0]);

sendString("' - I don't understand.\r"); break;

}

s[0] = '\0';

}

void Comm1(char s[])

{

responder(s,s[4]);

}

void Comm2(char s[])

{

responder(s,s[4]);

100

Chapter 6: C Functions and Program Structures

}

void Comm3(char s[])

{

responder(s,s[4]);

}

void Comm4(char s[])

{

responder(s,s[4]);

}

void responder(char s[], char c)

{

char sComm[11];

unsigned char i = 5, j = 0;

while( (s[i] != '\0') && (j <= 11) )

{

if( (s[i] >= '0') && (s[i] <= '9') )

{

sComm[j++] = s[i++];

}

else

{

sendString("Error - Comm"); sendChar(c);

sendString(" received a non integer: "); sendChar(s[i]);

sendChar('\r');

}

}

sComm[j] = '\0';

if(j>11)

{

sendString("Error - Comm"); sendChar(c);

sendString(" number too large\r"); sendChar('\r');

}

else

{

sendString("\rThank you for sending the number: "); sendString(sComm);

sendChar('\r');

}

}

101