come faccio ad usare il convertire A/D? ho configurato il portA per analogici, setto RA1 il port che devo leggere ma non va! Ho collegato il positivo al piedino centrale del trimmer, e un piedino esterno del trimmer su RA1. è così che va?
un programma di questo dovrebbe fare sempre la lettura e mettere il dato letto sul portb?
ed invece a me restano i led del portb sempre accesi! cosa sbaglio? Aiuto pleaseee


Codice: Seleziona tutto
#include <pic.h>
#include "delay.h"
#define NumeroLedTotale 19 //quanti led ci sono in totale (19:da 0 a 18)
#define Timer1H 0xFF //settaggi per il timer1, per farlo scadere prima
#define Timer1L 0xFC //settaggi per il timer1, per farlo scadere prima
#define Tacq 50
unsigned short valoreRA0, valoreRA1, valoreRA2; //variabili dei valori letti dai trimmer esterni
//METODO SETTAGGI INIZIALI
void setupStartup(void){
//RA0: TRIMMER x la durata dei led allo stato ON.
//RA1: TRIMMER x la velocità tra un led e l'altro.
//RA2: TRIMMER x la velocità del FADE.
//RC2/CCP1: Segnale di interrupt esterno x far spegnere il circuito.
//RE0: Se è 1 indica che stai salendo, 0 stai scendendo.
//RE1: Interruttore: Se è 1 inverte la modalità di spegnimeno.
//RE2: Attiva o disattiva il FADE.
//RD0: (out) quando è 1 devi tenere alimentato il circuito, quando passa a 0 spegni tutto.
//SETUP CONVERTITORI A/D
//i registri ADCON0 e ADCON1 servono per impostare i convertitori
//ADCON1= 1 (ADMF: 1 allineamento a destra) 000 (non usati) 0010 (PGF: RA0 a RA5 analogiche, a RA2 Digitale)
ADCON1=0b10000010;
//ADCON0= 11 (clock) 000 (RA0) 0 (1 start conversion) 0 (non usato) 1 ( 1 per abilitare i convertitori)
//gli ingressi analogici possono essere: RA0, RA1, RA2, RA3, RA5, RE0, RE1 e RE2
// 000 = channel 0, (RA0/AN0)
// 001 = channel 1, (RA1/AN1)
// 010 = channel 2, (RA2/AN2)
// 011 = channel 3, (RA3/AN3)
// 100 = channel 4, (RA5/AN4)
// 101 = channel 5, (RE0/AN5)(1)
// 110 = channel 6, (RE1/AN6)(1)
// 111 = channel 7, (RE2/AN7)(1)
ADCON0=0b11000001; //configuro x il convertitore RA0.
//SETUP PORTx: Setta PORTx come in o out.
TRISA = 0b11111111; //setta il portA come input
TRISB = 0b00000000; //setta il portb come output
PORTB = 0b00000000; //azzera le uscite
TRISC = 0b00001111; //setta il portc come: RC0 a RC3 input, RC4 a RC7 come output. su RC2 c'è il segnale interrupt per spegnere tutto.
PORTC = 0b00000000; //azzera le uscite
TRISD = 0b00000000; //setta il portd come output
PORTD = 0b00000001; //azzera le uscite - RD0=1: tiene alimentato il circuito.
TRISE = 0b00001111; //setta il portE come input
//SETUP INTERRUPT:
//set up capture port interrupt
//CCP1CON= 00 (non usati) 00 (00 x capture mode o compare, x pwm è diverso) 0100 (Capture mode, every falling edge)
CCP1CON=0B00000100; //every falling edge of clock interrupts it
//cambio valori nel registro PIE1
//The PIR1 register contains the individual flag bits for the peripheral interrupts in PIE1
//The PIE1 register contains the individual enable bits for the peripheral interrupts.
CCP1IE=1; //enable capture port interrupt on data line -- Enables the CCP1 interrupt
//ADIE: A/D Converter Interrupt Enable bit
ADIE=0;
//TMR1IE=1; //Enables the TMR1 overflow interrupt
//TMR2IE=1; //Enables the TMR2 overflow interrupt
//INTCON Register is a readable and writable register,which contains various enable and flag bits for the
//INTCON= 1 (GIE Global interrupt enable) 1 (PEIE Peripheral Interrupt Enable) 0 (T0IE: TMR0 Overflow Interrupt Enable bit)
// 0 (INTE: RB0/INT External Interrupt Enable bit) 0 (RBIE: RB Port Change Interrupt Enable bit) 0 (T0IF: TMR0 Overflow Interrupt Flag bit)
// 0 (INTF: RB0/INT External Interrupt Flag bit) 0 (RBIF: RB Port Change Interrupt Flag bit)
INTCON = 0b11000000;
//SETUP TIMERx:
//T1CON: TIMER1 CONTROL REGISTER (ADDRESS 10h)
T1CON = 0b00110000; // Prescaler 1/8, TMR1CS:0 Internal clock (FOSC/4) , Timer OFF
TMR1IE = 1; // Interrupt ON PIE1 bit 0: TMR1IE: TMR1 Overflow Interrupt Enable bit
// gli dico di darmi un interrupt ad ogni overflow del timer1
TMR1IF = 0; // Overflow ON PIR1 bit 0: TMR1IF: TMR1 Overflow Interrupt Flag bit
T2CON = 0b00111010;
TMR2IE = 1; // Interrupt ON PIE1 bit 0: TMR1IE: TMR1 Overflow Interrupt Enable bit
// gli dico di darmi un interrupt ad ogni overflow del timer1
TMR2IF = 0; // Overflow ON PIR1 bit 0: TMR1IF: TMR1 Overflow Interrupt Flag bit
}
//METODO MAIN
int main(){
setupStartup(); //inizializza tutto, PORT, INTERRUPT, TIMER, VARIABILI.
while(1){ //ciclo infinito.
//lettura del trimmer situato su RA2: TRIMMER x la velocità del FADE.
RD0=0;
ADCON0=0b11010001; //Setto il convertitore da usare: RA2 xx010xxx
DelayUs(Tacq); //aspetto che il dato da acquisire sia pronto, condensatore caricato
ADGO=1; //faccio partire il convertitore
while(ADGO){} //attendo che la conversione sia finita
RD0=1;
//Leggere il risultato della conversione dei registri ADRESH e ADRESL;
//valoreRA2 = (ADRESL+(ADRESH<<8)); //assegno alla variabile valore il contenuto a 10bit -- variare da 0 a 1023
valoreRA2 = ADRESL; //assegno alla variabile valore il contenuto a 8bit -- variare da 0 a 256
ADIF=0; //Azzerare ADIF se necessario; (Questo bit si trova in PIR1 ed è il numero 6)
PORTB=valoreRA2;
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}