Browse Source

add DAC magic for AD5542.

git-svn-id: https://bucket.mit.edu/svn/nilm/zoom@5476 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/zoom-1.0
jim 16 years ago
parent
commit
18b75e5ef4
4 changed files with 56 additions and 8 deletions
  1. +32
    -5
      firmware/dac.c
  2. +3
    -0
      firmware/dac.h
  3. +21
    -3
      firmware/zoom.c
  4. BIN
      firmware/zoom.mcw

+ 32
- 5
firmware/dac.c View File

@@ -1,15 +1,42 @@
#include "config.h"
#include "dac.h"
/* Initialize DAC */
void dac_init(void)
{
/* data bus on port D */
TRISD = 0;
TRISEbits.TRISE0 = 0;
// TODO
/* SPI2 */
IEC2bits.SPI2IE = 0;
SPI2CON1bits.DISSCK = 0;
SPI2CON1bits.DISSDO = 0;
SPI2CON1bits.MODE16 = 1;
SPI2CON1bits.SMP = 0;
SPI2CON1bits.CKE = 0;
SPI2CON1bits.SSEN = 0;
SPI2CON1bits.CKP = 1;
SPI2CON1bits.MSTEN = 1;
SPI2CON1bits.SPRE = 4;
SPI2CON1bits.PPRE = 3;
/* There's no framed mode that does the normal
"chip select" behavior, so control /SS2 manually */
SPI2CON2bits.FRMEN = 0;
LATGbits.LATG9 = 1;
TRISGbits.TRISG9 = 0;
SPI2STATbits.SPISIDL = 0;
SPI2STATbits.SPIEN = 1;
dac_write(0x0000);
}
/* Write raw 16-bit value to DAC */
void dac_write(uint16_t val)
{
// TODO
LATGbits.LATG9 = 0;
SPI2BUF = val;
while (!SPI2STATbits.SPIRBF)
continue;
(void) SPI2BUF;
LATGbits.LATG9 = 1;
}

+ 3
- 0
firmware/dac.h View File

@@ -3,7 +3,10 @@
#include "config.h"
/* Initialize ADC */
void dac_init(void);
/* Write raw 16-bit value to DAC */
void dac_write(uint16_t val);
#endif

+ 21
- 3
firmware/zoom.c View File

@@ -1,19 +1,22 @@
#include "config.h"
#include "adc.h"
#include "dac.h"
#include "uart.h"
#include <stdio.h>

int main(void)
{
uint32_t v;
char ch;
config_init();
uart1_init(115200);
adc_init();
uart1_put_string("ADC test\r\n");
dac_init();
uart1_put_string("ADC/DAC test\r\n");

for(;;) {
switch(uart1_get()) {
switch((ch = uart1_get())) {
case 'r':
uart1_put_string("read");
v = adc_read();
@@ -33,8 +36,23 @@ int main(void)
uart1_put('.');
uart1_put_string("done\r\n");
break;
case '1'...'9':
v = (ch - '1') * 0x2000L;
if (v > 65535) v = 65535;
uart1_put_string("write 0x");
uart1_put_hex16(v);
uart1_put_string("\r\n");
dac_write(v);
break;
case 't':
for (v = 0; v < 8; v++) {
dac_write(0x0000);
dac_write(0xFFFF);
}
break;
default:
uart1_put_string("choose: Read Convert\r\n");
uart1_put_string("ADC: Read Convert\r\n");
uart1_put_string("DAC: 123456789 Toggle\r\n");
break;
}
}


BIN
firmware/zoom.mcw View File


Loading…
Cancel
Save