找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2388|回复: 0
打印 上一主题 下一主题
收起左侧

STM32学习--FW_V3.0.0模板

[复制链接]
跳转到指定楼层
楼主
ID:81272 发表于 2015-5-27 17:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
完整程序源代码工程文件下载地址: FW_V3.0.0模板.RAR (231.31 KB, 下载次数: 11)

  1. /*******************************************************************************

  2. STM32学习日志(1)----FW_V3.0.0模板

  3. 编译环境:        EWARM V5.30
  4. 硬件环境:        51hei
  5. 作者        :        szlihongtao
  6. 时间        :          2016-06-29
  7. *******************************************************************************/
  8. /**
  9.   ******************************************************************************
  10.   * @file    Project/Template/main.c
  11.   * @author  MCD Application Team
  12.   * @version V3.0.0
  13.   * @date    04/06/2009
  14.   * @brief   Main program body
  15.   ******************************************************************************
  16.   * @copy
  17.   *
  18.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  19.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  20.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  21.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  22.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  23.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  24.   *
  25.   * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
  26.   */

  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f10x.h"
  29. #include "stm32_m.h"

  30. /** @addtogroup Template_Project
  31.   * @{
  32.   */

  33. /* Private typedef -----------------------------------------------------------*/
  34. /* Private define ------------------------------------------------------------*/
  35. /* Private macro -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private function prototypes -----------------------------------------------*/
  38. /* Private functions ---------------------------------------------------------*/

  39. /**
  40.   * @brief  Main program.
  41.   * @param  None
  42.   * @retval : None
  43.   */
  44. //******************************************************************************
  45. // 延时50ms
  46. //******************************************************************************
  47. static void delay(void)
  48. {
  49.         INT32U i;
  50.     static INT32U jjj=5240*50;

  51.         for (i=0; i<jjj; i++);
  52. }
  53. //******************************************************************************
  54. // 时钟设置初始化
  55. //******************************************************************************
  56. static void RCC_Configuration(void)
  57. {
  58.   ErrorStatus HSEStartUpStatus;
  59. /*
  60. RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
  61. RCC_ITConfig 使能或者失能指定的RCC中断
  62. RCC_ClearFlag 清除RCC的复位标志位
  63. RCC_GetITStatus 检查指定的RCC中断发生与否
  64. RCC_ClearITPendingBit 清除RCC的中断待处理位
  65. */
  66.           /* RCC system reset(for debug purpose) */
  67.           // 时钟系统复位
  68.           RCC_DeInit();

  69.         // 使能外部的8M晶振
  70.         // 设置外部高速晶振(HSE)
  71.           /* Enable HSE */
  72.           RCC_HSEConfig(RCC_HSE_ON);

  73.         // 使能或者失能内部高速晶振(HSI)
  74.         RCC_HSICmd(DISABLE);

  75.         // 等待HSE起振
  76.         // 该函数将等待直到HSE就绪,或者在超时的情况下退出
  77.           /* Wait till HSE is ready */
  78.           HSEStartUpStatus = RCC_WaitForHSEStartUp();

  79.           if(HSEStartUpStatus == SUCCESS)
  80.           {
  81.             /* HCLK = SYSCLK */
  82.                 // 设置AHB时钟(HCLK)
  83.             RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

  84.             /* PCLK1 = HCLK/2 */
  85.                 // 设置低速AHB时钟(PCLK1)
  86.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  87.             /* PCLK2 = HCLK */
  88.                 // 设置高速AHB时钟(PCLK2)
  89.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  90.             /* ADCCLK = PCLK2/8 */
  91.                 // 设置ADC时钟(ADCCLK)
  92.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  93.                 // 设置USB时钟(USBCLK)
  94.                 // USB时钟 = PLL时钟除以1.5
  95.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  96.                 // 设置外部低速晶振(LSE)
  97.                 RCC_LSEConfig(RCC_LSE_OFF);

  98.                 // 使能或者失能内部低速晶振(LSI)
  99.                 // LSE晶振OFF
  100.                 RCC_LSICmd(DISABLE);

  101.                 // 设置RTC时钟(RTCCLK)
  102.                 // 选择HSE时钟频率除以128作为RTC时钟
  103.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  104.                 // 使能或者失能RTC时钟
  105.                 // RTC时钟的新状态
  106.                 RCC_RTCCLKCmd(DISABLE);

  107.             /* Flash 2 wait state */
  108.             FLASH_SetLatency(FLASH_Latency_2);

  109.             /* Enable Prefetch Buffer */
  110.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  111.             /* PLLCLK = 8MHz * 9 = 72 MHz */
  112.                 // 设置PLL时钟源及倍频系数
  113.             RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  114.             /* Enable PLL */
  115.                 // 使能或者失能PLL
  116.             RCC_PLLCmd(ENABLE);

  117.             /* Wait till PLL is ready */
  118.                 // 检查指定的RCC标志位设置与否
  119.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  120.             {
  121.             }

  122.             /* Select PLL as system clock source */
  123.                 // 设置系统时钟(SYSCLK)
  124.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  125.             /* Wait till PLL is used as system clock source */
  126.                 // 返回用作系统时钟的时钟源
  127.             while(RCC_GetSYSCLKSource() != 0x08)
  128.             {
  129.             }
  130.   }

  131. #if 0
  132.         // 读取返回不同片上时钟的频率,单位 Hz
  133.         {
  134.                 /* Get the frequencies of different on chip clocks */
  135.                 RCC_ClocksTypeDef RCC_Clocks;// 指向结构RCC_ClocksTypeDef的指针,包含了各个时钟的频率

  136.                 RCC_GetClocksFreq(&RCC_Clocks);        // 可以在仿真环境下看到各个不同的频率

  137.                 delay();
  138.         }
  139. #endif

  140.         // 使能或者失能AHB外设时钟
  141.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
  142.                                                         |RCC_AHBPeriph_DMA2
  143.                                                         |RCC_AHBPeriph_SRAM
  144.                                                         |RCC_AHBPeriph_FLITF
  145.                                                         |RCC_AHBPeriph_CRC
  146.                                                         |RCC_AHBPeriph_FSMC
  147.                                                         |RCC_AHBPeriph_SDIO,DISABLE);
  148.         // 使能或者失能APB1外设时钟
  149.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

  150.         // 强制或者释放高速APB(APB2)外设复位
  151.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
  152.         // 退出复位状态
  153.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

  154.         // 强制或者释放低速APB(APB1)外设复位
  155.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);

  156.         // 强制或者释放后备域复位
  157.         RCC_BackupResetCmd(ENABLE);

  158.         // 使能或者失能时钟安全系统
  159.         RCC_ClockSecuritySystemCmd(DISABLE);
  160. //------------------------------------------------------------------------------
  161. #if 0
  162.         {
  163.                 GPIO_InitTypeDef GPIO_InitStructure;

  164.                  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);

  165.                   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  166.                   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  167.                   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  168.                   GPIO_Init(GPIOA, &GPIO_InitStructure);

  169.                 // 选择在MCO管脚上输出的时钟源
  170.                 // 选中HSE
  171.                 // 警告:当选中系统时钟作为MCO管脚的输出时,注意它的时钟频率不超过50MHz(最大I/O速率)。
  172.                 // PA8/USART1_CK/ TIM1_CH1/MCO    STM32F103VBT6---pin67
  173.                 //RCC_MCOConfig(RCC_MCO_NoClock);// 示波器实际输出低电平
  174.                 RCC_MCOConfig(RCC_MCO_HSE);                // 示波器实际测量频率8.00047MHz,外部晶振
  175.         }
  176. #endif
  177. }
  178. //******************************************************************************
  179. // NVIC设置
  180. //******************************************************************************
  181. static void NVIC_Configuration(void)
  182. {
  183. }
  184. //******************************************************************************
  185. // SysTick设置初始化
  186. //******************************************************************************
  187. static void SysTick_Config1(void)
  188. {
  189.         #define SystemFreq                72000000.0        // 单位为Hz
  190.         #define TB_SysTick                100000.0                // 单位为uS,与示波器实测一致

  191.         INT32U ticks;

  192.         ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
  193.         ticks=ticks;
  194.         //SysTick_Config(ticks);
  195. }
  196. //******************************************************************************
  197. // GPIO设置
  198. //******************************************************************************
  199. static void GPIO_Configuration(void)
  200. {
  201.         GPIO_InitTypeDef GPIO_InitStructure;

  202.         // 使能或者失能APB2外设时钟
  203.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  204.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
  205.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  206.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  207.           GPIO_Init(GPIOC, &GPIO_InitStructure);
  208. }
  209. //******************************************************************************
  210. // LED亮
  211. //******************************************************************************
  212. void Led_RW_ON(void)
  213. {
  214.   GPIO_SetBits(GPIOC,GPIO_Pin_4);
  215. }
  216. //******************************************************************************
  217. // LED灭
  218. //******************************************************************************
  219. void Led_RW_OFF(void)
  220. {
  221.   GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  222. }
  223. //******************************************************************************
  224. // 主程序
  225. //******************************************************************************
  226. int main(void)
  227. {
  228.           RCC_Configuration();
  229.           GPIO_Configuration();
  230.         NVIC_Configuration();
  231.           SysTick_Config1();
  232. //------------------------------------------------------------------------------
  233.         for (;;)
  234.         {
  235.                 Led_RW_ON();
  236.                 delay();
  237.                 Led_RW_OFF();
  238.                 delay();
  239.         }
  240. }
  241. //******************************************************************************
  242. #ifdef  USE_FULL_ASSERT

  243. /**
  244.   * @brief  Reports the name of the source file and the source line number
  245.   *   where the assert_param error has occurred.
  246.   * @param file: pointer to the source file name
  247.   * @param line: assert_param error line source number
  248.   * @retval : None
  249.   */
  250. void assert_failed(uint8_t* file, uint32_t line)
  251. {
  252.   /* User can add his own implementation to report the file name and line number,
  253.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  254.   /* Infinite loop */
  255.   while (1)
  256.   {
  257.   }
  258. }
  259. #endif

  260. /**
  261.   * @}
  262.   */
  263. //******************************************************************************
  264. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  265. //******************************************************************************
  266. /*
  267.         LED2---------PC7
  268.         LED3---------PC6
  269.         LED4---------PC5
  270.         LED5---------PC4

  271.         KEY2---------PD3
  272.         KEY3---------PD4
  273. */
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表