PIC16F15325 SSD1306 I2C表示試験
解決したいこと
PIC16F15325でSSD1306に何か表示したい。通信はI2C。
MPLABを使用して以下のプログラムを作成しましたが、なにも表示されません。
解決方法を教えて下さい。
設定は
MCC Classic
Device ResourcesからMSSP1追加 interrupt driven:off serial:I2C, mode:Master clock:100kHz
Foundation ServiceからI2CSIMPLE追加 select I2C master: MSSP1
Interrupt Module MSSP BCLI: on, MSSP SSPI: on, Pin Module : off
System Module Oscillator Select:HFINTOSC, Ext Clock Select: not enable, HF Internal Clock: 32MHz, Clock Divider: 1, WDT Enable: WDT Disabled
プログラムmain.cは以下の通りです。
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/examples/i2c1_master_example.h"
#define _XTAL_FREQ 32000000 // 32MHz
#define SSD1306_ADDR 0x78 // SSD1306 I2C address
#define SSD1306_CMD 0x00 // Command mode
#define SSD1306_DATA 0x40 // Data mode
// SSD1306 initialization commands
const uint8_t SSD1306_InitSeq[] = {
0xAE, // Display OFF
0xD5, 0x80, // Set display clock divide ratio/oscillator frequency
0xA8, 0x3F, // Set multiplex ratio (height of display)
0xD3, 0x00, // Set display offset
0x40, // Set start line
0x8D, 0x14, // Charge pump
0x20, 0x00, // Memory addressing mode (horizontal addressing)
0xA0, // Segment remap
0xC0, // COM scan direction
0xDA, 0x12, // Set COM pins hardware configuration
0x81, 0x7F, // Set contrast control
0xD9, 0xF1, // Set pre-charge period
0xDB, 0x40, // Set VCOMH deselect level
0xA4, // Entire display ON
0xA6, // Normal display
0xAF // Display ON
};
// Function to send command to SSD1306
void SSD1306_SendCommand(uint8_t command) {
I2C1_WriteNBytes(SSD1306_ADDR, &command, 1);
}
// Function to send data to SSD1306
void SSD1306_SendData(uint8_t data) {
I2C1_WriteNBytes(SSD1306_ADDR, &data, 1);
}
// Function to initialize SSD1306
void SSD1306_Init(void) {
for (uint8_t i = 0; i < sizeof(SSD1306_InitSeq); i++) {
SSD1306_SendCommand(SSD1306_CMD); // Command mode
SSD1306_SendData(SSD1306_InitSeq[i]);
}
}
// Function to clear the display (all pixels off)
void SSD1306_Clear(void) {
for (uint8_t page = 0; page < 8; page++) {
SSD1306_SendCommand(0xB0 + page); // Set page address
SSD1306_SendCommand(0x00); // Set lower column address
SSD1306_SendCommand(0x10); // Set higher column address
for (uint8_t col = 0; col < 128; col++) {
SSD1306_SendData(0x00); // Clear the entire display
}
}
}
// Function to set the cursor to the beginning of the display
void SSD1306_SetCursor(uint8_t x, uint8_t y) {
SSD1306_SendCommand(0xB0 + y); // Set page address
SSD1306_SendCommand(0x00 + (x & 0x0F)); // Set lower column address
SSD1306_SendCommand(0x10 + ((x >> 4) & 0x0F)); // Set higher column address
}
// Function to draw a horizontal line at a specified position
void SSD1306_DrawHorizontalLine(uint8_t x_start, uint8_t y_page, uint8_t length) {
SSD1306_SetCursor(x_start, y_page); // Set start position
for (uint8_t i = 0; i < length; i++) {
SSD1306_SendData(0xFF); // Draw pixel (all pixels on for the line)
}
}
// Function to draw a vertical line at a specified position
void SSD1306_DrawVerticalLine(uint8_t x, uint8_t y_start, uint8_t length) {
for (uint8_t i = y_start; i < y_start + length; i++) {
SSD1306_SetCursor(x, i); // Set vertical position
SSD1306_SendData(0x01); // Draw a single pixel
}
}
// Function to draw a simple box (rectangular shape)
void SSD1306_DrawRectangle(uint8_t x_start, uint8_t y_start, uint8_t width, uint8_t height) {
// Draw the top and bottom horizontal lines
for (uint8_t x = x_start; x < x_start + width; x++) {
SSD1306_DrawHorizontalLine(x, y_start, 1); // Top line
SSD1306_DrawHorizontalLine(x, y_start + height - 1, 1); // Bottom line
}
// Draw the left and right vertical lines
SSD1306_DrawVerticalLine(x_start, y_start, height); // Left line
SSD1306_DrawVerticalLine(x_start + width - 1, y_start, height); // Right line
}
/*
Main application
*/
void main(void) {
// Initialize the device
SYSTEM_Initialize();
//
INTERRUPT_GlobalInterruptEnable();
//
INTERRUPT_PeripheralInterruptEnable();
// Initialize SSD1306 display
SSD1306_Init();
SSD1306_Clear();
// Draw a rectangle on the screen
SSD1306_DrawRectangle(10, 2, 50, 30); // Position (10, 2), Size (50x30)
while (1) {
// Main loop code (nothing to do in this case)
__delay_ms(500);
}
}
自分で試したこと
アドレスを0x78,0x79,0x3c,0x3dに変更したが改善されません。