Bonjour,
J'essaye désespérément d'utiliser les entrées analogiques d'un pic24f04ka200.... mais sans succès : la valeur stockée dans le buffer ADC1BUF0 reste à 0.
Je vous joint le code ci-dessous. Il a pour but de faire varier la période d'un signal carré en fonction d'une tension analogique en entrée (c'est juste un code pour me familiariser avec le pic). Quelqu'un voit-il où est l'erreur ?
Par ailleurs, je n'arrive pas à utiliser le simulateur de MPLABX pour simuler une entrée analogique (dans la fenêtre I/O pins, le pin en question reste en digital input).
Merci d'avance de m'aider dans la résolution de ce problème.
Voilà le code :
Code:#include <xc.h> // FGS #pragma config GWRP = OFF // General Segment Code Flash Write Protection bit (General segment may be written) #pragma config GCP = OFF // General Segment Code Flash Code Protection bit (No protection) // FOSCSEL #pragma config FNOSC = FRC // Oscillator Select (Fast RC oscillator (FRC)) #pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-Speed Start-up disabled)) // FOSC #pragma config POSCMOD = NONE // Primary Oscillator Configuration bits (Primary oscillator disabled) #pragma config OSCIOFNC = ON // CLKO Enable Configuration bit (CLKO output signal active on the OSCO pin; primary oscillator must be disabled or configured for the External Clock mode (EC) for the CLKO to be active (POSCMD<1:0>)) #pragma config POSCFREQ = HS // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency greater than 8 MHz) #pragma config SOSCSEL = SOSCHP // SOSC Power Selection Configuration bits (Secondary oscillator configured for high-power operation) #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock switching is disabled, Fail-Safe Clock Monitor is disabled) // FWDT #pragma config WDTPS = PS32768 // Watchdog Timer Postscale Select bits (1:32,768) #pragma config FWPSA = PR128 // WDT Prescaler (WDT prescaler ratio of 1:128) #pragma config WINDIS = OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected; windowed WDT disabled) #pragma config FWDTEN = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit)) // FPOR #pragma config BOREN = BOR0 // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware; SBOREN bit disabled) #pragma config PWRTEN = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config BORV = 0b10 // Brown-out Reset Voltage bits #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RA5 input pin disabled) // FICD #pragma config ICS = PGx2 // Reserved (PGC2/PGD2 are used for programming the device) // FDS #pragma config DSWDTPS = DSWDTPSF // Deep Sleep Watchdog Timer Postscale Select bits (1:2,147,483,648 (25.7 Days)) #pragma config DSLPBOR = OFF // Deep Sleep Zero-Power BOR Enable bit (Deep Sleep BOR disabled in Deep Sleep) #pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable bit (DSWDT disabled) void main(void) { PORTA=0; PORTB=0; TRISA=0; TRISB=0; TRISBbits.TRISB4=1; T1CON=0b1000000000110000; AD1PCFG=0b1111111111111111; AD1PCFGbits.PCFG2=0; //AD1CON1 AD1CON1bits.ADSIDL = 0; //continue in idle AD1CON1bits.FORM = 0; //integer output AD1CON1bits.SSRC = 0b111; //auto convert AD1CON1bits.ASAM = 1; //auto start sampling AD1CON1bits.SAMP = 0; //status bit - no meaning here AD1CON1bits.DONE = 0; //status bit - no meaning here //AD1CON2 AD1CON2bits.VCFG = 0; //AVDD & AVSS AD1CON2bits.CSCNA = 0; // AD1CON2bits.BUFS = 0; //status bit - no meaning here AD1CON2bits.SMPI = 0; // AD1CON2bits.BUFM = 0; //buffer is one 16 word buffer AD1CON2bits.ALTS = 0; //always use MUX A //AD1CON3 AD1CON3bits.ADRC = 0; AD1CON3bits.SAMC = 0b11111; //31 TAD AD1CON3bits.ADCS = 0b101; //5 * TCY AD1CSSL = 0; //No scanned inputs AD1CON1bits.ADON=1; int a=0; while(1) { while (!AD1CON1bits.DONE); a=ADC1BUF0; TMR1=0; while(TMR1<=a+2) { PORTAbits.RA2=1; } TMR1=0; while(TMR1<=2+a) { PORTAbits.RA2=0; } } return; }
-----