Friday, October 2, 2015

PIC 16F877 SPI porotocol using MPLAB IDE SPI write to 74HC164(SIPO).

Hi how are you guys doing.. 

This is the tutorial and code for the PIC 16F877,(MSSP moduel) SPI protocol in master mode interfaced with 74HC164( a serial in parallel out shift register). I think this code helps you guys in your project developments.  code works perfectly fine for any development. the clock frequency is Fosc/4.

Description of the code: the code below sends numbers from 0-254 to the 74HC164. If you want a detailed tutorial please comment below. (Code is self explanatory)

IDE Used: MPLABIDE and Proteus

SPI configuration for master mode:
CKE =0 ; SMP=0; CKP=1. 

Circuit Diagram: http://i61.tinypic.com/zx8swy.jpg


Source Code:

// PIC16F877 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = EXTRC     // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF         // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low Voltage In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = ON         // FLASH Program Memory Write Enable (Unprotected program memory may be written to by EECON control)

void delay();

void main()

    TRISC=0x00;
    PORTC=0x00;
   delay();delay();delay();
   delay();delay();delay();
   PORTC=0xff;
    TRISC |= 0<<3 | 0<<5| 1<<4;
    PORTC=
    SSPCON=0x00;
    SSPSTATbits.SMP=0;
    SSPSTATbits.CKE=0;
    SSPCONbits.CKP=1;
    TRISA=0x00;
    PORTAbits.RA5=1;
    ADCON0bits.ADON=0;     
    ADCON1 = 0x0F;                         
    INTCONbits.GIE=1;
    PIE1bits.SSPIE=1;
    PIR1bits.SSPIF=0;
    SSPCONbits.SSPEN=1;
    unsigned char p=0x00;
  
    TRISB=0x00;
    PORTB=0xff;
   
   
   
   
    while(p<254)
    {
      
       delay();
       delay();
       delay();
       delay();
       p++;
       SSPIF=0;
       PORTAbits.RA5=0;
       PORTB=p;
    
       SSPBUF=p;
       delay();
       delay();
       while(!SSPIF);
   delay();
       delay();
       delay();
       PORTAbits.RA5=1;
       delay();
       delay();
       delay();
      // SSPIF=0;
  
      
    }
   // Write your code here
   
}
 void delay()
 {
    unsigned int i;
    for(i=0;i<100;i++){}
    }

 void interrupt ISR()
 {
     SSPIF=0;
 }


 // please use the code, I am open sourcing.
//To say I developed it on my self, so just comment me if you have any issues with the code.

No comments:

Post a Comment