Forum: Mikrocontroller und Digitale Elektronik SPI mit PIC18F4550


von Pierre (Gast)


Lesenswert?

Hallo zusammen
Ich arbeite mit PIC18F4550 und der Compiler C18. Mit den Informationen, 
die auf den Datenblatt liegt habe ich versuch   ein Programm zu 
schreiben. Bei Ausführung wird das Programm an der Stelle den 
While-Schleife blockiert. Was soll ich machen um das Problem zu 
vermeiden.
1
#include "p18F4550.h"
2
#include <stdio.h>
3
#include <delays.h>
4
#include "Spi_Init.h"
5
#include "PortConfiguration.h"
6
7
8
  int u08LSend_Data=0x75;
9
10
void SPI_Init_Master(void)
11
{
12
  SSPSTATbits.SMP=0 ;   //1= Input data sampled at end of data output time
13
                        //0= Input data sampled at middle of data output time
14
                      
15
  SSPSTATbits.CKE=1 ;   //1= Transmit occurs on transition from active to Idle clock state
16
                        //0= transmit occurs on transition from Idle to active clock state
17
                      
18
  SSPCON1bits.WCOL=0 ;  //1= The SSPBUF register is written while it is still transmitting the previous word (must be clear in Software)
19
                        //0= No collision
20
                        
21
  SSPCON1bits.SSPEN= 1; //1= Enables serial port and configures SCK, SDO, SDI and SS as serial port pins
22
                        //0= Disables serial port and configures these pins as I/O port pins
23
                        
24
  SSPCON1bits.CKP=1;    //1= Idle state for clock is a high level
25
                        //0= Idle state for clock is a low level
26
                        
27
 //SSPM3:SSPM0: Master sychronous Serial Port Mode  Select bits
28
  SSPCON1bits.SSPM3= 0; //0101= SPI Slave mode, clock =SCK pin, SS pin control disabled, ss can be used as I/O pin 
29
  SSPCON1bits.SSPM2= 0; //0100= SPI slave mode, clock =SCK pin, SS pin control enabled
30
  SSPCON1bits.SSPM1= 0; //0011= SPI Master mode, clock = TMR2 output/2 
31
  SSPCON1bits.SSPM0= 1; //0010= SPI Master mode, clock = FOSC/64
32
                        //0001= SPI Master mode, clock = FOSC/16
33
                        //0000= SPI Master mode, clock = FOSC/4
34
  
35
  PIR1bits.SSPIF=0;    //1= The transmission/reception is complete(must be cleared in software)                    
36
                       //0= wating to transmit/receive 
37
38
                        
39
 TRISCbits.TRISC7=0;      // Serial Data Out
40
 TRISBbits.TRISB0=1;      // Serial Data In
41
 //CLK
42
 //SS
43
}
44
45
46
void SPI_MASTER_WRITE(void)
47
{
48
    int i=0;
49
  INTCONbits.GIEH = 0;
50
    SSPBUF = u08LSend_Data;         // load the register SSPBUF
51
     if ( SSPCON1bits.SSPOV)       // test if write collision occurred
52
     {
53
   return ( -1 );              // if WCOL bit is set return negative #
54
   }
55
  else
56
   {
57
    while(!SSPSTATbits.BF) ;        // control wenn the register is full
58
   
59
    u08LSend_Data= SSPBUF;
60
    SSPSTATbits.BF=0;               //
61
    
62
   } 
63
   INTCONbits.GIEH = 1;
64
}

von Daniel P. (ppowers)


Lesenswert?

>> while(!SSPSTATbits.BF);
Ich glaube diese Zeile tut nicht das, was Du vor hast. Das Bit BF ist 
high, wenn der Transfer noch nicht abgeschlossen ist. Ist dieses Bit 
low, kannst Du getrost die Daten senden.
Dein Programm bleibt also bei freiem SPI-Puffer hängen. Mach das 
Ausrufezeichen weg, dann sollte es laufen.

Abgesehen davon kannst Du in einer void-Funktion keinen Wert 
zurückgeben. Die Funktion sollte in diesem Fall als signed int 
deklariert werden.

gruß
daniel

von Pierre (Gast)


Lesenswert?

