Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Микропроцессорная техника Сторожок / CE135_SPI_Demo_14aug09 / CE135_SPI_Demo / adcDrv1
.c/**********************************************************************
*
*
* SOFTWARE LICENSE AGREEMENT:
* Microchip Technology Incorporated ("Microchip") retains all ownership and
* intellectual property rights in the code accompanying this message and in all
* derivatives hereto. You may use this code, and any derivatives created by
* any person or entity by or on your behalf, exclusively with Microchip's
* proprietary products. Your acceptance and/or use of this code constitutes
* agreement to the terms and conditions of this notice.
*
* CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS". NO
* WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIP'S
* PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
*
* YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER
* IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY),
* STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL,
* PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF
* ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN
* ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
* ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO
* THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO
* HAVE THIS CODE DEVELOPED.
*
* You agree that you are solely responsible for testing the code and
* determining its suitability. Microchip has no obligation to modify, test,
* certify, or support the code.
*
* REVISION HISTORY:
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Author Date Comments on this revision
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~**
*
**********************************************************************/
#include "p33fxxxx.h"
#include "adcDrv1.h"
#include "commonvars.h"
volatile unsigned int ADC_Result;
//volatile unsigned char adc_lcd_update;
/*=============================================================================
ADC INITIALIZATION FOR CHANNEL SCAN
=============================================================================*/
void initAdc1( void )
{
/* set port configuration here */
AD1PCFGLbits.PCFG4 = 0; // ensure AN4/RB4 is analog (Temp Sensor)
AD1PCFGLbits.PCFG5 = 0; // ensure AN5/RB5 is analog (Analog Pot)
AD1PCFGLbits.PCFG8 = 0; // ensure AN8/RB8 is analog (PGA output)
/* set channel scanning here, auto sampling and convert,
with default read-format mode */
AD1CON1 = 0x00E4;
/* select 10 or 12-bit, 1 channel ADC operation */
AD1CON1bits.AD12B = 1; // uncomment this, comment below to make a 12 bit ADC
// AD1CON1bits.AD12B = 0; // uncomment this, comment above to make a 10 bit ADC
/* No channel scan for CH0+, Use MUX A,
SMPI = 16 per interrupt, Vref = AVdd/AVss */
AD1CON2 = 0x003C;
/* Set Samples and bit conversion time */
// AD1CON3 = 0x032F; // lets try to speed up conversions
AD1CON3 = 0x0404;
/* set channel scanning here for AN4 and AN5 */
AD1CSSL = 0x0000;
/* channel select AN5 */
AD1CHS0 = 0x0005;
/* reset ADC interrupt flag */
IFS0bits.AD1IF = 0;
/* enable ADC interrupts, disable this interrupt if the DMA is enabled */
IEC0bits.AD1IE = 1;
/* turn on ADC module */
AD1CON1bits.ADON = 1;
}
/*=============================================================================
Timer1 Initialization
=============================================================================*/
void initTmr1() {
TMR1 = 0;
PR1 = 0xFFFF;
T1CONbits.TCKPS = 3;
T1CONbits.TON = 1;
IEC0bits.T1IE = 1;
}
/*=============================================================================
PortA Initialization
=============================================================================*/
void initPortA() {
TRISA = 0xFF00;
TRISD = 0xFFFF;
TRISBbits.TRISB2 = 0; // just make this an output
PORTBbits.RB2 = 1; // make it hi to spoof CS
}
/*=============================================================================
SPI1 INTERRUPT SERVICE ROUTINE (clk polarity is high, Master)
=============================================================================*/
void Init_SPI1() { // since sending only, no need for INTS.
SPI1CON1 = 0x057A; // 0000 0000 0111 1111
SPI1STATbits.SPIROV = 0; // Clear Overflow bit
SPI1STATbits.SPIEN = 1; // Enable the peripheral
}
/*=============================================================================
ADC INTERRUPT SERVICE ROUTINE
=============================================================================*/
void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void) {
// I want to stick the ADC result into a register for use later.
ADC_Result = ADC1BUF0;
IFS0bits.AD1IF = 0; //Clear the DMA0 Interrupt Flag
}