Also derzeit mit dem ADC auf meinem PIC16F917 herumspielen. Ich habe Einzeltastenfunktionen zum Laufen gebracht und alles funktioniert gut.
Wobei ich wirklich gerne helfen würde, ist, wie man die MCU dazu bringt, 2 Eingänge zu akzeptieren, ohne dass der zweite ein ISR ist.
Wie kann ich diesen Code ändern, um ihn so zu machen, wenn AN1 > 512 geht, PORTDbits.RD1 neben AN0 und RD0 hoch geht. Funktionieren wie zwei getrennte Systeme, arbeiten aber gleichzeitig. Ist es überhaupt möglich?
Nochmals vielen Dank, Esra
#include <xc.h>
#include "config-bits.h"
#define _XTAL_FREQ 4000000
void delay()
{
int i;
for(i=0; i<5000; i++)
{
/*Timer Stuff*/
}
}
int main()
{
TRISD=1; //Set all D pins to input
TRISDbits.TRISD0 = 0; //LED output
ANSELbits.ANS0 = 1; //Select ADC input
ADCON0bits.ADFM = 1; //ADC result is right justified
ADCON0bits.VCFG = 0; //Vdd is the +ve reference
ADCON1bits.ADCS = 0b001; //Fosc/8 is the conversion clock
//This is selected because the conversion
//clock period (Tad) must be greater than 1.5us.
//With a Fosc of 4MHz, Fosc/8 results in a Tad
//of 2us.
ADCON0bits.CHS = 0; //select analog input, AN2
ADCON0bits.ADON = 1; //Turn on the ADC
int result;
while(1)
{
__delay_us(5); //Wait the acquisition time (about 5us).
ADCON0bits.GO = 1; //start the conversion
while(ADCON0bits.GO==1){}; //wait for the conversion to end
result = (ADRESH<<8)+ADRESL; //combine the 10 bits of the conversion
if(result > 512)
{
PORTDbits.RD0 = 1;
delay();
PORTDbits.RD0 = 0;
delay();
}
else
{
PORTDbits.RD0 = 0;
}
}
}
Mit diesem PIC ist keine echte Gleichzeitigkeit möglich, da es nur ein Sample-and-Hold und einen Wandler gibt. Wenn fast gut genug ist, würde es funktionieren, Ihren Code so anzupassen, dass er einfach zwischen den Eingaben wechselt. Wenn nicht, dann wäre ein PIC mit mehr als einem ADC/Sample and Holds der richtige Weg, zB ein dsPIC wie der dsPIC33FJ128GP802 . Oder Sie könnten natürlich einen externen ADC hinzufügen.
esra_vdj
Oli Glaser
esra_vdj
Oli Glaser