DSP28335读写M24M02程序
时间: 2023-09-28 14:04:58 浏览: 177
以下是DSP28335读写M24M02程序的示例:
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#include "M24M02.h" //M24M02芯片的驱动头文件
void InitSPI(void);
void Init_M24M02(void);
Uint16 data = 0xABCD; //需要写入M24M02的数据
void main(void)
{
InitSysCtrl();
InitSPI();
Init_M24M02();
M24M02_Write(0x0000, data); //将数据写入M24M02的地址0x0000处
data = M24M02_Read(0x0000); //从M24M02的地址0x0000处读取数据
while(1);
}
void InitSPI(void)
{
EALLOW;
//SPI引脚的GPIO配置
GpioCtrlRegs.GPAMUX1.bit.GPIO16 = 1; //SPISTEA
GpioCtrlRegs.GPAMUX1.bit.GPIO17 = 1; //SPICLKA
GpioCtrlRegs.GPAMUX1.bit.GPIO18 = 1; //SPISIA
GpioCtrlRegs.GPAMUX1.bit.GPIO19 = 1; //SPISTOA
//SPI时钟的配置
SpiaRegs.SPICCR.bit.SPISWRESET = 0; //复位SPI
SpiaRegs.SPICCR.bit.SPICHAR = 7; //8位数据
SpiaRegs.SPICCR.bit.SPILBK = 0; //非循环模式
SpiaRegs.SPICCR.bit.CLKPOLARITY = 1; //时钟极性为1
SpiaRegs.SPICCR.bit.SPISWRESET = 1; //启动SPI
SpiaRegs.SPIBRR = 499; //SPI时钟频率为150kHz
//SPI中断的配置
SpiaRegs.SPICTL.bit.SPIINTENA = 0; //禁止SPI中断
SpiaRegs.SPIPRI.bit.FREE = 1; //允许其它中断优先级高于SPI中断
EDIS;
}
void Init_M24M02(void)
{
EALLOW;
M24M02_Init(); //初始化M24M02
EDIS;
}
M24M02.h的代码如下:
#ifndef __M24M02_H__
#define __M24M02_H__
#include "DSP2833x_Device.h"
#define M24M02_CS_HIGH GpioDataRegs.GPASET.bit.GPIO19 = 1 //将SPI CS引脚置高
#define M24M02_CS_LOW GpioDataRegs.GPACLEAR.bit.GPIO19 = 1 //将SPI CS引脚置低
void M24M02_Init(void);
void M24M02_Write(Uint16 addr, Uint16 data);
Uint16 M24M02_Read(Uint16 addr);
#endif
M24M02.c的代码如下:
#include "M24M02.h"
void M24M02_Init(void)
{
M24M02_CS_HIGH;
//写入M24M02的初始化命令
SpiaRegs.SPITXBUF = 0x06;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
//等待M24M02初始化完成
DELAY_US(2000);
}
void M24M02_Write(Uint16 addr, Uint16 data)
{
M24M02_CS_LOW;
//写入M24M02的写入命令和地址
SpiaRegs.SPITXBUF = 0x02;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
SpiaRegs.SPITXBUF = (addr >> 8) & 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
SpiaRegs.SPITXBUF = addr & 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
//写入数据
SpiaRegs.SPITXBUF = (data >> 8) & 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
SpiaRegs.SPITXBUF = data & 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
M24M02_CS_HIGH;
//等待M24M02写入完成
DELAY_US(10);
}
Uint16 M24M02_Read(Uint16 addr)
{
Uint16 data = 0;
M24M02_CS_LOW;
//写入M24M02的读取命令和地址
SpiaRegs.SPITXBUF = 0x03;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
SpiaRegs.SPITXBUF = (addr >> 8) & 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
SpiaRegs.SPITXBUF = addr & 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
SpiaRegs.SPIRXBUF;
//读取数据
SpiaRegs.SPITXBUF = 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
data = SpiaRegs.SPIRXBUF << 8;
SpiaRegs.SPITXBUF = 0xFF;
while(SpiaRegs.SPISTS.bit.INT_FLAG == 0);
data |= SpiaRegs.SPIRXBUF;
M24M02_CS_HIGH;
return data;
}
#endif
以上代码仅供参考,具体实现还需要根据自己的实际情况进行修改。
阅读全文