毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> 嵌入式 >> 正文

C8051F330 SPI从机程序无法正常运行

更新时间:2012-11-28:  来源:毕业论文

以下是我用C8051F330SPI模块做的从机程序。采用的是空闲时高电平,上升沿采样的模式。但是当我和相应主机通信时,在MISO上却始终是高电平,从机不能正常发送数据。困扰好久,求大侠给点指点,万分感激

以下是代码
//-----------------------------------------------------------------------------
// F33x_SPI0_Slave.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program configures a C8051F33x as a SPI Slave. The 'F33x MCU
// is configured in 4-wire Slave Mode.
//
// This example is intended to be used with the SPI0_Master example.
//
// Pinout:
//
// P0.0 - SPI SCK    (digital input, open-drain)
// P0.1 - SPI MISO   (digital output, push-pull)
// P0.2 - SPI MOSI   (digital input, open-drain)
// P0.3 - SPI NSS    (digital input, open-drain)
//
// P1.3 - LED        (digital output, push-pull)
//
// all other port pins unused.
//
//
// How To Test:
//
// 1) Download the code to a F330-TB that is connected as above to
//    another device running the SPI0_Master code.
// 2) Verify that the J6 jumper is not populated.
// 3) Verify the LED pins of jumper J3 are populated.
// 4) Run the code.
// 5) If the communication passes, the LEDs on both the Master and Slave
//    boards will blink slowly. If it fails, the LEDs will be OFF.
//
// Target:         C8051F33x
// Tool chain:     Keil C51 7.50 / Keil EVAL C51
// Command Line:   None
//
// Release 1.0
//    -Initial Revision (TP)
//    -12 DEC 2006
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <C8051F330.h>                 // SFR declarations

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define SYSCLK             24500000    // Internal oscillator frequency in Hz

#define MAX_BUFFER_SIZE    8           // Maximum buffer Master will send

// Instruction Set
#define  SLAVE_LED_ON      0x01        // Turn the Slave LED on
#define  SLAVE_LED_OFF     0x02        // Turn the Slave LED off
#define  SPI_WRITE         0x04        // Send a byte from the Master to the
                                       // Slave
#define  SPI_READ          0x08        // Send a byte from the Slave to the
                                       // Master
#define  SPI_WRITE_BUFFER  0x10        // Send a series of bytes from the
                                       // Master to the Slave
#define  SPI_READ_BUFFER   0x20        // Send a series of bytes from the Slave
                                       // to the Master
#define  ERROR_OCCURRED    0x40        // Indicator for the Slave to tell the
                                       // Master an error occurred

sbit LED = P1^3;                       // LED='1' means ON

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------

unsigned char SPI_Data = 0xA5;

unsigned char SPI_Data_Array[MAX_BUFFER_SIZE] = {0};

unsigned char SLAVE_code[]={
0x8e,0x8e,0xde,0x8e,0xd,0x8e,0x0,0x8e,0x11,0x8e,0x3f,0x20,0x0,0x8e,0x0,0x38,0x0,0x0,0x0,0x8e,0x91,
0x8e,0x2b,0x2b,0x0,0x8e,0x2,0x8e,0x5d,0x8e,0x0,0x0,0x0,0x0,0x53,0x49,0x5c,0xf,0x0,0x0,0x0,0x8e,0x3f,
0x20,0x0,0x8e,0x0,0x38,0x0,0x0,0x0,0x8e,0x8,0x3,0x0,0x8e,0x91
};

int receiveIndex = 0;
int sendIndex = 0;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void PCA0_Init (void);
void Oscillator_Init (void);
void Port_Init (void);
void SPI0_Init (void);
void Init_Device (void);

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
   Init_Device ();                     // Initializes hardware peripherals

   EA = 1;                             // Enable global interrupts

   LED = 0;

   while (1);                          // Loop forever
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// PCA0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function disables the watchdog timer.
//
//-----------------------------------------------------------------------------
void PCA0_Init (void)
{
   PCA0MD &= ~0x40;                    // Disable the Watchdog Timer
   PCA0MD = 0x00;
}

//-----------------------------------------------------------------------------
// Oscillator_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function initializes the system clock to use the internal oscillator
// at 24.5 MHz.
//
//-----------------------------------------------------------------------------
void Oscillator_Init (void)
{
   OSCICN = 0x83;                      // Set the internal oscillator to
                                       // 24.5 MHz
}

//-----------------------------------------------------------------------------
// Port_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0  -  SCK  (SPI0), Open-Drain, Digital
// P0.1  -  MISO (SPI0), Push-Pull,  Digital
// P0.2  -  MOSI (SPI0), Open-Drain, Digital
// P0.3  -  NSS  (SPI0), Open-Drain, Digital
//
// P1.3  -  Skipped,     Push-Pull,  Digital (LED D2 on Target Board)
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
   P0MDOUT = 0x02;                     // Make MISO push-pull
   P1MDOUT = 0x08;                     // Make the LED push-pull

   P1SKIP = 0x08;                      // Skip the LED (P1.3)

   XBR0 = 0x02;                        // Enable the SPI on the XBAR
   XBR1 = 0x40;                        // Enable the XBAR and weak pull-ups
}

//-----------------------------------------------------------------------------
// SPI0_Init
//-----------------------------------------------------------------------------

void SPI0_Init()
{

    SPI0CFG   = 0x30;                  //3-wire slave mode. SCK line high in idle state.raising edge sampling
    SPI0CN    = 0x01;

SPI0DAT = 0x8e;

    ESPI0 = 1;                          // Enable SPI interrupts
}

//-----------------------------------------------------------------------------
// Init_Device
//-----------------------------------------------------------------------------
void Init_Device (void)
{
   PCA0_Init ();                       // Disable the Watchdog Timer first
   Oscillator_Init ();
   Port_Init ();
   SPI0_Init ();
}


//-----------------------------------------------------------------------------
// SPI_ISR
//-----------------------------------------------------------------------------
void SPI_ISR (void) interrupt 6
{
 // static unsigned char command;
 // static unsigned char array_index = 0;
 // static unsigned char state = 0;
 // unsigned char dummy_byte;

      // Write collision occurred
WCOL = 0;
    //  SPI0DAT = ERROR_OCCURRED;        // Indicate an error occurred


     // SPI0DAT = ERROR_OCCURRED;        // Indicate an error occurred
    RXOVRN = 0;                      // Clear the Receive Overrun flag

SPI_Data_Array[receiveIndex++] = SPI0DAT;
SPI0DAT = SLAVE_code[sendIndex++];
if(receiveIndex>=MAX_BUFFER_SIZE)
receiveIndex = 0;
if(sendIndex>=sizeof(SLAVE_code))
receiveIndex = 0;

SPIF = 0;   

}
检查硬件及寄存器配件

P0端口交叉开关,分配的不对,即便是特殊功能脚,也应该输出分配一,输入分配0

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。