📄 lcd.c
字号:
#include <p18cxxx.h>
#include <p18f4520.h>
typedef unsigned char BYTE;
unsigned char arr1[] = "RTCC:Timer1:sync"; // Displayed on LCD for -
unsigned char arr2[] = "RA4=--> RB0=++"; // - user intercation
unsigned char arr3[] = " RB0 to start ";
unsigned char arr4[] = " RA4:Set Time? ";
unsigned char Flag = 0;
unsigned char hours; // Hours counter.
unsigned char minutes; // Minutes counter.
unsigned char seconds; // Second counter.
//unsigned char Flag = 0;
extern unsigned char MSD , MsD , LSD ; // For Bin2BCD function
void bin_2_bcd(unsigned int );
void Bin2BCD (unsigned char); // Coverts Binary Value to BCD and ASCII for LCD
unsigned char TenK,Thou,Hund,Tens,Ones;
unsigned char tempbcd[5];
unsigned char Most_Sig_Byte,Middle_Sig_Byte,Least_Sig_Byte;
void System_init(void);
//void Supportfn(void);
void Init_IO(void);
void initRTC_Registers(void);
//void Poll_Key_S3(void);
void Display_Start_MSG(void);
void Clear_Display(void);
void Display_MSG(void);
void Display_Time(void);
void Display_MSG1(void);
void set_time(void); // Sets time based on user input through switches (RA4, RB0)
//void DEBOUNCE_DELAY(void); // Debounce delay for switches
void displayHRS(unsigned char); // Displays Hours on LCD
void displaySEC(unsigned char); // Displays Seconds on LCD
void displayMNS(unsigned char); // Displays Minutes on LCD
void LCDwrite(BYTE); //Write characters to the LCD panel
void LCDInit(void ); //Initialize the LCD module
void Delay(void);
void Delay750us(void);
void LongDelay(void);
void LCDWriteNibble(BYTE,BYTE);
// 1st BYTE is nibble to write with nibble in upper four bits
// 2nd BYTE is 0x0 for instruction register, 0x1 for data register
void LCDbusy(void);
// NOTE: Busy bit does not work, this routine is just a time delay
void LCDLine1(void); //Set LCD to Line 1
void LCDLine2(void); //Set LCD to Line 2
void DisplayClr(void); //Clear LCD display
void text_display(unsigned char*);
#define LCD_PWR LATCbits.LATC7 // ; LCD power pin
#define LCD_EN LATCbits.LATC6 // ; LCD enable
#define LCD_RW LATCbits.LATC5 // ; LCD read/write line
#define LCD_RS LATCbits.LATC4 // ; LCD register select line
#define LCD_EN_DIR TRISCbits.TRISC6 // 6
#define LCD_RW_DIR TRISCbits.TRISC5 // 5
#define LCD_RS_DIR TRISCbits.TRISC4 // 4
void LCDInit()
{
LCD_PWR = 1;
TRISD = 0x00;
LCD_PWR=1; // to power up the LCD
// These values were obtained from Ocular tech support to properly
// initialize the LCD module
Delay();
LCDWriteNibble(0x20,0x0); //#1 control sequence
Delay();
LCDWriteNibble(0x20,0x0); //#2 control sequence
Delay750us();
LCDWriteNibble(0x80,0x0); //#3 control sequence
Delay750us();
LCDWriteNibble(0x00,0x0); //#4 control sequence
LCDWriteNibble(0xC0,0x0); //#4 control sequence
Delay750us();
LCDWriteNibble(0x00,0x0); //#5 control sequence
LCDWriteNibble(0x10,0x0); //#5 control sequence
Delay750us();
LongDelay();
LCDWriteNibble(0x00,0x0); //#6 control sequence
LCDWriteNibble(0x20,0x0); //#6 control sequence
Delay750us();
LongDelay();
LCDbusy();
LCDWriteNibble(0x40,0x1); //#7 control sequence
LCDWriteNibble(0xE0,0x1); //#7 control sequence
}
////////////////////////////////////////////////////
//Function to write data into the display
////////////////////////////////////////////////////
void LCDwrite(BYTE chardata)
{
LCDbusy();
LCDWriteNibble(chardata,0x1);
chardata=chardata<<4;
LCDbusy();
LCDWriteNibble(chardata,0x1);
}
////////////////////////////////////////////////////
// Fuction to goto Line1
////////////////////////////////////////////////////
void LCDLine1(void)
{
LCDbusy();// instruction 0x80 for going to line 1
LCDWriteNibble(0x80,0x0);
LCDbusy();
LCDWriteNibble(0x00,0x0);
}
////////////////////////////////////////////////////
// Fuction to goto Line2
////////////////////////////////////////////////////
void LCDLine2(void)
{
LCDbusy();// instruction 0xC0 for going to line 2
LCDWriteNibble(0xC0,0x0);
LCDbusy();
LCDWriteNibble(0x00,0x0);
}
/////////////////////////////////////////////////////////////////////////
// Fuction to write a nibble
// (can be inststucion or data depending on the second passed data 0 or 1)
//////////////////////////////////////////////////////////////////////////
void LCDWriteNibble( BYTE data,BYTE b)
{
BYTE tempdata;
tempdata=data;
if(b==0)
{
LCD_RS=0; //Select instruction register
}
else
{
LCD_RS=1; //Select data register
}
LCD_RW=0; //RW - set write mode
LATCbits.LATC0=0;
LATCbits.LATC1=0;
LATCbits.LATC2=0;
LATCbits.LATC3=0;
Nop();
Nop();
LCD_EN=1; //set up enable
/////////// Write Nibble //////////////////
//bit4
tempdata=(tempdata&0x80);
if( tempdata==0x80)
{
LATCbits.LATC3=1;
}
else
{
LATCbits.LATC3=0;
}
tempdata=data;
//bit3
tempdata=(tempdata&0x40);
if( tempdata==0x40)
{
LATCbits.LATC2=1;
}
else
{
LATCbits.LATC2=0;
}
tempdata=data;
//bit 2
tempdata=(tempdata&0x20);
if( tempdata==0x20)
{
LATCbits.LATC1=1;
}
else
{
LATCbits.LATC1=0;
}
tempdata=data;
//bit1
tempdata=(tempdata&0x10);
if( tempdata==0x10)
{
LATCbits.LATC0=1;
}
else
{
LATCbits.LATC0=0;
}
Nop();
Nop();
LCD_EN =0;
}
void Delay(void)
{
int i;
Nop();
for(i=0;i<10300;i++)
{
Nop();
}
}
void Delay750us(void)
{
int i;
Nop();
for(i=0;i<40;i++)
{
}
Nop();
}
void LongDelay(void)
{
int i;
Nop();
for(i=0;i<1000;i++)
{
Nop();
}
Nop();
}
void LCDbusy(void )
{
Delay750us();
Delay750us();
Delay750us();
Delay750us();
Delay750us();
Delay750us();
}
void text_display(unsigned char* text) // Single level pointer is used.
{
while (*text) // Continue display characters from STRING untill NULL character appears.
{
LCDwrite(*text); // Display selected character from the STRING.
text++; // Increment character pointer addressed in STRING.
}
}
void DisplayClr (void)
{
LCDbusy();
LCDWriteNibble(0x00, 0x0);
LCDWriteNibble(0x10, 0x0);
}
void Display_Start_MSG(void)
{
LCDLine1(); // Set cursor to Line 1 of LCD
text_display(arr1); // At the start up Display "RTCC:Timer1:sync"
LCDLine2(); // Select Line 2 of LCD module
text_display(arr3); // Display " RB0 to start " ; Program continues further after RB0 (PORTB.0) pressed.
}
/******************************************************************************
* void Clear_Display(void)
*****************************************************************************/
void Clear_Display(void)
{
DisplayClr(); // Clear Display
}
/******************************************************************************
* void Display_MSG(void)
*****************************************************************************/
void Display_MSG(void)
{
LCDLine2(); // Select Line 2 of LCD
text_display(arr4); // Display " RA4:Set Time?", press RA4 to set time if required.
}
/******************************************************************************
* void Display_MSG1(void)
*****************************************************************************/
void Display_MSG1(void)
{
LCDLine2(); // Select Line two of LCD
// cursor_on(); // Turn on cursor and have it blink
text_display(arr2); // Display "RA4=--> RB0=++", to interact with user
}
/******************************************************************************
* void Display_Time(void)
*****************************************************************************/
void Display_Time(void)
{
DisplayClr(); // Clear LCD display
LCDLine1(); // Set Cursor on LCD to line 1
displayHRS(hours); // Display Hours counted on LCD
displayMNS(minutes); // Display Minutes counted on LCD
displaySEC(seconds); // Display Seconds counted on LCD
}
/*****************************************************************************************
Display Functions
Sequnce:
1. Set Cursor to point Line one and first character
2. Display Hours (Format is explained inside each functions)
3. Display Minutes
4. Display Seconds.
******************************************************************************************/
void displayHRS(unsigned char hours)
{
// ***NOTE : Cursor has to be pointed to Line 1 before calling this
Bin2BCD(hours); // Convert Hours counted to BCD format
LCDwrite(Middle_Sig_Byte); // Display Middle digit (Most Digit 'MSD' is always ZERO)
LCDwrite(Least_Sig_Byte); // Display Least Digit
LCDwrite(':'); // Display Colon (End of Hours Field)
}
void displayMNS(unsigned char minutes)
{
Bin2BCD(minutes); // Convert Minutes counted to BCD format
LCDwrite(Middle_Sig_Byte); // Dsiplay Middle digit (Most Digit 'MSD' is always ZERO)
LCDwrite(Least_Sig_Byte); // Display Least Digit
LCDwrite(':'); // Display Colon (End of Minutes Field)
}
void displaySEC(unsigned char seconds)
{
Bin2BCD(seconds); // Convert Seconds counted to BCD format
LCDwrite(Middle_Sig_Byte); // Dsiplay Middle digit (Most Digit 'MSD' is always ZERO)
LCDwrite(Least_Sig_Byte); // Display Least Digit
}
/*****************************************************************************
Initialise RTC registers
******************************************************************************/
void initRTC_Registers(void)
{
seconds = 00; // Clear Seconds register
minutes = 00; // Clear minutes register
hours = 00; // Clear hours register
}
void bin_2_bcd(unsigned int counter)
{
int i;
for(i=0;i<5;i++)
tempbcd[i]=0;
while(counter!=0)
{
counter--;
tempbcd[0]++;
if(tempbcd[0]==0x0a)
{
tempbcd[0]=0;
tempbcd[1]++;
if(tempbcd[1]==0x0a)
{
tempbcd[1]=0;
tempbcd[2]++;
if(tempbcd[2]==0x0a)
{
tempbcd[2]=0;
tempbcd[3]++;
if(tempbcd[3]==0x0a)
{
tempbcd[3]=0;
tempbcd[4]++;
}
}
}
}
}
Hund = tempbcd[2];
Tens = tempbcd[1];
Ones = tempbcd[0];
}
void Bin2BCD (unsigned char temp)
{
Least_Sig_Byte = temp;
for (Most_Sig_Byte = 0 ; Least_Sig_Byte >= 100 ; Least_Sig_Byte>=100?(Least_Sig_Byte -= 100):Least_Sig_Byte, Most_Sig_Byte++ );
for (Middle_Sig_Byte = 0 ; Least_Sig_Byte >= 10 ; Least_Sig_Byte>=10?(Least_Sig_Byte -= 10):Least_Sig_Byte, Middle_Sig_Byte++);
Most_Sig_Byte += 0x30;
Middle_Sig_Byte += 0x30;
Least_Sig_Byte += 0x30;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -