找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32学习--Gpio测试程序

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

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

  2. STM32学习日志(2)----GPIO测试程序

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

  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm32f10x.h"
  32. #include "stm32_m.h"

  33. /** @addtogroup Template_Project
  34.   * @{
  35.   */

  36. /* Private typedef -----------------------------------------------------------*/
  37. /* Private define ------------------------------------------------------------*/
  38. /* Private macro -------------------------------------------------------------*/
  39. /* Private variables ---------------------------------------------------------*/
  40. /* Private function prototypes -----------------------------------------------*/
  41. /* Private functions ---------------------------------------------------------*/

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

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

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

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

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

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

  87.             /* PCLK1 = HCLK/2 */
  88.                 // 设置低速AHB时钟(PCLK1)
  89.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  90.             /* PCLK2 = HCLK */
  91.                 // 设置高速AHB时钟(PCLK2)
  92.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  93.             /* ADCCLK = PCLK2/8 */
  94.                 // 设置ADC时钟(ADCCLK)
  95.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  96.                 // 设置USB时钟(USBCLK)
  97.                 // USB时钟 = PLL时钟除以1.5
  98.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

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

  101.                 // 使能或者失能内部低速晶振(LSI)
  102.                 // LSE晶振OFF
  103.                 RCC_LSICmd(DISABLE);

  104.                 // 设置RTC时钟(RTCCLK)
  105.                 // 选择HSE时钟频率除以128作为RTC时钟
  106.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  107.                 // 使能或者失能RTC时钟
  108.                 // RTC时钟的新状态
  109.                 RCC_RTCCLKCmd(DISABLE);

  110.             /* Flash 2 wait state */
  111.             FLASH_SetLatency(FLASH_Latency_2);

  112.             /* Enable Prefetch Buffer */
  113.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

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

  117.             /* Enable PLL */
  118.                 // 使能或者失能PLL
  119.             RCC_PLLCmd(ENABLE);

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

  125.             /* Select PLL as system clock source */
  126.                 // 设置系统时钟(SYSCLK)
  127.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

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

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

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

  140.                 delay();
  141.         }
  142. #endif

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

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

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

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

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

  167.                  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);

  168.                   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  169.                   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  170.                   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  171.                   GPIO_Init(GPIOA, &GPIO_InitStructure);

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

  194.         INT32U ticks;

  195.         ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
  196.         ticks=ticks;
  197.         //SysTick_Config(ticks);
  198. }
  199. //******************************************************************************
  200. // GPIO设置
  201. //******************************************************************************
  202. static void GPIO_Configuration(void)
  203. {
  204.         #define CONFIG_BUT  1

  205.         GPIO_InitTypeDef GPIO_InitStructure;

  206.         // 使能或者失能APB2外设时钟
  207.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);

  208.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  209.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  210.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  211.           GPIO_Init(GPIOC, &GPIO_InitStructure);

  212.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
  213.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  214.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  215.           GPIO_Init(GPIOD, &GPIO_InitStructure);

  216. #if (CONFIG_BUT==0)
  217.         /*
  218.                 IO口为输入模式,由于电路板上有外接的10K上拉电阻
  219.                 实际测量端口电压为3.300V
  220.         */
  221.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  222.         GPIO_Init(GPIOD, &GPIO_InitStructure);

  223. #elif (CONFIG_BUT==1)
  224.         /*
  225.                 IO口为输入上拉模式,输出端口应该写入1,以便使能内部的上拉电阻
  226.                 这时,外部的10K上拉电阻可以不接
  227.         */
  228.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  229.         GPIO_Init(GPIOD, &GPIO_InitStructure);

  230.         // 这时,测量引脚电压为3.300V

  231.         GPIO_SetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);// 这条语句可以取消,因为有外部上拉电阻
  232.         // 这时,测量引脚电压为3.300V

  233.         //GPIO_ResetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);
  234.         // 这时,测量引脚电压为2.650V,根据欧姆定律推算内部的下拉电阻为40K

  235. #elif (CONFIG_BUT==2)
  236.         /*
  237.                 IO口为输入下拉模式,输出端口应该写入0,以便使能内部的电阻
  238.                 这时,外部的10K上拉电阻一定要接
  239.         */
  240.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  241.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  242.         // 这时,测量引脚电压为2.650V

  243.         //GPIO_SetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);// 这条语句可以取消,因为有外部上拉电阻
  244.         // 这时,测量引脚电压为3.300V

  245.         GPIO_ResetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);
  246.         // 这时,测量引脚电压为2.650V,根据欧姆定律推算内部的下拉电阻为40K
  247. #else
  248.         error!
  249. #endif
  250. //------------------------------------------------------------------------------
  251. /*
  252. 总结:
  253.     按键输入的时候,以下5种设置都是可以运行的!

  254.     1.设定为悬空输入 GPIO_Mode_IN_FLOATING,外接电电阻,推荐

  255.     2.设定为上拉输入 GPIO_Mode_IPU,输出写1,可以不外接电阻,强烈推荐

  256.     3.设定为上拉输入 GPIO_Mode_IPU,输出写0,外接电阻,不推荐!!!!!!!!!!

  257.     4.设定为下拉输入 GPIO_Mode_IPD,输出写0,外接电阻,不推荐!!!!!!!!!!

  258.     5.设定为下拉输入 GPIO_Mode_IPD,输出写1,可以不外接电阻,可以
  259. */
  260. }
  261. //******************************************************************************
  262. // LED5亮
  263. //******************************************************************************
  264. void Led_RW_ON(void)
  265. {
  266.           GPIO_SetBits(GPIOC,GPIO_Pin_4);
  267. }
  268. //******************************************************************************
  269. // LED5灭
  270. //******************************************************************************
  271. void Led_RW_OFF(void)
  272. {
  273.           GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  274. }
  275. //******************************************************************************
  276. // 主程序
  277. //******************************************************************************
  278. int main(void)
  279. {
  280.         GPIO_InitTypeDef GPIO_InitStructure;

  281.         RCC_Configuration();
  282.           GPIO_Configuration();
  283.         NVIC_Configuration();
  284.           SysTick_Config1();
  285.         Led_RW_ON();
  286.         delay();
  287.         Led_RW_OFF();
  288. //------------------------------------------------------------------------------
  289. // 以下代码验证引脚配置的锁定功能
  290. //------------------------------------------------------------------------------
  291. #if 0
  292.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
  293.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;        // oc门
  294.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  295.           GPIO_Init(GPIOD, &GPIO_InitStructure);

  296.         GPIO_ResetBits(GPIOD,GPIO_Pin_4);                        // OC门设置的话,输出必须设定0

  297.         GPIO_PinLockConfig(GPIOD, GPIO_Pin_4);        // 锁定引脚设置寄存器

  298.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 重新设定为输入
  299.           GPIO_Init(GPIOD, &GPIO_InitStructure);

  300.         for(;;)
  301.         {
  302.                 // 按key3没有反映,因为引脚设置锁定了,修改无效
  303.                 if (GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4)==Bit_SET)
  304.                         Led_RW_ON();
  305.                 else
  306.                         Led_RW_OFF();
  307.         }
  308. #endif
  309. //------------------------------------------------------------------------------
  310. // 以下代码 JTAG引脚配置为普通的IO口
  311. //------------------------------------------------------------------------------
  312. #if 1
  313.         if (GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4)==Bit_RESET)                /* Key3 is pressed */
  314.         {
  315.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
  316.                 delay();

  317.             /* Disable the Serial Wire Jtag Debug Port SWJ-DP */
  318.             GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);

  319.             /* Configure PA.13 (JTMS/SWDAT), PA.14 (JTCK/SWCLK) and PA.15 (JTDI) as
  320.                output push-pull */
  321.             GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  322.             GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  323.             GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  324.             GPIO_Init(GPIOA, &GPIO_InitStructure);

  325.             /* Configure PB.03 (JTDO) and PB.04 (JTRST) as output push-pull */
  326.             GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
  327.             GPIO_Init(GPIOB, &GPIO_InitStructure);

  328.                    for (;;)                                                        /* Toggle JTMS/SWDAT pin */
  329.                    {
  330.                         GPIOA->ODR ^= GPIO_Pin_13;                // JTMS/SWDAT, PIN72
  331.                         GPIOA->ODR ^= GPIO_Pin_14;                // JTCK/SWCLK, PIN76
  332.                         GPIOA->ODR ^= GPIO_Pin_15;                // JTDI, PIN77

  333.                         GPIOB->ODR ^= GPIO_Pin_3;                // JTDO, PIN89
  334.                         GPIOB->ODR ^= GPIO_Pin_4;                // JTRST, PIN90

  335.                         delay();
  336.                         GPIOC->ODR ^= GPIO_Pin_4;                // led5 toogle
  337.                 }
  338.         }
  339. #endif
  340. //------------------------------------------------------------------------------
  341.         for (;;)
  342.         {
  343.                 delay();

  344.                 if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3))        // key2
  345.                         GPIO_SetBits(GPIOC,GPIO_Pin_7);                                // led2
  346.                 else
  347.                         GPIO_ResetBits(GPIOC,GPIO_Pin_7);

  348.                 GPIO_WriteBit(GPIOC,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_5)));// LED4

  349.                 //GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_4)));//LED5
  350.                 GPIOC->ODR ^= GPIO_Pin_4;                // led5 toogle
  351.         }
  352. }
  353. //******************************************************************************
  354. #ifdef  USE_FULL_ASSERT

  355. /**
  356.   * @brief  Reports the name of the source file and the source line number
  357.   *   where the assert_param error has occurred.
  358.   * @param file: pointer to the source file name
  359.   * @param line: assert_param error line source number
  360.   * @retval : None
  361.   */
  362. void assert_failed(uint8_t* file, uint32_t line)
  363. {
  364.   /* User can add his own implementation to report the file name and line number,
  365.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  366.   /* Infinite loop */
  367.   while (1)
  368.   {
  369.   }
  370. }
  371. #endif

  372. /**
  373.   * @}
  374.   */
  375. //******************************************************************************
  376. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  377. //******************************************************************************
  378. /*
  379.         LED2---------PC7
  380.         LED3---------PC6
  381.         LED4---------PC5
  382.         LED5---------PC4

  383.         KEY2---------PD3
  384.         KEY3---------PD4
  385. */
复制代码



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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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