使用STM32F103C8T6单片机驱动I2C-1602A模块
单片机源程序如下:
- #include <stm32f10x.h>
- #include "delay.h"
- #include "IIC_1602.h"
- extern unsigned char LCD_data;
- /**
- * @brief I2C_Configuration,初始化硬件IIC引脚
- * @param 无
- * @retval 无
- */
- void I2C_Configuration(void)
- {
- I2C_InitTypeDef I2C_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- /*STM32F103C8T6芯片的硬件I2C: PB6 -- SCL; PB7 -- SDA */
- 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;//I2C必须开漏输出
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- I2C_DeInit(I2C1);//使用I2C1
- I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
- I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
- I2C_InitStructure.I2C_OwnAddress1 = 0x30;//主机的I2C地址,随便写的
- I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
- I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
- I2C_InitStructure.I2C_ClockSpeed = 400000;//400K
- I2C_Cmd(I2C1, ENABLE);
- I2C_Init(I2C1, &I2C_InitStructure);
- }
- /**
- * @brief I2C_WriteByte,向OLED寄存器地址写一个byte的数据
- * @param addr:寄存器地址
- * data:要写入的数据
- * @retval 无
- */
- void I2C_WriteByte(unsigned char data)
- {
- while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
-
- I2C_GenerateSTART(I2C1, ENABLE);//开启I2C1
- while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/
- I2C_Send7bitAddress(I2C1, LCD_ADDR, I2C_Direction_Transmitter);//器件地址 --
- while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
- // I2C_SendData(I2C1, addr);//寄存器地址
- // while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
- I2C_SendData(I2C1, data);//发送数据
- while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
-
- I2C_GenerateSTOP(I2C1, ENABLE);//关闭I2C1总线
- }
- void LCD_EN()
- {
-
- LCD_data |= 1<<2;
- I2C_WriteByte(LCD_data);//1 读取数据
- DelayUs(25);//延时
- LCD_data ^= 1<<2;
- I2C_WriteByte(LCD_data);//1→0 执行指令
-
- }
- void LCD_WriteCMD(unsigned char cmd)
- {
-
- DelayUs(25);
- LCD_data &=~(1<<(1-1));//RS=0;
- LCD_data &=~(1<<(2-1));//RW=0;
- I2C_WriteByte(LCD_data);
- LCD_data &= 0X0f; //清高四位
- LCD_data |= cmd & 0xf0; //写指令高四位
- I2C_WriteByte(LCD_data);
- LCD_EN();
- cmd = cmd<<4;
- LCD_data &= 0X0f; //清高四位
- LCD_data |= cmd & 0xf0; //写指令低四位
- I2C_WriteByte(LCD_data);
- LCD_EN();
- }
- void LCD_WriteDAT(unsigned char dat)
- {
-
- DelayUs(25);
- LCD_data |= (1<<(1-1));//RS=0;
- LCD_data &=~(1<<(2-1));//RW=0;
- I2C_WriteByte(LCD_data);
- LCD_data &= 0X0f; //清高四位
- LCD_data |= dat & 0xf0; //写数据高四位
- I2C_WriteByte(LCD_data);
- LCD_EN();
- dat = dat<<4;
- LCD_data &= 0X0f; //清高四位
- LCD_data |= dat & 0xf0; //写数据低四位
- I2C_WriteByte(LCD_data);
- LCD_EN();
- }
- void Set_Position(unsigned char x,unsigned char y)
- {
-
- unsigned char x_pos;
- x_pos = x%0x10;
- if(y==0)
- {
- LCD_WriteCMD(0x80+x_pos);
- }
- else
- {
- LCD_WriteCMD(0xc0+x_pos);
- }
-
- }
- void LCD_ShowStr(unsigned char x,unsigned char y,unsigned char * str)
- {
- Set_Position(x,y);
- while(*str)
- {
-
- LCD_WriteDAT(*str);
- str++;
- }
-
- }
- void LCD_Init()
- {
- LCD_WriteCMD(0x28);//4线操作模式
- LCD_WriteCMD(0x0c);//
- LCD_WriteCMD(0x80);
- LCD_WriteCMD(0x01);
- DelayMs(5);
- }
- void LCD_DispConf(unsigned char DispON,unsigned char CursorON,unsigned char GlareON)
- {
- unsigned char x = 0x08;
- if(DispON!=0)
- {
- x = x|(DispON<<2);
- }
- if(CursorON!=0)
- {
- x = x|(CursorON<<1);
- }
- if(GlareON!=0)
- {
- x = x|GlareON;
- }
- LCD_WriteCMD(x);
- }
复制代码
所有程序51hei提供下载:
IIC_1602A.7z
(21.31 KB, 下载次数: 105)
|