?
#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);
}
}
|