Escolar Documentos
Profissional Documentos
Cultura Documentos
Apostila Sobre SPI
Apostila Sobre SPI
Tutorial de SPI
Data: 12/12/2012
Pgina 1
ITAndroids Ser a melhor equipe de robtica do mundo
1. Introduo
2. Interface
Pgina 2
ITAndroids Ser a melhor equipe de robtica do mundo
Legenda:
Pgina 3
ITAndroids Ser a melhor equipe de robtica do mundo
3. Registradores
SSPCON
SSPSTAT
SSPBUF
SSPRS
4. Operao
Pgina 4
ITAndroids Ser a melhor equipe de robtica do mundo
5. Implementao
Mos obra!
Pgina 5
ITAndroids Ser a melhor equipe de robtica do mundo
Cdigo do Master:
//ITAndroids
char rec;
char x = 0b00000001;
void init()
//Configurao do SPI
//SSPSTAT:
//SSPCON:
Pgina 6
ITAndroids Ser a melhor equipe de robtica do mundo
SSPCON.SSPEN = 1; //Enables serial port and configures SCK, SDO, SDI, and SS as serial
port pins
SSPCON.SSPM3 = 0;
SSPCON.SSPM1 = 0;
SSPCON.SSPM0 = 0;
//Configurando portas
TRISD = 0b00000000; //A porta D ser usada para vermos o que o Master est recebendo
void main()
init();
while(1)
delay_ms(10); //Espera Slave carregar buffer. IMPORTANTE! O Slave tem que carregar
seu buffer antes de o Master carregar! Teste comentar essa
//linha. O Master recebe a mesma coisa que ele enviou, pois no d tempo de o Slave
carregar seu buffer.
PORTD = rec;
if(x == 0b00000000)
x = 0b00000001;
Pgina 7
ITAndroids Ser a melhor equipe de robtica do mundo
Cdigo do Slave:
//ITAndroids
char rec;
int cont = 0;
void init()
//Configurao do SPI
//SSPSTAT:
//SSPCON:
Pgina 8
ITAndroids Ser a melhor equipe de robtica do mundo
SSPCON.SSPEN = 1; //Enables serial port and configures SCK, SDO, SDI, and SS as serial
port pins
SSPCON.SSPM3 = 0;
SSPCON.SSPM2 = 1; //SPI Slave mode, clock = SCK pin. SS pin control disabled. SS can
be used as I/O pin.
SSPCON.SSPM1 = 0;
SSPCON.SSPM0 = 1;
//Configurando portas
TRISD = 0b00000000; //A porta D ser usada para vermos o que o Slave est recebendo
void main()
init();
while(1)
cont = 0;
Pgina 9
ITAndroids Ser a melhor equipe de robtica do mundo
1 Master 1 Slave.avi
Cdigo do Master:
//ITAndroids
Pgina 10
ITAndroids Ser a melhor equipe de robtica do mundo
char rec;
char x = 0b00000001;
char y = 0b10000000;
while(SSPCON.WCOL);
SSPBUF = buf;
while(!SSPSTAT.BF);
return SSPBUF;
void init()
//Configurao do SPI
//SSPSTAT:
SSPSTAT.SMP = 0;
SSPSTAT.CKE = 0;
//SSPCON:
SSPCON.SSPEN = 1;
SSPCON.CKP = 0;
SSPCON.SSPM3 = 0;
SSPCON.SSPM2 = 0;
SSPCON.SSPM1 = 0;
SSPCON.SSPM0 = 0;
//Configurando portas
TRISB.F6 = 0;
Pgina 11
ITAndroids Ser a melhor equipe de robtica do mundo
TRISB.F7 = 0;
TRISC.F3 = 0;
TRISC.F4 = 1;
TRISC.F5 = 0;
void main()
init();
while(1)
delay_ms(1);
rec = SPI(x);
delay_ms(500);
x = x << 1;
if(x == 0b00000000)
x = 0b00000001;
delay_ms(1);
rec = SPI(y);
delay_ms(500);
y = y >> 1;
if(y == 0b00000000)
Pgina 12
ITAndroids Ser a melhor equipe de robtica do mundo
y = 0b10000000;
//ITAndroids
char rec;
while(SSPCON.WCOL);
SSPBUF = buf;
while(!SSPSTAT.BF);
return SSPBUF;
void init()
//Configurao do SPI
//SSPSTAT:
SSPSTAT.SMP = 0;
SSPSTAT.CKE = 0;
//SSPCON:
SSPCON.SSPEN = 1;
Pgina 13
ITAndroids Ser a melhor equipe de robtica do mundo
SSPCON.CKP = 0;
SSPCON.SSPM3 = 0;
SSPCON.SSPM2 = 1; //SPI Slave mode, clock = SCK pin. SS pin control enabled.
SSPCON.SSPM1 = 0;
SSPCON.SSPM0 = 0;
//Configurando portas
ADCON1.PCFG3 = 0;
ADCON1.PCFG3 = 1;
ADCON1.PCFG3 = 0;
TRISC.F3 = 1;
TRISC.F4 = 1;
TRISC.F5 = 0;
TRISD = 0b00000000;
void main()
init();
while(1)
rec = SPI(0);
PORTD = rec;
delay_ms(1);
Pgina 14
ITAndroids Ser a melhor equipe de robtica do mundo
Vdeo do funcionamento:
1 Master e 2 Slaves.avi
6. Bibliografia:
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
Pgina 15