XS128 SPI communicate with MMA7455  problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

XS128 SPI communicate with MMA7455  problem

Jump to solution
881 Views
toyota
Contributor II

#define MMA_CS_D DDRM_DDRM3
#define MMA_MISO_D DDRM_DDRM2
#define MMA_MOSI_D DDRM_DDRM4
#define MMA_SCK_D DDRM_DDRM5
#define MMA_INT1_D DDRT_DDRT6
#define MMA_INT2_D DDRT_DDRT5
#define MMA_CS PTM_PTM3
#define MMA_MISO PTM_PTM2
#define MMA_MOSI PTM_PTM4
#define MMA_SCK PTM_PTM5
#define MMA_INT1 PTT_PTT6
#define MMA_INT2 PTT_PTT5
void mma7455_Init(void){
  MODRR_MODRR4=1; //Module Routing Register (MODRR) 设置引脚的第二功能 启用PM2345
  MMA_CS_D=1; //必须初始化IO方向
  MMA_MISO_D=0;
  MMA_MOSI_D=1;
  MMA_SCK_D=1;
   MMA_CS=1; //CS=1
  SPI0CR1=0x5e; //SPI初始化 0101 0000 中断禁止 主机模式 时钟空闲为高 先发最高位
  SPI0CR2=0x10; //ss 引脚 受MODFEN 控制
  SPI0BR=0x06; //SPI 128分频 16/128MHz
  SPI_Write_Reg_mma7455(0x16,0x05); //SPI四线模式 不自检 范围2g 测量模式
  SPI_Write_Reg_mma7455(0x0D,0x80); //禁止IIC功能
  }
    unsigned int SPI_Read_Reg_mma7455(unsigned int uiReg){
    unsigned int uiTemp=0;
    MMA_CS=0; //CS=0
    uiTemp=SPI0DRL;
    while (!SPI0SR_SPTEF); //判断发送寄存器是空的
    SPI0DRL= ((uiReg &0x3F)<<1); //向寄存器地址写 读的命令
    while(!SPI0SR_SPIF); //写完读取从机的寄存器的指令之后 等待从机反馈数据
    uiTemp=SPI0DRL; //读取MMA7455反馈的数据
    SPI0DRL=0xff; //主机产生8个时钟信号,MMA7455返回 数据
    while(!SPI0SR_SPIF);
    uiTemp=SPI0DRL;
    MMA_CS=1; //CS=1
    return (uiTemp);
    }
  void SPI_Write_Reg_mma7455(unsigned int uiReg,unsigned int uiValue)
  {
   unsigned int uiTemp=0;
    MMA_CS=0; //CS=0
   while (!SPI0SR_SPTEF); //判断发送寄存器是空的
   SPI0DRL=(((uiReg &0x3F)<<1)|0x80); //选择像哪个寄存器写 指令
   while (!SPI0SR_SPIF); //判断SPI0IF是否产生中断
   uiTemp=SPI0DRL;
   while (!SPI0SR_SPTEF);
   SPI0DRL=uiValue;
   while(!SPI0SR_SPIF);
   uiTemp=SPI0DRL;
   MMA_CS=1; //CS=1

 }

i'm sorry my english is very poor so i want use chinese to tall you problem

上面程序是用XS128的SPI模块与mma7455通信的程序段,可是我不知道为什么一直得不到数据~请大家给我看看~谢谢~

Labels (1)
0 Kudos
1 Solution
418 Views
toyota
Contributor II

Thank you, was I thought I put in the problem won't have a reply, did not think of you respond to me, your questions I immediately adjusted, if you say a problem can be solved the problem of my I come again thank you to you. My English is very poor, so use translation software written Chinese replies to you, many wrong place to please you don't mind

 

 

 thanks

form  TianFeng

View solution in original post

0 Kudos
2 Replies
418 Views
bigmac
Specialist III

Hello, and welcome to the forum.

 

Unfortunately, my Chinese is non-existent, so I cannot be sure of your difficulty.

 

I gather that the SPI module has been set up for 8-bit operation (assuming 16-bit operation is also available for the particular device).  Your code suggests that 8-bit operation is actually required.  However, I can see that your parameters and the variable are 16-bit size.  It would be preferred to match these to the actual data size.

 

 

#define SPI_send(x)  (void)SPI_transfer(x)#define SPI_recv()   SPI_transfer(0xFF)     // Returns received valueunsigned char SPI_transfer( unsigned char val){   while (!SPI0SR_SPTEF);   SPI0DRL = val;   while (!SPI0SR_SPIF);   return SPI0DRL;}   unsigned char SPI_Read_Reg_mma7455( unsigned char ucReg){   unsigned char ucTemp;      MMA_CS = 0;                   // CS = 0   SPI_send( (ucReg & 0x3F) << 1);   ucTemp = SPI_recv();   MMA_CS = 1;                   // CS = 1   return ucTemp;}void SPI_Write_Reg_mma7455( unsigned char ucReg, unsigned char ucValue){   MMA_CS = 0;                   // CS = 0   SPI_send( ((ucReg & 0x3F) << 1) | 0x80);   SPI_send( ucValue);      MMA_CS = 1;                   // CS = 1}

 

Another issue may be whether you have set the CPHA and CPOL bits to suit the MMA7455?  I believe that CPHA = 0, CPOL = 0 is required.

 

Regards,

Mac

 

0 Kudos
419 Views
toyota
Contributor II

Thank you, was I thought I put in the problem won't have a reply, did not think of you respond to me, your questions I immediately adjusted, if you say a problem can be solved the problem of my I come again thank you to you. My English is very poor, so use translation software written Chinese replies to you, many wrong place to please you don't mind

 

 

 thanks

form  TianFeng

0 Kudos