Hello all,
I'm using PIC18 Explorer Board's **broken link removed**
PIC18F8722 chip
https://ww1.microchip.com/downloads/en/devicedoc/39646c.pdf
MPLAB C18 Compiler
I wrote this to test out the external ADC interrupt functions before I proceed on with other parts of my project
I'm quite new to MCUs, so pardon my noobish way of coding.
Code:
#include <p18f8722.h>
#include<delays.h>
/***************************
* Device configuration *
***************************/
#pragma config OSC = HSPLL // Using 10 MHz crystal with PLL (max = 10MHz*4)
#pragma config FCMEN = OFF // Disable Fail-Safe Clock Monitor
#pragma config IESO = OFF // Disable Oscillator Switchover mode
#pragma config PWRT = OFF // Disable Power-up timer
#pragma config WDT = OFF // Disable Watchdog timer
#pragma config MCLRE = ON // Enable MCLR Enable
#pragma config LVP = OFF // Disable low voltage ICSP
// Function declarations
void config_adc(void);
void config_adc(void)
{
TRISAbits.TRISA1 = 1; //AN1 = RA1 TRIS set to input
TRISDbits.TRISD6 = 0; //LED output
ADCON0 = 0b00000101; //AN1 selected
ADCON1 = 0b00001101; //AN0-AN1 analog
ADCON2 = 0b00001101;//left justified, 2Tad, Fosc 16
//ADC Interrupts config
INTCONbits.GIE = 1; // enable interrupts
INTCONbits.PEIE = 1; // enable peripheral interrupts.
INTCONbits.INT0IE =1; //enables external interrupts
PIE1bits.ADIE=1;//set the A/D converter interrupt enable bit. 1 is enable
PIR1bits.ADIF=0;//clear (initialise) A/D converter interrupt flag bit
}
// start ISR code
#pragma code isr = 0x08 // store the below code at address 0x08
#pragma interrupt isr
void isr (void)
{
if (PIR1bits.ADIF == 1) {
LATDbits.LATD6 = 1; //switch on LED
Delay1KTCYx(1);
LATDbits.LATD6 = 0;
PIR1bits.ADIF=0;
ADCON0bits.GO_DONE=1;//reset GO/DONE to start conversion
}
}
#pragma code // return to the default code section
void main (void)
{
config_adc();
ADCON0bits.ADON=1; //switch on the adc module forever, to add a pushbutton interrupt to switch off
ADCON0bits.GO_DONE=1; //Start conversion (in progress)
while(1)
{
}
}
I just want to switch on my board, when I input an analog signal into RA1/AN1, an LED will light up, and power off after a few seconds.
This is a prelude to my other parts of the project, where I will use the ADC values to display, count etc.
My interrupt code is activated upon power on of the board, before I input my desired analog signal.
The LED at RD6 is switched on, and this command is only in the ISR, no where else in my main function.
I realised that if I were to change ADCON0bits.GO_DONE=1 to 0, the light will be switched off.
But it doesn't make sense to me to set it to 0 in the ISR, otherwise I will never be able to turn it on again while the program is running.
I went through it line by line drawing out link maps and all to see what is done but I cannot find the error in my code.
I ran the "Watch" window in MPLAB SIM and realised that LATD is set to 0x40 on processor reset. This corresponds to LAT.D6
I removed the LATD6=1 line yet it still shows 0x40.
Could any veteran point out my mistake to me please? Thank you.