OLED_I2C_Init(); while (1) { for (int i =8; i < 12; i++) { GPIO_ResetBits(GPIOA, 1 << i); for (int j = 12; j < 16; j++) { if (GPIO_ReadInputDataBit(GPIOB, 1 << j) == RESET) { OLED_ShowChar(i-7, j-11, 'a'); } } GPIO_SetBits(GPIOA, 1 << i); }
时间: 2024-03-31 18:37:24 浏览: 13
这是一段嵌入式C语言代码,它使用了STM32的GPIO库和OLED显示屏的I2C通讯库。代码先调用了OLED_I2C_Init函数来初始化OLED显示屏,然后进入了一个无限循环。
在循环中,代码使用两个for循环来遍历GPIOA的第8到11个引脚,并将它们依次重置为低电平状态。然后,在GPIOB的第12到15个引脚中,找到一个处于低电平状态的引脚,并在OLED屏幕上显示一个字符'a',位置为(i-7, j-11)。最后,代码将GPIOA的对应引脚恢复为高电平状态,等待下一次遍历。
这段代码的具体作用需要结合具体的硬件连接来理解,但大致上可以理解为通过GPIO控制OLED显示屏,在特定的位置显示字符。
相关问题
0.96寸OLED STM32F103 I2C程序
以下是使用STM32F103单片机和0.96寸OLED屏幕的I2C程序。
首先,需要确保你已经连接好了OLED屏幕和STM32F103单片机,然后将以下代码复制到你的STM32F103开发板的IAR或Keil等开发环境中进行编译:
```c
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#define OLED_I2C_ADDR 0x78
void OLED_I2C_Init(void);
void OLED_I2C_WriteCmd(uint8_t I2C_Command);
void OLED_I2C_WriteData(uint8_t I2C_Data);
void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size);
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no);
void OLED_Set_Pos(uint8_t x, uint8_t y);
void Delay(__IO uint32_t nCount);
int main(void)
{
OLED_I2C_Init();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,2,"Hello,World!",16);
OLED_ShowString(0,4,"这是一行中文测试",16);
while(1);
}
void OLED_I2C_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB,&GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Init(I2C1,&I2C_InitStructure);
I2C_Cmd(I2C1,ENABLE);
}
void OLED_I2C_WriteCmd(uint8_t I2C_Command)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,OLED_I2C_ADDR,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,0x00);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,I2C_Command);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1,ENABLE);
Delay(200);
}
void OLED_I2C_WriteData(uint8_t I2C_Data)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,OLED_I2C_ADDR,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,0x40);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,I2C_Data);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1,ENABLE);
Delay(200);
}
void OLED_Init(void)
{
Delay(200);
OLED_I2C_WriteCmd(0xAE);
OLED_I2C_WriteCmd(0x00);
OLED_I2C_WriteCmd(0x10);
OLED_I2C_WriteCmd(0x40);
OLED_I2C_WriteCmd(0x81);
OLED_I2C_WriteCmd(0xCF);
OLED_I2C_WriteCmd(0xA1);
OLED_I2C_WriteCmd(0xC8);
OLED_I2C_WriteCmd(0xA6);
OLED_I2C_WriteCmd(0xA8);
OLED_I2C_WriteCmd(0x3F);
OLED_I2C_WriteCmd(0xD3);
OLED_I2C_WriteCmd(0x00);
OLED_I2C_WriteCmd(0xD5);
OLED_I2C_WriteCmd(0x80);
OLED_I2C_WriteCmd(0xD9);
OLED_I2C_WriteCmd(0xF1);
OLED_I2C_WriteCmd(0xDA);
OLED_I2C_WriteCmd(0x12);
OLED_I2C_WriteCmd(0xDB);
OLED_I2C_WriteCmd(0x40);
OLED_I2C_WriteCmd(0x20);
OLED_I2C_WriteCmd(0x02);
OLED_I2C_WriteCmd(0x8D);
OLED_I2C_WriteCmd(0x14);
OLED_I2C_WriteCmd(0xA4);
OLED_I2C_WriteCmd(0xA6);
OLED_I2C_WriteCmd(0xAF);
OLED_Clear();
}
void OLED_Clear(void)
{
uint8_t i,j;
for(i=0;i<8;i++)
{
OLED_I2C_WriteCmd(0xb0+i);
OLED_I2C_WriteCmd(0x00);
OLED_I2C_WriteCmd(0x10);
for(j=0;j<128;j++)
{
OLED_I2C_WriteData(0);
}
}
}
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
uint8_t c = 0,i = 0,j = 0;
while (chr[j]!='\0')
{
c = chr[j] - 32;
if(x > 120)
{
x = 0;
y++;
}
OLED_Set_Pos(x,y);
for(i=0;i<Char_Size;i++)
{
OLED_I2C_WriteData(F8X16[c*16+i]);
}
OLED_Set_Pos(x,y+1);
for(i=0;i<Char_Size;i++)
{
OLED_I2C_WriteData(F8X16[c*16+i+8]);
}
x += Char_Size;
j++;
}
}
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
{
uint8_t t,adder = 0;
OLED_Set_Pos(x , y);
for(t=0;t<16;t++)
{
OLED_I2C_WriteData(CHS[2*no][t]);
adder += 1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_I2C_WriteData(CHS[2*no+1][t]);
adder += 1;
}
}
void OLED_Set_Pos(uint8_t x, uint8_t y)
{
OLED_I2C_WriteCmd(0xb0+y);
OLED_I2C_WriteCmd(((x&0xf0)>>4)|0x10);
OLED_I2C_WriteCmd((x&0x0f)|0x01);
}
void Delay(__IO uint32_t nCount)
{
for(;nCount!=0;nCount--);
}
```
在上面的代码中,我们首先定义了OLED屏幕的I2C地址,然后实现了一些函数来初始化OLED屏幕、清屏、显示字符串、显示汉字等。在主函数中,我们初始化OLED屏幕并显示一些测试信息。
请注意,这只是一个示例程序,你需要根据你的具体需求进行修改。
oled12864与STM32实现I2C通信
要在STM32上实现与OLED12864的I2C通信,你需要完成以下步骤:
1. 确认OLED12864的I2C地址,一般为0x3C或0x3D。
2. 在STM32上配置I2C接口,设置时钟频率、地址模式等参数。
3. 初始化OLED12864,发送相应的初始化指令。
4. 发送数据到OLED12864,例如显示字符或图片等。
5. 关闭I2C接口和OLED12864。
以下是一个简单的例程,可以帮助你了解如何在STM32上实现I2C通信和控制OLED12864显示:
```c
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "oled12864.h"
#define OLED_I2C I2C1
#define OLED_I2C_CLK RCC_APB1Periph_I2C1
#define OLED_I2C_SCL_GPIO GPIOB
#define OLED_I2C_SCL_PIN GPIO_Pin_6
#define OLED_I2C_SDA_GPIO GPIOB
#define OLED_I2C_SDA_PIN GPIO_Pin_7
#define OLED_I2C_SPEED 400000
#define OLED_I2C_ADDR 0x3C
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* Enable I2C and GPIO clocks */
RCC_APB1PeriphClockCmd(OLED_I2C_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure I2C pins */
GPIO_InitStructure.GPIO_Pin = OLED_I2C_SCL_PIN | OLED_I2C_SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(OLED_I2C_SCL_GPIO, &GPIO_InitStructure);
/* Configure I2C */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = OLED_I2C_SPEED;
I2C_Init(OLED_I2C, &I2C_InitStructure);
/* Enable I2C */
I2C_Cmd(OLED_I2C, ENABLE);
}
void OLED_Init(void)
{
uint8_t init_cmd[] = {0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0x8D, 0x14, 0x20, 0x00, 0xA1, 0xC8, 0xDA, 0x12, 0x81, 0xCF, 0xD9, 0xF1, 0xDB, 0x40, 0xA4, 0xA6, 0x2E, 0xAF};
uint8_t i;
/* Send initialization commands */
for (i = 0; i < sizeof(init_cmd); i++) {
OLED_WriteCommand(init_cmd[i]);
}
}
void OLED_WriteCommand(uint8_t cmd)
{
/* Send command */
while (I2C_GetFlagStatus(OLED_I2C, I2C_FLAG_BUSY)) ;
I2C_GenerateSTART(OLED_I2C, ENABLE);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_MODE_SELECT)) ;
I2C_Send7bitAddress(OLED_I2C, OLED_I2C_ADDR, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) ;
I2C_SendData(OLED_I2C, 0x00);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_SendData(OLED_I2C, cmd);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_GenerateSTOP(OLED_I2C, ENABLE);
}
void OLED_WriteData(uint8_t data)
{
/* Send data */
while (I2C_GetFlagStatus(OLED_I2C, I2C_FLAG_BUSY)) ;
I2C_GenerateSTART(OLED_I2C, ENABLE);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_MODE_SELECT)) ;
I2C_Send7bitAddress(OLED_I2C, OLED_I2C_ADDR, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) ;
I2C_SendData(OLED_I2C, 0x40);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_SendData(OLED_I2C, data);
while (!I2C_CheckEvent(OLED_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
I2C_GenerateSTOP(OLED_I2C, ENABLE);
}
int main(void)
{
/* Configure I2C */
I2C_Configuration();
/* Initialize OLED */
OLED_Init();
/* Display "Hello, world!" */
OLED_GotoXY(0, 0);
OLED_Puts("Hello, world!");
while (1) {
/* Do something */
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)