📄 sdhal.c
字号:
/*****************************************************************************************************
** Descriptions:sd 卡驱动软件包: 硬件抽象层 ---- SPI操作函数
********************************************************************************************************/
#include "define.h"
/**************************************************************
读写SD卡的SPI接口函数: SPI接口设置,发送/接收字节函数
**************************************************************/
/*
*********************************************************************************************************
** 函数名称:SSP_Init()
** 函数功能:将SSP控制器设置为主机SPI。
** 入口参数:无
** 出口参数:无
*********************************************************************************************************
*/
void SPI_INIT(void)
{
SSPCR1 = 0x00;
PINSEL1 |= (0x02 << 2) | //连接SSP模块
(0x02 << 4) |
(0x02 << 6);
SPI_CS_OUT(); //片选脚设置为输出
SPI_CS_SET(); //设置片选脚为高电平
SSPCR0 = 0x0707; //8位数据长度,CPOL = 0, CPHA = 0,SCR=15,SPI模式
SSPCPSR = 0x2; //时钟分频器
SSPCR1 = 0x02; //SSP使能
}
/*******************************************************************************************************************
** 函数名称: void SD_HardWareInit() Name: void SD_HardWareInit()
** 功能描述: 初始化访问SD卡的硬件条件 Function: initialize the hardware condiction that access sd card
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SD_HardWareInit(void)
{
SPI_INIT(); /* 初始化SPI接口 */
SPI_CS_SET(); /* CS置高 */
}
/*******************************************************************************************************************
** 函数名称: void SPI_Clk400k() Name: void SPI_Clk400k()
** 功能描述: 设置SPI的时钟小于400kHZ Function: set the clock of SPI less than 400kHZ
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_Clk400k(void)
{
SSPCPSR = 128; /* 设置SPI时钟分频值为128 Set the value of dividing frequency to 128 */
}
/*******************************************************************************************************************
** 函数名称: void SPI_ClkToMax() Name: void SPI_ClkToMax()
** 功能描述: 设置SPI的clock到最大值 Function: set the clock of SPI to maximum
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_ClkToMax(void)
{
SSPCPSR = 8; /* 设置SPI时钟分频值为8 Set the value of dividing frequency to 8 */
}
/*******************************************************************************************************************
** 函数名称: void SPI_SendByte() Name: void SPI_SendByte()
** 功能描述: 通过SPI接口发送一个字节 Function: send a byte by SPI interface
** 输 入: INT8U byte: 发送的字节 Input: INT8U byte: the byte that will be send
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_SendByte(uint8 byte)
{
uint8 temp;
SSPDR = byte;
while ( (SSPSR & 0x01) == 0 );
temp = SSPDR; /* 刷新 RxFIFO */
}
/*******************************************************************************************************************
** 函数名称: INT8U SPI_RecByte() Name: INT8U SPI_RecByte()
** 功能描述: 从SPI接口接收一个字节 Function: receive a byte from SPI interface
** 输 入: 无 Input: NULL
** 输 出: 收到的字节 Output: the byte that be received
********************************************************************************************************************/
uint8 SPI_RecByte(void)
{
SSPDR = 0xFF;
while(SSPSR & 0x10); /* 等待SPIF置位,即等待收到数据 */
/* wait for SPIF being set, that is, wait for being received data */
return(SSPDR); /* 读取收到的字节 read the byte received */
}
/*******************************************************************************************************************
** 函数名称: void SPI_CS_Assert() Name: void SPI_CS_Assert()
** 功能描述: 片选SPI从机 Function: select the SPI slave
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_CS_Assert(void)
{
SPI_CS_CLR(); /* 片选SPI从机 select the SPI slave */
}
/*******************************************************************************************************************
** 函数名称: void SPI_CS_Deassert() Name: void SPI_CS_Deassert()
** 功能描述: 不片选SPI从机 Function: not select the SPI slave
** 输 入: 无 Input: NULL
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_CS_Deassert(void)
{
SPI_CS_SET(); /* 不片选SPI从机 not select the SPI slave */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -