View previous topic :: View next topic |
Author |
Message |
MikeP
Joined: 07 Sep 2003 Posts: 49
|
Trying to get 16f877a HW SPI to mcp6s22 to work |
Posted: Sat Jul 29, 2006 12:32 am |
|
|
Im tring to use spi to talk to the mcp6s22
The code below hangs on spi_write()
If I leave it alone for 1 hour+ or so it finally gets to the while(TRUE) loop
The data sheet for the mcp6s22 says it can use mode 0,0 or mode 1,1 spi
I think I have everything set to use mode 0,0
I looked over many post here about SPI problems, tried many of them none helped with or talked about spi_write() hanging.
Code: |
#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT
#FUSES NOLVP
#FUSES HS
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define PGA_SELECT PIN_B0
#define PGA_DI PIN_C5
#define PGA_CLK PIN_C3
#define PRG_GAIN 0b01000000 // PROGRAM GAIN
#define PRG_CHANNEL 0b01000001 // PROGRAM CHANNEL
#define PGA_SHDN 0b00100000 // SHUTDOWN PGA
void init_pga()
{
output_high( PGA_CLK );
output_high(PGA_SELECT);
setup_spi(SPI_MASTER | 0x4000 | SPI_CLK_DIV_4 );
}
#define RED_LED PIN_B5
int16 value_a0, value_a1;
void main() {
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(FALSE);
setup_vref(FALSE);
// set_tris_a( 0xff );
// set_tris_c( 0 );
init_pga();
puts( "PGA INIT DONE" );
output_low( PGA_SELECT );
spi_write( PRG_CHANNEL );
spi_write( 1 );
output_high(PGA_SELECT);
puts("CHANNEL SET TO 1");
while(TRUE)
{
output_high( RED_LED );
delay_ms( 500 );
output_low( RED_LED );
delay_ms( 500 );
set_adc_channel(0);
delay_us(10);
value_a0 = read_adc();
set_adc_channel(1);
delay_us(10);
value_a1 = read_adc();
printf( "A0='%Lu A1='%Lu'\r\n", value_a0, value_a1 );
}
}
|
|
|
|
MikeP
Joined: 07 Sep 2003 Posts: 49
|
Forgot to say what compiler version im using |
Posted: Sat Jul 29, 2006 12:46 am |
|
|
Forgot to say what compiler verion I was using pcw 3.248 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 29, 2006 12:17 pm |
|
|
You have more than one instance of spi_write(). You didn't say
which one hangs.
You also didn't say if you're running this in the MPLAB simulator,
in ICD or ICD2 debugger mode (and in single step or animate),
or an ICE, or on a PIC running in standalone mode. |
|
|
MikeP
Joined: 07 Sep 2003 Posts: 49
|
|
Posted: Sat Jul 29, 2006 12:57 pm |
|
|
Im using a ICD-u40 to program with in standalone mode
running on a CCS prototying board 16f877a
It hangs on the first spi_write in the program.
At this point i dont care if the mcp6s22 is reciving the data, I cant get the pic to start sending data
I get nothing out of the spi CLK, theres something wrong with my spi setup or compiler is messing up.
when spi is setup for master mode it should just send data out the port even if it not connected to anything right, did this to see if the mcp6s22 was broken or something
Im gonna d/l a older version of pcw and see if that does anything.
If you dont have any ideas as to why this isnt working after getting this info and the older version of pcw doesnt work ill have to bit bang it like they do in all the App Notes Ive seen from microchip for the mcp6s22 demo board
No I dont use the demo board I have one wired up on a breadboard |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 29, 2006 1:46 pm |
|
|
The code shown below was generated by PCM vs. 3.248 for a 16F877A.
It puts the data into SSPBUF and then polls the BF bit until
it goes low, which means the data has been clocked out and
the buffer is now empty.
You said you have no clock. That could happen if:
1. The PIC is not being configured for SPI Master mode.
2. The SCLK pin is not configured as an output.
3. The PIC's SPI module is defective.
4. The proper HEX file is not actually being programmed into the PIC.
A HEX file from a different folder is accidently being used.
(Or the same thing, except for the C source file).
5. The PIC's crystal or oscillator is flaky.
There may be other causes. I can't spend any more time on this now.
I have to leave. You should check those things above, and check the
connections on your board. Possibly try replacing the PIC. See if that
helps.
Code: |
0000 00657 ... output_low( PGA_SELECT );
011F 1683 00658 BSF 03.5
0120 1006 00659 BCF 06.0
0121 1283 00660 BCF 03.5
0122 1006 00661 BCF 06.0
0000 00662 .... spi_write( PRG_CHANNEL );
0123 0813 00663 MOVF 13,W // Read SSPBUF
0124 3041 00664 MOVLW 41 // Write 0x41 to SSPBUF
0125 0093 00665 MOVWF 13
0126 1683 00666 BSF 03.5 // Bank 1
0127 1814 00667 BTFSC 14.0 // Wait in loop until
0128 292B 00668 GOTO 12B // buffer goes empty
0129 1283 00669 BCF 03.5
012A 2926 00670 GOTO 126
0000 00671 ... spi_write( 1 );
012B 1283 00672 BCF 03.5
012C 0813 00673 MOVF 13,W
012D 3001 00674 MOVLW 01
012E 0093 00675 MOVWF 13
012F 1683 00676 BSF 03.5
0130 1814 00677 BTFSC 14.0
0131 2934 00678 GOTO 134
0132 1283 00679 BCF 03.5
0133 292F 00680 GOTO 12F
0000 00681 .... output_high(PGA_SELECT);
0134 1006 00682 BCF 06.0
0135 1283 00683 BCF 03.5
0136 1406 00684 BSF 06.0
|
|
|
|
|