Hello everyone.
Im trying to use the ADC module of the dsPIC33ep512mu810, reading the potentiometer of the Explorar 16 board, and show the binary conversion in PORT A (LEDs). Up to now I did not have success and I don't know whats missing or wrong. Could someone help please?
//DEFINES or PARAMETERS
#define FOSC 8000000 //OSCILLATION FREQUENCY
#define PLL 1 //SETS PLL
#define FCY (FOSC*PLL/2) //CYCLE FREQUENCY
#include <stdio.h>
#include <stdlib.h>
#include <p33ep512mu810.h>
//MACROS or CONFIGURATION BITS
_FOSCSEL(FNOSC_PRI & IESO_OFF)
// Primary oscillator as source
// Start up with user-selected oscillator source
_FOSC(FCKSM_CSDCMD & POSCMD_XT & OSCIOFNC_ON)
// Both Clock switching and Fail-safe Clock Monitor are disabled
// XT Crystal Oscillator Mode
// OSC2 is general purpose digital I/O pin
_FWDT(FWDTEN_OFF)
// Watchdog timer enabled/disabled by user software
_FPOR(FPWRT_PWR16 & BOREN_ON)
// Power-on Reset Timer Value and bor enabled
_FGS(GWRP_OFF & GSS_OFF & GSSK_OFF)
// General Segment may be written
// General Segment Code protect is disabled
// General Segment Write Protection and Code Protection is Disabled
// FUNCTION DECLARATIONS
void adc(void);
void delay_ms(long ms);
/*
*
*/
int main() {
TRISA = 0;
adc();
//loop to sample and acquire data
while(1){
AD1CON1bits.SAMP = 1; //sampling
delay_ms(10); //sample time
AD1CON1bits.SAMP = 0; //holding
while(!AD1CON1bits.DONE); //is the conversion done?
PORTA = ADC1BUF1; //data is shown in port A (LEDs)
}
return (EXIT_SUCCESS);
}
void adc(void){
ANSELA = ANSELB = ANSELC = ANSELD = ANSELE = ANSELG = 0x0000;
// Set ports A, B, C, D, E, G as I/O digital ports.
ANSELBbits.ANSB5 = 1; //makes AN5/RB5 analog input.
AD1CON1bits.AD12B = 1; // Sets 12 bits ADC.
AD1CON1 = 0x0000;
AD1CON2 = 0x0000;
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CHS0 = 0x0005;
AD1CHS123 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CON1bits.ADON = 1; //enables ADC
delay_ms(10);
}
void delay_ms(long ms){
T1CON = 0; //reset timer
TMR1 = 0; //load timer count register to 0
IPC0bits.T1IP = 1; // interrupt priority count
PR1 = ((FCY/256)*ms)/1000; //20ms delay. Comes from (((Fosc/4)/prescaler)*20ms)/(1000ms)
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 0;
T1CON = 0X8030; //hexadecimal of 1000000000110000
// which means start the timer and select 256 prescaler
while(IFS0bits.T1IF == 0);
}
Im trying to use the ADC module of the dsPIC33ep512mu810, reading the potentiometer of the Explorar 16 board, and show the binary conversion in PORT A (LEDs). Up to now I did not have success and I don't know whats missing or wrong. Could someone help please?
//DEFINES or PARAMETERS
#define FOSC 8000000 //OSCILLATION FREQUENCY
#define PLL 1 //SETS PLL
#define FCY (FOSC*PLL/2) //CYCLE FREQUENCY
#include <stdio.h>
#include <stdlib.h>
#include <p33ep512mu810.h>
//MACROS or CONFIGURATION BITS
_FOSCSEL(FNOSC_PRI & IESO_OFF)
// Primary oscillator as source
// Start up with user-selected oscillator source
_FOSC(FCKSM_CSDCMD & POSCMD_XT & OSCIOFNC_ON)
// Both Clock switching and Fail-safe Clock Monitor are disabled
// XT Crystal Oscillator Mode
// OSC2 is general purpose digital I/O pin
_FWDT(FWDTEN_OFF)
// Watchdog timer enabled/disabled by user software
_FPOR(FPWRT_PWR16 & BOREN_ON)
// Power-on Reset Timer Value and bor enabled
_FGS(GWRP_OFF & GSS_OFF & GSSK_OFF)
// General Segment may be written
// General Segment Code protect is disabled
// General Segment Write Protection and Code Protection is Disabled
// FUNCTION DECLARATIONS
void adc(void);
void delay_ms(long ms);
/*
*
*/
int main() {
TRISA = 0;
adc();
//loop to sample and acquire data
while(1){
AD1CON1bits.SAMP = 1; //sampling
delay_ms(10); //sample time
AD1CON1bits.SAMP = 0; //holding
while(!AD1CON1bits.DONE); //is the conversion done?
PORTA = ADC1BUF1; //data is shown in port A (LEDs)
}
return (EXIT_SUCCESS);
}
void adc(void){
ANSELA = ANSELB = ANSELC = ANSELD = ANSELE = ANSELG = 0x0000;
// Set ports A, B, C, D, E, G as I/O digital ports.
ANSELBbits.ANSB5 = 1; //makes AN5/RB5 analog input.
AD1CON1bits.AD12B = 1; // Sets 12 bits ADC.
AD1CON1 = 0x0000;
AD1CON2 = 0x0000;
AD1CON3 = 0x000F;
AD1CON4 = 0x0000;
AD1CHS0 = 0x0005;
AD1CHS123 = 0x0000;
AD1CSSH = 0x0000;
AD1CSSL = 0x0000;
AD1CON1bits.ADON = 1; //enables ADC
delay_ms(10);
}
void delay_ms(long ms){
T1CON = 0; //reset timer
TMR1 = 0; //load timer count register to 0
IPC0bits.T1IP = 1; // interrupt priority count
PR1 = ((FCY/256)*ms)/1000; //20ms delay. Comes from (((Fosc/4)/prescaler)*20ms)/(1000ms)
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 0;
T1CON = 0X8030; //hexadecimal of 1000000000110000
// which means start the timer and select 256 prescaler
while(IFS0bits.T1IF == 0);
}