打印
[ZLG-MCU]

P89X51单片机的SPI接口如何使用?

[复制链接]
1362|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jakezlc|  楼主 | 2009-5-20 13:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
原来我用的是AT89C51系列单片机,因为不带SPI接口,所以是用模拟方式实现的。

现在改用NXP的P89X51单片机,片子自带了SPI接口,启动我弄明白了,置位SPCR即可;

搞不明白的问题:

1、 作主接机时收时到的数据放到哪里,是不是有专门的寄存器?还要自己设置个变量一位一位的移入操作(如果是这样那还不如用模拟方式来实现的,毕竟两种芯片的价格差得很多啊)。

2、如何接收16位数据,因为项目里用TLC2543AD芯片,一次输出有12位和16位等几种方式,而P89V51“一个字节的数据传输结束后,SPI时钟关闭,SPIF标志置位”,那我收了前8位,后8位怎么办啊???

相关帖子

沙发
liqindz| | 2009-5-20 13:35 | 只看该作者

RE

//-------------------------------------------------
//Company: ZLGMCU
//MCU:  P89V51RD2
//Crystal: 11.0592Mhz
//Writer:   ZLG900
//Function: Read/Write Cat25C02
//-------------------------------------------------
#include "reg52.h"

sfr SPCR=0xD5;  //SPI Control
sfr SPSR=0xAA;  //SPI Status
sfr SPDAT=0x86; 
 
sbit CS=P1^4;
//sbit SI=P1^5;
//sbit SO=P1^6;
//sbit SCK=P1^7;

//------------------------------------
//Delay():  delay 4 ms,(11.0592MHz)
//------------------------------------
void  Delay()
{
 int i;
 for(i=0x00;i<0x500;i++);
}


//------------------------------------
//SPI_Init: Init SPI 
//------------------------------------
void  SPI_Init()
{

 SPCR=0x56;  //0101 0110 ,Disable SPI Interrupt, Enable SPI, Master Mode,CHOL=0,CHOA=1;
 SPSR=0x00;  //0000 0000 ,Clr SPIF
 CS=1;
// ES=1;   
// EA=1;

}

//------------------------------------
//EnableWrite:
//function:   Enable=0,Disable write  
//            Enable=1,Enable write 
//------------------------------------
void EnableWrite(char Enable)
{
 CS=0;

 if(Enable==0)
 {
  SPDAT= 0x04;    //Disable Command
 }
 else
 {
  SPDAT= 0x06;    //Enable Command
 }
 while((SPSR&0x80)==0);
 SPSR=0x00;

 CS=1;
}

//------------------------------------
//ReadData:
//------------------------------------
void ReadData(unsigned char address, unsigned char *buf, unsigned char length)
{
 CS=0;

 SPDAT=0x03;     //Read Data Command
 while((SPSR&0x80)==0);
 SPSR=0x00;
  
 SPDAT=address;    //Read address
 while((SPSR&0x80)==0);
 SPSR=0x00;

 while(length--)
 {
  SPDAT=0xff;
  while((SPSR&0x80)==0);
  *buf=SPDAT;

  buf++;
  SPSR=0x00;
 }

 CS=1;
}


//------------------------------------
//ReadStatus
//------------------------------------
unsigned char ReadStatus (void)
{
 unsigned char status;

 CS = 0;

 SPDAT = 0x05;     //Read Status Command
 while ((SPSR & 0x80) == 0);
 SPSR=0x00; 

 SPDAT = 0xFF;     //Read SPI status
 while ((SPSR & 0x80) == 0);
 status = SPDAT;

 SPSR=0x00; 
 CS = 1;

 return (status);
}

//------------------------------------
//WriteData:
//------------------------------------
void WriteData(unsigned char address,unsigned char *buf,unsigned char length)
{

 EnableWrite(1);
 while (ReadStatus() & 0x01);

   CS = 0;
   SPDAT = 0x02;      //Write Data Command
 while((SPSR&0x80)==0);
 SPSR=0x00;  

   SPDAT = address;     //Write address
 while((SPSR&0x80)==0);
 SPSR=0x00;

 for (; length>0 ;length-- )
 {
    SPDAT = *buf;
  while((SPSR&0x80)==0);
  SPSR=0x00;
  
  address++;
  buf++;
 }
   CS = 1;

 Delay();
 EnableWrite(0);
}


//------------------------------------
//main:
//------------------------------------
void main()
{
 unsigned char temp[8];
 unsigned char SaveData[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x70,0x80};

 SPI_Init(); 

 while(1)
 {

  WriteData(0x00,SaveData,8);
  ReadData(0x00,temp,8);

 }
}

//------------------------------------
//END
//------------------------------------

 

使用特权

评论回复
板凳
jakecumt| | 2009-5-20 16:48 | 只看该作者

你可以一次读16位的 好好看下时序图

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

14

主题

49

帖子

0

粉丝