SPI(serial peripheral interface)串行外圍設(shè)備接口。是一種全雙工同步通信總線。本文為大家介紹兩種spi從機(jī)程序。
STM32F407的SPI主從機(jī)通信程序
#include “spi.h”
u8 SPI_Data[3];
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//使能SPI1時(shí)鐘
//GPIOFB3,4,5初始化設(shè)置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//PB3~5復(fù)用功能輸出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//復(fù)用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); //PB3復(fù)用為 SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1); //PB4復(fù)用為 SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); //PB5復(fù)用為 SPI1
//這里只針對(duì)SPI口初始化
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//復(fù)位SPI1
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//停止復(fù)位SPI1
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //設(shè)置SPI單向或者雙向的數(shù)據(jù)模式:SPI設(shè)置為雙線雙向全雙工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //設(shè)置SPI工作模式:設(shè)置為主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //設(shè)置SPI的數(shù)據(jù)大小:SPI發(fā)送接收8位幀結(jié)構(gòu)
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行同步時(shí)鐘的空閑狀態(tài)為高電平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //串行同步時(shí)鐘的第二個(gè)跳變沿(上升或下降)數(shù)據(jù)被采樣
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信號(hào)由硬件(NSS管腳)還是軟件(使用SSI位)管理:內(nèi)部NSS信號(hào)有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //定義波特率預(yù)分頻的值:波特率預(yù)分頻值為256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定數(shù)據(jù)傳輸從MSB位還是LSB位開(kāi)始:數(shù)據(jù)傳輸從MSB位開(kāi)始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值計(jì)算的多項(xiàng)式
SPI_Init(SPI1, &SPI_InitStructure); //根據(jù)SPI_InitStruct中指定的參數(shù)初始化外設(shè)SPIx寄存器
SPI_Cmd(SPI1, ENABLE); //使能SPI外設(shè)
//SPI1_ReadWriteByte(0xff);//啟動(dòng)傳輸
}
//SPI1速度設(shè)置函數(shù)
//SPI速度=fAPB2/分頻系數(shù)
//@ref SPI_BaudRate_Prescaler:SPI_BaudRatePrescaler_2~SPI_BaudRatePrescaler_256
//fAPB2時(shí)鐘一般為84Mhz:
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//判斷有效性
SPI1-》CR1&=0XFFC7;//位3-5清零,用來(lái)設(shè)置波特率
SPI1-》CR1|=SPI_BaudRatePrescaler; //設(shè)置SPI1速度
SPI_Cmd(SPI1,ENABLE); //使能SPI1
}
//SPI1 讀寫(xiě)一個(gè)字節(jié)
//TxData:要寫(xiě)入的字節(jié)
//返回值:讀取到的字節(jié)
void SPI1_WriteByte(u8 TxData)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}//等待發(fā)送區(qū)空
SPI_I2S_SendData(SPI1, TxData); //通過(guò)外設(shè)SPIx發(fā)送一個(gè)byte 數(shù)據(jù)
//while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){} //等待接收完一個(gè)byte
//return SPI_I2S_ReceiveData(SPI1); //返回通過(guò)SPIx最近接收的數(shù)據(jù)
}
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOA時(shí)鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); //使能SPI1時(shí)鐘
//GPIOFB3,4,5初始化設(shè)置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;//PB3~5復(fù)用功能輸出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//復(fù)用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2); //PB13復(fù)用為 SPI2
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2); //PB14復(fù)用為 SPI2
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); //PB15復(fù)用為 SPI2
//這里只針對(duì)SPI口初始化
RCC_APB2PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);//復(fù)位SPI1
RCC_APB2PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//停止復(fù)位SPI1
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //設(shè)置SPI單向或者雙向的數(shù)據(jù)模式:SPI設(shè)置為雙線雙向全雙工
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; //設(shè)置SPI工作模式:設(shè)置為主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //設(shè)置SPI的數(shù)據(jù)大?。篠PI發(fā)送接收8位幀結(jié)構(gòu)
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行同步時(shí)鐘的空閑狀態(tài)為高電平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //串行同步時(shí)鐘的第二個(gè)跳變沿(上升或下降)數(shù)據(jù)被采樣
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信號(hào)由硬件(NSS管腳)還是軟件(使用SSI位)管理:內(nèi)部NSS信號(hào)有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //定義波特率預(yù)分頻的值:波特率預(yù)分頻值為256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定數(shù)據(jù)傳輸從MSB位還是LSB位開(kāi)始:數(shù)據(jù)傳輸從MSB位開(kāi)始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值計(jì)算的多項(xiàng)式
SPI_Init(SPI2, &SPI_InitStructure); //根據(jù)SPI_InitStruct中指定的參數(shù)初始化外設(shè)SPIx寄存器
SPI_Cmd(SPI2, ENABLE); //使能SPI外設(shè)
}
//SPI1速度設(shè)置函數(shù)
//SPI速度=fAPB2/分頻系數(shù)
//@ref SPI_BaudRate_Prescaler:SPI_BaudRatePrescaler_2~SPI_BaudRatePrescaler_256
//fAPB2時(shí)鐘一般為84Mhz:
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//判斷有效性
SPI2-》CR1&=0XFFC7;//位3-5清零,用來(lái)設(shè)置波特率
SPI2-》CR1|=SPI_BaudRatePrescaler; //設(shè)置SPI2速度
SPI_Cmd(SPI2,ENABLE); //使能SPI1
}
//SPI2 讀寫(xiě)一個(gè)字節(jié)
//TxData:要寫(xiě)入的字節(jié)
//返回值:讀取到的字節(jié)
u8 SPI2_ReadByte()
{
//while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){}//等待發(fā)送區(qū)空
//SPI_I2S_SendData(SPI2, TxData); //通過(guò)外設(shè)SPIx發(fā)送一個(gè)byte 數(shù)據(jù)
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET){} //等待接收完一個(gè)byte
return SPI_I2S_ReceiveData(SPI2); //返回通過(guò)SPIx最近接收的數(shù)據(jù)
}
void SPI2_WriteByte(u8 TxData)
{
SPI_I2S_SendData(SPI2, TxData); //通過(guò)外設(shè)SPIx發(fā)送一個(gè)byte 數(shù)據(jù)
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){}//等待發(fā)送區(qū)空
}
//外部中斷3服務(wù)程序
void EXTI3_IRQHandler(void)
{
Flage=Flage|0x02;
CS2=0;
EXTI_ClearITPendingBit(EXTI_Line3); //清除LINE3上的中斷標(biāo)志位
}
//外部中斷4服務(wù)程序
void EXTI4_IRQHandler(void)
{
Flage=Flage|0x04;
CS3=0;
EXTI_ClearITPendingBit(EXTI_Line4); //清除LINE4上的中斷標(biāo)志位
}
//外部中斷5服務(wù)程序
//外部中斷初始化程序
void EXTIX_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能SYSCFG時(shí)鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //使能GPIOE時(shí)鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通輸入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4,5
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource2);//PE2 連接到中斷線2
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource3);//PE3 連接到中斷線3
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);//PE4 連接到中斷線4
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource5);//PE5 連接到中斷線5
/* 配置EXTI_Line2,3,4 */
EXTI_InitStructure.EXTI_Line = EXTI_Line2 | EXTI_Line3 | EXTI_Line4 | EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中斷事件
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿觸發(fā)
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中斷線使能
EXTI_Init(&EXTI_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;//外部中斷2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//搶占優(yōu)先級(jí)3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//外部中斷3
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;//搶占優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//外部中斷4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//搶占優(yōu)先級(jí)1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中斷4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;//搶占優(yōu)先級(jí)1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子優(yōu)先級(jí)2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中斷通道
NVIC_Init(&NVIC_InitStructure);//配置
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置系統(tǒng)中斷優(yōu)先級(jí)分組2
delay_init(100); //初始化延時(shí)函數(shù)
LED_Init(); //初始化LED
CS_Init();
SPI1_Init();
SPI2_Init();
EXTIX_Init();
while(1)
{
CS2=1;
if(Flage&0x02)
{
Flage=Flage&0xfd;
CS3=1;
}
if(Flage&0x04)
{
Flage=Flage&0xfb;
SPI1_WriteByte(0xaa);
SPI_Data[0]=SPI2_ReadByte();
SPI1_WriteByte(0xc3);
SPI_Data[1]=SPI2_ReadByte();
SPI1_WriteByte(0x55);
SPI_Data[2]=SPI2_ReadByte();
if((SPI_Data[0]==0xaa)&&(SPI_Data[1]==0xc3)&&(SPI_Data[2]==0x55))
{
BEEP=~BEEP;
LED0=~LED0;
LED1=~LED1;
delay_ms(500);
}
memset(SPI_Data,0,3);
}
}
}
基于計(jì)數(shù)器的spi從機(jī)程序設(shè)計(jì)
SPI 即為:serial peripheral interface,串行外圍設(shè)備接口。是一種全雙工同步通信總線。通信是通過(guò)數(shù)據(jù)傳輸來(lái)完成的,SPI是串行通信協(xié)議,也就是說(shuō),數(shù)據(jù)時(shí)一位一位傳輸?shù)?。也就是時(shí)鐘線存在的原因,由于時(shí)鐘線提供的時(shí)鐘脈沖,數(shù)據(jù)發(fā)送和數(shù)據(jù)接收都是基于這個(gè)時(shí)鐘脈沖完成數(shù)據(jù)傳輸?shù)?,?shù)據(jù)通過(guò)數(shù)據(jù)輸出線輸出,數(shù)據(jù)在時(shí)鐘上升沿或者下降沿時(shí)改變,在緊接著的下降沿或者上升沿被讀取,完成一次數(shù)據(jù)傳輸,輸入原理和輸出一樣。也就是說(shuō)數(shù)據(jù)輸入和輸出是在同一個(gè)時(shí)鐘完成的,而區(qū)別就是在時(shí)鐘的上升沿輸入那么在時(shí)鐘下降沿就輸出,或者相反。為了實(shí)現(xiàn)這一點(diǎn),一下設(shè)計(jì)就是通過(guò)一個(gè)計(jì)數(shù)器,在計(jì)數(shù)器數(shù)到偶數(shù)的時(shí)候則輸出,計(jì)數(shù)器數(shù)到奇數(shù)的時(shí)候則輸入。代碼如下:
評(píng)論
查看更多