打印

51单片机SPI求教

[复制链接]
1590|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zaiwu2000|  楼主 | 2013-2-28 11:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近接触51单片机SPI,单片机采用ATMELAT89C51RB2,想利用其自带的SPI功能,编译通过,但在仿真时总没看到期望的波形,望高手指导一二。仿真文件见附件 AT89C51RB2.zip (813.32 KB)
源码如下:
#include<at89c51RB2.h>

#define uchar unsigned char
#define uint unsigned int

#define Key_UP P1_2
#define Key_DOWN P1_3
#define LED1 P0_0
#define LED2 P0_1

/*********SPI控制寄存器****************/
sfr SPCON = 0xC3;               
#define SPR2  0x80
#define SPEN  0x40
#define SSDIS 0x20  
#define MSTR  0x10
#define CPOL  0x08
#define CPHA  0x04
#define SPR1  0x02
#define SPR0  0x01

/*********SPI状态寄存器****************/
sfr SPSTA = 0xC4;
#define SPIF  0x80
#define WCOL  0x40
#define SSERR 0x20  
#define MODF  0x10

sfr SPDAT = 0xC5;        

/*********SPI中断***************/
/*     sfr IEN1 = 0xB1;        */
/*******************************/
#define ESPI 0x04; //
/***************************************/

uchar data_buf;

bit start_flag = 0;

//uchar y[4];
                                                 
void key_scanl();
void led();
void key_scanl();

///////////////////////////////////////////////////////////
void main()
{
/************************************************************/
        SPCON |= 0x10;                 //MSTR:主模式
//        P1_1=1;                         //SS:主模式
        SPCON |= 0x82;                 //SPR2、SPR1:128分频
        SPCON &= ~0x08;         //CPOL:SPI时钟前后沿选择,如此后沿取数
        SPCON |= 0x04;                 //CPHA:时钟相位
        IEN1 |= 0x04;                 //ESPI:开SPI中断
        SPCON |= 0x40;                 //SPEN:允许运行SPI
        EA=1;                                 //开总中断
/************************************************************/
        start_flag=0;
        data_buf=0x11;
        LED1=0;
        LED2=0;
        P1=0x3F;
/****************************************************************/
        while (1)
        {
                key_scanl();
                led();
/***********************************************************/
                if (start_flag=0)                 //第一次数据由主程序上传,其后由中断服务程序负责
                {                                                  
                        SPDAT=data_buf;
                }                                                                
        }
}

void delay(int time)                          //delay(50)是50ms,delay(5000)是5S左右
{
        uint i,j;
        for(i=0;i<time;i++)
        {
                for(j=0;j<1000;j++);
        }
}

/***************SPI中断函数**********************/
/*           SPI中断时SPIF变为零                                   */
/************************************************/
void it_SPI(void) interrupt 9         // interrupt address is 0x004B
{
        switch( SPSTA )                         // read and clear spi status register
        {
                case 0x80:                                //SPIF:允许SPI中断
                start_flag=1;                        // set software flag
                SPDAT=data_buf;
                break;
                case 0x10:                                //接受出错
                break;
                case 0x40:                                //SPI写冲突标志
                break;
        }
}



void key_scanl()
{
        if(!Key_UP)          
                data_buf=0x10;
        if(!Key_DOWN)
                data_buf=0x11;
}

void led()
{
        if(data_buf==16)
        LED1=1;
        else
        LED1=0;
        if(data_buf==17)
        LED2=1;
        else
        LED2=0;
}


相关帖子

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

本版积分规则

6

主题

15

帖子

1

粉丝