[应用相关] 在AT32F403A中,你可以使用SPI_I2S_DataTransmit函数来发送数据。

[复制链接]
2768|1
 楼主| kzlzqi 发表于 2024-3-20 00:08 | 显示全部楼层 |阅读模式
93C46是一款串行EEPROM(电可擦可编程只读存储器),通常使用SPI进行通信。根据93C46的规格,它的操作码通常是3位的。

对于AT32F403A的硬件SPI模块,通常是可以发送任意位数的数据的,因此可以发送3位的操作码。SPI接口通常有一个发送数据寄存器,你可以使用该寄存器来发送操作码和其他数据。

在AT32F403A中,你可以使用SPI_I2S_DataTransmit函数来发送数据。以下是一个简单的示例代码,演示了如何使用硬件SPI来读写93C46,并发送3位的操作码:

  1. #include "AT32f4xx.h"

  2. // 初始化SPI
  3. void SPI_Init() {
  4.     // 选择SPI控制器
  5.     SPIx = SPI1;
  6.    
  7.     // 使能SPI时钟
  8.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  9.    
  10.     // 配置SPI参数
  11.     SPI_InitTypeDef SPI_InitStructure;
  12.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // 主模式
  13.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // 全双工模式
  14.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // 数据位宽8位
  15.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // 时钟极性为低电平
  16.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // 时钟相位为第一边沿
  17.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // 使用软件控制片选
  18.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // 波特率分频256
  19.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 先发送最高位
  20.     SPI_Init(SPIx, &SPI_InitStructure);
  21.    
  22.     // 使能SPI
  23.     SPI_Cmd(SPIx, ENABLE);
  24. }

  25. // 读取93C46
  26. uint8_t read_93C46(uint8_t opcode, uint8_t address) {
  27.     // 发送操作码和地址
  28.     SPI_I2S_SendData(SPIx, opcode);
  29.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  30.     uint8_t received_data = SPI_I2S_ReceiveData(SPIx);
  31.    
  32.     SPI_I2S_SendData(SPIx, address);
  33.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  34.     received_data = SPI_I2S_ReceiveData(SPIx);
  35.    
  36.     // 读取数据
  37.     SPI_I2S_SendData(SPIx, 0x00); // 发送一个空字节,以便从93C46读取数据
  38.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  39.     received_data = SPI_I2S_ReceiveData(SPIx);
  40.    
  41.     return received_data;
  42. }

  43. // 写入93C46
  44. void write_93C46(uint8_t opcode, uint8_t address, uint8_t data) {
  45.     // 发送操作码和地址
  46.     SPI_I2S_SendData(SPIx, opcode);
  47.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  48.     uint8_t received_data = SPI_I2S_ReceiveData(SPIx);
  49.    
  50.     SPI_I2S_SendData(SPIx, address);
  51.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  52.     received_data = SPI_I2S_ReceiveData(SPIx);
  53.    
  54.     // 写入数据
  55.     SPI_I2S_SendData(SPIx, data);
  56.     while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  57.     received_data = SPI_I2S_ReceiveData(SPIx);
  58. }

  59. int main() {
  60.     // 初始化SPI
  61.     SPI_Init();
  62.    
  63.     // 读取93C46的数据
  64.     uint8_t opcode = 0b110; // 示例操作码,根据需要修改
  65.     uint8_t address = 0b0101; // 示例地址,根据需要修改
  66.     uint8_t data = read_93C46(opcode, address);
  67.    
  68.     // 写入93C46的数据
  1.     // 示例数据,根据需要修改
  2.     uint8_t write_data = 0xAB;

  3.     // 写入93C46的数据
  4.     write_93C46(opcode, address, write_data);

  5.     while (1) {
  6.         // 循环执行其他任务
  7.     }
  8. }

  9. // 读取93C46
  10. uint8_t read_93C46(uint8_t opcode, uint8_t address) {
  11.     // 发送操作码和地址
  12.     SPI_I2S_SendData(SPIx, opcode << 5 | address);
  13.     while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  14.     uint8_t received_data = SPI_I2S_ReceiveData(SPIx);

  15.     // 读取数据
  16.     SPI_I2S_SendData(SPIx, 0x00); // 发送一个空字节,以便从93C46读取数据
  17.     while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  18.     received_data = SPI_I2S_ReceiveData(SPIx);

  19.     return received_data;
  20. }

  21. // 写入93C46
  22. void write_93C46(uint8_t opcode, uint8_t address, uint8_t data) {
  23.     // 发送操作码、地址和数据
  24.     SPI_I2S_SendData(SPIx, opcode << 5 | address);
  25.     while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  26.     uint8_t received_data = SPI_I2S_ReceiveData(SPIx);

  27.     // 写入数据
  28.     SPI_I2S_SendData(SPIx, data);
  29.     while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
  30.     received_data = SPI_I2S_ReceiveData(SPIx);
  31. }


 楼主| kzlzqi 发表于 2024-3-20 00:09 | 显示全部楼层
在这个示例中,我添加了读取93C46和写入93C46的函数,并做了一些修改以发送操作码和地址。读取函数 read_93C46() 接收操作码和地址作为参数,并使用位移和按位或操作将它们合并为一个字节发送。写入函数 write_93C46() 也类似,发送了合并的操作码和地址,然后发送要写入的数据。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

127

主题

996

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部