今天搞了一下新定义ES003板子上的OLED
SCL接PC0,SDA接PC1。
主程序代码:
#include "main.h"
#include "oled.h"
/** @addtogroup Examples
@{
*/
uint8_t temp;
uint8_t txBuffer[50];
uint8_t rxBuffer[10];
uint8_t cord_h,cord_l;//???????
volatile uint32_t nBytesRead = 0;
char CharData[] = { "bbs.21ic.com" };
char CharData1[] = { "APM32F107VC" };
/** @addtogroup USART_Printf
@{
*/
/** @addtogroup USART_Printf_Variables Variables
@{
*/
volatile uint32_t tick = 0;
uint8_t txBuf[] = "Key Value = : \r\n";
/**@} end of group USART_Printf_Variables */
void DelayMs(uint16_t nms)
{
uint16_t i,j;
for(i=0;i<nms;i++)
for(j=0;j<200;j++);
}
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program
*
* @param None
*
* @retval None
*
*/
int main(void)
{
uint8_t i;
GPIO_Config_T GPIO_ConfigStruct;
USART_Config_T USART_ConfigStruct;
APM_MINI_LEDInit(LED2);
RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1));
GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
GPIO_ConfigStruct.pin = GPIO_PIN_9;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
USART_ConfigStruct.baudRate = 115200;
USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
USART_ConfigStruct.mode = USART_MODE_TX;
USART_ConfigStruct.parity = USART_PARITY_NONE;
USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
USART_Config(USART1, &USART_ConfigStruct);
USART_Enable(USART1);
SysTick_Config(SystemCoreClock / 1000);
I2C_Init();
OLED_Init();
OLED_Clear();
OLED_ShowString(10,0,(u8 *)CharData,16);
OLED_ShowCHinese(10,2,0);
OLED_ShowCHinese(26,2,1);
OLED_ShowCHinese(42,2,2);
OLED_ShowString(10,4,(u8 *)CharData1,16);
OLED_ShowCHinese(10,6,3);
OLED_ShowCHinese(26,6,4);
OLED_ShowCHinese(42,6,5);
OLED_ShowCHinese(58,6,6);
OLED_ShowCHinese(74,6,7);
while(1)
{
// for(i = 0; i < sizeof(txBuf); i++)
// {
// while(USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
// USART_TxData(USART1, txBuf[i]);
// }
}
}
/*!
* @brief Delay
*
* @param None
*
* @retval None
*
*/
void Delay(void)
{
tick = 0;
while(tick < 500);
}
/**@} end of group USART_Printf_Functions */
/**@} end of group USART_Printf */
/**@} end of group Examples */
OLED.C:
#include "oled.h"
#define Max_Column 128
//OLED的显存
//存放格式如下.
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
void Delay1(unsigned long time)
{
/*<UserCodeStart>*//*<SinOne-Tag><64>*/
for(;time>0;time--);
/*<UserCodeEnd>*//*<SinOne-Tag><64>*/
}
/**
* @brief Write_I2C_Data 向OLED写入数据
* @param I2C_Data:数据
* @retval 无
*/
u8 TWI_WriteOneByte(u8 DeviceAddr,u8 DataAddr,u8 u8Data)
{
u8 Error_Flag=0;
I2C_Start();
I2C_Send_Byte(DeviceAddr);//器件地址+数据地址
I2C_Wait_Ack();
I2C_Send_Byte(DataAddr);//双字节是数据地址低位
//单字节是数据地址低位
I2C_Wait_Ack();
I2C_Send_Byte(u8Data);
I2C_Wait_Ack();
I2C_Stop();
return Error_Flag;
}
////OLED清屏
void OLED_Clear()
{
/*<UserCodeStart>*//*<SinOne-Tag><65>*/
unsigned int i,n;
for(i=0;i<8;i++)
{
TWI_WriteOneByte(0x78,0x00,0xb0+i);
TWI_WriteOneByte(0x78,0x00,0x00);
TWI_WriteOneByte(0x78,0x00,0x10);
for(n=0;n<128;n++)
{
TWI_WriteOneByte(0x78,0x40,0);
}
}
/*<UserCodeEnd>*//*<SinOne-Tag><65>*/
}
////OLED初始化
void OLED_Init()
{
/*<UserCodeStart>*//*<SinOne-Tag><66>*/
Delay1(100);
TWI_WriteOneByte(0x78,0x00,0xAE);
TWI_WriteOneByte(0x78,0x00,0x00);
TWI_WriteOneByte(0x78,0x00,0x10);
TWI_WriteOneByte(0x78,0x00,0x40);
TWI_WriteOneByte(0x78,0x00,0xB0);
TWI_WriteOneByte(0x78,0x00,0x81);
TWI_WriteOneByte(0x78,0x00,0xFF);
TWI_WriteOneByte(0x78,0x00,0xA1);
TWI_WriteOneByte(0x78,0x00,0xA6);
TWI_WriteOneByte(0x78,0x00,0xA8);
TWI_WriteOneByte(0x78,0x00,0x3F);
TWI_WriteOneByte(0x78,0x00,0xC8);
TWI_WriteOneByte(0x78,0x00,0xD3);
TWI_WriteOneByte(0x78,0x00,0x00);
TWI_WriteOneByte(0x78,0x00,0xD5);
TWI_WriteOneByte(0x78,0x00,0x80);
TWI_WriteOneByte(0x78,0x00,0xD8);
TWI_WriteOneByte(0x78,0x00,0x05);
TWI_WriteOneByte(0x78,0x00,0xD9);
TWI_WriteOneByte(0x78,0x00,0xF1);
TWI_WriteOneByte(0x78,0x00,0xDA);
TWI_WriteOneByte(0x78,0x00,0x12);
TWI_WriteOneByte(0x78,0x00,0xDB);
TWI_WriteOneByte(0x78,0x00,0x30);
TWI_WriteOneByte(0x78,0x00,0x8D);
TWI_WriteOneByte(0x78,0x00,0x14);
TWI_WriteOneByte(0x78,0x00,0xAF);
Delay1(100);
/*<UserCodeEnd>*//*<SinOne-Tag><66>*/
}
////清除指定区域字符x0,y0,左上角坐标x1,y1,右下角坐标
void OLED_PartClear(u8 x0,u8 y0,u8 x1,u8 y1)
{
/*<UserCodeStart>*//*<SinOne-Tag><67>*/
u8 i,n;
for( i=x0;i<x1;i++)
{
for( n=y0;n<y1;n++)
{
TWI_WriteOneByte(0x78,0x00,0xb0+i);
TWI_WriteOneByte(0x78,0x00,((n&0xf0)>>4)|0x10);
TWI_WriteOneByte(0x78,0x00,(n&0x0f));
TWI_WriteOneByte(0x78,0x40,0);
}
}
/*<UserCodeEnd>*//*<SinOne-Tag><67>*/
}
////m^n函数
unsigned long oled_pow(u8 m,u8 n)
{
/*<UserCodeStart>*//*<SinOne-Tag><68>*/
u32 result=1;
while(n--)
{
result*=m;
}
return result;
/*<UserCodeEnd>*//*<SinOne-Tag><68>*/
}
////坐标设置
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
/*<UserCodeStart>*//*<SinOne-Tag><69>*/
TWI_WriteOneByte(0x78,0x00,0xb0+y);
TWI_WriteOneByte(0x78,0x00,((x&0xf0)>>4)|0x10);
TWI_WriteOneByte(0x78,0x00,(x&0x0f));
/*<UserCodeEnd>*//*<SinOne-Tag><69>*/
}
////在指定位置显示一个字符,包括部分字符x:0~127y:0~8chr:字符地址size:选择字体16/12
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size)
{
/*<UserCodeStart>*//*<SinOne-Tag><70>*/
unsigned char c=0,i=0;
c=chr-' ';//得到偏移后的值
if(x>Max_Column-1)
{
x=0;
y=y+2;
}
if(Char_Size ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
{
TWI_WriteOneByte(0x78,0x40,F8X16[c*16+i]);
}
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
{
TWI_WriteOneByte(0x78,0x40,F8X16[c*16+i+8]);
}
}
else
{
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
{
TWI_WriteOneByte(0x78,0x40,F6x8[c][i]);
}
}
/*<UserCodeEnd>*//*<SinOne-Tag><70>*/
}
////显示汉字
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{
/*<UserCodeStart>*//*<SinOne-Tag><71>*/
u8 t,adder=0;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
TWI_WriteOneByte(0x78,0x40,Hzk[2*no][t]);
adder+=1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
TWI_WriteOneByte(0x78,0x40,Hzk[2*no+1][t]);
adder+=1;
}
/*<UserCodeEnd>*//*<SinOne-Tag><71>*/
}
/******************************************************************************************************
*显示2个数字x,y :起点坐标len :数字的位数size:字体大小num:数值(0~429496729
*5)
******************************************************************************************************/
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
{
/*<UserCodeStart>*//*<SinOne-Tag><72>*/
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
continue;
}
else
enshow=1;
}
OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
}
/*<UserCodeEnd>*//*<SinOne-Tag><72>*/
}
////显示一个字符串
void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
{
/*<UserCodeStart>*//*<SinOne-Tag><73>*/
unsigned char j=0;
while(chr[j]!='\0')
{
OLED_ShowChar(x,y,chr[j],Char_Size);
x+=8;
if(x>120)
{
x=0;
y+=2;
}
j++;
}
/*<UserCodeEnd>*//*<SinOne-Tag><73>*/
}
效果图:
|
|