|
/****************************************************************************
* 文 件 名:main.c
* 功 能:使用硬件SPI接口输出控制LED显示。(硬件:74HC595输出控制LED显示)
* 说 明:将SmartARM2200开发板上的J5上SPI1信号和LED子板的SPI接口相连接
****************************************************************************/
#include "config.h"
//#include "xxx.h"
#define C_POW (1<<23)
#define C_CS (1<<24)
#define C_INT (1<<25)
#define C_WP (1<<25)
int bl1,bl2;
uint8 rebuf[600];
int p=0;
uint8 read_SDcard[80000];
uint16 read;
int num,point=0;
/****************************************************************************
* 名 称:DelayNS()
* 功 能:长软件延时
* 入口参数:dly 延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
void DelayNS(uint32 dly)
{
uint32 i;
for(; dly>0; dly--)
{
for(i=0; i<50000; i++);
}
}
/*******************************************************************************************************************
** 函数名称: 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)
{
SPI_SPCCR = 30; /* 设置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)
{
SPI_SPCCR = 8; /* 设置SPI时钟分频值为8 Set the value of dividing frequency to 8 */
}
/****************************************************************************
* 名 称:MSpiInit()
* 功 能:初始化SPI接口,设置为主机。
* 入口参数:无
* 出口参数:无
****************************************************************************/
void MSpiInit(void)
{
PINSEL0 = 0x5500;
PINSEL1 = PINSEL1 & ( ~(3<<14) ); //将C_POW配置为输出模式
IO0DIR |=C_POW;
PINSEL1 = PINSEL1 & ( ~(3<<16) ); //将C_CS 配置为输出模式
IO0DIR |=C_CS;
IO1DIR &=~C_WP ; //将C_WP 配置为输入模式
PINSEL1 = PINSEL1 & ( ~(3<<18) ); //将C_INT 配置为输入模式
IO0DIR &=~C_INT;
SPI_Clk400k();
S0PCR = (1 << 3) | // CPHA = 0, 数据在SCK 的第一个时钟沿采样
(1 << 4) | // CPOL = 1, SCK 为低有效
(1 << 5) | // MSTR = 1, SPI 处于主模式
(0 << 6) | // LSBF = 0, SPI 数据传输MSB (位7)在先
(0 << 7); // SPIE = 0, SPI 中断被禁止
}
/*******************************************************************************************************************
** 函数名称: 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;
SPI_SPDR = byte; /* 发送数据放入SPI数据寄存器 */
while(0 == (SPI_SPSR & 0x80)); /* 等待SPIF置位,即等待数据发送完毕 */
/* wait for SPIF being set, that is, wait for finishing of data being send */
//temp = SPI_SPDR;
}
/*******************************************************************************************************************
** 函数名称: 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)
{
SPI_SPDR = 0xFF;
while(0 == (SPI_SPSR & 0x80)); /* 等待SPIF置位,即等待收到数据 */
/* wait for SPIF being set, that is, wait for being received data */
return(SPI_SPDR); /* 读取收到的字节 read the byte received */
}
/******显示*****/
void Image_320x240_dispaly(unsigned char *HZ_BUF)
{
int x,y;
for(y=0;y<240;y++)
{
for(x=0;x<320;x++)
{
GUI_Point(x,y, *(HZ_BUF+y*320+x) );
}
}
}
/*************************************************************
* 名称:main()
* 功能:
***************************************************************/
int main(void)
{
int i;
/************************初始化硬件************************************/
MSpiInit(); // 初始化SPI接口
IO0SET=C_POW; // 断电
DelayNS(10);
IO0CLR=C_POW; // 再供电
GUI_Initialize();
bl1= IO0PIN&C_INT; //有SD卡时是低电平 无SD卡时是高电平
if (bl1==0)
{
bl2= IO1PIN&C_WP; //写保护的时候输出高电平 否则输出低电平
}
/**************************复位设备CMD0******************************/
IO0SET |=C_CS;
SPI_SendByte(0xff);
DelayNS(2);
for(i=0;i<=10;i++) //大于74个时钟
SPI_SendByte(0xff);
DelayNS(2);
IO0CLR |=C_CS;
SPI_SendByte(0x40);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x95);
do
{
bl1= SPI_RecByte();
} while(bl1==0xff);
for (i=0;i<=10;i++)
{
/*************************激活卡 CMD1********************************/
IO0SET |=C_CS;
SPI_SendByte(0xff);
DelayNS(2);
IO0CLR |=C_CS;
SPI_SendByte(0x41);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0xff);
SPI_SendByte(0xff);
do
{
bl2= SPI_RecByte();
} while(bl2==0xff);
IO0SET |=C_CS;
DelayNS(2);
/************************* CMD 55 *********************************/
IO0SET |=C_CS;
SPI_SendByte(0xff);
DelayNS(2);
IO0CLR |=C_CS;
SPI_SendByte(0x40+55);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0xff);
SPI_SendByte(0xff);
do
{
bl2= SPI_RecByte();
} while(bl2==0xff);
IO0SET |=C_CS;
DelayNS(2);
/************************* ACMD 41 read****************************/
IO0SET |=C_CS;
SPI_SendByte(0xff);
DelayNS(2);
IO0CLR |=C_CS;
SPI_SendByte(0x40+41);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0xff);
SPI_SendByte(0xff);
do
{
bl2= SPI_RecByte();
} while(bl2==0xff);
IO0SET |=C_CS;
DelayNS(2);
}
/***********************CMD 51 READ block ***************************/
for(read=0;read<150;read++)
{
IO0SET |=C_CS;
SPI_SendByte(0xff);
DelayNS(2);
IO0CLR |=C_CS;
SPI_SendByte(0x51);
SPI_SendByte(0x00);
SPI_SendByte(0x06);
SPI_SendByte(0x82); //从第1扇区读出512字节数
SPI_SendByte(0x00+read);
SPI_SendByte(0xff);
do
{
bl2= SPI_RecByte();
} while(bl2==0xff);
do
{
bl2= SPI_RecByte();
} while(bl2==0xff);
p=0;
do{
bl1= SPI_RecByte();
rebuf[p++]=bl1;
} while(p<512);
IO0SET |=C_CS;
DelayNS(2);
for(num=0;num<512;num++)
{
read_SDcard[num+point]=rebuf[num];
}
point+=512;
}
/***********************CMD3 csd READ***************************/
/* for(i=0;i<60000;i++)
{
}
Image_320x240_dispaly((unsigned char*)read_SDcard);
while(1);
return(0);
}
我是将一张图片转换成bin文件放到sd卡上,从这个文件的首簇开始读扇区文件,共150个扇区,然后将图片打印出来,但程序一直卡在这里:
uint8 SPI_RecByte(void)
{
SPI_SPDR = 0xFF;
while(0 == (SPI_SPSR & 0x80)); /* 等待SPIF置位,即等待收到数据 */
/* wait for SPIF being set, that is, wait for being received data */
return(SPI_SPDR); /* 读取收到的字节 read the byte received */
}
高手帮忙看看这是什么错误 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|