|
完整源代碼下載:
crc.RAR
(117.13 KB, 下載次數(shù): 25)
2015-5-27 16:57 上傳
點(diǎn)擊文件名下載附件
- /*******************************************************************************
- STM32學(xué)習(xí)日志(5)----CRC測(cè)試程序
- 編譯環(huán)境: EWARM V5.30
- 硬件環(huán)境: 南京萬(wàn)利 EK-STM32F
- 主芯片 : STM32F103VBT6
- STM32 FW: V3.0.0
- 作者 : szlihongtao
- 時(shí)間 : 2010-06-29
- 說(shuō)明 : 驗(yàn)證了CRC的用法
- *******************************************************************************/
- /**
- ******************************************************************************
- * @file Project/Template/main.c
- * @author MCD Application Team
- * @version V3.0.0
- * @date 04/06/2009
- * @brief Main program body
- ******************************************************************************
- * @copy
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "stm32_m.h"
- /** @addtogroup Template_Project
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval : None
- */
- INT32U CRCValue;
- //******************************************************************************
- // 延時(shí)50ms
- //******************************************************************************
- static void delay(void)
- {
- INT32U i;
- static INT32U jjj=5240*50;
- for (i=0; i<jjj; i++);
- }
- //******************************************************************************
- static void delayms(INT16U cnt)
- {
- INT16U i;
- while(cnt--)
- for (i=0; i<7333; i++);
- }
- //******************************************************************************
- // 時(shí)鐘設(shè)置初始化
- //******************************************************************************
- static void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- /*
- RCC_AdjustHSICalibrationValue 調(diào)整內(nèi)部高速晶振(HSI)校準(zhǔn)值
- RCC_ITConfig 使能或者失能指定的RCC中斷
- RCC_ClearFlag 清除RCC的復(fù)位標(biāo)志位
- RCC_GetITStatus 檢查指定的RCC中斷發(fā)生與否
- RCC_ClearITPendingBit 清除RCC的中斷待處理位
- */
- /* RCC system reset(for debug purpose) */
- // 時(shí)鐘系統(tǒng)復(fù)位
- RCC_DeInit();
- // 使能外部的8M晶振
- // 設(shè)置外部高速晶振(HSE)
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- // 使能或者失能內(nèi)部高速晶振(HSI)
- RCC_HSICmd(DISABLE);
- // 等待HSE起振
- // 該函數(shù)將等待直到HSE就緒,或者在超時(shí)的情況下退出
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- // 設(shè)置AHB時(shí)鐘(HCLK)
- RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
- /* PCLK1 = HCLK/2 */
- // 設(shè)置低速AHB時(shí)鐘(PCLK1)
- RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
- /* PCLK2 = HCLK */
- // 設(shè)置高速AHB時(shí)鐘(PCLK2)
- RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
- /* ADCCLK = PCLK2/8 */
- // 設(shè)置ADC時(shí)鐘(ADCCLK)
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- // 設(shè)置USB時(shí)鐘(USBCLK)
- // USB時(shí)鐘 = PLL時(shí)鐘除以1.5
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
- // 設(shè)置外部低速晶振(LSE)
- RCC_LSEConfig(RCC_LSE_OFF);
- // 使能或者失能內(nèi)部低速晶振(LSI)
- // LSE晶振OFF
- RCC_LSICmd(DISABLE);
- // 設(shè)置RTC時(shí)鐘(RTCCLK)
- // 選擇HSE時(shí)鐘頻率除以128作為RTC時(shí)鐘
- RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
- // 使能或者失能RTC時(shí)鐘
- // RTC時(shí)鐘的新?tīng)顟B(tài)
- RCC_RTCCLKCmd(DISABLE);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- // 設(shè)置PLL時(shí)鐘源及倍頻系數(shù)
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- // 使能或者失能PLL
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- // 檢查指定的RCC標(biāo)志位設(shè)置與否
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- // 設(shè)置系統(tǒng)時(shí)鐘(SYSCLK)
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- // 返回用作系統(tǒng)時(shí)鐘的時(shí)鐘源
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- // 使能或者失能AHB外設(shè)時(shí)鐘
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
- |RCC_AHBPeriph_DMA2
- |RCC_AHBPeriph_SRAM
- |RCC_AHBPeriph_FLITF
- |RCC_AHBPeriph_CRC
- |RCC_AHBPeriph_FSMC
- |RCC_AHBPeriph_SDIO,DISABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); // CRC 模塊時(shí)鐘使能
- // 使能或者失能APB1外設(shè)時(shí)鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
- // 強(qiáng)制或者釋放高速APB(APB2)外設(shè)復(fù)位
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
- // 退出復(fù)位狀態(tài)
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
- // 強(qiáng)制或者釋放低速APB(APB1)外設(shè)復(fù)位
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
- // 強(qiáng)制或者釋放后備域復(fù)位
- RCC_BackupResetCmd(ENABLE);
- // 使能或者失能時(shí)鐘安全系統(tǒng)
- RCC_ClockSecuritySystemCmd(DISABLE);
- }
- //******************************************************************************
- // NVIC設(shè)置
- //******************************************************************************
- static void NVIC_Configuration(void)
- {
- }
- //******************************************************************************
- // SysTick設(shè)置初始化
- //******************************************************************************
- static void SysTick_Config1(void)
- {
- #define SystemFreq 72000000.0 // 單位為Hz
- #define TB_SysTick 100000.0 // 單位為uS,與示波器實(shí)測(cè)一致
- INT32U ticks;
- ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
- ticks=ticks;
- //SysTick_Config(ticks);
- }
- //******************************************************************************
- // GPIO設(shè)置
- //******************************************************************************
- static void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // 使能或者失能APB2外設(shè)時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- }
- //******************************************************************************
- // LED5亮
- //******************************************************************************
- void Led_RW_ON(void)
- {
- GPIO_SetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- // LED5滅
- //******************************************************************************
- void Led_RW_OFF(void)
- {
- GPIO_ResetBits(GPIOC,GPIO_Pin_4);
- }
- //******************************************************************************
- // 主程序
- //******************************************************************************
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- SysTick_Config1();
- delay();
- Led_RW_OFF();
- Led_RW_ON();
- //------------------------------------------------------------------------------
- CRC_ResetDR();
- CRC_CalcCRC(0X11223344);
- CRC_CalcCRC(0X55667788);
- CRCValue=CRC_GetCRC(); // 0XDABFB5CD
- CRC_CalcCRC(CRCValue);
- CRCValue=CRC_GetCRC(); // 0
- {
- static const INT32U DataBuffer[]={0X11223344,0X55667788,0XDABFB5CD};
- CRC_ResetDR();
- CRCValue = CRC_CalcBlockCRC((INT32U *)DataBuffer, 2);
- // CRCValue = 0XDABFB5CD
- CRC_ResetDR();
- CRCValue = CRC_CalcBlockCRC((INT32U *)DataBuffer, 3);
- // CRCValue = 0
- }
- //------------------------------------------------------------------------------
- CRCValue=8888; // 隨便設(shè)置一個(gè)初值
- CRC_ResetDR();
- CRC_CalcCRC(0X11223344);
- CRC_CalcCRC(0X55667788);
- CRC_CalcCRC(0XDABFB5CD);
- CRCValue=CRC_GetCRC(); // 0
- //------------------------------------------------------------------------------
- for (;;)
- {
- delayms(50);
- GPIOC->ODR ^= GPIO_Pin_7; // led2 toogle
- }
- }
- //******************************************************************************
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- //******************************************************************************
- /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
- //******************************************************************************
- /*
- LED2---------PC7
- LED3---------PC6
- LED4---------PC5
- LED5---------PC4
- KEY2---------PD3
- KEY3---------PD4
- */
復(fù)制代碼
|
|