Hallo  Daniel P.,
ich habe geändert wie du gemeint hast, trotzdem funktionniert nicht.
Hat jemand ein Beispiel Code für SPI, denn ich suche seit einiger Zeit 
ohne Erfolg.
Grüß

von Sebastian W. (mrhack)


Lesenswert?

Hi,

 zum Thema SPI gibts bei Microchip ein Interessantes PDF
http://ww1.microchip.com/downloads/en/DeviceDoc/spi.pdf

Hattest du schon mal die SPI-lib die beim C18 mit dabei getestet?
1
#include <spi.h>

Würd mich interessieren, falls du das zum laufen bekommen hast, ich häng 
beim C18 auch noch, bin deshalb im Moment mit'n CCS unterwegs.

btw
1
SSPSTATbits.BF=0;
 kann nicht beschrieben werden, nur gelesen.


Gruß
 Sebastian

von Pierre (Gast)


Lesenswert?

Hallo zusammen
ich habe noch probleme mit SPI. das sind meine gesamte code. könnte 
jemand mir helfen
1
/////Master
2
#include "p18F4550.h"
3
#include <stdio.h>
4
#include <delays.h>
5
#include "Spi_Init.h"
6
#include "PortConfiguration.h"
7
  int u08LSend_Data=0x75;
8
void SPI_Init_Master(void)
9
{
10
  SSPSTATbits.SMP=0 ;   //1= Input data sampled at end of data output time
11
                        //0= Input data sampled at middle of data output time
12
                      
13
  SSPSTATbits.CKE=1 ;   //1= Transmit occurs on transition from active to Idle clock state
14
                        //0= transmit occurs on transition from Idle to active clock state
15
                      
16
  SSPCON1bits.WCOL=0 ;  //1= The SSPBUF register is written while it is still transmitting the previous word (must be clear in Software)
17
                        //0= No collision
18
                        
19
  SSPCON1bits.SSPEN= 1; //1= Enables serial port and configures SCK, SDO, SDI and SS as serial port pins
20
                        //0= Disables serial port and configures these pins as I/O port pins
21
                        
22
  SSPCON1bits.CKP=1;    //1= Idle state for clock is a high level
23
                        //0= Idle state for clock is a low level            
24
 //SSPM3:SSPM0: Master sychronous Serial Port Mode  Select bits
25
  SSPCON1bits.SSPM3= 0; //0101= SPI Slave mode, clock =SCK pin, SS pin control disabled, ss can be used as I/O pin 
26
  SSPCON1bits.SSPM2= 0; //0100= SPI slave mode, clock =SCK pin, SS pin control enabled
27
  SSPCON1bits.SSPM1= 0; //0011= SPI Master mode, clock = TMR2 output/2 
28
  SSPCON1bits.SSPM0= 1; //0010= SPI Master mode, clock = FOSC/64
29
                        //0001= SPI Master mode, clock = FOSC/16
30
                        //0000= SPI Master mode, clock = FOSC/4
31
  PIR1bits.SSPIF=0;    //1= The transmission/reception is complete(must be cleared in software)                    
32
                       //0= wating to transmit/receive     
33
 TRISCbits.TRISC7=0;      // Serial Data Out
34
 TRISBbits.TRISB0=1;      // Serial Data In
35
 //CLK
36
 TRISBbits.TRISB1=0;
37
 //SS
38
 TRISDbits.TRISD5=0;
39
}
40
int SPI_MASTER_WRITE(void)
41
{
42
  INTCONbits.GIEH = 0;
43
  TRISDbits.TRISD5=0;
44
    SSPBUF = u08LSend_Data;         // load the register SSPBUF
45
     if ( SSPCON1bits.SSPOV)       // test if write collision occurred
46
     {
47
   return ( -1 );              // if WCOL bit is set return negative #
48
   }
49
  else
50
   {
51
    while(!SSPSTATbits.BF)         // control wenn the register is full
52
   
53
   TRISDbits.TRISD5=1;  // CS
54
      u08LSend_Data= SSPBUF;
55
      
56
    SSPSTATbits.BF=0;               //
57
    return SSPBUF; 
58
   } 
59
  INTCONbits.GIEH = 1;
60
}
61
/////Slave
62
#include "p18F4550.h"
63
#include <stdio.h>
64
#include <delays.h>
65
#include "Spi_Init.h"
66
#include "PortConfiguration.h"
67
68
void SPI_INIT_SLAVE(void)
69
{
70
  SSPSTATbits.SMP=0 ;   // SPM must be cleared when SPI is used in Slave mode
71
                      
72
  SSPSTATbits.CKE=0 ;   //1= Transmit occurs on transition from active to Idle clock state
73
                        //0= transmit occurs on transition from Idle to active clock state    
74
  SSPCON1bits.WCOL=0 ;  //1= The SSPBUF register is written while it is still transmitting the previous word (must be clear in Software)
75
                        //0= No collision     
76
  SSPCON1bits.SSPEN= 1; //1= Enables serial port and configures SCK, SDO, SDI and SS as serial port pins
77
                        //0= Disables serial port and configures these pins as I/O port pins             
78
  SSPCON1bits.CKP=1;    //1= Idle state for clock is a high level
79
                        //0= Idle state for clock is a low level            
80
 //SSPM3:SSPM0: Master sychronous Serial Port Mode  Select bits
81
  SSPCON1bits.SSPM3= 0; //0101= SPI Slave mode, clock =SCK pin, SS pin control disabled, ss can be used as I/O pin 
82
  SSPCON1bits.SSPM2= 1; //0100= SPI slave mode, clock =SCK pin, SS pin control enabled
83
  SSPCON1bits.SSPM1= 0; //0011= SPI Master mode, clock = TMR2 output/2 
84
  SSPCON1bits.SSPM0= 1; //0010= SPI Master mode, clock = FOSC/64
85
                        //0001= SPI Master mode, clock = FOSC/16
86
                        //0000= SPI Master mode, clock = FOSC/4
87
  PIR1bits.SSPIF=0;    //1= The transmission/reception is complete(must be cleared in software)                    
88
                       //0= wating to transmit/receive         
89
 TRISCbits.RC7=1;      // Serial Data Out
90
 TRISBbits.RB0=0;      // Serial Data In
91
 //CLK
92
 TRISBbits.TRISB1=1;
93
 //SS
94
 TRISDbits.TRISD5=1;
95
}
96
97
int SPI_SLAVE_READ( void )
98
{
99
  SSP1BUF = 0x00;                  // initiate bus cycle
100
  while ( !SSP1STATbits.BF );      // wait until cycle complete
101
  return ( SSP1BUF );              // return with byte read 
102
}

