STM32外设SPI读写TM1628查询方式非模拟独家贡献

//写数码管

uint8_t TM1628_WritePro(void)
{
  if(1!=TM1628_State)return 0;
  SPI1->CR1 |= SPI_Direction_1Line_Tx ;
  TM1628_State = 0 ;
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  SetSTB;
  DelayXu(10);
  ClrSTB;
  SPI_SendData8(SPI1,0x00);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);          
  SetSTB;
  DelayXu(10);
  ClrSTB;
  SPI1->DR = 0x44; 
  SPI_SendData8(SPI1,0x44);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);              
  SetSTB;
  DelayXu(10);
  ClrSTB;
  SPI_SendData8(SPI1,0xC0);
  SPI_SendData8(SPI1,ShowValue[0]);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);         
  SetSTB;
  DelayXu(10);
  ClrSTB;
  SPI_SendData8(SPI1,0xC2);
  SPI_SendData8(SPI1,ShowValue[1]);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); 
  SetSTB;
  DelayXu(10);
  ClrSTB;
  SPI_SendData8(SPI1,0xC4);
  SPI_SendData8(SPI1,ShowValue[2]);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); 
  SetSTB;
  DelayXu(10);
  ClrSTB;
  SPI_SendData8(SPI1,0xC6);
  SPI_SendData8(SPI1,ShowValue[3]);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);              
  SetSTB;
  DelayXu(10);
  ClrSTB;
  SPI_SendData8(SPI1,0x89);
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);              
  SetSTB; 
  return 0;
}

//读按键

uint8_t TM1628_ReadPro(void)
{
  if(2!=TM1628_State) return 0;
   SPI1->CR1  |= SPI_Direction_1Line_Tx;//
   SetSTB;
   DelayXu(10);
   ClrSTB;
   SPI_SendData8(SPI1,0x42);
   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
   DelayXu(10);
   
   SPI1->CR1  &= ~SPI_EN;;//修改配置
   SPI1->CR1  &= 0xbfff;//修改配置
   SPI1->CR1  |= SPI_EN;;//修改配置
   
   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;
   TM1628_State = 0;
   return 0;
}

//PB3 CLK
  GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Pin     = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType   = GPIO_OType_PP ;
  GPIO_InitStructure.GPIO_PuPd    = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
    //PB5 MOSI
  GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Pin     = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType   = GPIO_OType_OD ;
  GPIO_InitStructure.GPIO_PuPd    = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
   ////  PB3->SPI1SCK    PB5->SPI1MOSI
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_5);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_5);
  //SPI设置
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 , ENABLE);
  //SPI1 CR1控制
  SPI1->CR1  = (SPI_Direction_1Line_Tx|SPI_FirstBit_LSB |SPI_BaudRatePrescaler_256|SPI_MSTR|SPI_CPOL_High|SPI_CPHA_2Edge) |SPI_NSS_Soft|SPI_SSI_High ;//|
  // SPI1 CR2控制
  SPI1->CR2 = SPI_DataSize_8b|SPI_RxFIFOThreshold_QF;//|SPI_TXEIE|SPI_SSOE|SPI_NSSP 超过1/4 RXNE = 1
  SPI1->CR1 |=  SPI_EN;

你可能感兴趣的:(STM32F334,C语言)