单片机实验二NVIC中断实验.docx_第1页
单片机实验二NVIC中断实验.docx_第2页
单片机实验二NVIC中断实验.docx_第3页
单片机实验二NVIC中断实验.docx_第4页
单片机实验二NVIC中断实验.docx_第5页
免费预览已结束,剩余9页可下载查看

下载本文档

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

文档简介

实验二NVIC中断实验 班级: 计科0902 学号: 09281047姓名: 万晓松一、 实验目的1、 STM32 中断的基本概念以及嵌套向量中断控制器(NVIC)。2、 了解中断的优先处理权。3、 读懂所给C语言控制中断代码,学会应用STM32的库函数二、 实验内容1、 理解中断概念,通过实验现象理解代码及函数调用。三、 实验代码及解释EXITMain.c*/void GPIO_Configuration(void) GPIO_InitTypeDef GPIO_InitStructure; /* Configure PC.06 as Output push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; /初始化GPIO_Pin_6,速度为50MHz,为输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure PB.09 as input floating (EXTI Line 9) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; /初始化GPIO_Pin_9,为中断线9,为输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure);/*/* Function Name : NVIC_Configuration* Description : Configure the nested vectored interrupt controller.* Input : None* Output : None* Return : None*/void NVIC_Configuration(void) NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the EXTI9_5 Interrupt */* 配置外部中断 EXTI9_5 */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;/抢占优先级为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; /响应优先级位0,优先级最高,最先处理 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);It.c*/void EXTI9_5_IRQHandler(void)if(EXTI_GetITStatus(EXTI_Line9) != RESET)/key键控制EXTI_Line9,当他的状态未准备好去,按了一下key /* Toggle PC6 pin */ GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6);/把取到的状态 取反再写回显示 /* Clear the EXTI line 9 pending bit */ EXTI_ClearITPendingBit(EXTI_Line9); /注意EXTI_Line9-5清位 /*NVICMain.c/* Includes -*/#include stm32f10x_lib.h/* Private typedef -*/* Private define -*/* Private macro -*/* Private variables -*/NVIC_InitTypeDef NVIC_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;EXTI_InitTypeDef EXTI_InitStructure;bool PreemptionOccured = FALSE; /记录按系统时钟中断初始为FALSEu8 PreemptionPriorityValue = 0; /记录初始的优先级状态ErrorStatus HSEStartUpStatus; /* Private function prototypes -*/void RCC_Configuration(void);void GPIO_Configuration(void);void EXTI_Configuration(void);void Delay(vu32 nCount); /* Private functions -*/* Function Name : main* Description : Main program.* Input : None* Output : None* Return : None*/int main(void)#ifdef DEBUG debug();#endif /* Configure the system clocks */ RCC_Configuration(); /* Configure GPIOs */ GPIO_Configuration(); /* Configures the EXTI Lines */ EXTI_Configuration(); #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);/NVIC_PriorityGroupConfig() 选择使选择第1组优先级分组方式 /* Enable the EXTI0 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue;/EXTI0 初始抢占优先及为0,响应优先级为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI9_5 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;/EXTI9_5 初始抢占优先及为0,响应优先级为1,低于EXTI0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure the SysTick Handler Priority: Preemption priority and subpriority */ NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, !PreemptionPriorityValue, 0); /SysTick初始抢占优先及为0,响应优先级为0,默认情况下系统时钟优先级最高 while (1) if(PreemptionOccured != FALSE) /系统时钟是否被打断,只有系统时钟优先级低于外部时钟优先级,所以按key可执行中断(it.c),降低系统优先级 /系统时钟被打断,按wakeup则启动外部中断,执行以下程序 GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_6); /取GPIO_Pin_6当前状态再取反写回 Delay(0x5FFFF); GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_7); Delay(0x5FFFF); GPIO_WriteBit(GPIOC, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_8); Delay(0x5FFFF); GPIO_WriteBit(GPIOC, GPIO_Pin_9, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_9); Delay(0x5FFFF); /* Function Name : RCC_Configuration* Description : Configures the different system clocks* Input : None* Output : None* Return : None*/void RCC_Configuration(void) /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus = SUCCESS) /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) = RESET) /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) /* Enable GPIOA, GPIOB, GPIOC and AFIO Clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);/* Function Name : GPIO_Configuration* Description : Configures the used GPIO pins.* Input : None* Output : None* Return : None*/void GPIO_Configuration(void) /* Configure PC.06, PC.07, PC.08 and PC.09 as output push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; /定义GPIO灯6-9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure PA.00 as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure PB.09 as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure);it.c*/void EXTI9_5_IRQHandler(void) NVIC_InitTypeDef NVIC_InitStructure; if(EXTI_GetITStatus(EXTI_Line9) != RESET) PreemptionPriorityValue = !PreemptionPriorityValue; PreemptionOccured = FALSE; /* Modify the EXTI0 Interrupt Preemption Priority */ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel; NVIC_InitStructure.NVIC_I

温馨提示

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

评论

0/150

提交评论