Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC16F886, Hitech C, and SPI?

Status
Not open for further replies.

the site didn't posted the full project for SPI as they given the HEX file and small lines of code that explains how to use SPI, so we can't use the code as it is given in the site, so he wants the help to use in Hitech c
 

@kanni1303: that is where i got my link from

@Venkadesh_M: this is why I'm asking if anyone could help with doing the same with Hitech C
 

I finally got the HW SPI to work. I'm connecting two PIC16F886 one as master the other as slave. I added a push button to the master so it will send an integer through SPI to the slave.

The following is the code for the master (main.c):

Code:
#include <xc.h>

// CONFIG1
#pragma config FOSC = INTRC_NOCLKOUT, WDTE = OFF, PWRTE = OFF, MCLRE = OFF
#pragma config CP = OFF, CPD = OFF, BOREN = OFF, IESO = OFF, FCMEN = OFF
#pragma config LVP = OFF, DEBUG = OFF
// CONFIG2
#pragma config BOR4V = BOR40V, WRT = OFF

// Set Clock Freq. & Delays
#ifndef _XTAL_FREQ
    #define _XTAL_FREQ 20000000
    #define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/20000000.0)))
    #define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/20000.0)))
#endif

void main(void);
void portInit(void);
void SPIInit(void);

#define BTN01 RB7               // Push Button

#define SCK RC3
#define SDI RC4
#define SDO RC5
#define FALSE 0
#define TRUE 1

unsigned char testCounter = 0, dummy;

void Pic_Init(){
    ANSELH = 0;                 // putting LCD on PORTB
    TRISB = 0b10000000;         // push button on RB7
}

void PushButton(){
    __delay_ms(100);
    if(BTN01 == 0){
        SSPBUF = testCounter++;
        while(BF==FALSE) {}
        dummy = SSPBUF;             //reads SSPBUF
        __delay_ms(500);

        if(testCounter ==  50)
            testCounter = 0;
    }
}

void SPIInit(void) {
    INTCON = 0;         // Disable all interrupts
    TRISC3 = 0;         // SCK=RC3 is the serial clock
    TRISC4 = 1;         // SDI=RC4 is serial data input
    TRISC5 = 0;         // SDO=RC5 is serial data output
    SMP = 0;            // Input data sampled at middle data output time
    CKP = 1;            // Idle state for clock is a high level
    CKE = 0;            // Transmit occurs on idle to active clock state
    SSPEN = 1;          // Enables serial port
    SSPM3 = 0;          //
    SSPM2 = 0;          //
    SSPM1 = 0;          //
    SSPM0 = 0;          // SPI Master mode, clock = FOSC/4
    SSPIF = 0;
}

void main(){
    SPIInit();

    __delay_ms(500);

    while(1)
    {
        if(BTN01 == 0) PushButton();
    }
}

on the slave I added a 16x2 LCD to show the output (main.c):
Code:
* Created on October 19, 2014, 3:14 PM
 * 
 * LCD Ports:
 *      D4          RB0
 *      D5          RB1
 *      D6          RB2
 *      D7          RB3
 *      RS          RB4     
 *      RW          RB5     
 *      EN          RB6     
 *
 */

#include <xc.h>

// CONFIG1
#pragma config FOSC = INTRC_NOCLKOUT, WDTE = OFF, PWRTE = OFF, MCLRE = OFF
#pragma config CP = OFF, CPD = OFF, BOREN = OFF, IESO = OFF, FCMEN = OFF
#pragma config LVP = OFF, DEBUG = OFF
// CONFIG2
#pragma config BOR4V = BOR40V, WRT = OFF

// Set Clock Freq. & Delays
#ifndef _XTAL_FREQ
    #define _XTAL_FREQ 20000000
    #define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/20000000.0)))
    #define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/20000.0)))
#endif

void main(void);
void SPIInit(void);

#include "lcd_hd44780_pic16.h"

// SPI Ports
#define SCK RC3
#define SDI RC4
#define SDO RC5
#define FALSE 0
#define TRUE 1

void Pic_Init(){
    ANSELH = 0;                 // putting LCD on PORTB
    TRISB = 0;                  
}

void SPIInit(void) {
    INTCON = 0;         // Disable all interrupts
    TRISC3 = 1;         // SCK=RC3 is the serial clock
    TRISC4 = 1;         // SDI=RC4 is serial data input
    TRISC5 = 0;         // SDO=RC5 is serial data output
    SSPIF = 0;
    SMP = 0;            // Input data sampled at middle data output time
    CKP = 1;            // Idle state for clock is a high level
    CKE = 0;            // Transmit occurs on idle to active clock state
    SSPEN = 1;          // Enables serial port

    SSPM3 = 0;      
    SSPM2 = 1;      
    SSPM1 = 0;      
    SSPM0 = 1;      

}


void main(){
    unsigned char expVal = 0;
    int  errCount = 0;
    unsigned char dummy;

    Pic_Init();
    SPIInit();

    
    LCDInit(LS_NONE);                   //Initialize the LCD Module
    LCDClear();                         //Clear the display

    //Write a string [LCDWriteStringXY(col, row, string)]
    LCDWriteStringXY(0,0,"MegaTroniX");
    __delay_ms(100);
    LCDClear();
    
    while(1)
    {

        while(BF==FALSE){}                  // Wait for SSPBUF Reception complete
        dummy = SSPBUF;                     // Get data byte
        LCDWriteIntXY(0, 0, dummy, -1);
        LCDWriteIntXY(0, 1, expVal, -1);

        if(dummy != expVal){
            errCount++;
            LCDWriteIntXY(8, 1, errCount, -1);
        }
        
        expVal++;
        if(expVal == 50){
            LCDClear();
            expVal = 0;
        }
    }
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top