von holger (Gast)


Lesenswert?

>  while ( !SSP1STATbits.BF );      // wait until cycle complete
>  return ( SSP1BUF );              // return with byte read

SSP1BUF und SSP1STAT gibt es beim 18f4550 nicht.

von Pierre (Gast)


Lesenswert?

Hallo  holger, habe ich geandert trotzdem funktionniert nicht. wo könnte 
das Problem sein

von Zwirbeljupp (Gast)


Lesenswert?

Wenn das da oben tatsächlich der gesamte Code ist, dann wundert es 
mich nicht, dass dieses "Programm" nicht läuft...

von holger (Gast)


Lesenswert?

>Hallo  holger, habe ich geandert trotzdem funktionniert nicht. wo könnte
>das Problem sein

> //SS
> TRISDbits.TRISD5=0;

SS liegt auf PORTA

>  SSPCON1bits.SSPM3= 0; //0101= SPI Slave mode, clock =SCK pin, SS pin control 
disabled, ss can be used as I/O pin
>  SSPCON1bits.SSPM2= 1; //0100= SPI slave mode, clock =SCK pin, SS pin control 
enabled
>  SSPCON1bits.SSPM1= 0; //0011= SPI Master mode, clock = TMR2 output/2
>  SSPCON1bits.SSPM0= 1; //0010= SPI Master mode, clock = FOSC/64

Deine Auswahl ist SS control disabled.

von Christopher L. (Gast)


Lesenswert?

Hey mal an alle:

Muss man beim PIC im SPI MASTER MODE das SS (SlaveSelect) selbst 
erzeugen?
Oder generiert es der PIC automatisch an einem PIN? und wenn
wo am SS?
lg
christopher

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.