|
#include<STC15F2K60S2.H>
typedef unsigned char u8;
// 下面的应该不需要定义
// sbit OLED_MISO = P1^4; //单主机模式下,没有数据交互用不到
// sbit OLED_SCLK=P1^5; //时钟
// sbit OLED_MOSI = P1^3; //主机模式下,数据发送
sbit OLED_RES=P1^0; //复位
sbit OLED_SS = P1^2; //片选
sbit OLED_DC=P1^1; //数据|命令选择
void spi_init()
{
SPDAT = 0;
SPSTAT |= 0XC0; //清SPI中断标志位和写保护 写1清除
SPCTL |= 0XDC; //SPEN=1 SPI使能打开 SSIG=1 忽略SPI_SS引脚P1^2 MSTR=1 固定的单主机模式
P_SW1 |= 0X0C; //SPI选择第一组
}
void spi_write_byte(u8 byte, bit flag) // D/C = 0 写命令 = 1 写数据
{
if(flag == 0)
{
OLED_DC = 0;
}
else
{
OLED_DC = 1;
}
OLED_SS = 0;
SPDAT = byte;
OLED_SS = 1;
OLED_DC = 1;
}
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void ssd1306init()
{
OLED_RES = 1;
Delay100ms();
Delay100ms();
OLED_RES = 0;
Delay100ms();
Delay100ms();
OLED_RES = 1;
spi_write_byte(0xAE,0);
spi_write_byte(0x00,0);
spi_write_byte(0x10,0);
spi_write_byte(0x40,0);
spi_write_byte(0x81,0);
spi_write_byte(0xCF,0);
spi_write_byte(0xA1,0);
spi_write_byte(0xC8,0);
spi_write_byte(0xA6,0);
spi_write_byte(0xA8,0);
spi_write_byte(0x3F,0);
spi_write_byte(0xD3,0);
spi_write_byte(0x00,0);
spi_write_byte(0xD5,0);
spi_write_byte(0x80,0);
spi_write_byte(0xD9,0);
spi_write_byte(0xF1,0);
spi_write_byte(0xDA,0);
spi_write_byte(0x12,0);
spi_write_byte(0xDB,0);
spi_write_byte(0x40,0);
spi_write_byte(0x20,0);
spi_write_byte(0x02,0);
spi_write_byte(0x8D,0);
spi_write_byte(0x14,0);
spi_write_byte(0xA4,0);
spi_write_byte(0xA6,0);
spi_write_byte(0xAF,0);
}
void oled_display_on()
{
spi_write_byte(0X8D,0);
spi_write_byte(0X14,0);
spi_write_byte(0XAf,0);
}
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
spi_write_byte(0xb0+i,0); //设置页地址(0~7)
spi_write_byte(0x00,0); //设置显示位置—列低地址
spi_write_byte(0x10,0); //设置显示位置—列高地址
for(n=0;n<128;n++)
{
spi_write_byte(0XFF,1);
}
}
}
void main()
{
spi_init();
IE2 |= 0X02;
EA = 1;
ssd1306init();
while(1)
{
Delay100ms();
OLED_Clear();
Delay100ms();
oled_display_on();
Delay100ms();
}
}
void spi() interrupt 9
{
SPSTAT = 0XC0; //清除标志位
}
////////////////////////////////////////////
屏幕就是没反应,示波器测时钟和MOSI有输出,请教一下问题出在哪里?
|
-
-
TE.rar
23.15 KB, 下载次数: 228
keil
|