请高手帮忙分析一下代码,看看那里有问题,现在代码能够烧写正常,也能运行,指示灯闪烁,就是OLED屏不能点亮?
#define SCL_HIGH GPIO_ResetBits(GPIOB, GPIO_Pin_8)
#define SCL_LOW GPIO_SetBits(GPIOB, GPIO_Pin_8)
#define SDA_HIGH GPIO_ResetBits(GPIOB, GPIO_Pin_9)
#define SDA_LOW GPIO_SetBits(GPIOB, GPIO_Pin_9)
void IIC_InitFunc(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
/* PB8,9 SCL and SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusDevice;//I2C_Mode_SMBusHost; //I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x30;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;//100K速度
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
/*允许1字节1应答模式*/
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
void OLED_Initial()
{
Write_IIC_Command(0xAE); //display off
Write_IIC_Command(0x20); //Set Memory Addressing Mode
Write_IIC_Command(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
Write_IIC_Command(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
Write_IIC_Command(0xc8); //Set COM Output Scan Direction
Write_IIC_Command(0x00);//---set low column address
Write_IIC_Command(0x10);//---set high column address
Write_IIC_Command(0x40);//--set start line address
Write_IIC_Command(0x81);//--set contrast control register
Write_IIC_Command(0x7f);
Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
Write_IIC_Command(0xa6);//--set normal display
Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
Write_IIC_Command(0x3F);//
Write_IIC_Command(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
Write_IIC_Command(0xd3);//-set display offset
Write_IIC_Command(0x00);//-not offset
Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
Write_IIC_Command(0xf0);//--set divide ratio
Write_IIC_Command(0xd9);//--set pre-charge period
Write_IIC_Command(0x22); //
Write_IIC_Command(0xda);//--set com pins hardware configuration
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);//--set vcomh
Write_IIC_Command(0x20);//0x20,0.77xVcc
Write_IIC_Command(0x8d);//--set DC-DC enable
Write_IIC_Command(0x14);//
Write_IIC_Command(0xaf);//--turn on oled panel
}
/**********************************************
//IIC Start
**********************************************/
void IIC_Start()
{
SCL_HIGH;
//Delay_us(1);
SDA_HIGH;
//Delay_us(1);
SDA_LOW;
// Delay_us(1);
SCL_LOW;
// Delay_us(1);
}
/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop()
{
SCL_LOW;
//Delay_us(1);
SDA_LOW;
//Delay_us(1);
SCL_HIGH;
//Delay_us(1);
SDA_HIGH;
//Delay_us(1);
}
/**********************************************
// IIC Write byte
**********************************************/
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(IIC_Byte & 0x80) //1?0?
SDA_HIGH;
else
SDA_LOW;
//Delay_us(1);
SCL_HIGH;
//Delay_us(1);
SCL_LOW;
//Delay_us(1);
IIC_Byte<<=1; //loop
}
SDA_HIGH;
//Delay_us(1);
SCL_HIGH;
//Delay_us(1);
SCL_LOW;
//Delay_us(1);
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78); //Slave address,SA0=0
Write_IIC_Byte(0x00); //write command
Write_IIC_Byte(IIC_Command);
IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78);
Write_IIC_Byte(0x40); //write data
Write_IIC_Byte(IIC_Data);
IIC_Stop();
}
/********************************************
// fill_Picture
********************************************/
void fill_picture(unsigned char fill_Data)
{
unsigned char m,n;
for(m=0;m<8;m++)
{
Write_IIC_Command(0xb0+m); //page0-page1
Write_IIC_Command(0x00); //low column start address
Write_IIC_Command(0x10); //high column start address
for(n=0;n<128;n++)
{
Write_IIC_Data(fill_Data);
}
}
}
/******************************************
// picture
******************************************/
void Picture_1()
{
unsigned char x,y;
// unsigned int i=0;
for(y=0;y<8;y++)
{
Write_IIC_Command(0xb0+y);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
for(x=0;x<128;x++)
{
Write_IIC_Data(show1[x++]);
}
}
}
/***********************Delay****************************************/
void Delay_50ms(unsigned int Del_50ms) //
{
unsigned int m;
for(;Del_50ms>0;Del_50ms--)
for(m=6245;m>0;m--);
}
void Delay_1ms(unsigned int Del_1ms) //
{
unsigned char j;
while(Del_1ms--)
{
for(j=0;j<123;j++);
}
}
int main(void)
{
LED_GPIO_Config();
delay_init(72); //延时初始化
IIC_InitFunc();
OLED_Initial();
while(1)
{
LED4_OFF;
delay_ms(500); //延时1s
LED4_ON;
delay_ms(500); //延时1s
fill_picture(0xff); delay_ms(1500);
fill_picture(0xf0); delay_ms(1500);
Picture_1(); Delay_50ms(1500);
}
}
|