View previous topic :: View next topic |
Author |
Message |
Guest
|
Need help with SPI modes when using PIC to ctrl 2 chips |
Posted: Tue Dec 02, 2008 10:57 am |
|
|
Hi everyone,
I'm doing a project that requires a Direct Digital Synthesis(DDS) and a Digital Potentiometer(DPOT) to produce an analogue sine wave. I'm using the PIC16F877 to control these two chips via SPI. I have no problem controlling them separately (in 2 different circuit setups) via SPI.
The problem is when I integrate them together and use a single program to control both chips. As both require different SPI modes to operate in (DDS=mode 2or3; DPOT=mode 0) I don't know how to program the PIC using CCS to include both modes when require in the program. The program will just use the first defined mode in setup_spi throughout the whole program. So this makes either the DDS or DPOT working...but not both of them together.
Can I use two different modes in a single PIC program in CCS?
Below are my codes without the detail control signals:
Code: | #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)
//========================
void main()
{
do
{
setup_spi(SPI_MASTER|SPI_MODE_2|SPI_CLK_DIV_4);
//Below is the DDS code as an example...
//FSYNC (Chip Select) Coding Sequence: set FSYNC high unless transmitting serial data
output_high(PIN_B7);
delay_us(10);
output_low(PIN_B7);
delay_us(10);
//Resetting analogue output & Ctrl signal to send serial data to FREQ0
spi_write(0b00100001);
spi_write(0b00000000);
//send serial data to FREQ0 (LSB)
spi_write(0b01110011);
spi_write(0b00110011);
//send serial data to FREQ0 (MSB)
spi_write(0b01110011);
spi_write(0b00110011);
//More code in between have been removed for simplicity...
//select freq & phase reg & unreset
spi_write(0b00100000);
spi_write(0b00000000);
delay_us(10);
output_high(PIN_B7);
delay_us(180);
delay_us(1000000);
setup_spi(SPI_SLAVE|SPI_MODE_0|SPI_CLK_DIV_4);
//Code to control DPOT goes here
}
while (TRUE);
} |
I really appreciate your help in this. Many thanks!
Akmal |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: Need help with SPI modes when using PIC to ctrl 2 chips |
Posted: Tue Dec 02, 2008 11:16 am |
|
|
Why are you using SPI_SLAVE in your SPI setup for the DPOT? Your PIC16F877 is still the master, right? As for integrating two SPI modes, you will have to switch between them as your PIC comminicates with the DDS chip and then the DDS chip. You can either call spi_setup() every time you switch between the two chips, or, if that is too cumbersome, talk directly to the SPI registers when changing modes. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
akmal.wahab
Joined: 02 Dec 2008 Posts: 2
|
|
Posted: Tue Dec 02, 2008 11:26 am |
|
|
Many thanks for your reply RLScott. Apologies for a minor mistake in the previous code. The SPI_SLAVE at the end of the code is suppose to be SPI_MASTER. I was doing some testing earlier and forgot to change it back. So yes, the PIC16F877 is the master for both chips.
You said that I could call spi_setup() everytime I switch between the two chips, I thought I've done this in my code as shown here. Or do you think it's wrong?
Best regards,
Akmal Wahab
Code: | #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)
//========================
void main()
{
do
{
setup_spi(SPI_MASTER|SPI_MODE_2|SPI_CLK_DIV_4);
//Below is the DDS code as an example...
//FSYNC (Chip Select) Coding Sequence: set FSYNC high unless transmitting serial data
output_high(PIN_B7);
delay_us(10);
output_low(PIN_B7);
delay_us(10);
//Resetting analogue output & Ctrl signal to send serial data to FREQ0
spi_write(0b00100001);
spi_write(0b00000000);
//send serial data to FREQ0 (LSB)
spi_write(0b01110011);
spi_write(0b00110011);
//send serial data to FREQ0 (MSB)
spi_write(0b01110011);
spi_write(0b00110011);
//More code in between have been removed for simplicity...
//select freq & phase reg & unreset
spi_write(0b00100000);
spi_write(0b00000000);
delay_us(10);
output_high(PIN_B7);
delay_us(180);
delay_us(1000000);
setup_spi(SPI_MASTER|SPI_MODE_0|SPI_CLK_DIV_4);
//Code to control DPOT goes here
}
while (TRUE);
} |
|
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Tue Dec 02, 2008 12:25 pm |
|
|
akmal.wahab wrote: | ...I could call spi_setup() everytime I switch between the two chips, I thought I've done this in my code as shown here.. |
Yes, you did. I failed to notice the loop structure. So if you are already doing that, what is the problem? _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
akmal.wahab
Joined: 02 Dec 2008 Posts: 2
|
|
Posted: Tue Dec 02, 2008 12:55 pm |
|
|
Hi Robert,
The problem is when I probe the output of SCLK pin of the PIC, it always operate in mode 2 ie. SCLK is always HIGH unless there's any spi-write happening. I am not able to get any mode 0 SCLK signal (SCLK always LOW unless except for the clk signal when writing) even though I've included the setup_spi(SPI_MASTER|SPI_MODE_0|SPI_CLK_DIV_4); in.
Regards,
Akmal |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Tue Dec 02, 2008 1:01 pm |
|
|
akmal.wahab wrote: | Hi Robert,
The problem is when I probe the output of SCLK pin of the PIC, it always operate in mode 2 ie. SCLK is always HIGH unless there's any spi-write happening. ...l |
But if you turned things around and did the mode 0 setup first and then mode 2 afterwards, then the SPI would stay stuck in mode 0? Are you sure your code is getting to the second setup_spi()? _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
|