void InitCLK (void)
{
__builtin_write_OSCCONH(0x02);
__builtin_write_OSCCONL(0x1);
while (OSCCONbits.COSC != 2);
return;
} //End of clock initialization
getADC();
if (ADC6value >= 500)
PORTBbits.RB4 = 1;
else
PORTBbits.RB4 = 0;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 void getADC (void) { Delayms(10); ADCON1bits.SAMP = 0; // Start Conversion while(!ADCON1bits.DONE); // Wait for convert ADC1value = ADCBUF0; ADC2value = ADCBUF1; ADC3value = ADCBUF2; ADC4value = ADCBUF3; ADCON1bits.DONE = 0; return; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 void enableADC (void) { ADCON1bits.ADON = 0; //ADC Module is off ADCON3bits.ADCS = 2; // TAD = TCY*(ADCS + 1); Tcy = 25ns; Min TAD = 75ns ADCON1bits.FORM = 0; // Integer ADCON1bits.ADSIDL = 0; //Discontinue module in idle mode ADCON1bits.SSRC = 7; // Automatic Conversion Sequence ADCON1bits.SIMSAM = 1; //Samples multiple channels in sequence ADCON1bits.ASAM = 1; // Auto sampeling, after last sample ADCON2bits.VCFG = 0; // VrefH = AVdd / VrefL = AVss ADCON2bits.CSCNA = 1; //Scan inputs CH0+ ADCON2bits.CHPS = 3; // Channel CH0 CH1 CH2 conversance ADCON2bits.SMPI = 0; // Increments the DMA address after completion of every sample/conversion operation ADCON2bits.ALTS = 0; //Always use MUX A input multiplexer ADCHSbits.CH0SA = 0; ADCHSbits.CH123SA = 0; ADCON3bits.ADRC = 0; // ADC Clock Derived from System Clock ADCON3bits.SAMC = 1; // Auto Sample Time bits TAD ADCSSLbits.CSSL0 = 1; //sCAN INPUT PINS ADCSSLbits.CSSL1 = 1; //sCAN INPUT PINS ADCSSLbits.CSSL2 = 1; //sCAN INPUT PINS ADCSSLbits.CSSL3 = 1; //sCAN INPUT PINS ADCON1bits.ADON = 1; // Turn ADC on ADCON1bits.SAMP = 1; //Start sampeling return; }
void __attribute__((__interrupt__)) _ADCInterrupt(void)
{
iAdPlace++;
iAdPlace&=255;
unsigned int ADCVoltage2,ADCVoltage;
int i = 0;
// GraphicsHaveChanged=1;
unsigned int iPos =0;
unsigned int* iBuff ;
iBuff = & ADCBUF0;
for(iPos=0;iPos<SamplesPerInt;iPos++)
{
ADCVoltage = (ADCVoltage >>1) & 0xff;
ADCVoltage += 0xff & (((unsigned int)(*(iBuff+(iPos<<1))))>>2);
}
// GraphicsSpace[iAdPlace][0]= iNT3++; //~(BYTE) ADCVoltage ;
IFS0bits.ADIF=0;
return;
}
static void SetupADC()
{
return;
if(0)
{
//return;
ADPCFG=0b1111111111011; // Only AN12 in use.
TRISBbits.TRISB2=1; // Definately Input!!! // should be ok already but........
// 0b11100100 // Off, Auto sample start, auto-convert
ADCON1 = 0b0010000001000100 ; // Lots of stuff
ADCON1bits.FORM1 = 0x0;
ADCON1bits.FORM0 = 0x0;
ADCON2 = 0; // AVdd, AVss, int every conversion, MUXA only
ADCON2bits.BUFM=0;
ADCON3 = 0b1111111111111111; // 31 Tad auto-sample, Tad = Sysclock;
ADCON3bits.SAMC=0x0f; // Sample for 15 Clocks
ADCON3bits.ADCS=0x3f; // use 64 clocks to measure
ADCON3bits.ADRC=0;
ADCON2bits.SMPI=SamplesPerInt;
ADCSSL = 0x0000;
ADCHSbits.CH0NA=1;
ADCHSbits.CH0SA=0;
//AD1CHS=0xCC;
ADCON1bits.SAMP=0;
// ADCON2bits.CSCNA=1;
ADCON1bits.ASAM=1;
ADCON1bits.ADON=1;
}
ADPCFG = 0xFFFF ^ 8; //2nd channel is sampled and coverted
ADCON1 = 0x0040; //ADC off, output_format=INTEGER
//Timer 3 starts convesion
ADCON1bits.SSRC = 0b111;
ADCHS = 0x0002; //Connect RB2 on AN2 as CH0 input
ADCSSL = 0; //No scan
ADCON3 = 0x0F00; //TAD = internalTCY
//Sample time ends with timer 3 clock
IFS0bits.ADIF=0;
IEC0bits.ADIE=1;
ADCON1bits.ASAM = 1;
ADCON1bits.ADON=1;
ADCHSbits.CH0SA=1;
}
static void SetupTimer3()
{
T2CONbits.T32 = 0; // Use t2 as a separate 16bit timer, 16bits for t3,
// T3CONbits.T32 = 0; // Use t2 as a separate 16bit timer, 16bits for t3,
T3CONbits.TCKPS=0b11; // pre-scaler, only tick once per 4 "clocks"
T3CONbits.TCS=0; // internal clock
T3CONbits.TON=1;
IEC0bits.T3IE=1;
IFS0bits.T3IF=0;
PR3 = 0x0080;
}
static void SetupTimer2() // Screen update timer....
{
T2CONbits.T32 = 0; // Use t2 as a separate 16bit timer, 16bits for t3,
// T3CONbits.T32 = 0; // Use t2 as a separate 16bit timer, 16bits for t3,
T2CONbits.TCKPS=0b11; // pre-scaler, only tick once per 4 "clocks"
T2CONbits.TCS=0; // internal clock
T2CONbits.TON=1;
IEC0bits.T2IE=1;
IFS0bits.T2IF=0;
PR2 = 0x400;
}
void enableADC(void) {
ADCON1bits.ADON = 0; //ADC Module is off
ADCON3bits.ADCS = 2; // TAD = TCY*(ADCS + 1); Tcy = 25ns; Min TAD = 75ns
ADCON1bits.FORM = 0; // Integer
ADCON1bits.ADSIDL = 0; //Discontinue module in idle mode
ADCON1bits.SSRC = 7; // Automatic Conversion Sequence
ADCON1bits.SIMSAM = 1; //Samples multiple channels in sequence
ADCON1bits.ASAM = 0; // Auto sampeling, after last sample
ADCON2bits.VCFG = 0; // VrefH = AVdd / VrefL = AVss
ADCON2bits.CSCNA = 0; // Scan inputs CH0+
ADCON2bits.CHPS = 2; // Channel CH0 CH1 CH2 conversance
ADCON2bits.SMPI = 0; // Increments the DMA address after completion of every sample/conversion operation
ADCON2bits.ALTS = 0; // Always use MUX A input multiplexer
ADCON2bits.BUFM = 0;
ADCHSbits.CH0SA = 3; //Channel 0 Positive Input Select for MUX A Multiplexer Setting bits
ADCHSbits.CH123SA = 0; //Channel 1, 2, 3 Positive Input Select for MUX A Multiplexer Setting bit
ADCHSbits.CH0NA = 0; //Channel 0 Negative Input Select for MUX A Multiplexer Setting bit
ADCHSbits.CH123NA = 0; //Channel 1, 2, 3 Negative Input Select for MUX A Multiplexer Setting bits
ADCON3bits.ADRC = 0; // ADC Clock Derived from System Clock
ADCON3bits.SAMC = 1; // Auto Sample Time bits TAD
ADCON1bits.ADON = 1; // Turn ADC on
ADCON1bits.SAMP = 1; //Start sampeling
return;
}
ADCON1bits.DONE = 0;
ADCON1bits.SAMP = 1; // Start Conversion
while (!ADCON1bits.DONE); // Wait for convert
Nop();
void __attribute__((__interrupt__,psv_auto)) _ADC1Interrupt(void)
{
int i = 0;
unsigned int iPos =0;
unsigned int* iBuff ;
iBuff = &ADC1BUF0;
for(iPos=0;iPos<=SamplesPerInt;iPos++)
{
ADCVoltage = (ADCVoltage >>1) & 0xff;
ADCVoltage = 0xffff & (((unsigned int)(*(iBuff+iPos)))>>1);
}
IFS0bits.AD1IF=0;
IFS0bits.AD1IF=0;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?