stm32f103c8t6 模拟iIC驱动at24c02 和模拟spi 驱动的w25q64代码
时间: 2023-09-06 22:10:13 浏览: 234
以下是stm32f103c8t6模拟iIC驱动at24c02和模拟spi驱动的w25q64的代码示例:
模拟iIC驱动at24c02代码:
```
#include "stm32f10x.h"
#define ADDR_AT24C02 0xA0
void I2C1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
}
void I2C1_Mode_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
uint8_t I2C1_WriteByte(uint8_t dev_addr, uint8_t reg_addr, uint8_t data)
{
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, dev_addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg_addr);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, data);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
return 0;
}
uint8_t I2C1_ReadByte(uint8_t dev_addr, uint8_t reg_addr)
{
uint8_t data;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, dev_addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg_addr);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, dev_addr, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
data = I2C_ReceiveData(I2C1);
return data;
}
void AT24C02_Write(uint8_t addr, uint8_t data)
{
I2C1_WriteByte(ADDR_AT24C02, addr, data);
}
uint8_t AT24C02_Read(uint8_t addr)
{
return I2C1_ReadByte(ADDR_AT24C02, addr);
}
int main()
{
I2C1_GPIO_Config();
I2C1_Mode_Config();
uint8_t data = 0x55;
AT24C02_Write(0x00, data);
data = AT24C02_Read(0x00);
while (1);
}
```
模拟spi驱动的w25q64代码:
```
#include "stm32f10x.h"
#include "spi.h"
#include "w25q64.h"
void W25Q64_Init(void)
{
SPI2_Init();
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
uint8_t W25Q64_ReadSR1(void)
{
uint8_t cmd = W25Q64_CMD_READ_SR1;
uint8_t sr1;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
SPI2_WriteByte(cmd);
sr1 = SPI2_ReadByte();
GPIO_SetBits(GPIOB, GPIO_Pin_12);
return sr1;
}
void W25Q64_WriteEnable(void)
{
uint8_t cmd = W25Q64_CMD_WRITE_ENABLE;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
SPI2_WriteByte(cmd);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
void W25Q64_EraseChip(void)
{
uint8_t cmd = W25Q64_CMD_ERASE_CHIP;
W25Q64_WriteEnable();
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
SPI2_WriteByte(cmd);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
while (W25Q64_ReadSR1() & 0x01);
}
void W25Q64_WritePage(uint32_t addr, uint8_t *data, uint16_t len)
{
uint8_t cmd[4];
uint32_t i;
cmd[0] = W25Q64_CMD_PAGE_PROGRAM;
cmd[1] = (addr >> 16) & 0xFF;
cmd[2] = (addr >> 8) & 0xFF;
cmd[3] = addr & 0xFF;
W25Q64_WriteEnable();
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
for (i = 0; i < 4; i++) {
SPI2_WriteByte(cmd[i]);
}
for (i = 0; i < len; i++) {
SPI2_WriteByte(data[i]);
}
GPIO_SetBits(GPIOB, GPIO_Pin_12);
while (W25Q64_ReadSR1() & 0x01);
}
void W25Q64_ReadData(uint32_t addr, uint8_t *data, uint16_t len)
{
uint8_t cmd[4];
uint32_t i;
cmd[0] = W25Q64_CMD_READ_DATA;
cmd[1] = (addr >> 16) & 0xFF;
cmd[2] = (addr >> 8) & 0xFF;
cmd[3] = addr & 0xFF;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
for (i = 0; i < 4; i++) {
SPI2_WriteByte(cmd[i]);
}
for (i = 0; i < len; i++) {
data[i] = SPI2_ReadByte();
}
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
int main()
{
W25Q64_Init();
uint8_t data[256];
uint32_t i;
for (i = 0; i < 256; i++) {
data[i] = i;
}
W25Q64_EraseChip();
for (i = 0; i < 65536; i += 256) {
W25Q64_WritePage(i, data, 256);
}
for (i = 0; i < 65536; i += 256) {
W25Q64_ReadData(i, data, 256);
/* do something with data */
}
while (1);
}
```
阅读全文