26、江科大stm32视频学习笔记——W25Q64简介

一、电路图

1、软件模拟的SPI:线可以任意接

2、硬件模拟的SPI:要按以下方式连接

3、本次软件模拟和硬件模拟使用同一个电路图,方便切换

CS(片选):PA4                                   DO(从机输出):PA6

CLK(时钟):PA5                                 DI(从机输入):PA7

二、软件SPI读写W25Q64

1、SPI.c(初始化寄存器,实现读取一个字节的功能)

#include "stm32f10x.h"                  // Device header

void MySPI_W_SS(uint8_t BitValue)
{
	GPIO_WriteBit(GPIOA, GPIO_Pin_4, (BitAction)BitValue);
}

void MySPI_W_SCK(uint8_t BitValue)
{
	GPIO_WriteBit(GPIOA, GPIO_Pin_5, (BitAction)BitValue);
}

void MySPI_W_MOSI(uint8_t BitValue)
{
	GPIO_WriteBit(GPIOA, GPIO_Pin_7, (BitAction)BitValue);
}

uint8_t MySPI_R_MISO(void)
{
	return GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6);
}

void MySPI_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;  //推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                            //上拉输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	MySPI_W_SS(1);      //SS默认高电平(下降沿为开始工作,低电平状态为工作中,上升沿为结束工作)
	MySPI_W_SCK(0);     //SCK默认为低电平(上升沿移入数据,下降沿移出数据)
}

void MySPI_Start(void)
{
	MySPI_W_SS(0);
}

void MySPI_Stop(void)
{
	MySPI_W_SS(1);
}

uint8_t MySPI_SwapByte(uint8_t ByteSend)
{
	uint8_t i, ByteReceive = 0x00;
	for (i = 0; i < 8; i ++)
	{
		//先SS下降沿,移出数据,SCK上升沿,移入数据,再SCK下降沿,移出数据,下面只管主机,
		MySPI_W_MOSI(ByteSend & (0x80 >> i));   //将数据移出到MOSI线
		MySPI_W_SCK(1);                         //上升沿移入数据
		if (MySPI_R_MISO() == 1){ByteReceive |= (0x80 >> i);}  //将移入的数据读取出来
		MySPI_W_SCK(0);                         //下降沿移出数据
	}
	return ByteReceive;                         //读取出来的数据
}

 

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-22 07:12:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-22 07:12:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-22 07:12:02       82 阅读
  4. Python语言-面向对象

    2024-01-22 07:12:02       91 阅读

热门阅读

  1. markdown公式编写备忘录

    2024-01-22 07:12:02       54 阅读
  2. Effective Objective-C 学习第二周

    2024-01-22 07:12:02       45 阅读
  3. Docker compose部署Golang服务

    2024-01-22 07:12:02       53 阅读
  4. docker常用命令总结

    2024-01-22 07:12:02       52 阅读
  5. 几种排序算法

    2024-01-22 07:12:02       51 阅读
  6. briefly describe the classic sorting algorithm

    2024-01-22 07:12:02       48 阅读
  7. 【python学习】面向对象编程1

    2024-01-22 07:12:02       50 阅读