00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pic24_all.h"
00031 #include "stdio.h"
00032
00033
00034 #define USE_12BIT_ADC
00035
00036 #ifdef USE_12BIT_ADC
00037 #define ADC_LEN 12
00038 #define ADC_NSTEPS 4096
00039 #define ADC_12BIT_FLAG 1
00040 #else
00041 #define ADC_LEN 10
00042 #define ADC_NSTEPS 1024
00043 #define ADC_12BIT_FLAG 0
00044 #endif
00045
00046
00047 void configDAC(void);
00048 void writeDAC(uint16 u16_x, uint16 u16_y);
00049
00050 volatile uint8 u8_per, u8_amp;
00051 volatile uint16 u16_per;
00052
00053 const uint8 au8_sinetbl[] = {127,133,139,146,152,158,164,170,176,181, \
00054 187,192,198,203,208,212,217,221,225,229,233,236,239,242,244,247,249,250, \
00055 252,253,253,254,254,254,253,253,252,250,249,247,244,242,239,236,233,229, \
00056 225,221,217,212,208,203,198,192,187,181,176,170,164,158,152,146,139,133, \
00057 127,121,115,108,102,96,90,84,78,73,67,62,56,51,46,42,37,33,29,25,21,18, \
00058 15,12,10,7,5,4,2,1,1,0,0,0,1,1,2,4,5,7,10,12,15,18,21,25,29,33,37,42,46, \
00059 51,56,62,67,73,78,84,90,96,102,108,115,121
00060 };
00061
00062
00066
00067
00068 #define __DAC_MAX5353
00069
00070
00071
00072
00073 #ifdef __DAC_R2R
00074 #warning "DAC_R2R.C built for explicit R-2R DAC connected to RB9(MSb)-RB2(LSb)"
00075 void configDAC() {
00076 CONFIG_RB2_AS_DIG_OUTPUT();
00077 CONFIG_RB3_AS_DIG_OUTPUT();
00078 CONFIG_RB4_AS_DIG_OUTPUT();
00079 CONFIG_RB5_AS_DIG_OUTPUT();
00080 CONFIG_RB6_AS_DIG_OUTPUT();
00081 CONFIG_RB7_AS_DIG_OUTPUT();
00082 CONFIG_RB8_AS_DIG_OUTPUT();
00083 CONFIG_RB9_AS_DIG_OUTPUT();
00084 }
00085
00086 void writeDAC(uint16 u16_x, uint16 u16_y) {
00087 uint16 u16_temp;
00088
00089 u16_temp = LATB & 0xFC03;
00090 u16_temp |= ((u16_x & 0xFF00) >> 6);
00091 LATB = u16_temp;
00092 }
00093 #endif
00094
00095 #ifdef __DAC_MAX548A
00096 #warning "DAC_R2R.C built for SPI-based dual 8-bit MAX548A DAC connected to RP14(SDO) and RP13(SCLK)"
00097 #define CONFIG_MAX548A_ENABLE() CONFIG_RA2_AS_DIG_OUTPUT()
00098 #define MAX548A_ENABLE() _LATA2 = 0
00099 #define MAX548A_DISABLE() _LATA2 = 1
00100
00101 void configDAC(void) {
00102
00103 SPI1CON1 = SEC_PRESCAL_1_1 |
00104 PRI_PRESCAL_4_1 |
00105 CLK_POL_ACTIVE_HIGH |
00106 SPI_CKE_ON |
00107 SPI_MODE8_ON |
00108 MASTER_ENABLE_ON;
00109
00110
00111 CONFIG_RB14_AS_DIG_OUTPUT();
00112 CONFIG_SDO1_TO_RP(14);
00113 CONFIG_RB13_AS_DIG_OUTPUT();
00114 CONFIG_SCK1OUT_TO_RP(13);
00115 SPI1STATbits.SPIEN = 1;
00116 CONFIG_MAX548A_ENABLE();
00117 MAX548A_DISABLE();
00118 }
00119
00120 void writeDAC(uint16 u16_x, uint16 u16_y) {
00121
00122
00123
00124
00125
00126
00127
00128 MAX548A_ENABLE();
00129 ioMasterSPI1(0x02);
00130 ioMasterSPI1(u16_y>>8);
00131 MAX548A_DISABLE();
00132 MAX548A_ENABLE();
00133 ioMasterSPI1(0x09);
00134 ioMasterSPI1(u16_x>>8);
00135 MAX548A_DISABLE();
00136 }
00137 #endif
00138
00139 #ifdef __DAC_MAX5353
00140 #warning "DAC_R2R.C built for SPI-based 12-bit MAX5353 DAC connected to RP14(SDO) and RP13(SCLK)"
00141 #define CONFIG_MAX5353_ENABLE() CONFIG_RA3_AS_DIG_OUTPUT()
00142 #define MAX5353_CMD_ANDMASK 0x1FFE
00143 #define MAX5353_ENABLE() _LATA3 = 0
00144 #define MAX5353_DISABLE() _LATA3 = 1
00145
00146 void configDAC(void) {
00147
00148 SPI1CON1 = SEC_PRESCAL_1_1 |
00149 PRI_PRESCAL_4_1 |
00150 CLK_POL_ACTIVE_HIGH |
00151 SPI_CKE_ON |
00152 SPI_MODE16_ON |
00153 MASTER_ENABLE_ON;
00154
00155
00156 CONFIG_RB14_AS_DIG_OUTPUT();
00157 CONFIG_SDO1_TO_RP(14);
00158 CONFIG_RB13_AS_DIG_OUTPUT();
00159 CONFIG_SCK1OUT_TO_RP(13);
00160 SPI1STATbits.SPIEN = 1;
00161 CONFIG_MAX5353_ENABLE();
00162 MAX5353_DISABLE();
00163 }
00164
00165 void writeDAC(uint16 u16_x, uint16 u16_y) {
00166 MAX5353_ENABLE();
00167
00168 ioMasterSPI1((u16_x>>3) & MAX5353_CMD_ANDMASK);
00169 MAX5353_DISABLE();
00170 }
00171 #endif
00172
00173 #ifdef __DAC_MAX518
00174 #warning "DAC_R2R.C built for I2C-based single output 8-bit MAX518 DAC connected to RB9(SDA) and RB8(SCL)"
00175 #define MAX518_I2C_ADDR 0x58 // see MAX518 datasheet 19-0393 Figure 6
00176 #define MAX518_WRITE_DACA 0x00 // see MAX518 datasheet 19-0393 Figure 7
00177 #define MAX518_WRITE_DACB 0x01 // see MAX518 datasheet 19-0393 Figure 7
00178
00179 void configDAC(void) {
00180 configI2C1(400);
00181 }
00182
00183 void writeDAC(uint16 u16_x, uint16 u16_y) {
00184 write2I2C1(MAX518_I2C_ADDR, MAX518_WRITE_DACA, (uint8) (u16_x>>8) );
00185 }
00186 #endif
00187
00188 #ifdef __DAC_MAX518_DUAL
00189 #warning "DAC_R2R.C built for I2C-based DUAL output 8-bit MAX518 DAC connected to RB9(SDA) and RB8(SCL)"
00190 #warning "In this example, I2C SCL is beyond specification for the MAX518 DAC."
00191 #define MAX518_I2C_ADDR 0x58 // see MAX518 datasheet 19-0393 Figure 6
00192 #define MAX518_WRITE_DACA 0x00 // see MAX518 datasheet 19-0393 Figure 7
00193 #define MAX518_WRITE_DACB 0x01 // see MAX518 datasheet 19-0393 Figure 7
00194
00195 void configDAC(void) {
00196 configI2C1(700);
00197 }
00198
00199 void writeDAC(uint16 u16_x, uint16 u16_y) {
00200 static uint8 au8_buf[]={MAX518_WRITE_DACA, 0, MAX518_WRITE_DACB, 0 };
00201
00202 au8_buf[1] = (uint8) (u16_x>>8);
00203 au8_buf[3] = (uint8) (u16_y>>8);
00204 writeNI2C1(MAX518_I2C_ADDR, &au8_buf[0], 4);
00205
00206 }
00207 #endif
00208
00209
00210
00211 void _ISR _T3Interrupt (void) {
00212 static uint8 u8_idx;
00213 static uint16 u16_idx, u16_old;
00214 static uint16 u16_val;
00215
00216 writeDAC(u16_val, u16_idx);
00217
00218
00219 u16_idx+=u16_per;
00220 u8_idx = (uint8) (u16_idx>>9 );
00221 u16_val = ((uint16)au8_sinetbl[u8_idx])<<8;
00222 u16_val >>= u8_amp;
00223
00224 if ((u16_idx^u16_old)&0x8000) _LATB12 = !_LATB12;
00225 u16_old = u16_idx;
00226
00227 _T3IF = 0;
00228 }
00229
00230 void configTimer3(void){
00231
00232 T3CONbits.TON = 0;
00233 T3CON = T3_PS_1_1 | T3_SOURCE_INT;
00234 TMR3 = 0;
00235 PR3 = usToU16Ticks(50, getTimerPrescale(T3CONbits)) - 1;
00236 _T3IP=7;
00237 _T3IF=0;
00238 _T3IE=1;
00239 T3CONbits.TON = 1;
00240 }
00241
00242
00248 int main (void) {
00249 uint8 u8_uiCount;
00250
00251 configBasic(HELLO_MSG);
00252
00253 CONFIG_AN0_AS_ANALOG();
00254 CONFIG_AN1_AS_ANALOG();
00255 configDAC();
00256 configTimer3();
00257 CONFIG_RB12_AS_DIG_OUTPUT();
00258 _LATB12 = 0;
00259
00260 u8_uiCount=5;
00261 while (1) {
00262 configADC1_ManualCH0( ADC_CH0_POS_SAMPLEA_AN0, 31, ADC_12BIT_FLAG );
00263 DELAY_MS(100);
00264 u16_per =convertADC1();
00265 if (u16_per==0) u16_per++;
00266
00267 configADC1_ManualCH0( ADC_CH0_POS_SAMPLEA_AN1, 31, ADC_12BIT_FLAG );
00268 DELAY_MS(100);
00269 u8_amp = convertADC1()>>9;
00270
00271 if (!u8_uiCount) {
00272 printf("timestep=0x%04X amplitude shift = 0x%02X\n", u16_per, u8_amp );
00273 u8_uiCount=5;
00274 } else
00275 u8_uiCount--;
00276 }
00277 }