|
轉(zhuǎn)載STM32-SST25VF016B-SPI-mini 程序,需要的下載
單片機(jī)源程序如下:
- /****************************************************************************
- * Copyright (C), 2011 奮斗嵌入式工作室
- *
- * 本例程在 奮斗版STM32開(kāi)發(fā)板V2,2.1,V3,MINI上調(diào)試通過(guò)
- *
- * 文件名: main.c
- * 內(nèi)容簡(jiǎn)述:
- * 演示將一段字符串寫(xiě)入SST25VF016B的1頁(yè)中,然后讀出并通過(guò)USART1傳送出去,同時(shí)LED1亮。
- 字符串:SPI SST25VF016B Example: This is SPI DEMO, 終端上出現(xiàn)這一行字,說(shuō)明SST25VF016B的讀寫(xiě)正常
- *
- 定義:
- TXD1----- PA9-US1-TX
- RXD1----- PA10-US1-RX
- 速率:115200,n,8,1
- * 文件歷史:
- * 版本號(hào) 日期 作者 說(shuō)明
- * v0.2 2011-7-22 sun68 創(chuàng)建該文件
- *
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "stm32f10x_usart.h"
- extern void SPI_Flash_Init(void);
- extern unsigned char SST25_buffer[];
- /* Private define ------------------------------------------------------------*/
- #define TxBufferSize1 (countof(TxBuffer1) - 1)
- #define RxBufferSize1 (countof(TxBuffer1) - 1)
- /* Private macro -------------------------------------------------------------*/
- #define countof(a) (sizeof(a) / sizeof(*(a)))
- /* Private variables ---------------------------------------------------------*/
- uint8_t TxBuffer1[] = "SPI SST25VF016B Example: This is SPI DEMO, 終端上出現(xiàn)這一行字,說(shuō)明SST25VF016B的讀寫(xiě)正常";
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void Delay(__IO uint32_t nCount);
- void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,uint16_t Len);
- void Usart1_Init(void);
- GPIO_InitTypeDef GPIO_InitStructure;
- int16_t USART_FLAG;
- extern void SST25_W_BLOCK(uint32_t addr, u8 *readbuff, uint16_t BlockSize);
- extern void SST25_R_BLOCK(uint32_t addr, u8 *readbuff, uint16_t BlockSize);
- /* Private functions ---------------------------------------------------------*/
- /****************************************************************************
- * 名 稱(chēng):int main(void)
- * 功 能:主函數(shù)
- ****************************************************************************/
- int main(void)
- {
- uint8_t a=0;
- uint16_t i=0;
- RCC_Configuration(); //設(shè)置系統(tǒng)時(shí)鐘
-
- GPIO_Configuration(); //IO口設(shè)置
- Usart1_Init(); //串口1初始化
- SPI_Flash_Init(); //SPI1 SST25VF016B初始化
-
- /* 將測(cè)試用的數(shù)據(jù)復(fù)制到讀寫(xiě)緩存區(qū)里 */
- for(i=0; i<TxBufferSize1;i++) SST25_buffer[i]=TxBuffer1[i];
- SST25_W_BLOCK(0, SST25_buffer,4096); //將冊(cè)數(shù)數(shù)據(jù)寫(xiě)入到SST25VF016B的0頁(yè)里
- Delay(0xffff);
- SST25_R_BLOCK(0, SST25_buffer,4096); //從SST25VF016B的0頁(yè)里讀出數(shù)據(jù)
- a=0;
- for(i=0; i<TxBufferSize1;i++){
- if(SST25_buffer[i]==TxBuffer1[i]) a=1; //讀出的數(shù)據(jù)和測(cè)試數(shù)據(jù)進(jìn)行比較, 以判別是否讀寫(xiě)正常
- else {a=0; i=TxBufferSize1;}
- }
- if(a==1) {
- GPIO_SetBits(GPIOB, GPIO_Pin_5); //讀寫(xiě)正確LED1 亮
- USART_OUT(USART1,&SST25_buffer[0],TxBufferSize1); //將讀出的數(shù)據(jù)通過(guò)串口輸出
- }
- while (1);
- }
- /****************************************************************************
- * 名 稱(chēng):void Usart1_Init(void)
- * 功 能:串口1初始化函數(shù)
- ****************************************************************************/
- void Usart1_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 , ENABLE); //使能串口1時(shí)鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //復(fù)用開(kāi)漏輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
- USART_InitStructure.USART_BaudRate = 115200; //速率115200bps
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數(shù)據(jù)位8位
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位
- USART_InitStructure.USART_Parity = USART_Parity_No; //無(wú)校驗(yàn)位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無(wú)硬件流控
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
- /* Configure USART1 */
- USART_Init(USART1, &USART_InitStructure); //配置串口參數(shù)函數(shù)
- /* Enable the USART1 */
- USART_Cmd(USART1, ENABLE);
-
- }
- /****************************************************************************
- * 名 稱(chēng):void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,uint16_t Len)
- * 功 能:串口輸出函數(shù)
- ****************************************************************************/
- void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,uint16_t Len){
- uint16_t i;
- for(i=0; i<Len; i++){
- USART_SendData(USARTx, Data[i]);
- while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
- }
- }
- /****************************************************************************
- * 名 稱(chēng):void Delay(__IO uint32_t nCount)
- * 功 能:延時(shí)函數(shù)
- ****************************************************************************/
- void Delay(__IO uint32_t nCount)
- {
- for(; nCount != 0; nCount--);
- }
- /****************************************************************************
- * 名 稱(chēng):void RCC_Configuration(void)
- * 功 能:系統(tǒng)時(shí)鐘配置為72MHZ, 外設(shè)時(shí)鐘配置
- ****************************************************************************/
- void RCC_Configuration(void){
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
- | RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
- }
- /****************************************************************************
- * 名 稱(chēng):void GPIO_Configuration(void)
- * 功 能:通用IO口配置
- ****************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //狀態(tài)LED1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用推挽輸出模式
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //輸出模式最大速度50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
復(fù)制代碼
所有資料51hei提供下載:
STM32-SST25VF016B-SPI-mini.7z
(179.82 KB, 下載次數(shù): 16)
2020-4-29 18:28 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|