Some C Routines for the PIC16F877A, written in C2C from PicantIDE

These are from my current project, a PIC Based Home control system

Starting with the Constant definitions

  //--------------------------//  
 //   LCD Display Functions  //
//--------------------------//
 
#define clear_lcd         0x01 /* Clear Display                        */
#define return_home       0x02 /* Cursor to Home position              */
#define entry_mode        0x06 /* Normal entry mode                    */
#define entry_mode_shift  0x07 /* - with shift                         */
#define system_set_8_bit  0x38 /* 8 bit data mode 2 line ( 5x7 font )  */
#define system_set_4_bit  0x28 /* 4 bit data mode 2 line ( 5x7 font )  */
#define display_on        0x0c /* Switch ON Display                    */
#define display_off       0x08 /* Cursor plus blink                    */
#define set_dd_line1      0x80 /* Line 1 position 1                    */
#define set_dd_line2      0xC0 /* Line 2 position 1                    */
#define set_dd_ram        0x80 /* Line 1 position 1                    */
#define write_data        0x00 /* With RS = 1                          */
#define cursor_on         0x0E /* Switch Cursor ON                     */
#define cursor_off        0x0C /* Switch Cursor OFF                    */

#define LCD_NONE			0
#define LCD_RS				1
#define LCD_RW				2
#define LCD_E				4

#define LCDControl 	 portb		/* LCD data lines are on port b */
#define LCDData		 porte		/* LCD control lines are on port e */


Low level LCD routines
 
 // Initialise the LCD Display
LCD_Init()
{
    	LCD_SendCTRL(clear_lcd);		
    	LCD_SendCTRL(return_home);		
    	LCD_SendCTRL(entry_mode);		
    	LCD_SendCTRL(cursor_on);		
    	LCD_SendCTRL(system_set_8_bit);	
}


// Send the Databyte to the LCD as a command 
LCD_SendCTRL(int sData)
{
	LCDControl = LCD_NONE;
	LCDData = sData;
	LCDControl = LCD_E;
	delay_ms(1);
	LCDControl =LCD_NONE; 
	delay_ms(1);
}

// Send the Databyte to the LCD as data
LCD_SendData(int sData)
{
	LCDControl = LCD_RS;
	LCDData = sData;
	LCDControl = LCD_E + LCD_RS ;
	delay_ms(1);
	LCDControl = LCD_RS;
	delay_ms(1);
}

// Set LCD to start of specific line
LCD_Line(int sData)
{
	LCD_SendCTRL( 0x80 + ( 0x40 * ( sData - 1 ) ) );
}

// Set LCD to Specific Line and Column
LCD_Pos(int iLine, int iPos)
{							// the LCD lines start at 0x00 for line 1 and 0x40 for line 2
                                    			// Take 1 from iPos and add 0x80 command value to it. (so add 0x7f)
	iPos = iPos + 0x7f;     			// first character position of each line is +0
    iLine--;    					// only add 0x40 if it is line 2
	LCD_SendCTRL( (0x40 * iLine) + iPos);		// The LCD position command is 0x80
}
Simulate a printf, and sending a decimal representation of a value to the LCD
Unless otherwise stated, the display routines start on the next character pointed to by the display
so you need to set the position before you call these.

// Send String to LCD at current Location
void Lprintf(const char *strData )
{
  char i;
  i = 0;
                // Check for end of string
    while( strData[i] != 0)
         {
             LCD_SendData(strData[i++]);// Display on LCD 
         }
}

// The number of digits indicates whether to write   9, 09 or 009 for a value of 9. The maximum digits is 3.
void LCD_PrintBCD(char hex, int digits)
{
	if (digits == 3)
	{ 
		if(hex > 99)
			LCD_SendData(((hex / 100) % 10) + '0');
		else
			LCD_SendData(48);
	}
    if (digits >= 2)
    {
    if(hex > 9)
        LCD_SendData(((hex / 10) % 10) + '0');
    else
		LCD_SendData(48);
    }
    LCD_SendData((hex % 10) + '0');
}   
This routine assumes that there is a set of global values holding the
date and time. These are all identifiable in the code sample

   
//---- Show time on LCD display. HH:MM 
// currently from iHour and iMin registers
// 
void LCD_Time()
{
		LCD_PrintBCD(iHour,2);
		Lprintf(":");
		LCD_PrintBCD(iMin,2);

}	

//--- Show date on LCD display dd/mm/yy
// currently from iDay, iMon, iYear
//
void LCD_Date()
{
		LCD_PrintBCD(iDay,2);
		Lprintf("/");
		LCD_PrintBCD(iMon,2);
		Lprintf("/");
		LCD_PrintBCD(iYear,2);
}	

Home | Pic Page | Projects