|
完整程序源代码工程文件下载地址:
gpio.rar
(230.86 KB, 下载次数: 15)
- /*******************************************************************************
- STM32学习日志(2)----GPIO测试程序
- 编译环境: EWARM V5.30
- 硬件环境: 51hei EK-STM32F
- 作者 : szlihongtao
- 时间 : 2010-06-29
- 说明 : 1.验证引脚的输入输出功能
- 2.验证引脚配置的锁定功能
- 3.将JTAG引脚配置为普通的IO口
- *******************************************************************************/
- /**
- ******************************************************************************
- * @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
- */
- //******************************************************************************
- // 延时50ms
- //******************************************************************************
- static void delay(void)
- {
- INT32U i;
- static INT32U jjj=5240*50;
- for (i=0; i<jjj; i++);
- }
- //******************************************************************************
- // 时钟设置初始化
- //******************************************************************************
- static void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- /*
- RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
- RCC_ITConfig 使能或者失能指定的RCC中断
- RCC_ClearFlag 清除RCC的复位标志位
- RCC_GetITStatus 检查指定的RCC中断发生与否
- RCC_ClearITPendingBit 清除RCC的中断待处理位
- */
- /* RCC system reset(for debug purpose) */
- // 时钟系统复位
- RCC_DeInit();
- // 使能外部的8M晶振
- // 设置外部高速晶振(HSE)
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- // 使能或者失能内部高速晶振(HSI)
- RCC_HSICmd(DISABLE);
- // 等待HSE起振
- // 该函数将等待直到HSE就绪,或者在超时的情况下退出
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- // 设置AHB时钟(HCLK)
- RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
- /* PCLK1 = HCLK/2 */
- // 设置低速AHB时钟(PCLK1)
- RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
- /* PCLK2 = HCLK */
- // 设置高速AHB时钟(PCLK2)
- RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
- /* ADCCLK = PCLK2/8 */
- // 设置ADC时钟(ADCCLK)
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- // 设置USB时钟(USBCLK)
- // USB时钟 = PLL时钟除以1.5
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
- // 设置外部低速晶振(LSE)
- RCC_LSEConfig(RCC_LSE_OFF);
- // 使能或者失能内部低速晶振(LSI)
- // LSE晶振OFF
- RCC_LSICmd(DISABLE);
- // 设置RTC时钟(RTCCLK)
- // 选择HSE时钟频率除以128作为RTC时钟
- RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
- // 使能或者失能RTC时钟
- // RTC时钟的新状态
- 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 */
- // 设置PLL时钟源及倍频系数
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- // 使能或者失能PLL
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- // 检查指定的RCC标志位设置与否
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- // 设置系统时钟(SYSCLK)
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- // 返回用作系统时钟的时钟源
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- #if 0
- // 读取返回不同片上时钟的频率,单位 Hz
- {
- /* Get the frequencies of different on chip clocks */
- RCC_ClocksTypeDef RCC_Clocks;// 指向结构RCC_ClocksTypeDef的指针,包含了各个时钟的频率
- RCC_GetClocksFreq(&RCC_Clocks); // 可以在仿真环境下看到各个不同的频率
- delay();
- }
- #endif
- // 使能或者失能AHB外设时钟
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
- |RCC_AHBPeriph_DMA2
- |RCC_AHBPeriph_SRAM
- |RCC_AHBPeriph_FLITF
- |RCC_AHBPeriph_CRC
- |RCC_AHBPeriph_FSMC
- |RCC_AHBPeriph_SDIO,DISABLE);
- // 使能或者失能APB1外设时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
- // 强制或者释放高速APB(APB2)外设复位
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
- // 退出复位状态
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
- // 强制或者释放低速APB(APB1)外设复位
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
- // 强制或者释放后备域复位
- RCC_BackupResetCmd(ENABLE);
- // 使能或者失能时钟安全系统
- RCC_ClockSecuritySystemCmd(DISABLE);
- //------------------------------------------------------------------------------
- #if 0
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- // 选择在MCO管脚上输出的时钟源
- // 选中HSE
- // 警告:当选中系统时钟作为MCO管脚的输出时,注意它的时钟频率不超过50MHz(最大I/O速率)。
- // PA8/USART1_CK/ TIM1_CH1/MCO STM32F103VBT6---pin67
- //RCC_MCOConfig(RCC_MCO_NoClock);// 示波器实际输出低电平
- RCC_MCOConfig(RCC_MCO_HSE); // 示波器实际测量频率8.00047MHz,外部晶振
- }
- #endif
- }
- //******************************************************************************
- // NVIC设置
- //******************************************************************************
- static void NVIC_Configuration(void)
- {
- }
- //******************************************************************************
- // SysTick设置初始化
- //******************************************************************************
- static void SysTick_Config1(void)
- {
- #define SystemFreq 72000000.0 // 单位为Hz
- #define TB_SysTick 100000.0 // 单位为uS,与示波器实测一致
- INT32U ticks;
- ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
- ticks=ticks;
- //SysTick_Config(ticks);
- }
- //******************************************************************************
- // GPIO设置
- //******************************************************************************
- static void GPIO_Configuration(void)
- {
- #define CONFIG_BUT 1
- GPIO_InitTypeDef GPIO_InitStructure;
- // 使能或者失能APB2外设时钟
- 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);
- #if (CONFIG_BUT==0)
- /*
- IO口为输入模式,由于电路板上有外接的10K上拉电阻
- 实际测量端口电压为3.300V
- */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- #elif (CONFIG_BUT==1)
- /*
- IO口为输入上拉模式,输出端口应该写入1,以便使能内部的上拉电阻
- 这时,外部的10K上拉电阻可以不接
- */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- // 这时,测量引脚电压为3.300V
- GPIO_SetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);// 这条语句可以取消,因为有外部上拉电阻
- // 这时,测量引脚电压为3.300V
- //GPIO_ResetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);
- // 这时,测量引脚电压为2.650V,根据欧姆定律推算内部的下拉电阻为40K
- #elif (CONFIG_BUT==2)
- /*
- IO口为输入下拉模式,输出端口应该写入0,以便使能内部的电阻
- 这时,外部的10K上拉电阻一定要接
- */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- // 这时,测量引脚电压为2.650V
- //GPIO_SetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);// 这条语句可以取消,因为有外部上拉电阻
- // 这时,测量引脚电压为3.300V
- GPIO_ResetBits(GPIOD, GPIO_Pin_3 | GPIO_Pin_4);
- // 这时,测量引脚电压为2.650V,根据欧姆定律推算内部的下拉电阻为40K
- #else
- error!
- #endif
- //------------------------------------------------------------------------------
- /*
- 总结:
- 按键输入的时候,以下5种设置都是可以运行的!
- 1.设定为悬空输入 GPIO_Mode_IN_FLOATING,外接电电阻,推荐
- 2.设定为上拉输入 GPIO_Mode_IPU,输出写1,可以不外接电阻,强烈推荐
- 3.设定为上拉输入 GPIO_Mode_IPU,输出写0,外接电阻,不推荐!!!!!!!!!!
- 4.设定为下拉输入 GPIO_Mode_IPD,输出写0,外接电阻,不推荐!!!!!!!!!!
- 5.设定为下拉输入 GPIO_Mode_IPD,输出写1,可以不外接电阻,可以
- */
- }
- //******************************************************************************
- // 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)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- SysTick_Config1();
- Led_RW_ON();
- delay();
- Led_RW_OFF();
- //------------------------------------------------------------------------------
- // 以下代码验证引脚配置的锁定功能
- //------------------------------------------------------------------------------
- #if 0
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; // oc门
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOD,GPIO_Pin_4); // OC门设置的话,输出必须设定0
- GPIO_PinLockConfig(GPIOD, GPIO_Pin_4); // 锁定引脚设置寄存器
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 重新设定为输入
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- for(;;)
- {
- // 按key3没有反映,因为引脚设置锁定了,修改无效
- if (GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4)==Bit_SET)
- Led_RW_ON();
- else
- Led_RW_OFF();
- }
- #endif
- //------------------------------------------------------------------------------
- // 以下代码 JTAG引脚配置为普通的IO口
- //------------------------------------------------------------------------------
- #if 1
- if (GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4)==Bit_RESET) /* Key3 is pressed */
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
- delay();
- /* Disable the Serial Wire Jtag Debug Port SWJ-DP */
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
- /* Configure PA.13 (JTMS/SWDAT), PA.14 (JTCK/SWCLK) and PA.15 (JTDI) as
- output push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Configure PB.03 (JTDO) and PB.04 (JTRST) as output push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- for (;;) /* Toggle JTMS/SWDAT pin */
- {
- GPIOA->ODR ^= GPIO_Pin_13; // JTMS/SWDAT, PIN72
- GPIOA->ODR ^= GPIO_Pin_14; // JTCK/SWCLK, PIN76
- GPIOA->ODR ^= GPIO_Pin_15; // JTDI, PIN77
- GPIOB->ODR ^= GPIO_Pin_3; // JTDO, PIN89
- GPIOB->ODR ^= GPIO_Pin_4; // JTRST, PIN90
- delay();
- GPIOC->ODR ^= GPIO_Pin_4; // led5 toogle
- }
- }
- #endif
- //------------------------------------------------------------------------------
- for (;;)
- {
- delay();
- if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3)) // key2
- GPIO_SetBits(GPIOC,GPIO_Pin_7); // led2
- else
- GPIO_ResetBits(GPIOC,GPIO_Pin_7);
- GPIO_WriteBit(GPIOC,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_5)));// LED4
- //GPIO_WriteBit(GPIOC,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_4)));//LED5
- GPIOC->ODR ^= GPIO_Pin_4; // led5 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
- */
复制代码
|
|