|
|
View previous topic :: View next topic |
Author |
Message |
cxiong
Joined: 09 Sep 2003 Posts: 52
|
SPI_READ() !!! HELP!!! |
Posted: Mon Aug 16, 2004 8:41 am |
|
|
I am trying to write a routine to set value to digital POT MCP41010 and read it current wiper value. I got the SPI_WRITE function working ok. But I can't get the SPI_READ() function work.
I look at the CCS compiler manual but there is not enough iinfor for the spi_read().
Any body got any SPI_READ() routine that work?
See my code below:
Please help
Thanks.
//////////////////////////////////////////////////////////////////////////////////////
/// //
/// //
/// //
/// Digital Pot control MCP41010 //
/// //
/// //
///
/// ---------
/// | 1 |-B0------------
/// | 6 | | ---------
/// | F | |--CS---| 4 |
/// | 8 |-C3--------------SCK---| 1 |
/// | 7 | | 0 |
/// | 7 |-C5---------------SI---| 1 |
/// | | | 0 |
/// --------- ---------
///
/// _CS connect to RB0 ** Active Low
/// SCK connect to RC3
/// SI connect to RC5
/// PA0 connect to +5V
/// PB0 connect to GND
/// PW0 connect to Voltmeter.
///
/////////////////////////////////////////////////////////////////////////////////////
#include <16f877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <input.c>
#define CS (PIN_B0)
void main()
{
byte value,cmd,spi;
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
output_high(CS);
while(true)
{
printf("\r\nRead or Write: ");
cmd=getc();
cmd=toupper(cmd);
putc(cmd);
while ( (cmd!='R') && (cmd!='W') );
if(cmd=='W')
{
printf("\r\nNew value: ");
value = gethex();
printf("\n\r");
output_low(CS);
SPI_WRITE(0X13);
delay_us(1);
SPI_WRITE(value );
output_high(CS);
}
if(cmd=='R')
{
output_low(CS);
spi = SPI_READ(0);
printf("\r\nValue: %X\r\n",spi);
output_high(CS);
}
}
} |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Aug 16, 2004 9:19 am |
|
|
First, you shouldn't double post.
Second, you need to read the datasheet
http://ww1.microchip.com/downloads/en/DeviceDoc/11195c.pdf
Quote: |
Communications from the controller to the
MCP41XXX/42XXX digital potentiometers is accomplished
using the SPI serial interface. This interface
allows three commands:
1. Write a new value to the potentiometer data
register(s).
2. Cause a channel to enter low power shutdown
mode.
3. NOP (No Operation) command.
|
There is no read command. Data is shifted out of the device however strobing the CS line clears the registers which prevents you from clocking the 16 bits of data back out from the device
Quote: |
When using the daisy-chain configuration, keep in mind
that the shift register of each device is automatically
loaded with zeros whenever a command is executed
(CS = high). Because of this, the first 16 bits that come
out of the SO pin once the CS line goes low will always
be zeros. This means that when the first command is
being loaded into a device, it will always shift a NOP
command into the next device on the chain because
the command bits (and all the other bits) will be zeros.
|
The data sheet makes this pretty clear. I suggest you take a look at it for a better understanding of how the device works.
Regards,
Mark |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|