TLC59284驱动及应用

说明:本程序硬件电路为三个TLC59284串联使用,注意锁存使用否则IO动作会错乱,仅供参考
//定义器件操作IO
#define SPI_SCK(x) (x)?GPIO_SetBits(GPIOB, GPIO_Pin_3):GPIO_ResetBits(GPIOB, GPIO_Pin_3)
#define SPI_MOSI(x) (x)?GPIO_SetBits(GPIOB, GPIO_Pin_5):GPIO_ResetBits(GPIOB, GPIO_Pin_5)
#define SPI_MISO GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_4)
#define TLC59284_LAT(x) (x)?GPIO_SetBits(GPIOB, GPIO_Pin_6):GPIO_ResetBits(GPIOB, GPIO_Pin_6)
#define TLC59284_EN(x) (x)?GPIO_SetBits(GPIOB, GPIO_Pin_7):GPIO_ResetBits(GPIOB, GPIO_Pin_7)
//定义三个TLC59284数据数组,0xAAAA做校验使用
uint16_t TLC59284_Status[4]={0,0,0,0xAAAA};
//定义单个TLC59284根据某一通道转换到16bits的数据和
int TLC59284_Set_Data(uint8_t channel,uint8_t SIN_NUM)
{
uint16_t temp;
temp=1< TLC59284_Status[SIN_NUM-1]&=(~temp);
TLC59284_Status[SIN_NUM-1]|=temp;
return TLC59284_Status[SIN_NUM-1];
}
int TLC59284_Reset_Data(uint8_t channel,uint8_t SIN_NUM)
{
uint16_t temp;
temp=1< TLC59284_Status[SIN_NUM-1]&=(~temp);
return TLC59284_Status[SIN_NUM-1];
}
//写数据到TLC59284
void TLC59284_SIN(uint16_t SIN_DATA,uint8_t SIN_NUM)
{
uint8_t i,DATA_LAT=3;
uint16_t temp;
TLC59284_Status[SIN_NUM-1]=SIN_DATA;
SPI_SCK(0);
for(i=0;i<64;i++)
{
if(i%16= =0)temp=TLC59284_Status[DATA_LAT–];
if(temp&0x8000)SPI_MOSI(1); //SPI_MOSI
else SPI_MOSI(0);
SPI_SCK(1);
SPI_SCK(0);
temp<<=1;
}
TLC59284_LAT(1); //开锁存
TLC59284_LAT(0);
}
//使用实例
if(Relay_Number<0||Relay_Number>47){printf(“Relay_Number Error\r\n”);TLC59284_EN(1);return -1;}
TLC59284_EN(0);
if(Relay_Number>=0&&Relay_Number<=15)
{
sin_num=1;
if(Relay_Value= =1)sin_data=TLC59284_Set_Data(Relay_Number,sin_num);
else if(Relay_Value= =0)sin_data=TLC59284_Reset_Data(Relay_Number,sin_num);
TLC59284_SIN(sin_data,sin_num);
}
else if(Relay_Number>=16&&Relay_Number<=31)
{
sin_num=2;
if(Relay_Value= =1)sin_data=TLC59284_Set_Data(Relay_Number-16,sin_num);
else if(Relay_Value= =0)sin_data=TLC59284_Reset_Data(Relay_Number-16,sin_num);
TLC59284_SIN(sin_data,sin_num);
}
else if(Relay_Number>=32&&Relay_Number<=47)
{
sin_num=3;
if(Relay_Value= =1)sin_data=TLC59284_Set_Data(Relay_Number-32,sin_num);
else if(Relay_Value==0)sin_data=TLC59284_Reset_Data(Relay_Number-32,sin_num);
TLC59284_SIN(sin_data,sin_num);
}

你可能感兴趣的:(器件驱动)