SPI between pic16f18346 and pic16f1829

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
Braddu
Posts: 1
Joined: 28 Mar 2023 03:37

SPI between pic16f18346 and pic16f1829

#1 Post by Braddu » 28 Mar 2023 04:11

Hi all
I am trying to communicate these two pics, 16F18346 as Master and 16F1829 as Slave. The exersice is simple master sends 1 and 0 every 5s and turns an LED on and off on slave. The result is nothing. I mean nothing happening. I am on it two days and my ideas are finished. If someone can see the code and if he can see anyyhing that can help.

Code: Select all

THIS is SLAVE code

/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/


#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif


#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>
#include <pic16f1829.h>       /* For true/false definition */


#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp */


/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/
#define _XTAL_FREQ   4000000
#define LED          PORTBbits.RB7


/* i.e. uint8_t <variable_name>; */
char incoming = 0;


/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
void SPI_Slave_Init();
void SPI_Write(unsigned char data);
unsigned char SPI_Read();


void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();
    // SCS INTOSC; SPLLEN disabled; IRCF 4MHz_HF; 
    OSCCON = 0x6A;
    // TUN 0; 
    OSCTUNE = 0x00;
    // SBOREN disabled; 
    BORCON = 0x00;


    /* Initialise I/O and Peripherals for application */
    InitApp();
    SPI_Slave_Init();
    LED = 0;  //LED = off
    
    
    while(1)
    {
        incoming = SPI_Read();
        switch(incoming){
        case 1: LED = 1; //LED on
        break;
        case 0: LED = 0; //LED off
        break;
        }
    }


}


void SPI_Slave_Init()
{
    // SSPEN=1  Enable spi, SSPM=0b0101 SPI Slave mode,SSx pin control disabled
    SSP1CON1 = 0x25;
    // CKE=1, SMP=0 Input data sampled at middle of data output time
    SSP1STAT = 0x40;
    SSP1ADD = 0x00;
    
    TRISB = 0x70;// 0b0111 000 RB4->SDI, RB6->SCK, RB7->LED
    TRISC = 0x0F;// 0b0000 1111 RC7->SDO
    ANSELB = 0x20;// 0b0010 0000
    ANSELC = 0x4F;// 0b0100 1111
    
    /**
    APFCONx registers
    */
    APFCON0 = 0x00;
    APFCON1 = 0x00;
    
    TRISBbits.TRISB6 = 1;
    
}


void SPI_Write(unsigned char data)
{
    SSP1BUF = data;            /* Copy data in SSBUF to transmit */
    while(!SSP1STATbits.BF);
  
}


unsigned char SPI_Read()
{    
    while(!SSP1STATbits.BF);	/* Wait for complete 1 byte transmission */
    return(SSP1BUF);		/* Return received byte */   
}

Code: Select all

THIS is MASTER code

/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/


#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif


#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */


#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/
#define _XTAL_FREQ   4000000


/* i.e. uint8_t <variable_name>; */


/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/
void SPI_Master_Init();
void SPI_Write(unsigned char spidata);
unsigned char SPI_Read();
unsigned char DataRdySPI( void );


void main(void)
{
    /* Configure the oscillator for the device */
    ConfigureOscillator();
    // NOSC HFINTOSC; NDIV 4; 
    OSCCON1 = 0x62;
    // CSWHOLD may proceed; SOSCPWR Low power; SOSCBE crystal oscillator; 
    OSCCON3 = 0x00;
    // LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled; 
    OSCEN = 0x00;
    // HFFRQ 4_MHz; 
    OSCFRQ = 0x03;
    // HFTUN 0; 
    OSCTUNE = 0x00;


    /* Initialise I/O and Peripherals for application */
    InitApp();
    SPI_Master_Init();
    
    while(1)
    {
       
        SPI_Write(0x01);
        __delay_ms(5000);
        
        SPI_Write(0x00);
        __delay_ms(5000);
    }


}


void SPI_Master_Init()
{
	// SSPEN=1, CKP=0, SSPM=0b0000 SPI Master mode, clock = FOSC/4
    SSP1CON1 = 0x20;
    // CKE=1, SMP=0 Input data sampled at middle of data output time
    SSP1STAT = 0x40;
    SSP1ADD  = 0x01;
    // Configure The IO Pins For SPI Master Mode
    TRISB = 0xB0; // 0b1011 0000  RB4->SDI, RB6->SCK
    TRISC = 0xBF; // 0b1011 1111 RC6->SDO
    ANSELB = 0xA0;// 0b1010 0000
    ANSELC = 0xB0;// 0b1111 1111
    
    SSP1CLKPPS = 0x0E;  // RB6->MSSP1:SCK1;    
    RB6PPS = 0x18;      // RB6->MSSP1:SCK1;    
    RC6PPS = 0x19;      // RC6->MSSP1:SDO1;    
    SSP1DATPPS = 0x0C;  // RB4->MSSP1:SDI1;
 
}


void SPI_Write(unsigned char spidata)
{
    unsigned char Flush_Bufer;
    SSP1BUF = spidata;
    while(!SSP1STATbits.BF);
    Flush_Bufer = SSP1BUF;
}


unsigned char SPI_Read()
{    
    while(!SSP1STATbits.BF);	/* Wait for complete 1 byte transmission */
    return(SSP1BUF);		/* Return received byte */
}

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: SPI between pic16f18346 and pic16f1829

#2 Post by filip » 28 Mar 2023 09:33

Hi,

Please search through LibStock, there are several SPI slave examples for various MCUs :
https://libstock.mikroe.com

Regards,
Filip.

Post Reply

Return to “PIC PRO Compilers”