Advertisement
abdullahkahraman

I2C-Bitbang-Main-File

Dec 21st, 2012
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: abdullah
  4.  *
  5.  * Created on 19 Aralık 2012, Çarşamba, 22:05
  6.  */
  7.  
  8. #define _XTAL_FREQ 8000000 // XTAL frequency is 8MHz. This is needed for the __delay_us() function of the XC8.
  9. #include <xc.h> // Include the header file needed by the compiler
  10. #include "softwareRS232.h"
  11. #include "software_I2C.h"
  12. #define LCD_Backlight PORTAbits.RA0
  13. #define LCD_Backlight_TRIS TRISAbits.TRISA0
  14.  
  15. __CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_ON & IOSCFS_8MHZ & BOREN_ON);
  16.  
  17. /*
  18.  * CONFIGURATION WORD:
  19.  * -------------------
  20.  * Internal Oscillator is set to 8 MHz.
  21.  * I/O function on both of the oscillator pins, i.e on RA4 and on RA5.
  22.  * Master clear is disabled. RA3 pin is digital input.
  23.  * Power-up timer is enabled.
  24.  * Watchdog timer is enabled.
  25.  * Code protection is enabled.
  26.  * Brown-out reset is enabled.
  27.  */
  28.  
  29. #define I2C_LCD_clearDisplay()          { I2C_LCD_sendCommand(0x01); __delay_ms(2); }
  30. #define I2C_LCD_returnCursorHome()      { I2C_LCD_sendCommand(0x02); __delay_ms(2); }
  31. #define I2C_LCD_shiftCursorRight()      I2C_LCD_sendCommand(0x14)
  32. #define I2C_LCD_shiftCursorLeft()       I2C_LCD_sendCommand(0x10)
  33. #define I2C_LCD_shiftDisplayRight()     I2C_LCD_sendCommand(0x1C)
  34. #define I2C_LCD_shiftDisplayLeft()      I2C_LCD_sendCommand(0x18)
  35.  
  36. void I2C_LCD_sendCommand(unsigned char commandToBeSend)
  37. {
  38.     software_I2C_startCondition();
  39.     software_I2C_sendByte(0x7C);
  40.     software_I2C_sendByte(0x00);
  41.     software_I2C_sendByte(commandToBeSend);
  42.     software_I2C_stopCondition();
  43.     __delay_us(30);
  44. }
  45.  
  46. void I2C_LCD_sendData(unsigned char dataToBeSend)
  47. {
  48.     software_I2C_startCondition();
  49.     software_I2C_sendByte(0x7C);
  50.     software_I2C_sendByte(0x40);
  51.     software_I2C_sendByte(dataToBeSend);
  52.     software_I2C_stopCondition();
  53.     __delay_us(30);
  54. }
  55.  
  56. void I2C_LCD_write(unsigned char* stringToBeWritten)
  57. {
  58.     while (*stringToBeWritten != '\0')
  59.     {
  60.         I2C_LCD_sendData(*stringToBeWritten);
  61.         stringToBeWritten++;
  62.     }
  63. }
  64.  
  65. void I2C_LCD_displayControl(unsigned char display_ON_OFF, unsigned char cursor_ON_OFF, unsigned char cursorBlink_ON_OFF)
  66. {
  67.     if ((display_ON_OFF > 1) || (cursor_ON_OFF > 1) || (cursorBlink_ON_OFF > 1))
  68.     {
  69.         I2C_LCD_sendCommand(0x0C);
  70.     }
  71.     else
  72.     {
  73.         I2C_LCD_sendCommand(0x08 + (unsigned char) (display_ON_OFF << 2) + (unsigned char) (cursor_ON_OFF << 1) + cursorBlink_ON_OFF);
  74.     }
  75. }
  76.  
  77. void I2C_LCD_goto(unsigned char row, unsigned char column)
  78. {
  79.     if ((row == 1) && (column < 17) && (column != 0))
  80.     {
  81.         I2C_LCD_sendCommand(0x80 + column - 1);
  82.     }
  83.     else if ((row == 2) && (column < 17) && (column != 0))
  84.     {
  85.         I2C_LCD_sendCommand(0xC0 + column - 1);
  86.     }
  87. }
  88.  
  89. void I2C_LCD_setDisplayContrast(unsigned char contrastValue)
  90. {
  91.     if (contrastValue < 0x10)
  92.     {
  93.         I2C_LCD_sendCommand(0x70 + contrastValue);
  94.     }
  95. }
  96.  
  97. void I2C_LCD_initialize(void)
  98. {
  99.     I2C_LCD_sendCommand(0x38);
  100.     I2C_LCD_sendCommand(0x39);
  101.     I2C_LCD_sendCommand(0x14);
  102.     I2C_LCD_setDisplayContrast(0x08);
  103.     I2C_LCD_sendCommand(0x50);
  104.     I2C_LCD_sendCommand(0x6C);
  105.     __delay_ms(250);
  106.     I2C_LCD_displayControl(1, 1, 1);
  107.     I2C_LCD_clearDisplay();
  108. }
  109.  
  110. void main(void)
  111. {
  112.     unsigned char i = 0;
  113.     __delay_ms(2000);
  114.     ANSEL = 0; // All pins are digital input-output.
  115.     LCD_Backlight_TRIS = 0;
  116.     LCD_Backlight = 1; // Turn OFF the backlight (inverted logic)
  117.  
  118.     software_RS232_initialize();
  119.     software_I2C_initialize();
  120.  
  121.     I2C_LCD_initialize();
  122.  
  123.     while (1)
  124.     {
  125.         I2C_LCD_displayControl(1, 0, 0);
  126.         I2C_LCD_clearDisplay();
  127.         LCD_Backlight = 0; // Turn ON the backlight (inverted logic)
  128.         I2C_LCD_write("Starting LCD");
  129.         I2C_LCD_goto(2, 1);
  130.         I2C_LCD_write("self test...");
  131.         __delay_ms(2000);
  132.         I2C_LCD_displayControl(1, 1, 1);
  133.  
  134.         I2C_LCD_clearDisplay();
  135.         I2C_LCD_write("Contrast test...");
  136.         __delay_ms(1000);
  137.         for (i = 0; i < 0x10; i++)
  138.         {
  139.             I2C_LCD_setDisplayContrast(i);
  140.             __delay_ms(250);
  141.         }
  142.         I2C_LCD_setDisplayContrast(0x08);
  143.  
  144.         I2C_LCD_clearDisplay();
  145.         I2C_LCD_write("Shifting test...");
  146.         I2C_LCD_goto(1, 8);
  147.         __delay_ms(1000);
  148.         I2C_LCD_shiftCursorRight();
  149.         __delay_ms(1000);
  150.         I2C_LCD_shiftCursorLeft();
  151.         __delay_ms(1000);
  152.         I2C_LCD_shiftDisplayRight();
  153.         __delay_ms(1000);
  154.         I2C_LCD_shiftDisplayLeft();
  155.         __delay_ms(1000);
  156.  
  157.         I2C_LCD_clearDisplay();
  158.         I2C_LCD_write("Navigation...");
  159.         __delay_ms(1000);
  160.         I2C_LCD_goto(2, 16);
  161.         __delay_ms(1000);
  162.         I2C_LCD_returnCursorHome();
  163.         __delay_ms(1000);
  164.  
  165.         I2C_LCD_clearDisplay();
  166.         I2C_LCD_write("Backlight...");
  167.         LCD_Backlight = 1;
  168.         __delay_ms(1000);
  169.         LCD_Backlight = 0;
  170.         __delay_ms(1000);
  171.         LCD_Backlight = 1;
  172.         __delay_ms(1000);
  173.         LCD_Backlight = 0;
  174.  
  175.         I2C_LCD_clearDisplay();
  176.         I2C_LCD_write("End of test.");
  177.         __delay_ms(3000);
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement