Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: abdullah
- *
- * Created on 19 Aralık 2012, Çarşamba, 22:05
- */
- #define _XTAL_FREQ 8000000 // XTAL frequency is 8MHz. This is needed for the __delay_us() function of the XC8.
- #include <xc.h> // Include the header file needed by the compiler
- #include "softwareRS232.h"
- #include "software_I2C.h"
- #define LCD_Backlight PORTAbits.RA0
- #define LCD_Backlight_TRIS TRISAbits.TRISA0
- __CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_ON & IOSCFS_8MHZ & BOREN_ON);
- /*
- * CONFIGURATION WORD:
- * -------------------
- * Internal Oscillator is set to 8 MHz.
- * I/O function on both of the oscillator pins, i.e on RA4 and on RA5.
- * Master clear is disabled. RA3 pin is digital input.
- * Power-up timer is enabled.
- * Watchdog timer is enabled.
- * Code protection is enabled.
- * Brown-out reset is enabled.
- */
- #define I2C_LCD_clearDisplay() { I2C_LCD_sendCommand(0x01); __delay_ms(2); }
- #define I2C_LCD_returnCursorHome() { I2C_LCD_sendCommand(0x02); __delay_ms(2); }
- #define I2C_LCD_shiftCursorRight() I2C_LCD_sendCommand(0x14)
- #define I2C_LCD_shiftCursorLeft() I2C_LCD_sendCommand(0x10)
- #define I2C_LCD_shiftDisplayRight() I2C_LCD_sendCommand(0x1C)
- #define I2C_LCD_shiftDisplayLeft() I2C_LCD_sendCommand(0x18)
- void I2C_LCD_sendCommand(unsigned char commandToBeSend)
- {
- software_I2C_startCondition();
- software_I2C_sendByte(0x7C);
- software_I2C_sendByte(0x00);
- software_I2C_sendByte(commandToBeSend);
- software_I2C_stopCondition();
- __delay_us(30);
- }
- void I2C_LCD_sendData(unsigned char dataToBeSend)
- {
- software_I2C_startCondition();
- software_I2C_sendByte(0x7C);
- software_I2C_sendByte(0x40);
- software_I2C_sendByte(dataToBeSend);
- software_I2C_stopCondition();
- __delay_us(30);
- }
- void I2C_LCD_write(unsigned char* stringToBeWritten)
- {
- while (*stringToBeWritten != '\0')
- {
- I2C_LCD_sendData(*stringToBeWritten);
- stringToBeWritten++;
- }
- }
- void I2C_LCD_displayControl(unsigned char display_ON_OFF, unsigned char cursor_ON_OFF, unsigned char cursorBlink_ON_OFF)
- {
- if ((display_ON_OFF > 1) || (cursor_ON_OFF > 1) || (cursorBlink_ON_OFF > 1))
- {
- I2C_LCD_sendCommand(0x0C);
- }
- else
- {
- I2C_LCD_sendCommand(0x08 + (unsigned char) (display_ON_OFF << 2) + (unsigned char) (cursor_ON_OFF << 1) + cursorBlink_ON_OFF);
- }
- }
- void I2C_LCD_goto(unsigned char row, unsigned char column)
- {
- if ((row == 1) && (column < 17) && (column != 0))
- {
- I2C_LCD_sendCommand(0x80 + column - 1);
- }
- else if ((row == 2) && (column < 17) && (column != 0))
- {
- I2C_LCD_sendCommand(0xC0 + column - 1);
- }
- }
- void I2C_LCD_setDisplayContrast(unsigned char contrastValue)
- {
- if (contrastValue < 0x10)
- {
- I2C_LCD_sendCommand(0x70 + contrastValue);
- }
- }
- void I2C_LCD_initialize(void)
- {
- I2C_LCD_sendCommand(0x38);
- I2C_LCD_sendCommand(0x39);
- I2C_LCD_sendCommand(0x14);
- I2C_LCD_setDisplayContrast(0x08);
- I2C_LCD_sendCommand(0x50);
- I2C_LCD_sendCommand(0x6C);
- __delay_ms(250);
- I2C_LCD_displayControl(1, 1, 1);
- I2C_LCD_clearDisplay();
- }
- void main(void)
- {
- unsigned char i = 0;
- __delay_ms(2000);
- ANSEL = 0; // All pins are digital input-output.
- LCD_Backlight_TRIS = 0;
- LCD_Backlight = 1; // Turn OFF the backlight (inverted logic)
- software_RS232_initialize();
- software_I2C_initialize();
- I2C_LCD_initialize();
- while (1)
- {
- I2C_LCD_displayControl(1, 0, 0);
- I2C_LCD_clearDisplay();
- LCD_Backlight = 0; // Turn ON the backlight (inverted logic)
- I2C_LCD_write("Starting LCD");
- I2C_LCD_goto(2, 1);
- I2C_LCD_write("self test...");
- __delay_ms(2000);
- I2C_LCD_displayControl(1, 1, 1);
- I2C_LCD_clearDisplay();
- I2C_LCD_write("Contrast test...");
- __delay_ms(1000);
- for (i = 0; i < 0x10; i++)
- {
- I2C_LCD_setDisplayContrast(i);
- __delay_ms(250);
- }
- I2C_LCD_setDisplayContrast(0x08);
- I2C_LCD_clearDisplay();
- I2C_LCD_write("Shifting test...");
- I2C_LCD_goto(1, 8);
- __delay_ms(1000);
- I2C_LCD_shiftCursorRight();
- __delay_ms(1000);
- I2C_LCD_shiftCursorLeft();
- __delay_ms(1000);
- I2C_LCD_shiftDisplayRight();
- __delay_ms(1000);
- I2C_LCD_shiftDisplayLeft();
- __delay_ms(1000);
- I2C_LCD_clearDisplay();
- I2C_LCD_write("Navigation...");
- __delay_ms(1000);
- I2C_LCD_goto(2, 16);
- __delay_ms(1000);
- I2C_LCD_returnCursorHome();
- __delay_ms(1000);
- I2C_LCD_clearDisplay();
- I2C_LCD_write("Backlight...");
- LCD_Backlight = 1;
- __delay_ms(1000);
- LCD_Backlight = 0;
- __delay_ms(1000);
- LCD_Backlight = 1;
- __delay_ms(1000);
- LCD_Backlight = 0;
- I2C_LCD_clearDisplay();
- I2C_LCD_write("End of test.");
- __delay_ms(3000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement