当任何大于3VDC的电压施加到ADC通道0时,PIC 18F4520会自行复位并保持复位状态

时间:2018-08-08 08:25:35

标签: timer reset pic adc

我正在运行一个业余项目,但遇到一个问题。我使用带有XC8编译器的MPLABX对图片进行编码。 我正在使用3个模拟输入。它们是AN0,AN1和AN2。代码中也使用了Timer0中断。 当我在AN1和AN2上施加5V电压时,它不会打扰PIC uC,但是当对AN0施加大于3V电压时,Pic会自行复位并保持复位状态,直到电压降至3V以下。

还有其他人遇到这种问题吗?

编辑!

我禁用了定时器中断,并编写了以下代码。 每当向AN0施加5V电压时,仍然会产生相同的问题。

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <p18f4520.h>
#include "my_header.h"

#define _XTAL_FREQ 20000000 // Fosc  frequency for _delay()  library

#define green_1 PORTCbits.RC1
#define green_2 PORTCbits.RC2
#define green_3 PORTCbits.RC3
#define green_4 PORTDbits.RD0
#define green_5 PORTDbits.RD1
#define green_6 PORTDbits.RD2
#define green_7 PORTDbits.RD3
#define green_8 PORTCbits.RC4
#define green_9 PORTCbits.RC5
#define green_10 PORTCbits.RC6

void main(){
    int kg=0;
    //setting the Oscillator Freq. to 125KHz for 8TAd to be 3.2 MicroSeconds
    OSCCONbits.IRCF0=0;
    OSCCONbits.IRCF1=0;        
    OSCCONbits.IRCF2=1;
   //ADC Module Settings
    ADCON1=0b00001100; 
    ADCON2=0b10010000;

    TRISAbits.RA6=0;
    TRISAbits.RA7=0;
    TRISCbits.RC0=0;
    TRISCbits.RC1=0;
    TRISCbits.RC2=0;
    TRISCbits.RC3=0;
    TRISCbits.RC4=0;
    TRISCbits.RC5=0;
    TRISCbits.RC6=0;
    TRISCbits.RC7=0;    
    TRISDbits.RD0=0;
    TRISDbits.RD1=0;
    TRISDbits.RD2=0;
    TRISDbits.RD3=0;
    TRISDbits.RD4=0;
    TRISDbits.RD5=0;
    TRISDbits.RD6=0;
while(1){
    ADCON0=0x00;
    ADCON0=0x03;
    while(GODONE);
    ADON=0;
    kg=ADRES;

    if (kg >200){
        green_1=1;
        green_2=0;
        green_3=1;
        green_4=0;
        green_5=0;
        green_6=1;
        green_7=1;
        green_8=0;
        green_9=1;
        green_10=1;

    }
    else {
        green_1=0;
        green_2=0;
        green_3=0;
        green_4=0;
        green_5=0;
        green_6=0;
        green_7=0;
        green_8=0;
        green_9=0;
        green_10=0;
    }


}

}

谢谢大家。

1 个答案:

答案 0 :(得分:1)

您似乎正在更改OCCCON寄存器以使用125KHz的系统时钟,但告诉编译器您正在使用20MHz的晶体振荡器。

您尚未发布配置字设置,因此我们无法知道您的系统时钟设置实际上是什么。

无论如何,这就是我设置PIC18F4520的方式:

/*
 * File:     main.c
 * Target:   PIC18F4520
 * Compiler: XC8 v1.45
 * IDE:      MPLAB v8.92
 *
 *                            PIC18F4520
 *                  +------------:_:------------+
 *        VPP ->  1 : RE3/MCLR/VPP      PGD/RB7 : 40 <> PGD
 *  AIN(0-5V) ->  2 : RA0/AN0           PGC/RB6 : 39 <> PGC
 *            <>  3 : RA1               PGM/RB5 : 38 <>
 *            <>  4 : RA2                   RB4 : 37 <>
 *            <>  5 : RA3                   RB3 : 36 <>
 *            <>  6 : RA4                   RB2 : 35 <>
 *            <>  7 : RA5                   RB1 : 34 <>
 *            <>  8 : RE0                   RB0 : 33 <>
 *            <>  9 : RE1                   VDD : 32 <- PWR
 *            <> 10 : RE2                   VSS : 31 <- GND
 *        PWR -> 11 : VDD                   RD7 : 30 <>
 *        GND -> 12 : VSS                   RD6 : 29 <>
 * 20MHZ XTAL -> 13 : RA7/OSC1              RD5 : 28 <>
 * 20MHZ XTAL <- 14 : RA6/OSC2              RD4 : 27 <>
 *            <> 15 : RC0/SOSCO       RX/DT/RC7 : 26 <>
 *   GRN_LED1 <> 16 : RC1/SOSCI       TX/CK/RC6 : 25 <> GRN_LED10
 *   GRN_LED2 <> 17 : RC2/CCP1     SPI_MOSI/RC5 : 24 <> GRN_LED9
 *   GRN_LED3 <> 18 : RC3/SPI_CLK  SPI_MISO/RC4 : 23 <> GRN_LED8
 *   GRN_LED4 <> 19 : RD0                   RD3 : 22 <> GRN_LED7
 *   GRN_LED5 <> 20 : RD1                   RD2 : 21 <> GRN_LED6
 *                  +---------------------------:
 *                             DIP-40
 *
 */

#pragma config OSC      = HS
#pragma config FCMEN    = OFF
#pragma config IESO     = OFF
#pragma config BOREN    = OFF
#pragma config BORV     = 3
#pragma config PWRT     = OFF
#pragma config WDTPS    = 32768
#pragma config WDT      = OFF
#pragma config CCP2MX   = PORTC
#pragma config LPT1OSC  = ON
#pragma config MCLRE    = ON
#pragma config STVREN   = ON
#pragma config XINST    = OFF
#pragma config LVP      = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPD = OFF, CPB = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTD = OFF, WRTB = OFF, WRTC = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF

#include <xc.h>

/* Define the frequency we promise to set the clock at */
#define FSYS (20000000L)
#define FCYC (FSYS/4L)
#define _XTAL_FREQ FSYS
/*
 * Define the LED output bits
 */
#define green_1  LATCbits.LATC1
#define green_2  LATCbits.LATC2
#define green_3  LATCbits.LATC3
#define green_4  LATDbits.LATD0
#define green_5  LATDbits.LATD1
#define green_6  LATDbits.LATD2
#define green_7  LATDbits.LATD3
#define green_8  LATCbits.LATC4
#define green_9  LATCbits.LATC5
#define green_10 LATCbits.LATC6
/*
 * Main program
 */
void main(void)
{
    /* declare variable to receive ADC data */
    int kg;

    /* Disable all interrupt sources */
    INTCON = 0;
    PIE1   = 0;
    PIE2   = 0;

    /* Setup the ADC for input on AN0 only */
    ADCON0 = 0;             /* Disable ADC */
    ADCON1 = 0b00001110;    /* Set AN0(RA0) as the ADC input */
    ADCON2 = 0b10001101;    /* Set Right justified data, ACQT as 2 TAD, ADCS as FOSC/16 (20MHz system clock) */
    ADCON0bits.ADON = 1;    /* Enable ADC */

    /* Setup GPIO bits */
    TRISCbits.TRISC0=0;
    TRISCbits.TRISC1=0;
    TRISCbits.TRISC2=0;
    TRISCbits.TRISC3=0;
    TRISCbits.TRISC4=0;
    TRISCbits.TRISC5=0;
    TRISCbits.TRISC6=0;
    TRISCbits.TRISC7=0;
    TRISDbits.TRISD0=0;
    TRISDbits.TRISD1=0;
    TRISDbits.TRISD2=0;
    TRISDbits.TRISD3=0;
    TRISDbits.TRISD4=0;
    TRISDbits.TRISD5=0;
    TRISDbits.TRISD6=0;
    /*
     * Application loop
     */
    ADCON0bits.GO   = 1;    /* Start first ADC conversion */
    for(;;)
    {
        if(ADCON0bits.GO == 0)
        {
            /* Save value of ADC conversion */
            kg = ADRESL;
            kg |= (int)(ADRESH)<<8;

            /* Start next ADC conversion */
            ADCON0bits.GO   = 1;

            /* show results in the LEDs */
            if (kg >200){
                green_1=1;
                green_2=0;
                green_3=1;
                green_4=0;
                green_5=0;
                green_6=1;
                green_7=1;
                green_8=0;
                green_9=1;
                green_10=1;

            }
            else {
                green_1=0;
                green_2=0;
                green_3=0;
                green_4=0;
                green_5=0;
                green_6=0;
                green_7=0;
                green_8=0;
                green_9=0;
                green_10=0;
            }
        }
    }
}