本帖最后由 lilihua0721 于 2011-10-3 20:29 编辑
分享LM3S8962开发板SPI读取铁电代码前面很多网友提问LM3S的SPI不好用或者用不了,通常用GPIO模拟,经过我2天的调试,SPI完全能用,效率还不错,下面提供主要函数和代码,调试时根据布板情况先从低波特率往上加,我是从9600开始调,最高1M没有问题,再高有点不稳定因为我电路板是飞线的线有点长,后面附逻辑分析仪截图,我写的铁电型号是FM25L256
//SSI初始化
void SSIInit(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);//使能GPIOA
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);//使能SSI
SSIDisable(SSI0_BASE);
GPIOPinTypeSSI(GPIO_PORTA_BASE, (GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5));
GPIODirModeSet(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_DIR_MODE_OUT);//配置片选管脚为输出
GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD_WPU);
SSIConfig(SSI0_BASE,SSI_FRF_MOTO_MODE_3, SSI_MODE_MASTER, 9600, 8);//配置SSI
SSIEnable(SSI0_BASE);//使能SSI
}
//使能SPI
void SPIEnable()
{
unsigned long ulTemp;
SSIDisable(SSI0_BASE);
SSIConfig(SSI0_BASE, SSI_FRF_MOTO_MODE_3, SSI_MODE_MASTER, 9600, 8);
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_3);
GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ~GPIO_PIN_3);
SSIEnable(SSI0_BASE);
while(SSIDataGetNonBlocking(SSI0_BASE, &ulTemp) != 0)//清除FIFO
{
}
}
//禁止SPI
void SPIDisable()
{
unsigned long ulTemp;
while(SSIDataGetNonBlocking(SSI0_BASE, &ulTemp) != 0)//清除FIFO
{
}
SSIDisable(SSI0_BASE);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3);
GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
}
//发送一个字节
void Send_Byte(unsigned char data)
{
unsigned long NullData;
SSIDataPut(SSI0_BASE, data);
SSIDataGet(SSI0_BASE, &NullData);
}
//接收一个字节
unsigned char Get_Byte(void)
{
unsigned long ReadData;
SSIDataPut(SSI0_BASE, 0xff);//发送一个无效字节,以产生接收时钟
SSIDataGet(SSI0_BASE, &ReadData);
return (unsigned char)ReadData;
}
//----------main主函数------------------------------------
SSIInit();//初始化SPI
SPIEnable();//使能铁电
Send_Byte(0x3);//发送读命令
Send_Byte(0x0);//读取数据地址高字节
Send_Byte(0x0);//读取数据地址低字节
sfbuf[0]=Get_Byte();//读取数据
sfbuf[1]=Get_Byte();
sfbuf[2]=Get_Byte();
sfbuf[3]=Get_Byte();
SPIDisable();//禁止铁电
SysCtlDelay(100*(TheSysClock/3000));
SPIEnable();
Send_Byte(0x06);//发送写使能命令
SPIDisable();
SPIEnable();
Send_Byte(0x02);//发送字节数据烧写命令
Send_Byte(0x0);//发送2个字节的地址信息
Send_Byte(0x0);
Send_Byte(0x60);
Send_Byte(0x61);
Send_Byte(0x62);
Send_Byte(0x63);
SPIDisable();
SysCtlDelay(100*(TheSysClock/3000));
SPIEnable();//使能铁电
Send_Byte(0x3);//发送读命令
Send_Byte(0x0);//读取数据地址高字节
Send_Byte(0x0);//读取数据地址低字节
sfbuf[0]=Get_Byte();//读取数据
sfbuf[1]=Get_Byte();
sfbuf[2]=Get_Byte();
sfbuf[3]=Get_Byte();
SPIDisable();//禁止铁电
写铁电截图
读铁电截图
|