Estoy probando SPI de mi microcontrolador PIC24E512GU814. Para probar, conecté MOSI a MISO.
Escribí el siguiente código para la inicialización de SPI:
SPI3CON1 = 0;
SPI3CON1bits.CKE = 1;
SPI3CON1bits.SPRE = 7;
SPI3CON1bits.PPRE = 3;
SPI3CON1bits.MSTEN = 1;
SPI3STATbits.SPIEN = 1;
Luego para transmitir/recibir el siguiente código:
SPI3_ENABLE = 0; //chip select
delayUs(1);
while (SPI3STATbits.SPITBF);
SPI3BUF = test;
while (!SPI3STATbits.SPIRBF)
{
if (--timeout == 0)
{
debugOutput(0, "\r\nTimeout SPI 3\r\n");
}
}
temp = SPI3BUF;
debugOutput(0, "\r\nValue read on SPI 3 Port:%d\r\n", temp);
SPI3_ENABLE = 1;
Siempre obtengo la mitad del valor transmitido. Por ejemplo, si transmito 54, estoy leyendo 27 en el búfer de recepción. ¿Cuál puede ser el problema?
Modificado el código a lo siguiente no ayudó
delayUs(1);
//writeSPI(eSpiPort3, &test, sizeof(byte));
debugOutput(0, "\r\nWriting on SPI 3 Port:%d\r\n", test);
SPI3_ENABLE = 0;
while (SPI3STATbits.SPITBF);
SPI3BUF = test;
while (!SPI3STATbits.SPIRBF)
{
if (--timeout == 0)
{
debugOutput(0, "\r\nTimeout SPI 3\r\n");
}
}
temp = SPI3BUF;
SPI3_ENABLE = 1;
delayUs(1);
debugOutput(0, "\r\nValue read on SPI 3 Port:%d\r\n", temp);
Ha configurado los bits PPRE y SPRE en el registro SSPI3CON1 a 1:1. Esto es de la página 9 del manual de referencia .
No configure los preescaladores primario y secundario al valor de 1:1 al mismo tiempo.
abel tom
md.jamal
Pedro Smith
md.jamal
abel tom
md.jamal