View previous topic :: View next topic |
Author |
Message |
tophe59 Guest
|
SPI transition |
Posted: Sun May 15, 2005 11:31 am |
|
|
Hello,
I have problems wihth SPI. I want configure SPI in 16F876 in master mode and low to high transition
my code is:
Code: | set_tris_c(0x50);//port c 0=en sortie
setup_ccp1(ccp_pwm);// mode pwm
setup_ccp2(ccp_pwm);//
setup_timer_2(T2_DIV_BY_16,255,1);
set_pwm1_duty(125); // 50%
set_pwm2_duty(125); // 50%
setup_ccp1(ccp_off);// off
setup_ccp2(ccp_off);// off
setup_spi(spi_master|SPI_L_TO_H|spi_clk_div_64);
// setup_spi(spi_master|SPI_H_TO_L|spi_clk_div_64);
//*****************************
//for (;;) {output_bit(pin_b0,input(pin_a3));} //boucle sans fin
// for (;;) {output_c(read_adc());} //boucle sans fin
for (;;) {
output_low(sel);
delay_us(10);
vartemp=spi_read(0x80);
delay_us(10);
for (i=0;i<5;i++) {valpsx[i]=spi_read(0x42); //42
delay_us(10);}
//vartemp=spi_read(0xff);
//delay_us(10);
//vartemp=spi_read(0xff);
//delay_us(10);
//vartemp=spi_read(0xff);
//delay_us(10);
output_high(sel);
delay_us(100);
}
} |
i use 3.183 ccs version
When i see signals with osciloscope the transition clock of SPI is High to Low and no low to high
help me please
lst code:
Code: | .................... set_tris_c(0x50);//port c 0=en sortie
000A: MOVLW FF
000B: BCF 03.5
000C: MOVWF 28
000D: MOVLW 50
000E: BSF 03.5
000F: MOVWF 07
0010: BCF 03.5
0011: MOVWF 28
.................... setup_ccp1(ccp_pwm);// mode pwm
0012: BCF 28.2
0013: MOVF 28,W
0014: BSF 03.5
0015: MOVWF 07
0016: BCF 03.5
0017: BCF 07.2
0018: MOVLW 0C
0019: MOVWF 17
.................... setup_ccp2(ccp_pwm);//
001A: BCF 28.1
001B: MOVF 28,W
001C: BSF 03.5
001D: MOVWF 07
001E: BCF 03.5
001F: BCF 07.1
0020: MOVLW 0C
0021: MOVWF 1D
.................... setup_timer_2(T2_DIV_BY_16,255,1);
0022: MOVLW 00
0023: MOVWF 78
0024: IORLW 06
0025: MOVWF 12
0026: MOVLW FF
0027: BSF 03.5
0028: MOVWF 12
.................... set_pwm1_duty(125); // 50%
0029: MOVLW 7D
002A: BCF 03.5
002B: MOVWF 15
.................... set_pwm2_duty(125); // 50%
002C: MOVWF 1B
.................... setup_ccp1(ccp_off);// off
002D: BSF 28.2
002E: MOVF 28,W
002F: BSF 03.5
0030: MOVWF 07
0031: BCF 03.5
0032: CLRF 17
0033: CLRF 17
.................... setup_ccp2(ccp_off);// off
0034: BSF 28.1
0035: MOVF 28,W
0036: BSF 03.5
0037: MOVWF 07
0038: BCF 03.5
0039: CLRF 1D
003A: CLRF 1D
....................
.................... setup_spi(spi_master|SPI_L_TO_H|spi_clk_div_64);
003B: BCF 14.5
003C: BCF 28.5
003D: MOVF 28,W
003E: BSF 03.5
003F: MOVWF 07
0040: BCF 03.5
0041: BSF 28.4
0042: MOVF 28,W
0043: BSF 03.5
0044: MOVWF 07
0045: BCF 03.5
0046: BCF 28.3
0047: MOVF 28,W
0048: BSF 03.5
0049: MOVWF 07
004A: MOVLW 22
004B: BCF 03.5
004C: MOVWF 14
004D: MOVLW 00
004E: BSF 03.5
004F: MOVWF 14
.................... // setup_spi(spi_master|SPI_H_TO_L|spi_clk_div_64);
....................
.................... //*****************************
.................... //for (;;) {output_bit(pin_b0,input(pin_a3));} //boucle sans fin
.................... // for (;;) {output_c(read_adc());} //boucle sans fin
.................... for (;;) {
.................... output_low(sel);
0050: BCF 03.5
0051: BCF 28.7
0052: MOVF 28,W
0053: BSF 03.5
0054: MOVWF 07
0055: BCF 03.5
0056: BCF 07.7
.................... delay_us(10);
0057: MOVLW 10
0058: MOVWF 77
0059: DECFSZ 77,F
005A: GOTO 059
005B: NOP
.................... vartemp=spi_read(0x80);
005C: MOVF 13,W
005D: MOVLW 80
005E: MOVWF 13
005F: BSF 03.5
0060: BTFSC 14.0
0061: GOTO 064
0062: BCF 03.5
0063: GOTO 05F
0064: BCF 03.5
0065: MOVF 13,W
0066: MOVWF 22
.................... delay_us(10);
0067: MOVLW 10
0068: MOVWF 77
0069: DECFSZ 77,F
006A: GOTO 069
006B: NOP
.................... for (i=0;i<5;i++) {valpsx[i]=spi_read(0x42); //42
006C: CLRF 23
006D: MOVF 23,W
006E: SUBLW 04
006F: BTFSS 03.0
0070: GOTO 086
0071: MOVLW 24
0072: ADDWF 23,W
0073: MOVWF 04
0074: MOVF 13,W
0075: MOVLW 42
0076: MOVWF 13
0077: BSF 03.5
0078: BTFSC 14.0
0079: GOTO 07C
007A: BCF 03.5
007B: GOTO 077
007C: BCF 03.5
007D: MOVF 13,W
007E: MOVWF 00
.................... delay_us(10);}
007F: MOVLW 10
0080: MOVWF 77
0081: DECFSZ 77,F
0082: GOTO 081
0083: NOP
0084: INCF 23,F
0085: GOTO 06D
.................... //vartemp=spi_read(0xff);
.................... //delay_us(10);
.................... //vartemp=spi_read(0xff);
.................... //delay_us(10);
.................... //vartemp=spi_read(0xff);
.................... //delay_us(10);
.................... output_high(sel);
0086: BCF 28.7
0087: MOVF 28,W
0088: BSF 03.5
0089: MOVWF 07
008A: BCF 03.5
008B: BSF 07.7
.................... delay_us(100);
008C: MOVLW A6
008D: MOVWF 77
008E: DECFSZ 77,F
008F: GOTO 08E
0090: NOP
.................... }
0091: BSF 03.5
0092: GOTO 050
.................... }
....................
....................
0093: SLEEP |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 15, 2005 1:41 pm |
|
|
I think your version has a bug with setup_spi().
You can fix it by adding the following function to your code.
This function will properly setup the CKP and CKE bits in
the SPI control registers.
Code: | // These register address are for the 16F877.
// Refer to the data sheet if you use a different PIC.
#byte SSPCON = 0x14
#byte SSPSTAT = 0x94
#bit CKE = SSPSTAT.6
#bit CKP = SSPCON.4
#bit SSPEN = SSPCON.5
// SPI mode 0: CKP = 0 and CKE = 1
// SPI mode 1: CKP = 0 and CKE = 0
// SPI mode 2: CKP = 1 and CKE = 1
// SPI mode 3: CKP = 1 and CKE = 0
void change_spi_mode(char mode)
{
SSPEN = 0; // Disable the SPI module
mode &= 0x03;
// Set the CKP and CKE bits to select the desired SPI mode.
switch(mode)
{
case 0:
CKP = 0;
CKE = 1;
break;
case 1:
CKP = 0;
CKE = 0;
break;
case 2:
CKP = 1;
CKE = 1;
break;
case 3:
CKP = 1;
CKE = 0;
break;
}
SSPEN = 1; // Re-enable the SPI module
} |
Inside your program, you can use it as shown below.
First call the CCS function, setup_spi(). Then call the
change_spi_mode() function to set the mode properly.
Example:
Code: | void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_64);
change_spi_mode(0); // Select mode 0
while(1);
} |
|
|
|
tophe Guest
|
|
Posted: Mon May 16, 2005 5:19 am |
|
|
That work thanks |
|
|
tophe59 Guest
|
|
Posted: Wed Oct 12, 2005 3:16 pm |
|
|
Same bug with 18f4620 but not work.
I have change the @ for 18f4620
#byte SSPCON = 0xfc6
#byte SSPSTAT = 0xfc7
help me pleeaaaase |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 12, 2005 4:41 pm |
|
|
What is your version of the PCH compiler ? |
|
|
Guest
|
|
Posted: Thu Oct 13, 2005 12:11 am |
|
|
i use 3.183 ccs version |
|
|
tophe59
Joined: 13 Oct 2005 Posts: 32 Location: France
|
|
Posted: Thu Oct 13, 2005 11:10 am |
|
|
Anonymous wrote: | i use 3.183 ccs version |
up |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 13, 2005 12:08 pm |
|
|
Post a very short, but complete program that shows the problem.
Tell us exactly what your problem is. Don't just say "not work". |
|
|
tophe59
Joined: 13 Oct 2005 Posts: 32 Location: France
|
|
Posted: Thu Oct 13, 2005 12:17 pm |
|
|
Thank's PCM
Data is transmit high to low. i want transmit low to high
the code
Code: |
#byte SSPCON = 0xfc6 //0x14
#byte SSPSTAT = 0xfc7 //0x94
#bit CKE = SSPSTAT.6
#bit CKP = SSPCON.4
#bit SSPEN = SSPCON.5
void change_spi_mode(char mode)
{
SSPEN = 0; // Disable the SPI module
mode &= 0x03;
// Set the CKP and CKE bits to select the desired SPI mode.
switch(mode)
{
case 0:
CKP = 0;CKE = 1;break;
case 1:
CKP = 0;CKE = 0;break;
case 2:
CKP = 1;CKE = 1;break;
case 3:
CKP = 1;CKE = 0;break;
}
SSPEN = 1; // Re-enable the SPI module
}
....
setup_spi(spi_master|SPI_L_TO_H|spi_clk_div_64);
change_spi_mode(1);
.... |
Last edited by tophe59 on Thu Oct 13, 2005 12:59 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 13, 2005 12:50 pm |
|
|
Quote: | i want transmit low to high |
You want to transmit data on the rising edge of clock.
That means that your SPI slave device will sample
the data on the falling edge, which is SPI mode 1.
Change your code to this:
setup_spi(spi_master | SPI_L_TO_H | spi_clk_div_64);
change_spi_mode( 1 ); |
|
|
tophe59
Joined: 13 Oct 2005 Posts: 32 Location: France
|
|
Posted: Thu Oct 13, 2005 12:58 pm |
|
|
Excuse me PCM, the mode is already mode(1).
I editing message... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 13, 2005 1:10 pm |
|
|
What SPI slave chip are you using ? |
|
|
tophe59
Joined: 13 Oct 2005 Posts: 32 Location: France
|
|
Posted: Thu Oct 13, 2005 1:13 pm |
|
|
PCM programmer wrote: | What SPI slave chip are you using ? |
I use PSX joystick. But i see the bad transition with an oscilloscope ( good in english ?) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 13, 2005 1:42 pm |
|
|
I wonder if you have confused the SCK and SDO pins ?
You didn't post a complete test program, so I have posted one below.
Change the #fuses statement and the #use delay() statement
to fit your hardware.
1. Then compile it and look at the SCK and SDO pins with your
oscilloscope.
2. Try changing the parameter in the change_spi_mode()
function to different values (0, 1, 3). See if it works.
3. If not, then post the .LST file.
(For security reasons, you don't have to post your CCS customer
number, which is at the top of the .LST file, right after the compiler version. Just delete that number when you post the .LST file).
Code: |
#include <18F4620.H>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#byte SSPCON = 0xfc6 //0x14
#byte SSPSTAT = 0xfc7 //0x94
#bit CKE = SSPSTAT.6
#bit CKP = SSPCON.4
#bit SSPEN = SSPCON.5
void change_spi_mode(char mode)
{
SSPEN = 0; // Disable the SPI module
mode &= 0x03;
// Set the CKP and CKE bits to select the desired SPI mode.
switch(mode)
{
case 0:
CKP = 0;CKE = 1;break;
case 1:
CKP = 0;CKE = 0;break;
case 2:
CKP = 1;CKE = 1;break;
case 3:
CKP = 1;CKE = 0;break;
}
SSPEN = 1; // Re-enable the SPI module
}
//==============================
void main(void)
{
setup_spi(spi_master|SPI_L_TO_H|spi_clk_div_64);
change_spi_mode(1);
while(1)
{
spi_write(0x55);
}
} |
|
|
|
tophe59
Joined: 13 Oct 2005 Posts: 32 Location: France
|
|
Posted: Thu Oct 13, 2005 1:57 pm |
|
|
i'm confused that work (low to high) in mode(0)
Thank's PCM you're my saver ! |
|
|
|