单片机
返回首页

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;

}

进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • 家用电源无载自动断电装置的设计与制作

  • 用数字电路CD4069制作的万能遥控轻触开关

  • 使用ESP8266从NTP服务器获取时间并在OLED显示器上显示

  • 开关电源的基本组成及工作原理

  • 用NE555制作定时器

  • 带有短路保护系统的5V直流稳压电源电路图

    相关电子头条文章