Advertisement
Guest User

SSD1306

a guest
Jul 30th, 2024
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: matej
  4.  *
  5.  * Created on July 26, 2024, 11:45 AM
  6.  */
  7.  
  8. #include "config_bits.h"
  9.  
  10. #define _XTAL_FREQ 8000000
  11.  
  12. void oscillator_config() {
  13.     OSCCONbits.IRCF = 0b111;
  14.     OSCCONbits.SCS = 0b11;
  15. }
  16.  
  17. void i2c_setup() {
  18.     SSPCON1 = 0b00000000;
  19.     SSPCON1bits.SSPEN = 1;
  20.     SSPCON1bits.SSPM = 0b1000;
  21.     SSPSTAT = 0b00000000;
  22. //    SSPADD = (8000000 / (4 * 100000)) - 1;
  23.     SSPADD = 19;
  24. }
  25.  
  26. void i2c_start() {
  27.     SEN = 1;
  28.     while(!SSPIF);
  29.     SSPIF = 0;
  30. }
  31.  
  32. void i2c_stop() {
  33.     PEN = 1;
  34.     while(!SSPIF);
  35.     SSPIF = 0;
  36. }
  37.  
  38. void i2c_write(unsigned char data) {
  39.     SSPBUF = data;
  40.     while(!SSPIF);
  41.     SSPIF = 0;
  42. }
  43.  
  44. char i2c_read(unsigned char ack) {
  45.     RCEN = 1;
  46.     while(!SSPIF);
  47.     SSPIF = 0;
  48.     char received = SSPBUF;
  49.  
  50.     ACKDT = ack;
  51.     ACKEN = 1;
  52.     while(!SSPIF);
  53.     SSPIF = 0;
  54.    
  55.     return received;
  56. }
  57.  
  58. void ssd1306_command(unsigned char command) {
  59.     i2c_start();
  60.     i2c_write(0b01111010);
  61.     i2c_write(0b00000000);
  62.     i2c_write(command);
  63.     i2c_stop();
  64. }
  65.  
  66. void ssd1306_data(unsigned char data) {
  67.     i2c_start();
  68.     i2c_write(0b01111010);
  69.     i2c_write(0b01000000);
  70.     i2c_write(data);
  71.     i2c_stop();
  72. }
  73.  
  74. void ssd1306_init() {
  75.     ssd1306_command(0b10101110); // Display OFF
  76.     ssd1306_command(0b10101000); // Set MUX
  77.     ssd1306_command(0b00111111); // 1/64 duty
  78.     ssd1306_command(0b11010011); // Set Offset
  79.     ssd1306_command(0b00000000); // No Offset
  80.     ssd1306_command(0b01000000); // Set Display Start Line
  81.     ssd1306_command(0b10100000); // Set Segment Re-map
  82.     ssd1306_command(0b11000000); // Set COM Output Scan Direction
  83.     ssd1306_command(0b11011010); // Set COM Pins Hardware Configuration
  84.     ssd1306_command(0b00010010);
  85.     ssd1306_command(0b10000001); // Set Contrast control
  86.     ssd1306_command(0b01111111); // ?????
  87.     ssd1306_command(0b10100100); // Set Entire Display ON
  88.     ssd1306_command(0b10100110); // Set Normal Display
  89.     ssd1306_command(0b11010101); // Set Oscillator Frequency
  90.     ssd1306_command(0b10000000);
  91.     ssd1306_command(0b10001101); // Enable charge pump regulator
  92.     ssd1306_command(0b00010100);
  93.     ssd1306_command(0b10101111); // Display ON    
  94. }
  95.  
  96.  
  97. // Function to rotate the bitmap 90 degrees clockwise
  98. void rotateBitmap90DegreesClockwise(const unsigned char *original, unsigned char *rotated) {
  99.     for (int row = 0; row < 8; row++) {
  100.         for (int col = 0; col < 8; col++) {
  101.             int newRow = col;
  102.             int newCol = 7 - row;
  103.             if (original[row] & (1 << col)) {
  104.                 rotated[newRow] |= (1 << newCol);
  105.             }
  106.         }
  107.     }
  108. }
  109.  
  110. void main(void) {
  111.     TRISD0 = 0;
  112.     LATD0 = 1;
  113.     oscillator_config();
  114.     i2c_setup();
  115.     int time = 0;
  116.     while(1) {
  117.         i2c_start();
  118.         i2c_write(0b11010000);
  119.         i2c_write(0b00000000);
  120.         i2c_stop();
  121.        
  122.         i2c_start();
  123.         i2c_write(0b11010001);
  124.         if(!ACKSTAT) {
  125.             time = i2c_read(1);
  126.         }
  127.            
  128.         i2c_stop();
  129.        
  130.         ssd1306_init();
  131.         ssd1306_command(0b00100001); // Set column address
  132.         ssd1306_command(0b00000000); // Start
  133.         ssd1306_command(0b01111111); // End
  134.        
  135.         ssd1306_command(0b00100010); // Set page address
  136.         ssd1306_command(0b00000000); // Start
  137.         ssd1306_command(0b00000111); // End
  138.  
  139.         // Write the letter "M" bitmap to the display
  140. //        unsigned char M_bitmap[8] = {
  141. //            0x00,  // Row 0
  142. //            0x66,  // Row 1
  143. //            0x66,  // Row 2
  144. //            0x00,  // Row 3
  145. //            0x00,  // Row 4
  146. //            0x42,  // Row 5
  147. //            0x3C,  // Row 6
  148. //            0x00   // Row 7
  149. //        };
  150.        
  151.         unsigned char M_bitmap[8] = {
  152.             0b00000000,  // Row 0: 0x00
  153.             0b01100100,  // Row 1: 0x64
  154.             0b01100010,  // Row 2: 0x62
  155.             0b00000010,  // Row 3: 0x02
  156.             0b00000010,  // Row 4: 0x02
  157.             0b01100010,  // Row 5: 0x62
  158.             0b01100100,  // Row 6: 0x64
  159.             0b00000000   // Row 7: 0x00
  160.         };
  161.  
  162. //         unsigned char M_rotated_bitmap[8] = {0};
  163. //        rotateBitmap90DegreesClockwise(M_bitmap, M_rotated_bitmap);
  164.  
  165.         // Send the rotated bitmap to the display
  166.         for (int i = 0; i < 8; i++) {
  167.             ssd1306_data(M_bitmap[i]);
  168.         }
  169.        
  170.         __delay_ms(1000);
  171.     }
  172.     return;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement