程序如下,我想实现的功能是当按键按下产生外部中断3个灯闪一下,在中断中软件启动AD1转化片内温度;同时第4个等亮说明转化玩了,在用LCD显示出来;现在已经实现按下键产生中断了就是说第一个中断进了,但感觉没进第2个中断,第4个灯豪无放映,请交大家程序是哪里有问题啊 想很久了。。。。谢谢了
主程序:
# include"stm32f10x_lib.h"
# include"lcd.h"
vu32 TimingDelay;
ADC_InitTypeDef ADC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
ErrorStatus HSEStartUpStatus;
ADC_InitTypeDef ADC_InitStructrue;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
void RCC_init(void);
void NVIC_init(void);
void GPIO_init(void);
void ADC_init(void);
void EXTI_init(void);
void Delay(vu32 nCount);
void Decrement_TimingDelay(void);
void SysTick_Config(void);
void LED_Init(void);
int main(void)
{
RCC_init(); //初始化时钟;
LED_Init(); //LED初始化;
GPIO_init(); //初始化按键;
EXTI_init(); //初始化外部中断;
SysTick_Config(); //初始化SYSTICK;
NVIC_init(); //配置中断优先级;
STM3210E_LCD_Init(); //LCD初始化;
ADC_init(); //ADC初始化;
LCD_Clear(White); //LCD清屏白色;
LCD_SetBackColor(0x001F); //设置LCD背景颜色;
/* Set the LCD Text Color */
LCD_SetTextColor(0XFFFF); //设置LCD文字颜色;
LCD_DisplayStringLine(Line0, " EM_STM32E Demo ");
LCD_DisplayStringLine(Line1, " ADC_Test ");
LCD_DisplayStringLine(Line2, " welcome to Embest ");
LCD_DisplayStringLine(Line3, " www.embedinfo.com ");
while(1);
}
void RCC_init()
{
RCC_DeInit(); //初始化时钟;
RCC_HSEConfig(RCC_HSE_ON); //使能外部时钟8MHZ;
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS) //等待外部时钟稳定;
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1); //定义AHB时钟为外部时钟8MHZ;
RCC_PCLK2Config(RCC_HCLK_Div1); //配置高速外设APB2时钟为8MHZ;
RCC_PCLK1Config(RCC_HCLK_Div2); //配置低速外设APB1时钟为4MHZ;
RCC_ADCCLKConfig(RCC_PCLK2_Div4); //配置ADC时钟为APB2的四分频2MHZ;
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_7); //配置PLL时钟为外部时钟的7倍频7*8MHZ;
RCC_PLLCmd(ENABLE); //使能PLL时钟;
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY==RESET)) //最后等待PLL时钟稳定;
{}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟等于PLL时钟56MHZ;
while(RCC_GetSYSCLKSource()!=0x08) //等待系统时钟稳定;
{}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); //使能ADC时钟;
}
void LED_Init(void) //配置I/O口为LED;
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE); //使能I/O口时钟
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //配置GPIOFI/O口为推免输出;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOF,&GPIO_InitStructure);
}
void GPIO_init(void) //配置I/O为按键;
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void EXTI_init() //配置外部中断子程序;
{
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); //选择外部中断;
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);
}
void SysTick_Config(void) //设定SYSTICK为精确延时做准备;
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); //配置SYSTICK时钟为AHB时钟8MHZ;
SysTick_SetReload(80000); //设定初值当一个循环为80000/8MHZ=10MS;
SysTick_ITConfig(ENABLE); //使能SYSTICK中断;
}
void Delay(vu32 nCount) //精确延时子程序;
{
SysTick_CounterCmd(SysTick_Counter_Enable); //使能SYSTICK计算;
TimingDelay=nCount; //确定延时系数以确定延时=NCOUNT*10MS;
while(TimingDelay!=0) //等待延时;
{}
SysTick_CounterCmd(SysTick_Counter_Disable); //延时完成后失能SYSTICK计数;
SysTick_CounterCmd(SysTick_Counter_Clear); //清楚SYSTICK计数;
}
// void Decrement_TimingDelay(void) //也是一个延时子程序方法相同写法不同;
// {
// if(TimingDelay!=0x00)
// {
// TimingDelay--;}
// }
void NVIC_init(void) //为2个中断配置优先级;
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //选择优先级分组为2;
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQChannel; //配置外部(按键)中断优先级;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=ADC1_2_IRQChannel; //配置ADC转换玩后的中断优先级;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void ADC_init(void)
{
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent; //配置ADC为独立工作方式;
ADC_InitStructure.ADC_ScanConvMode=DISABLE; //配置ADC为单通道;
ADC_InitStructure.ADC_ContinuousConvMode=DISABLE; //配置ADC为单次执行;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None; //配置ADC的启动为软件触发;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right; //配置ADC的转换结果为右对齐;
ADC_InitStructure.ADC_NbrOfChannel=1; //选择ADC转化的通道个数;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE); //使能内部温度和内部参考电压;
ADC_RegularChannelConfig(ADC1,ADC_Channel_16,1,ADC_SampleTime_239Cycles5); //设置通道16(内部温度),和转换顺序???;和采样时间为239.5*CYCLESS?;
ADC_ExternalTrigConvCmd(ADC1,ENABLE); //使能外部触发启动ADC1(因为是软件触发);
ADC_Cmd(ADC1,ENABLE); //使能ADC1;
ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE); //使能ADC1中断;
}
中断程序:
void EXTI0_IRQHandler(void)
{ vu32 i;
if(EXTI_GetITStatus(EXTI_Line0)!=RESET) //等待按键中断好启动ADC;
GPIO_SetBits(GPIOF,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9) ;
for(i=0x8ffff;i>0;i--);
GPIO_ResetBits(GPIOF,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9); //启动ADC标志,散灯;
ADC_SoftwareStartConvCmd(ADC1,ENABLE); //软件启动ADC1;
EXTI_ClearITPendingBit(EXTI_Line0); //清楚ADC中断标志;
}
void ADC1_2_IRQHandler(void)
{
vu32 AD_Value;
char text[40];
LCD_DisplayStringLine(144, " www.embedinfo.com "); //显示一串字符说明进入ADC1中断;
if(ADC_GetITStatus(ADC1,ADC_IT_EOC)!=RESET) //等待ADC转换完中断;
{GPIO_SetBits(GPIOF,GPIO_Pin_8); //设置转换玩标志等亮;
ADC_SoftwareStartConvCmd(ADC1,DISABLE); //失能ADC软件启动功能;
AD_Value=(143-ADC_GetConversionValue(ADC1)*330/4096)*1000/34+2500; //处理ADC数据;
LCD_SetBackColor(0xFFFF); //设置LCD背景色;
LCD_SetTextColor(0X001F); //设置LCD文字颜色;
sprintf(text,"AD_Value=0x%04X",AD_Value);
LCD_DisplayStringLine(120,(u8*)text);} //显示;
ADC_ClearITPendingBit(ADC1,ADC_IT_EOC); //清楚ADC中断标志;
} |