⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_slave_no_ss.c

📁 我要做一个avr作为主控芯片的读卡器
💻 C
字号:
/**
 * @file $RCSfile: spi_slave_no_ss.c,v $
 *
 * Copyright (c) 2004 Atmel.
 *
 * Please read file license.txt for copyright notice.
 *
 * @brief This file is an example to use spi in slave mode.
 *
 * This file can be parsed by Doxygen for automatic documentation
 * generation.
 * Put here the functional description of this file within the software
 * architecture of your program.
 *
 * @version $Revision: 1.0 $ $Name:  $
 */

/* @section  I N C L U D E S */
#include "reg_c51.h"

bit transmit_completed;
char serial_data;

/**
 * FUNCTION_PURPOSE: This file set up spi in slave mode with 
 * Fclk Periph/128 as baud rate and without slave select pin.
 * FUNCTION_INPUTS: P1.5(MISO) serial input  
 * FUNCTION_OUTPUTS: P1.7(MOSI) serial output
 *                   P1.1
 */
void main(void)
{
SPCON |= 0x20;                /* P1.1 is available as standard I/O pin */
/* SPCON.5(SSDIS) has no effect if CPHA=0 in slave mode then P1.1 is used to slave select */ 
SPCON &= ~0x10;               /* slave mode */
SPCON &= ~0x08;               /* CPOL=0; transmit mode example*/
SPCON |= 0x04;                /* CPHA=1; transmit mode example*/
IEN1 |= 0x04;                 /* enable spi interrupt */
SPCON |= 0x40;                /* spi run */
transmit_completed = 0;       /* clear software transfert flag */
EA=1;                         /* enable interrupts */

while(1)	     					   /* endless  */
{
P1_1=~P1_1;                   /* P1.1 is available as standard I/O pin */
if(transmit_completed)
   {
   SPDAT = serial_data;       /* echo data to master */
   transmit_completed = 0;    /* clear software transfert flag */
   }
}


}


/**
 * FUNCTION_PURPOSE: spi interrupt, receive data to master
 * FUNCTION_INPUTS: void
 * FUNCTION_OUTPUTS: void
 */
void it_SPI(void) interrupt 9 /* interrupt address is 0x004B */
{
	switch	( SPSTA )         /* read and clear spi status register */
	{
		case 0x80:
         serial_data=SPDAT;   /* read receive data */
         transmit_completed=1;/* set software flag */
 		break;

		case 0x10:
         /* put here for mode fault tasking */	
		break;
	
		case 0x40:
         /* put here for overrun tasking */	
		break;
	}
   SPDAT=serial_data;         /* needed to complete clearing sequence */
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -