PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 18, 2009 2:13 pm |
|
|
Post a list of your external connections to the MAX186's pins.
It has 20 pins. Post what is connected to each pin. Especially
post the connections between the PIC pins and the MAX186 pins.
Just to save time, try using this sample program. I changed the
#use rs232() pins to use the Hardware UART on pins C6 and C7.
That's the standard way to do it. I also changed the baud rate
to 9600 just because that's normally what I use. Notice that
#fast io mode is not used. It's not necessary. I added comments
to explain what the code is doing. Notice how simple this code is.
Always try to write simple code. It's easier to understand.
Compare this code to Figure 9 (on page 13) of the Max186 data sheet.
You will see that the code follows the timing diagram in the data sheet.
http://datasheets.maxim-ic.com/en/ds/MAX186-MAX188.pdf
Code: |
#include <18F4620.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define MAX186_CS PIN_D0
// Control Byte: Ch.0, unipolar, single-ended, internal clock
#define MAX186_CTRL_BYTE 0x8E
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
// Always read channel 0.
int16 read_max186(void)
{
int8 lsb, msb;
int16 retval;
// Send control byte to start the conversion.
output_low(MAX186_CS);
spi_write(MAX186_CTRL_BYTE);
output_high(MAX186_CS);
// Allow time for the conversion to occur.
// Data sheet says 10us max, so wait 15us to be safe.
delay_us(15);
// Now read two bytes of the A/D result.
output_low(MAX186_CS);
msb=spi_read(0);
lsb=spi_read(0);
output_high(MAX186_CS);
// Convert two bytes into a 16-bit word.
retval = make16(msb, lsb);
retval >>= 4; // Right justify the 12-bit result
return(retval);
}
//===============================
main(void)
{
int16 result;
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);
while(1)
{
result = read_max186();
printf("%LX \n\r", result);
delay_ms(500);
}
} |
|
|