完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#define spiPortOfEEPROM 3
/******************************************************************************* 函数功能: 初始化EEPROM 输 入: 无 输 出: 无 返 回 值: 无 备 注: 无 *******************************************************************************/ void InitEEPROM(void) { //MX25L-CS,初始化片选 PA4 GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化对应spi SPI_Configuration(spiPortOfEEPROM); } /******************************************************************************* * Function Name : SPI_Configuration * Description : SPI模块初始化,【包括相关IO口的初始化】 * Input : None * Output : None * Return : None *******************************************************************************/ void SPI_Configuration(unsigned char port) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; switch(port) { case 1: //启动SPI1时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOB,GPIOC时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//使能SPI2时钟 GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PA5复用为 SPI1 GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PA6复用为 SPI1 GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PA7复用为 SPI1 //////下面是SPI相关GPIO初始化////// //Configure SPI1 pins: SCK, MISO and MOSI //GPIOA5~7初始化设置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//PA5~7复用功能输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 //Configure PA4 pin: MX25L_CS pin //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//PA4 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PA4 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); SPI_ReadWriteByte(1,0xff);//启动传输 break; case 2: //启动SPI2时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOB时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//使能SPI2时钟 GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2); //PB13复用为 SPI2 GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2); //PC2复用为 SPI2 GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); //PC3复用为 SPI2 //////下面是SPI相关GPIO初始化////// //Configure SPI2 pins: SCK, MISO and MOSI //GPIOB13~15初始化设置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;//PB13~15复用功能输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 //Configure PB12 pin: SD_CS pin GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//PB12 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); SPI_ReadWriteByte(2,0xff);//启动传输 break; case 3: RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE);//使能时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);//使能SPI3时钟 //////下面是SPI相关GPIO初始化////// //Configure SPI1 pins: SCK, MISO and MOSI //GPIOA5~7初始化设置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//PA5~7复用功能输出 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI3); GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_SPI3); GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_SPI3); //这里只针对SPI口初始化 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3,ENABLE);//复位SPI1 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3,DISABLE);//停止复位SPI1 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI3, &SPI_InitStructure); SPI_Cmd(SPI3, ENABLE); SPI_ReadWriteByte(3,0xAA);//启动传输 break; } } /******************************************************************************* * Function Name : SPI_ReadWriteByte * Description : SPI读写一个字节(发送完成后返回本次通讯读取的数据) * Input : u8 TxData 待发送的数 * Output : None * Return : u8 RxData 收到的数 *******************************************************************************/ unsigned char SPI_ReadWriteByte(unsigned char port,unsigned char TxData) { unsigned char RxData = 0; unsigned short j=0; switch(port) { case 1: j=0; //等待发送缓冲区空 while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) { if(j++>5000)break; } //发一个字节 SPI_I2S_SendData(SPI1, TxData); //等待数据接收 j=0; while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) { if(j++>5000)break; } //取数据 RxData = SPI_I2S_ReceiveData(SPI1); break; case 2: j=0; //等待发送缓冲区空 while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) { if(j++>5000)break; } //发一个字节 SPI_I2S_SendData(SPI2, TxData); //等待数据接收 j=0; while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) { if(j++>5000)break; } //取数据 RxData = SPI_I2S_ReceiveData(SPI2); break; case 3: j=0; //等待发送缓冲区空 while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET) { if(j++>5000)break; } //发一个字节 SPI_I2S_SendData(SPI2, TxData); //等待数据接收 j=0; while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET) { if(j++>5000)break; } //取数据 RxData = SPI_I2S_ReceiveData(SPI3); break; default: break; } return (unsigned char)RxData; } 调了一天了,一点波形都没有。无语中。。。 |
|
相关推荐 |
|
只有小组成员才能发言,加入小组>>
如何使用STM32+nrf24l01架构把有线USB设备无线化?
2547 浏览 7 评论
请问能利用51单片机和nRF24L01模块实现实时语音无线传输吗?
2332 浏览 5 评论
3161 浏览 3 评论
2804 浏览 8 评论
为什么ucosii上移植lwip后系统进入了HardFault_Handler?
2762 浏览 4 评论
请教各位大咖:有没有接收频率32M左右的芯片推荐的?先感谢啦!
610浏览 1评论
855浏览 0评论
970浏览 0评论
623浏览 0评论
449浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 23:00 , Processed in 1.065629 second(s), Total 59, Slave 46 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号