초보의 아웅다웅 설계하기/STM32

OLED QG-2864KSWLG01 내부 펌프 초기 코드

로망와니 2018. 9. 1. 21:04

OLED QG-2864KSWLG01를 구해서 프로그램을 넣었습니다.

웹에 도는 소스코드와 데이터시트로 구현을 하여도 동작을 하지 않았습니다.

명령어를 넣을 때 데이터 시트와 웹에서는 0x80을 실제로는 0x00을 넣어야했습니다.

그리고 ACK 자리에도 데이터 시트와 웹에서는 1을 실제로는 0을 넣어야 했습니다.

아래 샘플코드입니다.

I2C가 패리패럴로 구현되지 않고 IO로 구현되어있습니다.

RES는 회로상에서 NPN TR로 ON/OFF 해서 High와 Low가 역으로 되어있습니다.



#define OLED_ADDRESS 0x78


#define I2C_PORT GPIOB

#define I2C_SCL GPIO_Pin_6         

#define I2C_SDA GPIO_Pin_7   

#define LCDRSTPORT GPIOE

#define LCDRSTPIN GPIO_Pin_2

#define LCDENPORT GPIOB

#define LCDENPIN GPIO_Pin_5



/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

static void OLED_RES(FunctionalState NewState)

{

  if (NewState == DISABLE){

    GPIO_SetBits(LCDRSTPORT, LCDRSTPIN);

  }

  else{

    GPIO_ResetBits(LCDRSTPORT, LCDRSTPIN);

  }

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void OLED_EN(FunctionalState NewState)

{

  if (NewState == DISABLE){

    GPIO_ResetBits(LCDENPORT, LCDENPIN);

  }

  else{

    GPIO_SetBits(LCDENPORT, LCDENPIN);

  }

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void OLED_SDA(FunctionalState NewState)

{

  if (NewState == DISABLE){

    GPIO_ResetBits(I2C_PORT, I2C_SDA);

  }

  else{

    GPIO_SetBits(I2C_PORT, I2C_SDA);

  }

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void OLED_SCL(FunctionalState NewState)

{

  if (NewState == DISABLE){

    GPIO_ResetBits(I2C_PORT, I2C_SCL);

  }

  else{

    GPIO_SetBits(I2C_PORT, I2C_SCL);

  }

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void OLED_Config(void)

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);


OLED_RES(DISABLE);

OLED_EN(DISABLE);

GPIO_SetBits(I2C_PORT,I2C_SDA);

GPIO_SetBits(I2C_PORT,I2C_SCL);

/* LCD SDA/SCL */

GPIO_InitStructure.GPIO_Pin = I2C_SCL | I2C_SDA;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(I2C_PORT, &GPIO_InitStructure);  

/* LCD EN */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);  


/* LCD RST */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);  

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void I2C_WRITE(unsigned char dat)

{

u8 bit_cnt, tmp;

u8 bit_value;


for(bit_cnt=0; bit_cnt<8; bit_cnt++)

{

tmp = (dat << bit_cnt) & 0x80;

bit_value = (tmp >> 7) & 0x01;


if(bit_value == 1)

GPIO_SetBits(I2C_PORT,I2C_SDA);

else

GPIO_ResetBits(I2C_PORT,I2C_SDA);

delay_us(3);

GPIO_SetBits(I2C_PORT,I2C_SCL);

delay_us(3);

GPIO_ResetBits(I2C_PORT,I2C_SCL);

delay_us(3);

}

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void I2C_Start(void)

{

GPIO_SetBits(I2C_PORT,I2C_SCL);//SCL=1;

delay_us(3);

GPIO_SetBits(I2C_PORT,I2C_SDA);//SDA=1;

delay_us(3);

GPIO_ResetBits(I2C_PORT,I2C_SDA);//SDA=0;

delay_us(3);

GPIO_ResetBits(I2C_PORT,I2C_SCL);//SCL=0;

delay_us(3);

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void I2C_Stop(void)

{

GPIO_ResetBits(I2C_PORT,I2C_SDA);

delay_us(3);

GPIO_SetBits(I2C_PORT,I2C_SCL);

delay_us(3);

GPIO_SetBits(I2C_PORT,I2C_SDA);

delay_us(5);

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void I2C_Ack(u8 bit_value)

{

if(bit_value == 1)

GPIO_SetBits(I2C_PORT,I2C_SDA);

else

GPIO_ResetBits(I2C_PORT,I2C_SDA);


delay_us(3);

GPIO_SetBits(I2C_PORT,I2C_SCL);

delay_us(3);

GPIO_ResetBits(I2C_PORT,I2C_SCL);

delay_us(3);

GPIO_SetBits(I2C_PORT,I2C_SDA);

delay_us(3);

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void write_i(unsigned char ins)

{

I2C_Start();

I2C_WRITE(OLED_ADDRESS);

I2C_Ack(0);

I2C_WRITE(0x00);

I2C_Ack(0);

I2C_WRITE(ins);

I2C_Ack(0);

I2C_Stop();

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void write_d(unsigned char dat)

{

I2C_Start();

I2C_WRITE(OLED_ADDRESS);

I2C_Ack(0);

I2C_WRITE(0x40);

I2C_Ack(0);

I2C_WRITE(dat);

I2C_Ack(0);

I2C_Stop();

}          


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void OLED_Init_InternalPump(void)

{

OLED_EN(ENABLE);

delay_us(1000);


OLED_RES(ENABLE);////RES=1;  

delay_us(1000);

OLED_RES(DISABLE);//RES=0;   

delay_us(1000);      

OLED_RES(ENABLE);////RES=1;

delay_us(1000);


write_i(0xAE);    /*display off*/       


write_i(0x02);    /*set lower column address*/       

write_i(0x10);    /*set higher column address*/


write_i(0x40);    /*set display start line*/


write_i(0xB0);    /*set page address*/


write_i(0x81);    /*contract control*/

write_i(0x80);    /*128*/


write_i(0xA1);    /*set segment remap*/


write_i(0xA6);    /*normal / reverse*/


write_i(0xA8);    /*multiplex ratio*/

write_i(0x3F);    /*duty = 1/64*/


write_i(0xad);    /*set charge pump enable*/

write_i(0x8b);     /*    0x8B    ??VCC   */


write_i(0x33);    /*0X30---0X33  set VPP   9V */


write_i(0xC8);    /*Com scan direction*/


write_i(0xD3);    /*set display offset*/

write_i(0x00);   /*   0x20  */


write_i(0xD5);    /*set osc division*/

write_i(0x80);    


write_i(0xD9);    /*set pre-charge period*/

write_i(0x1f);    /*0x22*/


write_i(0xDA);    /*set COM pins*/

write_i(0x12);


write_i(0xdb);    /*set vcomh*/

write_i(0x30);           


Fill_RAM(0x00); // Clear Screen

write_i(0xAF);    /*display ON*/    

}


/*******************************************************************************

* Function Name : 

* Parameters    : 

* Return        : none

* Description   : 

*******************************************************************************/

void main(void)

{

OLED_Config();

OLED_Init_InternalPump();


}