|
发表于 2016-9-7 12:30:25
|
显示全部楼层
1注意中断服务程序的名字
void __attribute__((interrupt, no_auto_psv)) __AD1Interrupt(void)
2要仔细看手册
如果SSRCG = 0:
111 = 由内部计数器结束采样并启动转换(自动转换)
110 = 保留
101 = 由PWM 辅助特殊事件触发结束采样并启动转换(2)
100 = 由Timer5 比较结束采样并启动转换
011 = 由PWM 主特殊事件触发结束采样并启动转换(2)
010 = 由Timer3 比较结束采样并启动转换
001 = 由INT0 引脚上的有效电平跳变结束采样并启动转换
000 = 由清零采样位(SAMP)结束采样并启动转换(手动模式)
下面是可以进中断的程序
//--------------------------------------------------------------------------
#include "P33ep32mc202.h"
_FGS(0xff);//写保护关闭
_FOSCSEL(0xfb);//双速振荡器启动使能 PWM锁定 带PLL的主振荡器
_FOSC(0xdd);//禁止时钟切换 外设引脚多次配置 SOC2数字IO XT晶振模式
_FWDT(0x7f);//看门狗禁止
_FPOR(0xff);//I2C1被映射到SAD1/SCL1引脚 欠压复位_
_FICD(0x03);//禁止JTAG 通过PGEC1 PGED1通讯
void initAdc1(void);
int i, j;
int ADCValues[8] = {0, 0, 0, 0, 0, 0, 0, 0};
//void __attribute__((__interrupt__, auto_psv)) _ADC1Interrupt(void)
void __attribute__((interrupt, no_auto_psv)) _AD1Interrupt(void)
{
IFS0bits.AD1IF = 0; // Clear the ADC1 Interrupt Flag
ADCValues[0] = ADC1BUF0; // Read the AN5 conversion result
ADCValues[1] = ADC1BUF1; // Read the AN0 conversion result
ADCValues[2] = ADC1BUF2; // Read the AN1 conversion result
ADCValues[3] = ADC1BUF3; // Read the AN2 conversion result
if(ADCValues[2]>=500) PORTBbits.RB11=1;
else PORTBbits.RB11=0;
if(ADCValues[3]>=300) PORTBbits.RB12=1;
else PORTBbits.RB12=0;
if(ADCValues[0]>=1023) PORTBbits.RB13=1;
else PORTBbits.RB13=0;
if(ADCValues[1]>=300) PORTBbits.RB14=1;
else PORTBbits.RB14=0;
}
void initAdc1(void)
{
/* ADC端口配置 */
ANSELA = ANSELB = 0x0000;
ANSELAbits.ANSA0 = 1; // Ensure AN0/RA0 is analog
ANSELAbits.ANSA1 = 1; // Ensure AN1/RA1 is analog
ANSELBbits.ANSB0 = 1; // Ensure AN2/RB0 is analog
ANSELBbits.ANSB3 = 1; // Ensure AN5/RB3 is analog
/* Initialize and enable ADC module */
AD1CON1bits.FORM=0b00;//AD结果为整数
AD1CON1bits.SIMSAM=1; //同时采样
AD1CON1bits.ASAM=1; //上次采样结束立即开始采样
AD1CON1bits.SSRCG=0;
//AD1CON1bits.SSRC=0b110; //定时器3比较结束立即开始采样
AD1CON1bits.SSRC=0b010; //定时器3比较结束立即开始采样
AD1CON2bits.VCFG=0b000; //参考电压为VCC电压
AD1CON2bits.CHPS=0b11; //同时采样4个通道
AD1CON2bits.SMPI=0b00000;//每完成一次采样产生ADC中断
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CHS0bits.CH0SA = 5; // Select AN5 for CH0 +ve input
AD1CHS0bits.CH0NA = 0; // Select Vref- for CH0 -ve input
AD1CHS123bits.CH123SA = 0; // Select AN0 for CH1 +ve input
AD1CHS123bits.CH123NA = 0; // Select Vref- for CH1/CH2/CH3 -ve inputs
/*ADC中断设置*/
IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit
IEC0bits.AD1IE = 1; // Do Not Enable A/D interrupt
_AD1IP=6;
AD1CON1bits.ADON = 1; // Turn on the A/D converter
}
void Timer3_Timing_Init(void)//定时器模式
{
T3CON=0x0000;
TMR3 =0;//初始值*/
PR3 =2000;//周期值
_T3IP = 0x01; // 优先级1
IFS0bits.T3IF = 0; // Timer3中断标志状态位 清零
IEC0bits.T3IE = 0; // Timer3中断允许位 禁止
T3CONbits.TON = 1;//启动使能16位Timer3
}
int main(void)
{
int ADC_Dat1,ADC_Dat2;
PLLFBD = 41; /* M = 43 */
CLKDIVbits.PLLPOST = 0; /* N1 = 2 */
CLKDIVbits.PLLPRE = 0; /* N2 = 2 */
OSCTUN = 0;
/* Initiate Clock Switch to Primary Oscillator with PLL (NOSC = 0x3) */
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
while (OSCCONbits.COSC != 0x3);
while (_LOCK == 0); /* Wait for PLL lock at 40 MIPS */
//TRISB &= ~0xfc00;//RB10-RB15 清零
IOCON1 = 0x0000;//GPIO 模块控制 PWMxH PWMxL引脚
Timer3_Timing_Init();
initAdc1();
INTCON2bits.GIE = 1;// Enabled System Interrupt
while(1);
} |
|