Last active
April 27, 2023 14:46
-
-
Save sapher/c2e9c1baa6b819738887 to your computer and use it in GitHub Desktop.
PIC12F615 ADC interrupt exemple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "xc.h" | |
#include "config.h" | |
#define _XTAL_FREQ 4000000 | |
unsigned short pot = 0; | |
void interrupt isr() { | |
// Disable GIE (best practice?) | |
INTCONbits.GIE = 0; | |
// Handle ADC conversion end interrupt | |
if(PIR1bits.ADIF == 1) { | |
pot = (ADRESH<<8) + ADRESL; | |
PORTAbits.GP5 = (pot < 512) ? 1 : 0; | |
PIR1bits.ADIF = 0; | |
} | |
// Handle Rising edge interrupt | |
if(INTCONbits.INTF == 1) { | |
PORTAbits.GP0 =! PORTAbits.GP0; | |
INTCONbits.INTF = 0; | |
} | |
// Enable GIE (best practice?) | |
INTCONbits.GIE = 1; | |
} | |
void main(void) { | |
/*Configure peripheral*/ | |
//Configure GP2 | |
TRISAbits.TRISIO2 = 1; // GP2 as input | |
ANSELbits.AN2 = 0; // GP2 not analog | |
//Configure GP5 | |
TRISAbits.TRISIO5 = 0; // GP5 as output | |
PORTAbits.GPIO5 = 0; // GP5 set low | |
//Configure GP0 > PWM | |
ANSELbits.AN0 = 0; | |
TRISAbits.TRISIO0 = 0; // GP0 as output | |
PORTAbits.GPIO0 = 1; // GP0 set high | |
//Configure GP4/AN3/CHS2 > ADC | |
// > port | |
TRISAbits.TRISIO4 = 1; // GP4 as input | |
ANSELbits.ADCS0 = 1; | |
ANSELbits.AN3 = 1; // GP4 as analog | |
// > channel | |
ADCON0bits.CHS = 0b011; // select ADC channel 2 | |
// > left | |
ADCON0bits.ADFM = 1; | |
// > voltage reference | |
ADCON0bits.VCFG = 0; // VDD as Vref | |
// > clock source | |
ANSELbits.ADCS0 = 0b100;// Set clock source as FOSC/2 | |
// > enable ADC module | |
ADCON0bits.ADON = 1; // enable ADC | |
/*Configure interrupts*/ | |
//Button | |
//INTCONbits.GIE = 1; //enable global interrupt | |
INTCONbits.PEIE =1; | |
INTCONbits.INTE = 1; //enable external interrupt on GP2 | |
OPTION_REGbits.INTEDG = 1; //enable interrupt on rising edge on GP2 | |
INTCONbits.INTF = 0; //clear interrupt flag | |
//ADC | |
PIE1bits.ADIE = 1; // enable ADC interrupt | |
PIR1bits.ADIF = 0; // clear ADC interrupt flag | |
ei(); | |
while(1) { | |
ADCON0bits.GO = 1; | |
__delay_ms(500); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment