STM32 SPI和TM1628通信
2022-03-18 来源:eefocus
uint8_t TM1628_WritePro(void)
{undefined
//SPI1->CR1 |= SPI_Direction_1Line_Tx ;
SPI1->CR1 = 0xc3ff;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0x00);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0x44);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0xC0);
SPI_SendData8(SPI1,ShowValue[0]);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0xC2);
SPI_SendData8(SPI1,ShowValue[1]);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0xC4);
SPI_SendData8(SPI1,ShowValue[2]);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0xC6);
SPI_SendData8(SPI1,ShowValue[3]);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0x89);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SetSTB;
return 0;
}
uint8_t TM1628_ReadPro(void)
{undefined
uint8_t KeyTemp[4] = {0};
SPI1->CR1 = 0xc3ff;
//SPI1->CR1 |= SPI_Direction_1Line_Tx;//
SetSTB;
DelayXu(5);
ClrSTB;
DelayXu(2);
SPI_SendData8(SPI1,0x42);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
DelayXu(5);
SPI1->CR1 &= ~SPI_EN;
SPI1->CR1 &= SPI_Direction_Rx;
SPI1->CR1 |= SPI_EN;
DelayXu(5);
while((SPI1->SR&SPI_I2S_FLAG_RXNE) == RESET);
KeyTemp[0] = SPI_ReceiveData8(SPI1);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
KeyTemp[1] = SPI_ReceiveData8(SPI1);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
KeyTemp[2] = SPI_ReceiveData8(SPI1);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
KeyTemp[3] = SPI_ReceiveData8(SPI1);
//SPI1->CR1 |= SPI_Direction_1Line_Tx;//
SetSTB;
SPI1->CR1 = 0xc3ff;
//检验按键是否有效 若接线开路则返回错误
if(KeyTemp[1]&0xef) return 0 ;
if(KeyTemp[2]&0xed) return 0 ;
if(KeyTemp[3]&0xfd) return 0 ;
if(KeyTemp[1]&0x10) KeyValue |= BIT0 ;
else KeyValue &= ~BIT0 ;
if(KeyTemp[2]&0x10) KeyValue |= BIT1 ;
else KeyValue &= ~BIT1 ;
if(KeyTemp[2]&0x02) KeyValue |= BIT2 ;
else KeyValue &= ~BIT2 ;
if(KeyTemp[3]&0x02) KeyValue |= BIT3 ;
else KeyValue &= ~BIT3 ;
return 1;
}