?? 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 + -