IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> IS31F3729驱动程序,用的是CH32V103的I2C1 -> 正文阅读

[嵌入式]IS31F3729驱动程序,用的是CH32V103的I2C1

?

#include "debug.h"
#include "string.h"
#include <stdlib.h>


/* DATA ADDRESS Length Definition */
#define Address_8bit  0
#define Address_16bit  1

/* DATA ADDRESS Length Selection */
#define Address_Lenth   Address_8bit
//#define Address_Lenth   Address_16bit


#define  X_LED  (6)  //横向LED数量
#define  Y_LED  (15) //纵向LED数量
#define  SCALINGS (15) //scaling数量
#define DATA_MAX 60//(X_LED*Y_LED)

#define DATA_SIZE sizeof(data[DATA_MAX])
#define DEVICE1  0
#define DEVICE2  1
typedef struct{
   u8 B;
   u8 G;
   u8 R;
}RGB_LED;
RGB_LED data[DATA_MAX];

/*******************************************************************************
* Function Name  : IS31F3729_I2c_Init
* Description    : Initializes the IIC peripheral.
* Input          : None
* Return         : None
********************************************************************************/
void IS31F3729_I2c_Init( u32 bound, u16 address)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitTSturcture;

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE );
    GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOB, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOB, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOB, &GPIO_InitStructure );
    GPIO_SetBits(GPIOB, GPIO_Pin_6);     //拉高使能脚

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init( GPIOC, &GPIO_InitStructure );
    GPIO_ResetBits(GPIOC, GPIO_Pin_13);


    I2C_InitTSturcture.I2C_ClockSpeed = bound;
    I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
    I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_2;
    I2C_InitTSturcture.I2C_OwnAddress1 = address;
    I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
    I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_Init( I2C1, &I2C_InitTSturcture );

    I2C_Cmd( I2C1, ENABLE );

    I2C_AcknowledgeConfig( I2C1, ENABLE );
}

/*******************************************************************************
* Function Name  : IS31F3729_ReadOneByte
* Description    : Read one data from IS31F3729.
* Input          : ReadAddr: Read frist address.DEVICE1 address 0x68 and 0x69,DEVICE1 address 0x6e and 0x6f
* Return         : temp: Read data.
********************************************************************************/
u8 IS31F3729_ReadOneByte(u16 ReadAddr,u8 device)
{
    u8 temp=0;

    while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );
    I2C_GenerateSTART( I2C1, ENABLE );

    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );
    if(device==DEVICE1)
    {
        I2C_Send7bitAddress( I2C1, 0x68, I2C_Direction_Transmitter ); //如果AD接地  I2C的器件写地址是0x68
    }
    else if (device==DEVICE2)
    {
        I2C_Send7bitAddress( I2C1, 0x6e, I2C_Direction_Transmitter ); //如果AD接VCC I2C的器件写地址是0x6e
    }

    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );

#if (Address_Lenth  == Address_8bit)
    I2C_SendData( I2C1, (u8)(ReadAddr&0x00FF) );
    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

#elif (Address_Lenth  == Address_16bit)
    I2C_SendData( I2C1, (u8)(ReadAddr>>8) );
    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

    I2C_SendData( I2C1, (u8)(ReadAddr&0x00FF) );
    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

#endif

    I2C_GenerateSTART( I2C1, ENABLE );

    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );
    if(device==DEVICE1)
    {
        I2C_Send7bitAddress( I2C1, 0X69, I2C_Direction_Transmitter );  //如果AD接地  I2C的器件读地址是0x69
    }
    else if (device==DEVICE2)
    {
        I2C_Send7bitAddress( I2C1, 0x6f, I2C_Direction_Transmitter ); //如果AD接VCC  I2C的器件读地址是0x6F
    }

    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) );
  while( I2C_GetFlagStatus( I2C1, I2C_FLAG_RXNE ) ==  RESET )
    I2C_AcknowledgeConfig( I2C1, DISABLE );

    temp = I2C_ReceiveData( I2C1 );
  I2C_GenerateSTOP( I2C1, ENABLE );

    return temp;
}

/*******************************************************************************
* Function Name  : IS31F3729_WriteOneByte
* Description    : Write one data to IS31F3729.
* Input          : WriteAddr: Write frist address.DEVICE1 address 0x68 and 0x69,DEVICE1 address 0x6e and 0x6f
* Return         : DataToWrite: Write data.
********************************************************************************/
void IS31F3729_WriteOneByte(u16 WriteAddr,u8 DataToWrite,u8 device)
{
    while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );
    GPIO_SetBits(GPIOC, GPIO_Pin_13);
    I2C_GenerateSTART( I2C1, ENABLE );

    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );
    if(device==DEVICE1)
    {
        I2C_Send7bitAddress( I2C1, 0X68, I2C_Direction_Transmitter );//如果AD接地  I2C的器件写地址是0x68
    }
    else if (device==DEVICE2)
    {
        I2C_Send7bitAddress( I2C1, 0x6e, I2C_Direction_Transmitter );//如果AD接VCC I2C的器件写地址是0x6e
    }


    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );

#if (Address_Lenth  == Address_8bit)
    I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );
    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

#elif (Address_Lenth  == Address_16bit)
    I2C_SendData( I2C2, (u8)(WriteAddr>>8) );
    while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

    I2C_SendData( I2C2, (u8)(WriteAddr&0x00FF) );
    while( !I2C_CheckEvent( I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );

#endif

    if( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) !=  RESET )
    {
        I2C_SendData( I2C1, DataToWrite );
    }

    while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );
    I2C_GenerateSTOP( I2C1, ENABLE );
}
/*******************************************************************************
* Function Name  : IS31F3729_Read
* Description    : Read multiple data from IS31F3729.
* Input          : ReadAddr: Read frist address.
*                  pBuffer: Read data.
*                  NumToRead: Data number.
*                  device: choose device 1 and 2
* Return         : None
********************************************************************************/
void IS31F3729_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead,u8 device)
{
    while(NumToRead)
    {
        *pBuffer++=IS31F3729_ReadOneByte(ReadAddr++,device);
        NumToRead--;
    }
}

/*******************************************************************************
* Function Name  : IS31F3729_Write
* Description    : Write multiple data to IS31F3729.
* Input          : WriteAddr: Write frist address.
*                  pBuffer: Write data.
*                  NumToWrite: Data number.
*                  device: choose device 1 and 2
* Return         : None
********************************************************************************/
void IS31F3729_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite,u8 device)
{
    while(NumToWrite--)
    {
        IS31F3729_WriteOneByte(WriteAddr,*pBuffer,device);
        //printf("buff=%d\n",*pBuffer);
        //printf("addr=%d\n",WriteAddr);
        WriteAddr++;
        pBuffer++;
        //Delay_Ms(1);
    }
}
/*******************************************************************************
* Function Name  : IS31F3729_Init
* Description    : Initializes the DEVICE.
* Input          : None
* Return         : None
*******************************************************************************/
void IS31F3729_Init(void)
{
    u8 i;

    IS31F3729_I2c_Init(800000, 0x68);
    printf("start write data to DEVICE \r\n");
    for(i=0;i<((DATA_MAX/2)*3);i++)
    {
        IS31F3729_WriteOneByte(0X01+i,0x2f,DEVICE1);//pwm write 把全部LED全部点亮  PWM值为0x2f(0~0xff)
    }
    for(i=0;i<SCALINGS;i++)
    {
        IS31F3729_WriteOneByte(0X90+i,0xff,DEVICE1);//scaling write
    }
    IS31F3729_WriteOneByte(0xa0,0x09,DEVICE1);  //ctrl device open
    IS31F3729_WriteOneByte(0xa1,0x40,DEVICE1);  //GCC write
    //Device2 init
    for(i=0;i<((DATA_MAX/2)*3);i++)
    {
        IS31F3729_WriteOneByte(0X01+i,0x2f,DEVICE2);//pwm write 把全部LED全部点亮  PWM值为0x2f(0~0xff)
    }

    for(i=0;i<SCALINGS;i++)
    {
        IS31F3729_WriteOneByte(0X90+i,0xff,DEVICE2);//scaling write
    }
    IS31F3729_WriteOneByte(0xa0,0x09,DEVICE2);  //ctrl device open
    IS31F3729_WriteOneByte(0xa1,0x40,DEVICE2);  //GCC write
    printf("write data done! \r\n");
}

