00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 #include "pic24_all.h" 00030 #include <stdio.h> 00031 00038 #define CONFIG_SLAVE_ENABLE() CONFIG_RB3_AS_DIG_OUTPUT() 00039 #define SLAVE_ENABLE() _LATB3 = 0 //low true assertion 00040 #define SLAVE_DISABLE() _LATB3 = 1 00041 00042 00043 void configSPI1(void) { 00044 //spi clock = 40MHz/1*4 = 40MHz/4 = 10MHz 00045 SPI1CON1 = SEC_PRESCAL_1_1 | //1:1 secondary prescale 00046 PRI_PRESCAL_4_1 | //4:1 primary prescale 00047 CLK_POL_ACTIVE_HIGH | //clock active high (CKP = 0) 00048 SPI_CKE_ON | //out changes active to inactive (CKE=1) 00049 SPI_MODE8_ON | //8-bit mode 00050 MASTER_ENABLE_ON; //master mode 00051 //configure pins. Only need SDO, SCLK since POT is output only 00052 CONFIG_SDO1_TO_RP(6); //use RP6 for SDO 00053 CONFIG_SCK1OUT_TO_RP(7); //use RP7 for SCLK 00054 00055 SPI1STATbits.SPIEN = 1; //enable SPI mode 00056 } 00057 void configDAC() { 00058 CONFIG_SLAVE_ENABLE(); //chip select for DAC 00059 SLAVE_DISABLE(); //disable the chip select 00060 } 00061 00062 void writeDAC (uint8 dacval) { 00063 SLAVE_ENABLE(); //assert Chipselect line to DAC 00064 ioMasterSPI1(0b00001001); //control byte that enables DAC A 00065 ioMasterSPI1(dacval); //write DAC value 00066 SLAVE_DISABLE(); 00067 } 00068 00069 #define VREF 3.3 //assume Vref = 3.3 volts 00070 00071 int main (void) { 00072 uint16 u16_adcVal; 00073 uint8 u8_dacVal; 00074 float f_adcVal; 00075 float f_dacVal; 00076 00077 configBasic(HELLO_MSG); 00078 CONFIG_AN0_AS_ANALOG(); 00079 // Configure A/D to sample AN0 for 31 Tad periods in 12-bit mode 00080 // then perform a single conversion. 00081 configADC1_ManualCH0(ADC_CH0_POS_SAMPLEA_AN0, 31, 1); 00082 configSPI1(); 00083 configDAC(); 00084 while (1) { 00085 u16_adcVal = convertADC1(); //get ADC value 00086 u8_dacVal = (u16_adcVal>>4) & 0x00FF; //upper 8 bits to DAC value 00087 writeDAC(u8_dacVal); 00088 f_adcVal = u16_adcVal; 00089 f_adcVal = f_adcVal/4096.0 * VREF; //convert to float 0.0 to VREF 00090 f_dacVal = u8_dacVal; 00091 f_dacVal = f_dacVal/256.0 * VREF; 00092 printf("ADC in: %4.3f V (0x%04x), To DAC: %4.3f V (0x%02x) \n", 00093 (double) f_adcVal, u16_adcVal, (double) f_dacVal, u8_dacVal); 00094 DELAY_MS(300); //delay so that we do not flood the UART. 00095 } //end while(1) 00096 00097 }