Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: matej
- *
- * Created on July 26, 2024, 11:45 AM
- */
- #include "config_bits.h"
- #define _XTAL_FREQ 8000000
- void oscillator_config() {
- OSCCONbits.IRCF = 0b111;
- OSCCONbits.SCS = 0b11;
- }
- void i2c_setup() {
- SSPCON1 = 0b00000000;
- SSPCON1bits.SSPEN = 1;
- SSPCON1bits.SSPM = 0b1000;
- SSPSTAT = 0b00000000;
- // SSPADD = (8000000 / (4 * 100000)) - 1;
- SSPADD = 19;
- }
- void i2c_start() {
- SEN = 1;
- while(!SSPIF);
- SSPIF = 0;
- }
- void i2c_stop() {
- PEN = 1;
- while(!SSPIF);
- SSPIF = 0;
- }
- void i2c_write(unsigned char data) {
- SSPBUF = data;
- while(!SSPIF);
- SSPIF = 0;
- }
- char i2c_read(unsigned char ack) {
- RCEN = 1;
- while(!SSPIF);
- SSPIF = 0;
- char received = SSPBUF;
- ACKDT = ack;
- ACKEN = 1;
- while(!SSPIF);
- SSPIF = 0;
- return received;
- }
- void ssd1306_command(unsigned char command) {
- i2c_start();
- i2c_write(0b01111010);
- i2c_write(0b00000000);
- i2c_write(command);
- i2c_stop();
- }
- void ssd1306_data(unsigned char data) {
- i2c_start();
- i2c_write(0b01111010);
- i2c_write(0b01000000);
- i2c_write(data);
- i2c_stop();
- }
- void ssd1306_init() {
- ssd1306_command(0b10101110); // Display OFF
- ssd1306_command(0b10101000); // Set MUX
- ssd1306_command(0b00111111); // 1/64 duty
- ssd1306_command(0b11010011); // Set Offset
- ssd1306_command(0b00000000); // No Offset
- ssd1306_command(0b01000000); // Set Display Start Line
- ssd1306_command(0b10100000); // Set Segment Re-map
- ssd1306_command(0b11000000); // Set COM Output Scan Direction
- ssd1306_command(0b11011010); // Set COM Pins Hardware Configuration
- ssd1306_command(0b00010010);
- ssd1306_command(0b10000001); // Set Contrast control
- ssd1306_command(0b01111111); // ?????
- ssd1306_command(0b10100100); // Set Entire Display ON
- ssd1306_command(0b10100110); // Set Normal Display
- ssd1306_command(0b11010101); // Set Oscillator Frequency
- ssd1306_command(0b10000000);
- ssd1306_command(0b10001101); // Enable charge pump regulator
- ssd1306_command(0b00010100);
- ssd1306_command(0b10101111); // Display ON
- }
- // Function to rotate the bitmap 90 degrees clockwise
- void rotateBitmap90DegreesClockwise(const unsigned char *original, unsigned char *rotated) {
- for (int row = 0; row < 8; row++) {
- for (int col = 0; col < 8; col++) {
- int newRow = col;
- int newCol = 7 - row;
- if (original[row] & (1 << col)) {
- rotated[newRow] |= (1 << newCol);
- }
- }
- }
- }
- void main(void) {
- TRISD0 = 0;
- LATD0 = 1;
- oscillator_config();
- i2c_setup();
- int time = 0;
- while(1) {
- i2c_start();
- i2c_write(0b11010000);
- i2c_write(0b00000000);
- i2c_stop();
- i2c_start();
- i2c_write(0b11010001);
- if(!ACKSTAT) {
- time = i2c_read(1);
- }
- i2c_stop();
- ssd1306_init();
- ssd1306_command(0b00100001); // Set column address
- ssd1306_command(0b00000000); // Start
- ssd1306_command(0b01111111); // End
- ssd1306_command(0b00100010); // Set page address
- ssd1306_command(0b00000000); // Start
- ssd1306_command(0b00000111); // End
- // Write the letter "M" bitmap to the display
- // unsigned char M_bitmap[8] = {
- // 0x00, // Row 0
- // 0x66, // Row 1
- // 0x66, // Row 2
- // 0x00, // Row 3
- // 0x00, // Row 4
- // 0x42, // Row 5
- // 0x3C, // Row 6
- // 0x00 // Row 7
- // };
- unsigned char M_bitmap[8] = {
- 0b00000000, // Row 0: 0x00
- 0b01100100, // Row 1: 0x64
- 0b01100010, // Row 2: 0x62
- 0b00000010, // Row 3: 0x02
- 0b00000010, // Row 4: 0x02
- 0b01100010, // Row 5: 0x62
- 0b01100100, // Row 6: 0x64
- 0b00000000 // Row 7: 0x00
- };
- // unsigned char M_rotated_bitmap[8] = {0};
- // rotateBitmap90DegreesClockwise(M_bitmap, M_rotated_bitmap);
- // Send the rotated bitmap to the display
- for (int i = 0; i < 8; i++) {
- ssd1306_data(M_bitmap[i]);
- }
- __delay_ms(1000);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement