[技术问答]

N76E003的ADC中断

[复制链接]
1778|8
手机看帖
扫描二维码
随时随地手机跟帖
whtwhtw|  楼主 | 2019-4-16 17:36 | 显示全部楼层 |阅读模式
关于N76E003的ADC中断的例子,官方例程就没有,只有PWM或者STADC触发。网上也没有相关资料,自己写一个ADC中断函数也莫名其妙,有时候能中断,有时候不行。
有没有知道的大神解惑一下

使用特权

评论回复
whtwhtw|  楼主 | 2019-4-16 17:38 | 显示全部楼层
void ADC_INIT (void)
{
        clr_ADCF;
        clr_EADC;
        clr_ADCS;
        
        set_PADCH;
        set_PADC;                                                                                                                                                        //中断优先级最高3
        
        set_EADC;                                                                                                                                                        // Enable ADC interrupt (if use interrupt)

}

void ADC_ISR (void) interrupt 11
{
P30 = ~P30;
}
在主程序中开启全局中断set_EA,set_ADCS启动ADC转换,实际上都没进中断

使用特权

评论回复
tianxj01| | 2019-4-16 18:05 | 显示全部楼层
本帖最后由 tianxj01 于 2019-4-16 18:14 编辑
whtwhtw 发表于 2019-4-16 17:38
void ADC_INIT (void)
{
        clr_ADCF;

003的ADCF中断,动作非常可靠。
如果你发现中断不可靠,那么很明显原因有3:
全局中断使能问题;
ADC本身的中断使能问题;
ADC没有使能;
ADC转换没有完成或者根本没有开始。
由于软件触发的ADC。必须保证ADCF位为0,才能启动一次新的ADC转换,而且ADCF必须软件清零,否则就退不出ADC中断,使用时候这些尤其必须注意。
看你的ADC中断函数,很明显没有处理这些必须位的动作,这种情况下,ADC中断在发生一次后,就直接卡在中断里面,后续动作不再发生,毛病应该就在这里。
修改一下:
void ADC_ISR (void) interrupt 11
{
clr_ADCF;
set_ADCS;
P30 = ~P30;
}

使用特权

评论回复
whtwhtw|  楼主 | 2019-4-17 08:13 | 显示全部楼层
tianxj01 发表于 2019-4-16 18:05
003的ADCF中断,动作非常可靠。
如果你发现中断不可靠,那么很明显原因有3:
全局中断使能问题;

谢谢指导,问题找到了,为了简化问题,代码没贴全,中断中clr_adcf,set_adcs都有,我这里的问题经过你提醒,我发现是在开始中断前没有开启任何一路adc通道,例如Enable_ADC_AIN0;因为使能ADC就是在这个宏里。我程序是在中断中切换ADC通道,居然漏了中断前也需要先开启一路

使用特权

评论回复
xinxianshi| | 2019-4-17 13:00 | 显示全部楼层
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technoledge Corp.
//  Website: http://www.nuvoton.com
//  E-Mail : MicroC-8bit@nuvoton.com
//  Date   : Apr/21/2017
//***********************************************************************************************************

//***********************************************************************************************************
//  File Function: N76E003 ADC demo code
//***********************************************************************************************************

#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"



//*****************  The Following is in define in Fucntion_define.h  ***************************
//****** Always include Function_define.h call the define you want, detail see main(void) *******
//***********************************************************************************************
#if 0
#define Enable_ADC_AIN0                        ADCCON0&=0xF0;P17_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT0;ADCCON1|=SET_BIT0                                                                        //P17
#define Enable_ADC_AIN1                        ADCCON0&=0xF0;ADCCON0|=0x01;P30_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT1;ADCCON1|=SET_BIT0                //P30
#define Enable_ADC_AIN2                        ADCCON0&=0xF0;ADCCON0|=0x02;P07_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT2;ADCCON1|=SET_BIT0                //P07
#define Enable_ADC_AIN3                        ADCCON0&=0xF0;ADCCON0|=0x03;P06_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT3;ADCCON1|=SET_BIT0                //P06
#define Enable_ADC_AIN4                        ADCCON0&=0xF0;ADCCON0|=0x04;P05_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT4;ADCCON1|=SET_BIT0                //P05
#define Enable_ADC_AIN5                        ADCCON0&=0xF0;ADCCON0|=0x05;P04_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT5;ADCCON1|=SET_BIT0                //P04
#define Enable_ADC_AIN6                        ADCCON0&=0xF0;ADCCON0|=0x06;P03_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT6;ADCCON1|=SET_BIT0                //P03
#define Enable_ADC_AIN7                        ADCCON0&=0xF0;ADCCON0|=0x07;P11_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT7;ADCCON1|=SET_BIT0                //P11
#define Enable_ADC_BandGap        ADCCON0|=0x0F;ADCCON1|=SET_BIT0                                                                                                                                                                                                                                                                //Band-gap 1.22V

#define PWM0_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
#define PWM2_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
#define PWM4_FALLINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
#define PWM0_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
#define PWM2_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
#define PWM4_RISINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1

#define P04_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
#define P13_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
#define P04_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
#define P13_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
#endif

/******************************************************************************
* FUNCTION_PURPOSE: ADC interrupt Service Routine
******************************************************************************/
void ADC_ISR (void) interrupt 11
{
    clr_ADCF;                               //clear ADC interrupt flag
                printf ("\n Value = 0x%bx",ADCRH);
}

/******************************************************************************
The main C function.  Program execution starts
here after stack initialization.
******************************************************************************/
void main (void)
{
    Set_All_GPIO_Quasi_Mode;
                InitialUART0_Timer1(115200);
/*---------------------------------------------------------------
        ADC port trig initial setting toggle P0.4 to start ADC
----------------------------------------------------------------*/
                Enable_ADC_AIN0;                                                                                                                        // Enable AIN0 P1.7 as ADC pin
                P04_FALLINGEDGE_TRIG_ADC;                                                                                        // P0.4 falling edge as adc start trig signal
// find ADC result in ADC interrupt
    set_EADC;                                                                                                                                                        // Enable ADC interrupt (if use interrupt)
                EA = 1;                                                                                                                                                                // Enable global interrupt
//        set_ADCS;                                                                                                                                                        // Trig P04 falling edge to start adc, no need set ADCS bit
                while(1);                                                                                                                                                        // Wait ADC interrupt


}

使用特权

评论回复
xinxianshi| | 2019-4-17 13:00 | 显示全部楼层
有例子,被注释了。
楼主最后写好的例子分享分享

使用特权

评论回复
643757107| | 2019-4-18 22:16 | 显示全部楼层
参考例子,注意上面注释掉的宏,那些是用来方便你写自己程序的。

使用特权

评论回复
天灵灵地灵灵| | 2019-4-19 00:00 | 显示全部楼层
配置错了

使用特权

评论回复
734774645| | 2019-4-20 07:27 | 显示全部楼层
注释内容里应该有。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

72

主题

2501

帖子

35

粉丝