学前班
最后登录1970-1-1
在线时间 小时
注册时间2016-8-17
|
最近突然想弄下模拟spi所以就结合了火哥的硬件spi读flash和液晶驱动两个程序弄出个残次品!就是一直读不对flash的设备id,读出来的是0xc2,实际是0x16,想请教下是哪点弄错了吗?小弟初学者,虚心求教!在此先谢谢了!
#include "spi.h"
static void DelayUS(vu32 cnt)
{
uint16_t i;
for(i = 0;i<cnt;i++)
{
uint8_t us = 12; /* 设置值为12,大约延1微秒 */
while (us--) /* 延1微秒 */
{
;
}
}
}
void GPIO_SPI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 开启GPIO时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* 模拟SPI GPIO初始化 */
GPIO_InitStructure.GPIO_Pin=SPI_CLK_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz ;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(SPI_CLK_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN;
GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(SPI_MISO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SPI_CS_PORT, &GPIO_InitStructure);
GPIO_ResetBits(SPI_CS_PORT,SPI_CS_PIN);//拉低片选
}
static void SPI_WriteCmd(unsigned char cmd)
{
unsigned char buf;
unsigned char i;
SPI_DIN(0);
SPI_DCLK(0);
for(i=0;i<8;i++)
{
buf=(cmd>>(7-i))&0x1;
SPI_DIN(buf);
DelayUS(5);
SPI_DCLK(1);
DelayUS(5);
SPI_DCLK(0);
}
}
static unsigned short SPI_ReadCmd(void)
{
unsigned short buf=0,temp;
unsigned char i;
SPI_DIN(0);
SPI_DCLK(1);
for(i=0;i<8;i++)
{
SPI_DCLK(0);
temp= (SPI_DOUT) ? 1:0;
buf|=(temp<<(7-i));
SPI_DCLK(1);
}
buf&=0x0fff;
return(buf);
}
unsigned short SPI_Read(uint8_t dt)
{
SPI_WriteCmd(dt);
return SPI_ReadCmd();
}
u32 SPI_FLASH_ReadDeviceID(void)
{
u32 Temp = 0;
/* Select the FLASH: Chip Select low */
GPIO_ResetBits(SPI_CS_PORT,SPI_CS_PIN);
/* Send "RDID " instruction */
SPI_Read(0xAB);
SPI_Read(0xFF);
SPI_Read(0xFF);
SPI_Read(0xFF);
/* Read a byte from the FLASH */
Temp = SPI_Read(0xFF);
/* Deselect the FLASH: Chip Select high */
GPIO_SetBits(SPI_CS_PORT,SPI_CS_PIN);
return Temp;
} |
|