/*******************************************************************************
* Function Name  : LED_Refresh
* Description    : Refresh matrix LED screen.write data to two IS31F3729
* Input          : None
* Return         : None
********************************************************************************/
void LED_Refresh(void)
{
    u8 i;


    for(i = 0;i<60;i++)
    {
        if(i<(60/2))
        {
            if(i/5==0||i/5==3||i/5==6||i/5==9)
            {
                IS31F3729_WriteOneByte(0X01+i*3,data[i].B,DEVICE1);//PWM write
                IS31F3729_WriteOneByte(0X02+i*3,data[i].G,DEVICE1);//PWM write
                IS31F3729_WriteOneByte(0X03+i*3,data[i].R,DEVICE1);//PWM write
            }
            if(i/5==1||i/5==4||i/5==7||i/5==10)
            {
                IS31F3729_WriteOneByte(0X01+i*3,data[i].R,DEVICE1);//PWM write
                IS31F3729_WriteOneByte(0X02+i*3,data[i].B,DEVICE1);//PWM write
                IS31F3729_WriteOneByte(0X03+i*3,data[i].G,DEVICE1);//PWM write
            }
            if(i/5==2||i/5==5||i/5==8||i/5==11)
            {
                IS31F3729_WriteOneByte(0X01+i*3,data[i].G,DEVICE1);//PWM write
                IS31F3729_WriteOneByte(0X02+i*3,data[i].R,DEVICE1);//PWM write
                IS31F3729_WriteOneByte(0X03+i*3,data[i].B,DEVICE1);//PWM write
            }
        }
        else if(i>=(60/2))
        {
            if(i/5==0||i/5==3||i/5==6||i/5==9)
            {
                IS31F3729_WriteOneByte(0X01+(i-(60/2))*3,data[i].B,DEVICE2);//PWM write
                IS31F3729_WriteOneByte(0X02+(i-(60/2))*3,data[i].G,DEVICE2);//PWM write
                IS31F3729_WriteOneByte(0X03+(i-(60/2))*3,data[i].R,DEVICE2);//PWM write
            }
            if(i/5==1||i/5==4||i/5==7||i/5==10)
            {
                IS31F3729_WriteOneByte(0X01+(i-(60/2))*3,data[i].R,DEVICE2);//PWM write
                IS31F3729_WriteOneByte(0X02+(i-(60/2))*3,data[i].B,DEVICE2);//PWM write
                IS31F3729_WriteOneByte(0X03+(i-(60/2))*3,data[i].G,DEVICE2);//PWM write
            }
            if(i/5==2||i/5==5||i/5==8||i/5==11)
            {
                IS31F3729_WriteOneByte(0X01+(i-(60/2))*3,data[i].G,DEVICE2);//PWM write
                IS31F3729_WriteOneByte(0X02+(i-(60/2))*3,data[i].R,DEVICE2);//PWM write
                IS31F3729_WriteOneByte(0X03+(i-(60/2))*3,data[i].B,DEVICE2);//PWM write
            }

        }
    }

}

/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Return         : None
*******************************************************************************/
int main(void)
{
	u8 i;
	Delay_Init();
	USART_Printf_Init(115200);
	printf("SystemClk:%d\r\n",SystemCoreClock);
	IS31F3729_Init();
    while(1)
    {

        for (i = 0; i <= DATA_MAX; i++)
        {
           data[i].B =(rand()%0XFF);
           data[i].R =(rand()%0XFF);
           data[i].G =(rand()%0XFF);
        }
        LED_Refresh();
        Delay_Ms(300);


    }
}

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2022-02-04 11:11:54  更:2022-02-04 11:13:50 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 10:32:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码