STM32外部中程序断(2).doc_第1页
STM32外部中程序断(2).doc_第2页
STM32外部中程序断(2).doc_第3页
STM32外部中程序断(2).doc_第4页
STM32外部中程序断(2).doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/*/ 火牛开发板基础实验/ 外部中断实验/ 分别按下S3、S4,在串口1打印出相关信息/*/#include stm32f10x.h/#include string.h/*/ 外设时钟使能/*/void RCC_Configuration(void)/ /* 使能外设时钟 */RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO |/ RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | / RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE);/ /RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);/*/ / 全部用到的引脚将在在配置/*/void GPIO_Configuration(void)/GPIO_InitTypeDef GPIO_InitStructure;/ /* 配置串口1引脚 */ / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_8;/ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;/ / GPIO_Init(GPIOB, &GPIO_InitStructure);/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;/ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;/ GPIO_Init(GPIOE, &GPIO_InitStructure);/*外部中断引脚*/GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;/GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;/ GPIO_Init(GPIOE, &GPIO_InitStructure);/*/ 全部中断在此配置/*/void NVIC_Configuration(void)/ NVIC_InitTypeDef NVIC_InitStructure;/ /* Configure the NVIC Preemption Priority Bits */ / NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);/ /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;/NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;/ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;/ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/ NVIC_Init(&NVIC_InitStructure);/NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;/NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;/ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;/ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/ NVIC_Init(&NVIC_InitStructure);/NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;/ S4中断入口/ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;/ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/ NVIC_Init(&NVIC_InitStructure);/NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;/ S3中断入口/ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;/ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;/ NVIC_Init(&NVIC_InitStructure);/*/ 外部中断在此配置/*/void EXTI_Configuration(void)/ EXTI_InitTypeDef EXTI_InitStructure;/GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource2); / S4中断/GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource3);/EXTI_ClearITPendingBit(EXTI_Line2);/EXTI_ClearITPendingBit(EXTI_Line3);/ EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;/ EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;/EXTI_InitStructure.EXTI_Line=EXTI_Line2|EXTI_Line3;/ EXTI_InitStructure.EXTI_LineCmd = ENABLE;/ EXTI_Init(&EXTI_InitStructure);/GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); / S3中断/EXTI_InitStructure.EXTI_Line = EXTI_Line0;/ EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;/ EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;/ EXTI_InitStructure.EXTI_LineCmd = ENABLE;/ EXTI_Init(&EXTI_InitStructure);/EXTI_GenerateSWInterrupt(EXTI_Line13);/*/ 初始化时钟晶振 72MHZ/*/void SysClock_Init(void)/ErrorStatus HSEStartUpStatus; /RCC_DeInit();/RCC_HSEConfig(RCC_HSE_ON);/HSEStartUpStatus = RCC_WaitForHSEStartUp();/if(HSEStartUpStatus = SUCCESS)/ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);/FLASH_SetLatency(FLASH_Latency_2); / RCC_HCLKConfig(RCC_SYSCLK_Div1); / RCC_PCLK2Config(RCC_HCLK_Div1); / RCC_PCLK1Config(RCC_HCLK_Div2);/ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);/ RCC_PLLCmd(ENABLE);/ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) = RESET)/ ;/ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/ while(RCC_GetSYSCLKSource() != 0x08)/ ;/*/ USART1 初始化 baud 波特率/*/void USART1_Init(unsigned int baud)/ USART_InitTypeDef USART_InitStructure;/ USART_InitStructure.USART_BaudRate = baud;/ USART_InitStructure.USART_WordLength = USART_WordLength_8b;/ USART_InitStructure.USART_StopBits = USART_StopBits_1;/ USART_InitStructure.USART_Parity = USART_Parity_No;/ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/ USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;/ USART_Init(USART1, &USART_InitStructure);/ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);/ USART_Cmd(USART1, ENABLE);/*/ 从 USART1 发送一个字节/*/void USART1_SendByte(unsigned char temp)/ USART_SendData(USART1, temp);/ while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) = RESET);/*/ 从 USART1 发送字符串/*/void USART1_Printf(char *pch)/ while(*pch != 0)/ USART1_SendByte(*(unsigned char *)pch);/ pch+;/ /*/* /* 延时程序 ms/*/ */void Delay(unsigned short time)/ unsigned short i, j;/for(; time 0; time-)/ for(j = 0; j 10; j+)/ for(i = 0; i 0; time-) for(j = 0; j 10; j+) for(i = 0; i 1000; i+);/*MAIN 函数 */int main(void) SysClock_Init(); / 初始化系统时钟 72MHZRCC_Configuration(); /使能外设GPIO_Configuration(); /配置引脚 EXTI_Configuration();NVIC_Configuration(); / 配置中断USART1_Init(9600); / 配置串口1,波特率9600 while(1) GPIO_ResetBits(GPIOD, GPIO_Pin_9);/Delay(400); GPIO_ResetBits(GPIOD, GPIO_Pin_11);/Delay(400); GPIO_ResetBits(GPIOD, GPIO_Pin_8);/Delay(400); GPIO_ResetBits(GPIOD, GPIO_Pin_10); Delay(400); GPIO_SetBits(GPIOD, GPIO_Pin_8); GPIO_SetBits(GPIOD, GPIO_Pin_11); /Delay(400); GPIO_SetBits(GPIOD, GPIO_Pin_10);/Delay(100); GPIO_SetBits(GPIOD, GPIO_Pin_9 );Delay(400); GPIO_SetBits(GPIOD, GPIO_Pin_8); Delay(400); GPIO_ResetBits(GPIOD, GPIO_Pin_8); Delay(400); GPIO_SetBits(GPIOD, GPIO_Pin_9); Delay(400); GPIO_ResetBits(GPIOD, GPIO_Pin_9);Delay(400); GPIO_SetBits(GPIOD, GPIO_Pin_10); Delay(400); GPIO_ResetBits(GPIOD, GPIO_Pin_10); Delay(400); GPIO_SetBits(GPIOD, GPIO_Pin_11); Delay(400); GPIO_ResetBits(GPIOD, GPIO_Pin_11); Delay(400); /Delay(400);/ GPIO_SetBits(GPIOD, GPIO_Pin_8);/ /Delay(400); /Delay(400);/ GPIO_SetBits(GPIOD, GPIO_Pin_8); / /Delay(400);/ GPIO_SetBits(GPIOD, GPIO_Pin_10);/Delay(100); / GPIO_ResetBits(GPIOD, GPIO_Pin_11);/Delay(400);/ GPIO_SetBits(GPIOD, GPIO_Pin_9);/ /Delay(400); /* * * file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c * author MCD Application Team * version V3.4.0 * date 10/15/2010 * brief Main Interrupt Service Routines. * This file provides template for all exceptions handler and * peripherals interrupt service routine. * * 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. * * © COPYRIGHT 2010 STMicroelectronics */ /* Includes -*/#include stm32f10x_it.hextern void USART1_Printf(char *pch);/* addtogroup STM32F10x_StdPeriph_Template * */* Private typedef -*/* Private define -*/* Private macro -*/* Private variables -*/* Private function prototypes -*/* Private functions -*/*/* Cortex-M3 Processor Exceptions Handlers */*/* * brief This function handles NMI exception. * param None * retval None */void NMI_Handler(void)/* * brief This func

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论