Author Topic: Need help using ENC28J60 and PIC16F1939 SPI  (Read 1019 times)

0 Members and 1 Guest are viewing this topic.

Offline MoondeckTopic starter

  • Regular Contributor
  • *
  • Posts: 142
  • Country: dk
  • i really like compilers
Need help using ENC28J60 and PIC16F1939 SPI
« on: November 22, 2015, 02:14:32 pm »
Hi everyone,
so, i have this board with a PIC16F1939 and an ENC28J60 chip on it, and i have tried to write some code for it, but it does not seem to work. Its supposed to make the 2 ethernet LEDs blink slowly. Here is the code:

spicom.c
Code: [Select]
#include <xc.h>
#define _XTAL_FREQ 32000000
void SPI_init(void)
{
    LATA = 0x00; //Used Microchip Code Configurator to figure out the pins (for now)
    TRISA = 0xFF;
    ANSELA = 0x3F;
    LATB = 0x00;
    TRISB = 0xFF;
    ANSELB = 0x3F;
    WPUB = 0x00;
    LATC = 0x00;
    TRISC = 0xD7;
    LATD = 0x00;
    TRISD = 0xFF;
    ANSELD = 0xFF;
    LATE = 0x00;
    TRISE = 0x0F;
    ANSELE = 0x07;
    WPUE = 0x00;
    OPTION_REGbits.nWPUEN = 0x01;
    APFCON = 0x00;
    SSPSTAT = 0x00;
    SSPCON1 = 0x20;
    SSPADD = 0x00;
}

void SPI_writechar(char writechar)
{
    SSPBUF = writechar;
}

void ENC_WCR(char adr,char value)
{
    SSPBUF = (adr + 0b01000000);
    __delay_ms(1);
    SSPBUF = value;
}

main.c
Code: [Select]
#include <xc.h>
#include "spicom.h"

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config VCAPEN = OFF     // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)b

#define _XTAL_FREQ 32000000

void main(void)
{
    OSCCONbits.IRCF = 0b1110; //8 MHz, 32MHz with x4 PLL
    TRISA = 0;
    TRISB = 0;
    TRISC = 0;
    SPI_init();
    ENC_WCR(0x1F,0b11010010); //set Bank 3 in the ENC28J60
    ENC_WCR(0x14,0x14); //set MIREGADR to PHLCON address
    ENC_WCR(0x16,0b10111000);
    ENC_WCR(0x17,0b00001011);
    while(1);
}

spicom.h
Code: [Select]
#include <xc.h>

void SPI_init(void);
void SPI_writechar(char writechar);
void ENC_WCR(char adr,char value);
I have moved the topic from the Microcontroller & FPGA section to here
I'm selling 100ml bottles of free energy, PM me for pricing.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf