spi.c

来自「一个在PROTEUS 中方真lpcarm的ucosii移植」· C语言 代码 · 共 67 行

C
67
字号
/****************************************Copyright (c)**************************************************
**--------------File Info-------------------------------------------------------------------------------
** File name: SPI.C
** Last modified Date:2007-12-20
** Last Version: 1.0
** Descriptions: SPI数据操作
**------------------------------------------------------------------------------------------------------
** Created by:   Rein Lee
** Created date: 2007-12-20
** Version:      1.0
** Descriptions: The original version
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
********************************************************************************************************/
#include   "config.h"
#define   HC595_CS    1<<24            /*P0.24口为74HC595的片选*/
/*********************************************************************************************************
** 函数名称: MSpiIni()
** 功能描述: 初始化SPI接口,设置为主
** 输 入: 无
** 输 出: 无       
** 全局变量: 无
** 调用模块: 无
**------------------------------------------------------------------------------------------------------
** Created by    Rein Lee
** Created date: 2007-12-20
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void MSpiIni(void)
{ 
	PINSEL1 &= ~(3 << (2 * (24 - 16)));	//P0.23 CS
	PINSEL1 = (PINSEL1 & (~0xcc)) | 0x88;	// 不影响其它管脚连接,设置I/O连接到I2C
	IO0DIR |= HC595_CS; 
	S1PCCR=0x52;           //设置SPI时钟分频
	S1PCR=0x30;            //设置SPI接口模式,MSTR=1,CPOL=1,CPHA=0,LSBF=0
}
/*********************************************************************************************************
** 函数名称: MSendData()
** 功能描述: 向SPI总线发送数据
** 输 入:   待发送的数据
** 输 出:   返回发送的数据       
** 全局变量: 无
** 调用模块: 无
**------------------------------------------------------------------------------------------------------
** Created by    Rein Lee
** Created date: 2007-12-20
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
uint8 MSendData(uint8 data)
{ IO0CLR=HC595_CS;                             //片选
  S1PDR=data;
  while(0==(S1PSR&0x80));                  //等待SPIF置位,即等待数据发送完毕
  IO0SET=HC595_CS;
  return(S1PDR);
}
/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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