本帖最后由 龙御_沧海 于 2014-10-16 22:10 编辑
#include "stm32f10x_lib.h"
#include "stdio.h"
#include "string.h"
#define BufferSize 32
#define DELAY() {for(i=0;i<200;i++);}
SPI_InitTypeDef SPI_InitStructure;
const u8 SPI1_Buffer_Tx[BufferSize]=
{
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20
};
const u8 SPI2_Buffer_Tx[BufferSize]=
{
0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,
0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,
0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,
0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70
};
u8 SPI1_Buffer_Rx[BufferSize];
u8 SPI2_Buffer_Rx[BufferSize];
u8 Tx_Idx=0;
u8 Rx_Idx=0;
u8 k=0;
u8 i=0;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void SPI_Configuration(void);
void USART_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
SPI_Configuration();
USART_Configuration();
memset(SPI1_Buffer_Rx,0,sizeof(SPI1_Buffer_Rx));
memset(SPI2_Buffer_Rx,0,sizeof(SPI2_Buffer_Rx));
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_InitStructure.SPI_Mode=SPI_Mode_Slave;
SPI_Init(SPI2,&SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);
while(Tx_Idx<BufferSize)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2,SPI2_Buffer_Tx[Tx_Idx]);
SPI_I2S_SendData(SPI1,SPI1_Buffer_Tx[Tx_Idx++]);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
SPI2_Buffer_Rx[Rx_Idx]=SPI_I2S_ReceiveData(SPI2);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
SPI1_Buffer_Rx[Rx_Idx++]=SPI_I2S_ReceiveData(SPI1);
}
printf("
The First transfer between the two SPI peripherals:"
"The SPI1 Master andd the SPI2 slaver.
");
printf("
The SPI1 has sended data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI1_Buffer_Tx[k]);
DELAY();
}
printf("
The SPI2 has receive data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI2_Buffer_Rx[k]);
DELAY();
}
printf("
");
printf("
The SPI2 has sended data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI2_Buffer_Tx[k]);
DELAY();
}
printf("
The SPI1 has receive data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI1_Buffer_Rx[k]);
DELAY();
}
Tx_Idx=0;
Rx_Idx=0;
memset(SPI1_Buffer_Rx,0,sizeof(SPI1_Buffer_Rx));
memset(SPI2_Buffer_Rx,0,sizeof(SPI2_Buffer_Rx));
SPI_Cmd(SPI1,DISABLE);
SPI_Cmd(SPI2,DISABLE);
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_Init(SPI2,&SPI_InitStructure);
SPI_InitStructure.SPI_Mode=SPI_Mode_Slave;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);
while(Tx_Idx<BufferSize)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,SPI1_Buffer_Tx[Tx_Idx]);
SPI_I2S_SendData(SPI2,SPI2_Buffer_Tx[Tx_Idx++]);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
SPI1_Buffer_Rx[Rx_Idx]=SPI_I2S_ReceiveData(SPI1);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
SPI2_Buffer_Rx[Rx_Idx++]=SPI_I2S_ReceiveData(SPI2);
}
printf("
The Second transfer between the two SPI peripherals:"
"The SPI2 Master and the SPI1 slaver.
");
printf("
The SPI2 has sended data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI2_Buffer_Tx[k]);
DELAY();
}
printf("
The SPI1 has receive data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI1_Buffer_Rx[k]);
DELAY();
}
printf("
");
printf("
The SPI1 has sended data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI1_Buffer_Tx[k]);
DELAY();
}
printf("
The SPI2 has receive data below:
");
for(k=0;k<BufferSize;k++)
{
printf("%0.2d
",SPI2_Buffer_Rx[k]);
DELAY();
}
while(1);
}
void RCC_Configuration(void)
{
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit ();
RCC_HSEConfig (RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp ();
if (HSEStartUpStatus == SUCCESS)
{
RCC_HCLKConfig (RCC_SYSCLK_Div1);
RCC_PCLK2Config (RCC_HCLK_Div1);
RCC_PCLK1Config (RCC_HCLK_Div2);
FLASH_SetLatency (FLASH_Latency_2);
FLASH_PrefetchBufferCmd (FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig (RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd (ENABLE);
while (RCC_GetFlagStatus (RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
while (RCC_GetSYSCLKSource () != 0x08);
}
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1|RCC_APB2Periph_SPI1,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void SPI_Configuration(void)
{
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_InitStructure.SPI_Mode=SPI_Mode_Slave;
SPI_Init(SPI2,&SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockInitStructure.USART_Clock=USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL=USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA=USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit=USART_LastBit_Disable;
USART_ClockInit(USART1,&USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
return ch;
}
此帖出自
小平头技术问答
一周热门 更多>