Файл: C Programming for microcontrollers (Joe Pardue, 2005).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 2460
Скачиваний: 2
Chapter 11 The Butterfly LCD
Chapter 11 The Butterfly LCD
I read a book (I think it was David Brin’s ‘Practice Effect’) where some primitive people found a digital watch with an LCD display. They were amazed that whoever made the thing was able to train all the little black bugs to run around and align themselves in such peculiar patterns. And that’s the extent of the detail I’ll give on the underlying technology of LCDs. We’ll concentrate instead on using C to train the little black bugs to do our tricks. If you must know the magic, then Atmel has an application note: AVR065: LCD Driver for the STK502 and AVR Butterfly available from their website that will get you deep into the gory details. And the Atmega169 data book has a nice chapter ‘LCD Controller’ that is a sure cure for insomnia.
To keep our ignorance even more intact we will begin by using software based on the LCD-Test software available on http://www.siwawi.arubi.unikl.de/avr_projects/#bf_app noting that the main.c file begins with the confidence building:
// mt - used for debugging only - may not work
However, with a few changes and some shoehorning it all into a demonstrator module, it works just fine. We use these functions without attempting to understand them. Another way to say this is that we will enhance our productivity by reusing existing code and conform to object oriented principles by not allowing ourselves to mess with perfectly good software.
Functions at our disposal in LCD_functions module:
•void LCD_putc(uint8_t digit, char character); o Writes a character to the LCD digit
•void LCD_puts(char *pStr, char scrollmode); o Writes a string to the LCD
o *pStr is a pointer to the string o scrollmode is not used
•void LCD_puts_f(const char *pFlashStr, char scrollmode); o Writes to the LCD a string stored in flash
o *pFlashStr is a pointer to the flash string
261
Chapter 11 The Butterfly LCD
oscrollmode is not used
•void LCD_Clear(void);
oClears the LCD
•void LCD_Colon(char show);
oIf show = 0 disables Colon, otherwise enables Colon
•char SetContrast(char input);
oUses the value of input from 0 to 15 to set the contrast
PC to LCD test program
Lets modify our messenger program so that we can send strings to the Butterfly to display on the LCD. We will also add commands to set the contrast, show/hide the colon, clear the display, set the flash rate, and send strings with flashing characters.
Instead of running off willy-nilly and writing software, lets start with a short specification of what we want to test from the PC users perspective.
We will test each function by sending the following command strings to the Butterfly:
•PUTCdigitcharacter to test LCD_putc(uint8_t digit, char character);
oSend PUTCdigitcharacter where character is a char to be displayed
and digit is the LCD segment to display the input on. For example PUTC6A will cause the character A to be displayed on the 6th LCD
digit.
oVerify function by seeing the correct char in the correct position
•PUTF to test LCD_puts_f(const char *pFlashStr, char scrollmode);
oVerify function by seeing the correct string on the LCD
•PUTSstring to test LCD_puts(char *pStr, char scrollmode);
oSend PUTSstring where string is a string to be displayed. For example PUTSHello World! will cause the LCD to display ‘Hello
World!’.
oVerify function by seeing the correct string on the LCD
•CLEAR to test LCD_Clear(void);
262
Chapter 11 The Butterfly LCD
oSend CLEAR while displaying text on the LCD
oVerify function by seeing the LCD clear
•COLON to test LCD_Colon(char show);
oSend COLON,on/off where on/off is either ON or OFF
oVerify function by seeing colons on LCD turn on or off
•SETC## to test char SetContrast(char input);
oSend SETC## where ## is from 0 to 15 and sets the contrast.
We will use this to design the functions needed on the Butterfly. We already have a command processor designed, so we will reuse that to call the functions on receipt of the correct command.
•PUTCdigitcharacter
o Call LCD_putc(digit, character);
oSend “Called LCD_putc” to PC where # are the values sent in decimal
•PUTF
oSet a pointer, *pFlashStr, to a string in flash
o Call LCD_puts_f(*pFlashStr, scrollmode);
oSend “Called LCD_puts_f” to the PC.
•PUTSstring
oLoad the string and point pStr to it.
oCall LCD_puts(*pStr, scrollmode);
oSend “Called LCD_puts with ‘string’” to the PC where string is the string sent.
•CLEAR
oCall LCD_Clear();
oSend “Called “LCD_Clear()” to the PC
•COLON#
oIf # == 1 call LCD_Colon(1);
o Else if # == 0 call LCD_Colon(0);
oSend “Called LCD_Colon” to the PC where # is the one sent.
•SETC##
oConvert ## characters to a numerical value ‘input’
o Call SetContrast(input);
263
Chapter 11 The Butterfly LCD
oSend “Called SetContrast to the PC where # is the decimal number sent.
NOTE: In LCD_driver.c must comment out #include “main.h”
Now that we have our specification we can run off willy-nilly and write the software.
We write the Demonstrator.h file:
// Demonstrator.h LCD demo version void initializer(void);
void parseInput(char *);
And the Demonstrator.c file:
// Demonstrator.c LCD demo version
#include "PC_Comm.h" #include "Demonstrator.h" #include "LCD_test.h" #include "LCD_driver.h" #include "LCD_functions.h"
// identify yourself specifically
const char TALKING_TO[] PROGMEM = "\r\rYou are talking to the \0"; const char WHO_DEMO[] PROGMEM = "'LCD' demo.\r\r\0";
// bad command
const char BAD_COMMAND1[] PROGMEM = "\rYou sent: '\0";
const char BAD_COMMAND2[] PROGMEM = "' - I don't understand.\r\0";
const char LCD_START_msg[] PROGMEM = "LCD demo\0";
void initializer()
{
//Calibrate the oscillator: OSCCAL_calibration();
//Initialize the USART USARTinit();
//initialize the LCD
LCD_Init();
// Display instructions on PC
264
Chapter 11 The Butterfly LCD
sendFString(TALKING_TO); sendFString(WHO_DEMO);
LCD_puts_f(LCD_START_msg, 1);
}
void parseInput(char s[])
{
// parse first character switch (s[0])
{
case 'd':
if( (s[1] == 'e') && (s[2] == 'm') && (s[3] == 'o') && (s[4] == '?') ) sendFString(TALKING_TO);
sendFString(WHO_DEMO); break;
case 'C':
if( (s[1] == 'L') && (s[2] == 'E') && (s[3] == 'A') && (s[4] == 'R')) OnCLEAR();
else if ((s[1] == 'O')&&(s[2] == 'L')&&(s[3] == 'O')&&(s[4] == 'N')) OnCOLON(s);
break; case 'P' :
if( (s[1] == 'U') && (s[2] == 'T') && (s[3] == 'C')) OnPUTC(s);
else if( (s[1] == 'U') && (s[2] == 'T') && (s[3] == 'F')) OnPUTF(s);
else if( (s[1] == 'U') && (s[2] == 'T') && (s[3] == 'S')) OnPUTS(s);
break; case 'S' :
if((s[1]== C')&&(s[2 =='R')&&(s[3]=='O')&&(s[4]=='L')&&(s[5] == 'L')) OnSCROLL(s);
else if( (s[1] == 'E') && (s[2] == 'T') && (s[3] == 'C') ) OnSETC(s);
break;
default: sendFString(BAD_COMMAND1); sendChar(s[0]); sendFString(BAD_COMMAND2); break;
s[0] = '\0';
}
}
We write, the Messenges.h, LCD_test.h and LCD_test.c files:
// Messages.h
265
Chapter 11 The Butterfly LCD
// LCD test messages
const char PUTF_msg[] PROGMEM = "Called LCD_puts_f \0"; const char PUTS_msg[] PROGMEM = "Called LCD_puts with \0"; const char PUTC_msg[] PROGMEM = "Called LCD_putc\r\0"; const char CLEAR_msg[] PROGMEM = "LCD_Clear()\r\0";
const char COLON_msg[] PROGMEM = "Called LCD_Colon\r\0"; const char SETC_msg[] PROGMEM = "Called SetContrast\r\0"; const char SCROLL_msg[] PROGMEM = "Called OnScroll\r\0";
// LCD_test.h
void OnPUTF(char *PUTFstr); void OnPUTS(char *pStr); void OnPUTC(char *PUTCstr); void OnCLEAR(void);
void OnCOLON(char *pOnoff); void OnSETC(char *SETCstr); void OnSCROLL(char *SCROLL);
// LCD_test.c
#include "LCD_driver.h" #include "LCD_functions.h" #include "LCD_test.h" #include "PC_Comm.h" #include "Messages.h"
// Start-up delay before scrolling a string over the LCD. "LCD_driver.c" extern char gLCD_Start_Scroll_Timer;
//PUTF,#
//Verify that # represents a valid string in flash. //Set the pFlashStr pointer to the string
//Call LCD_puts_f(*pFlashStr, scrollmode); //Send "Called LCD_puts_f" to the PC
void OnPUTF(char *PUTFstr)
{
sendFString(PUTF_msg);
PGM_P text;
text = PSTR("LCD_put_f test\0"); // won't show the _
LCD_puts_f(text, 1);
}
//PUTS,string
//Load the string and point pStr to it. //Call LCD_puts(*pStr, scrollmode);
266
Chapter 11 The Butterfly LCD
//Send "Called LCD_puts with 'string'" to the PC. void OnPUTS(char *pStr)
{
sendFString(PUTS_msg); sendString(pStr);
LCD_puts(&pStr[4],0); //Overlook the PUTS part of the string
}
//PUTC,digit,character
//Call LCD_putc(digit, character); //Send "Called LCD_putc" to PC. void OnPUTC(char *PUTCstr)
{
uint8_t digit;
sendFString(PUTC_msg);
digit = (uint8_t)(PUTCstr[4] - 48);// convert to integer
if(digit <= 6)
{
LCD_putc(digit,PUTCstr[5]); LCD_UpdateRequired(1,0);
}
}
//CLEAR
//Call LCD_Clear();
//Send "Called "LCD_Clear()" to the PC void OnCLEAR(void)
{
sendFString(CLEAR_msg); LCD_Clear();
}
//COLON,on/off
//Verify that on/off is either "ON" or "OFF" //If ON call LCD_Colon(1);
//Else call LCD_Colon(0);
//Send "Called LCD_Colon" to the PC. void OnCOLON(char *pOnoff)
{
sendFString(COLON_msg);
if(pOnoff[5] == '1')
{
LCD_Colon(1);
267
Chapter 11 The Butterfly LCD
}
else if (pOnoff[5] == '0')
{
LCD_Colon(0);
}
}
//SETC
//Call SetContrast(input);
//Send "Called SetContrast(#) to the PC where # is the decimal number //sent. Note values are from 0 to 15
void OnSETC(char *SETCstr)
{
char temp[] = {'\0','\0','\0'}; int input;
sendFString(SETC_msg);
temp[0] = SETCstr[4]; temp[1] = SETCstr[5];
input = atoi(temp);
SetContrast(input);
}
//SCROLL
//Start scroll if input == 1
//Stop scroll if input == 0
//Send “Called OnScroll” to the PC void OnSCROLL(char *scroll)
{
sendFString(SCROLL_msg);
if(scroll[6] == '1')
{
gScrollMode = 1; // Scroll if text is longer than display size gScroll = 0;
gLCD_Start_Scroll_Timer = 3; //Start-up delay before scrolling
}
else if (scroll[6] == '0')
{
gScrollMode = 0; gScroll = 0;
}
}
268
Chapter 11 The Butterfly LCD
Modify the makefile with:
SRC += Demonstrator.c \
LCD_test.c \
LCD_driver.c \
LCD_functions.c
And we make sure the LCD_driver and LCD_functions .h and .c files are in the same directory as the rest. Compile and download as usual.
Open Bray’s Terminal and connect to the Butterfly. You should see:
You are talking to the 'LCD' demo.
And the Butterfly LCD will be scrolling “LCD DEMO”
In the output window of Bray’s Terminal type:
CLEAR
The LCD will clear. Now type:
PUTC0A
PUTC1B
And you will see AB on the LCD. Type in:
SCROLL1
And the LCD will scroll the AB. Type in:
SCROLL0
And the LCD will stop scrolling the AB. Type in:
PUTF
And the LCD will show:
269