今天對我意義很重大,終于把NAND閃存的ID號給讀出來了,這就意味著我的時序是沒有問題的,操作也是沒有問題的,就這么干以后,當我讀出數據:AD F1 80 1D,沸騰了。也許這就是我想要的,這樣的快樂,知足了。
這是我用IO驅動外部器件的第一步,也是很值得紀念的日子,
下面是程序:(這里我采用的是庫代碼與裸寄存器操作)
- #include "stm32f10x.h"
- #include "stm32f10x_flash.h"
- #define SET_CLE GPIO_SetBits(GPIOD, GPIO_Pin_6)
- #define CLR_CLE GPIO_ResetBits(GPIOD, GPIO_Pin_6)
- #define SET_ALE GPIO_SetBits(GPIOD, GPIO_Pin_5)
- #define CLR_ALE GPIO_ResetBits(GPIOD, GPIO_Pin_5)
- #define SET_CE GPIO_SetBits(GPIOD, GPIO_Pin_7)
- #define CLR_CE GPIO_ResetBits(GPIOD, GPIO_Pin_7)
- #define SET_WE GPIO_SetBits(GPIOD, GPIO_Pin_14)
- #define CLR_WE GPIO_ResetBits(GPIOD, GPIO_Pin_14)
- #define SET_RE GPIO_SetBits(GPIOD, GPIO_Pin_15)
- #define CLR_RE GPIO_ResetBits(GPIOD, GPIO_Pin_15)
- void mysysinit(void);//系統時鐘初始
- void my_USART_init(void);//初始化
- void my_send_byte(unsigned char send_date); //發送一個字節
- GPIO_InitTypeDef GPIO_InitStructure;
- void READ_NAND_ID(void);
- void delay_1us(uint32_t);
- void my_NAND_INIT(void ); //nand flash 初始化
- void wait_NAND_readay(void);
- int main()
- {
-
- mysysinit();//RCC初始化,系統時鐘設置72MHZ
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能APB2的GPIO_D時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//使能APB2的GPIO_D時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能APB2的GPIO_B時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);//使能APB2的GPIO_E時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//使能APB2的GPIO_E時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能APB1的USART2時鐘
- /* led*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9| GPIO_Pin_10| GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* 設置PB.5用于控制NAND的為上拉輸入 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- my_USART_init();
-
-
-
-
-
- wait_NAND_readay();
- GPIO_SetBits(GPIOD, GPIO_Pin_8);
-
- my_send_byte(0);
- my_send_byte(1);
- my_send_byte(2);
- my_send_byte(3);
-
- READ_NAND_ID();
- while(1);
- }
- /*等待芯片不忙*/
- void wait_NAND_readay()
- {
- uint8_t wait=0;//忙閑信號
- //等待芯片不忙
- do
- { wait=GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5);//讀取忙閑引腳
- }
- while(0x00==wait);
- }
- /*讀取NAND閃存的ID序列號串口發送*/
- void READ_NAND_ID()
- {
- uint8_t a=0,b=0,c=0,d=0;
-
- /* 設置PD口用于控制NAND的為輸出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_14| GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /*設置PE口得低八位為輸出*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3| GPIO_Pin_4|GPIO_Pin_7 | GPIO_Pin_6| GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- //delay_1us(1000000);
- CLR_CE;//開啟片選
- SET_CLE;//命令鎖存開啟
- CLR_WE;//寫使能
- CLR_ALE;//地址鎖存關閉
- SET_RE;//讀關閉
- GPIO_Write(GPIOE, 0x90); //讀ID命令
- SET_WE;//關閉寫
- CLR_CLE;
- SET_ALE;
- CLR_WE;
- GPIO_Write(GPIOE, 0x00); //地址00
- SET_WE;
-
-
- /*設置PE口得低八位為輸入*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3| GPIO_Pin_4|GPIO_Pin_7 | GPIO_Pin_6| GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- CLR_ALE; //注意啊,這里一定把他放在下面應為是有時間要求的;時序圖TAR=15納秒之后才是RE的操作;
- //讀出的結果是AD F1 80 1D ,2011年8月26日,與天津第四項目部宿舍
- CLR_RE;
-
- a=(GPIOE->IDR);
- SET_RE;
-
- CLR_RE;
- b=(GPIOE->IDR);
- SET_RE;
-
- CLR_RE;
- c=(GPIOE->IDR);
- SET_RE;
-
- CLR_RE;
- d=(GPIOE->IDR);//delay_1us(1);
- SET_RE;
- SET_CE;//關閉片選
- my_send_byte(a);
- my_send_byte(b);
- my_send_byte(c);
- my_send_byte(d);
- }
- /********************
- 延時函數
- 形參:uint8_t time
- 功能:延時數值為time值
- *********************/
- void delay_1us(uint32_t time)
- { uint32_t b,c;
- for(c=time;c;c--) //定時=time*3*333.6=time*1000.8ns
- {
- for(b=8;b;b--); //8*13.9*3= 333.6
- }
-
- }
- /***********************************
- 發送一個字節函數通過串口
- ************************************/
- void my_send_byte(unsigned char send_date )
- {
- while( (USART1->SR&0x00000080)!=0x80);//發送寄存器為空
- USART1->DR=send_date;
- }
- /**********************************
- 初始化串口
- **********************************/
- void my_USART_init()
- {
- /*USART2的優先級設為5*/
- NVIC->IP[37]=5;
- /*開啟38號中斷即USART2,關閉其他所有外部的中斷*/
- NVIC->ISER[1]=0x00000020;
- /*設置復用模式下的引腳模式為全雙工:TX輸出推挽復用,RX為輸入上拉模式,速度50MHZ*/
- GPIOA->CRH=0x000008b0;
- /* 1.開啟USART,
- *
- */
- USART1->CR1=0x2000;
- /* 1.關閉局域網模式
- * 2.1個停止位
- * 3.CK引腳禁能
- */
- USART1->CR2=0;
- /* 1.關閉調制解調模式
- * 2.關閉DMA模式
- * 3.關閉智能卡、紅外模式
- * 4.關閉錯誤中斷
- */
- USART1->CR3=0;
- /* 波特率設置
- 2011年8月11日
- 王均偉
- 天津第四項目部宿舍
- BRR中的第四位(DIV_Fraction)作為小數,高12位(DIV_MANtissa)作為整數部分,
-
- 1,根據公式:波特率=fck/16*usardiv,其中usardivBRR寄存器的值,所以變形得:USARDIV=fck/16*波特率
- 2.算出來BRR寄存器的值后就要把這個值變成16進制數據寫入BRR寄存器中,
- 遵循以下規則:
- 小數部分*16=DIV_Fraction或者取近似的值
- 整數部分直接=DIV_MANtissa
- 3.把這個16進制值寫入BRR寄存器
- 例如我要算波特率設成9600bps的BRR寄存器值,
- 1.先求USARDIV=36000000/16*9600=234.375
- 2.換成十六進制:DIV_Fraction=16*0.375=0x6
- DIV_MANtissa=234=0xea
- 3.組合并寫入寄存器
- USART2->BRR=0x0ea6;值得注意的是這里是16位半字操作,所以不要以為是32位。
- */
- USART1->BRR=0x0ea6;
- /* 1.開啟USART
- * 2.開啟接收完畢中斷
- * 3.開啟發送功能
- * 4.開啟接收功能
- */
- USART1->CR1=0x202c;
-
- }
- void mysysinit()//系統初始化程序
- {
- ErrorStatus HSEStartUpStatus;//說明標志位
- RCC_DeInit();//所有外設全部缺省設置
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- /* Wait till HSE is ready and if Time out is reached exit */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)//啟動成功
- {
- /*這兩條FLASH指令必須加上,不知為啥?不加上就運行幾秒后出錯,參照系統初始化*/
- /* Enable The Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//FLASH緩存開啟
- /* Configure the Latency cycle: Set 2 Latency cycles */
- FLASH_SetLatency(FLASH_Latency_2); //設置FLASH這些位表示SYSCLK(系統時鐘)周期與閃存訪問時間的比例,為010:兩個等待狀態,當 48MHz < SYSCLK ≤ 72MHz
- /* Set PLL clock output to 72MHz using HSE (8MHz) as entry clock */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//外部時鐘為8M,PLL的輸入時鐘=8MHZ,倍頻系數9,
- /* Configure HCLK such as HCLK = SYSCLK */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);//設置了啦AHB分頻器的分頻系數=1,即HCLK=SYSCLK=72MHZ
- /* Configure PCLK1 such as PCLK1 = HCLK/2 */
- RCC_PCLK1Config(RCC_HCLK_Div2);//設置了APB1外設的時鐘頻率最大是36M這里是APB1的分頻器設為2,PCLK1=HCLK/2=72/2=36MHZ正好是最大值
- /* Configure PCLK2 such as PCLK2 = HCLK */
- RCC_PCLK2Config(RCC_HCLK_Div1);//設置PLCK2=HCLK=72MHZ,的APB2分頻器=1
- /* Select the PLL as system clock source */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//設置了SYSCLK的提供者為PLL,頻率由上面算出=72MHZ
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_PLLRDY, DISABLE);//PLL中斷關閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSERDY,DISABLE);//HSE中斷關閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_HSIRDY, DISABLE); //HSI中斷關閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSERDY, DISABLE); //LSE中斷關閉
- /* disable PLL Ready interrupt */
- RCC_ITConfig(RCC_IT_LSIRDY, DISABLE); //LSI中斷關閉
- /* PLL clock divided by 1.5 used as USB clock source */
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//設置USB的時鐘為=72、1.5=48mhz
- /* Configure ADCCLK such as ADCCLK = PCLK2/2 */
- RCC_ADCCLKConfig(RCC_PCLK2_Div2);//設置ADC時鐘=PCLK2/2= 36MHZ
- /* disable the LSE */
- RCC_LSEConfig(RCC_LSE_OFF);//外部低速晶振關閉
- /*DISable the RTC clock */
- RCC_RTCCLKCmd(DISABLE);
- /* DISable the Clock Security System */
- RCC_ClockSecuritySystemCmd(DISABLE);
- /* Enable the PLL */
- RCC_PLLCmd(ENABLE);//使能PLL
-
-
-
- /* PLL ans system clock config */
- }
- else
- {
- /* Add here some code to deal with this error */
- }
- }
復制代碼
|