PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
adc_spidac_test.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 #include "pic24_all.h"
30 #include <stdio.h>
31 
32 /** \file
33  * Demonstrates reading the internal ADC in 12-bit mode and
34  * then sending the upper 8-bits to an external
35  * 8-bit SPI DAC (MAXIM 548A)
36  */
37 
38 #define CONFIG_SLAVE_ENABLE() CONFIG_RB3_AS_DIG_OUTPUT()
39 #define SLAVE_ENABLE() _LATB3 = 0 //low true assertion
40 #define SLAVE_DISABLE() _LATB3 = 1
41 
42 
43 void configSPI1(void) {
44  //spi clock = 40MHz/1*4 = 40MHz/4 = 10MHz
45  SPI1CON1 = SEC_PRESCAL_1_1 | //1:1 secondary prescale
46  PRI_PRESCAL_4_1 | //4:1 primary prescale
47  CLK_POL_ACTIVE_HIGH | //clock active high (CKP = 0)
48  SPI_CKE_ON | //out changes active to inactive (CKE=1)
49  SPI_MODE8_ON | //8-bit mode
50  MASTER_ENABLE_ON; //master mode
51 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
52  //nothing to do here. On this family, the SPI1 port uses dedicated
53  //pins for higher speed. The SPI2 port can be used with remappable pins.
54  //you may need to add code to disable analog functionality if the SPI ports
55  //are on analog-capable pins.
56 #else
57  CONFIG_SDO1_TO_RP(6); //use RP6 for SDO
58  CONFIG_SCK1OUT_TO_RP(7); //use RP7 for SCLK
59 #endif
60 
61  SPI1STATbits.SPIEN = 1; //enable SPI mode
62 }
63 void configDAC() {
64  CONFIG_SLAVE_ENABLE(); //chip select for DAC
65  SLAVE_DISABLE(); //disable the chip select
66 }
67 
68 void writeDAC (uint8_t dacval) {
69  SLAVE_ENABLE(); //assert Chipselect line to DAC
70  ioMasterSPI1(0b00001001); //control byte that enables DAC A
71  ioMasterSPI1(dacval); //write DAC value
72  SLAVE_DISABLE();
73 }
74 
75 #define VREF 3.3 //assume Vref = 3.3 volts
76 
77 int main (void) {
78  uint16_t u16_adcVal;
79  uint8_t u8_dacVal;
80  float f_adcVal;
81  float f_dacVal;
82 
83  configBasic(HELLO_MSG);
84  CONFIG_AN0_AS_ANALOG();
85  // Configure A/D to sample AN0 for 31 Tad periods in 12-bit mode
86  // then perform a single conversion.
87  configADC1_ManualCH0(ADC_CH0_POS_SAMPLEA_AN0, 31, 1);
88  configSPI1();
89  configDAC();
90  while (1) {
91  u16_adcVal = convertADC1(); //get ADC value
92  u8_dacVal = (u16_adcVal>>4) & 0x00FF; //upper 8 bits to DAC value
93  writeDAC(u8_dacVal);
94  f_adcVal = u16_adcVal;
95  f_adcVal = f_adcVal/4096.0 * VREF; //convert to float 0.0 to VREF
96  f_dacVal = u8_dacVal;
97  f_dacVal = f_dacVal/256.0 * VREF;
98 #ifdef SMALLRAM
99  {
100  uint16_t ui16_adcValmv, ui16_dacValmv;
101  ui16_adcValmv = f_adcVal * 1000;
102  ui16_dacValmv = f_dacVal * 1000;
103  printf("ADC in: %d mV (0x%04x), To DAC: %d mV (0x%02x) \n",
104  ui16_adcValmv, u16_adcVal, ui16_dacValmv, u8_dacVal);
105  }
106 #else
107  printf("ADC in: %4.3f V (0x%04x), To DAC: %4.3f V (0x%02x) \n",
108  (double) f_adcVal, u16_adcVal, (double) f_dacVal, u8_dacVal);
109 #endif
110  DELAY_MS(300); //delay so that we do not flood the UART.
111  } //end while(1)
112 
113 }