zhanzhipeng 发表于 2012-4-27 21:36:09

求助STC12C5A60S2的SPI问题!!

怎么给SPDAT赋值?P0口流水灯直接赋SPDAT的值都没办法显示。
SPIF没办法置位,也就是说传输没办法完成啊,那该怎么办。。。
也一直进入不了中断

搞了几天了一直弄不出来,求教~~

附上我的程序

# include <STC12C5A60S2.h>
# define MASTER 1                                     //作为从机程序时,将该行注释掉

# define uchar unsigned char
# define uintunsigned int
typedef unsigned char BYTE;

//SPSTAT
#define SPIF      0x80      //SPSTAT.7
#define WCOL      0x40      //SPSTAT.6
//SPCTL
#define SSIG      0x80      //SPCTL.7
#define SPEN      0x40      //SPCTL.6
#define DORD      0x20      //SPCTL.5
#define MSTR      0x10      //SPCTL.4
#define CPOL      0x08      //SPCTL.3
#define CPHA      0x04      //SPCTL.2
#define SPDHH      0x00      //CPU_CLK/4
#define SPDH      0x01      //CPU_CLK/16
#define SPDL      0x02      //CPU_CLK/64
#define SPDLL       0x03      //CPU_CLK/128

sbit LED= P0^0;

bit SPI_Receive;                                              //SPI 端口收到数据标志位

void SPI_Send(uchar b);
void delay(uint x);

void main()
{               
        uchar shuju;
#ifdef MASTER
        SPCTL=0xff;       
#else
        SPCTL=0xef;
#endif
        SPDAT = 01101101;
        SPSTAT = 0xc0;
        IE2=IE2|0x02;                 //ESPI(IE2.1)=1,允许SPIF产生中断
        SPI_Receive=0;                  //清标志字
        EA=1;                     //开总中断

        LED=1;

        while(1)
        {

#ifdef MASTER
        SPI_Send(011101010111010101);
        P2=SPDAT;
        SPI_Send(000011000100110101);
        P2=SPDAT;

        if(SPI_Receive)
        {
                shuju=SPDAT;
                P2=SPDAT;
                delay(1000);       
        }
#else
        SPI_Send(0x6b);
        P2=SPDAT;
               
        if(SPI_Receive)
        {
                shuju=SPDAT;
                P2=SPDAT;
                delay(10000);       
        }
#endif                       
        }
}

void SPI_Send(BYTE b)
{
        IE2&=0x01;
        SPDAT = b;
//        while(!(SPSTAT&0x80));        //等待发送完毕 SPIF = 1
        IE2|=0x02;       //打开中断
        LED=~LED;
        delay(5000);
}

void delay(uint x)
{
        uint y,z;
        for(y=x;y>0;y--)
        for(z=110;z>0;z--);
}

void SPI_Routine(void) interrupt 9
{
        SPSTAT = 0xc0;
        SPI_Receive=1;                 //设置SPI端口收到数据标志
}

Volftooth 发表于 2012-4-27 23:12:58

SPI_Send(000011000100110101);

哈时候C语言可以直接识别二进制了?、

zhanzhipeng 发表于 2012-4-28 18:53:04

Volftooth 发表于 2012-4-27 23:12 static/image/common/back.gif
SPI_Send(000011000100110101);

哈时候C语言可以直接识别二进制了?、

本人不才。。。

zhanzhipeng 发表于 2012-4-28 19:20:00

两个单片机之间的SPI通信,跪求例程啊~~
页: [1]
查看完整版本: 求助STC12C5A60S2的SPI问题!!