📄 spi.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: main.c
** Last modified Date: 2004-09-16
** Last Version: 1.0
** Descriptions: The main() function example template
**
**------------------------------------------------------------------------------------------------------
** Created by: Chenmingji
** Created date: 2004-09-16
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "UART.h"
/************************************************************************************************
** Function name:
** Descriptions: SPI主机模式初始化
** input parameters:
** output parameters: 无
** Returned value: 无
************************************************************************************************/
void SPI_Init(void)
{
PINSEL0 = PINSEL0 & 0xFFFF00FF |
(0x00 << 14) |
(0x01 << 12) |
(0x01 << 10) |
(0x01 << 8);
//P0.4-P0.7设置为SPI引脚
SPI_SPCCR = 0x08; //时钟分频
SPI_SPCR = (0x00 << 2) | //8位传输
(0x00 << 3) | //第一个跳变沿采样
(0x00 << 4) | //SCK高电平时钟有效
(0x01 << 5) | //主模式
(0x00 << 6) | //MSB传输时在前(SSP固定为MSB在前)
(0x00 << 7) ; //禁止中断
}
/************************************************************************************************
** Function name:
** Descriptions: SSP从机模式初始化
** input parameters:
** output parameters: 无
** Returned value: 无
************************************************************************************************/
void SSP_Init(void)
{
PINSEL0 = PINSEL0 & (~(0x03 << 28)) | (0x02 << 28); //P0.14为SCK
PINSEL1 = PINSEL1 & 0xFFFFF54F; //P0.19-21分别为MISO,MOSI,SSEL
SSPCR0 = (0x07 << 0) | //8位传输
(0x00 << 4) | //SPI格式
(0x00 << 6) | //SCK高电平有效
(0x00 << 8); //位速率为默认值
SSPCR1 = (0x00 << 0) | //正常工作方式
(0x01 << 1) | //SSP使能
(0x01 << 2) | //从机模式
(0x00 << 3); //允许从机输出
}
/************************************************************************************************
** Function name:
** Descriptions: SPI主发送程序
** input parameters:
** output parameters: 无
** Returned value: 无
************************************************************************************************/
void SPI_SendData(uint8 sdata)
{
IO0CLR = (0x01 << 13); //P0.13接到SSEL片选SSP
SPI_SPDR = sdata;
while((SPI_SPSR & 0x80) == 0); //等待传输完成
IO0SET = (0x01 << 13);
}
/************************************************************************************************
** Function name:
** Descriptions: SSP从接收程序
** input parameters:
** output parameters: 无
** Returned value: 无
************************************************************************************************/
uint8 SSP_ReceiveData(void)
{
while((SSPSR & 0x04) == 0);
return(SSPDR);
}
/************************************************************************************************
** Function name:
** Descriptions: main初始化,SPI,SSP引脚连接设置,初始化UART,SPI,SSP,SPI发送数据,SSP接收数据,
发送给PC显示.
** input parameters:
** output parameters: 无
** Returned value: 无
************************************************************************************************/
int main (void)
{// add user source code
uint8 rdata,sdata = 0x06,j;
PINSEL0 = PINSEL0 & (~(0x03 << 26)); //P0.13作IO端口输出,作SSEL片选
IO0DIR = (0x01 << 13);
IO0SET = (0x01 << 13); //SSEL给高电平,总线空闲
UARTInit();
SPI_Init();
SSP_Init();
while(1)
{
SPI_SendData(sdata); //发送'1'的显示码
rdata = SSP_ReceiveData();
for(j = 0;j < 8;j++)
PCDispChar (j,rdata);
}
return 0;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -