Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 07, 2016, 11:24:36 11:24


Login with username, password and session length


Pages: [1]
Print
Author Topic: PIC 16F917 AND AT25256 Interface using SPI  (Read 1671 times)
0 Members and 1 Guest are viewing this topic.
hock
Active Member
***
Offline Offline

Posts: 152

Thank You
-Given: 18
-Receive: 68


« on: April 03, 2009, 07:44:16 19:44 »

i am trying to use AT25256 SPI eeprom with PIC 16F917.the first byte generates an error, all other bytes are correct. i am suspecting a bug is in read function. the SPI clock and DATA is visible on CRO. i am not sure if it is correct. do not have a deep memory scope.
can some one please help.

this is the code in main()

# include <htc.h>
# include "pic_function.h"
# include "delay.h"
# include "AT25256.h"

unsigned char i;


main()
{
init_pic(); // init the pic paripheral
init_AT25256();

while(1)
{
write_AT25256(0xBC,0x0100);
i = read_AT25256(0x0100);
if (i == 0xBC)
{
RB3 = 1;
RB4=0;
}
else
{
RB4 = 1;
RB3=0;
}
}
}

this is the code for init_pic


# include <htc.h>
# include "pic_function.h"

__CONFIG(0x20E2); // watch dog timer is off

void init_pic(void)
{
// Oscillator Configuration register
// OSCCON =

//init interrupt register
INTCON = 0xE0;

//init port directions
TRISA = 0xFF;
TRISB = 0xE7;
TRISC = 0xAF;
TRISD = 0xF0;
TRISE = 0xFF;

RB3 = 0;
RB4 = 0;

// init spi

SSPEN = 0;
SSPSTAT |= 0x80;
SSPCON |= 0x10;
SSPBUF = 0x00;
SSPEN = 1;

PEIE = 1; // Enable Peripheral Interrupt Flag
GIE = 1; // enable Global Interrupt Flag

}

this is the code for AT25256

# include <htc.h>
# include "pic_function.h"
# include "delay.h"
# include "AT25256.h"

void init_AT25256(void)
{
ram_cs = 1;
delay_in_us(10);
ram_cs = 0;
write_spi(WREN);
ram_cs = 1;
delay_in_us(10);
ram_cs = 0;
write_spi(WRSR);
write_spi(NOPROT);
ram_cs = 1;
}

void write_AT25256(unsigned char data,unsigned int address)
{
ram_cs = 1;
delay_in_us(10);
ram_cs = 0;
write_spi(WREN);
ram_cs = 1;
delay_in_us(10);
ram_cs = 0;

write_spi(WRITE);
write_spi((address >> Cool);
write_spi((address & 0xFF));
write_spi(data);
ram_cs = 1;
}

unsigned char read_AT25256(unsigned int address)
{
unsigned char data;
ram_cs = 1;
delay_in_us(10);
ram_cs = 0;
write_spi(READ);
write_spi((address >> Cool);
write_spi((address & 0xFF));
data = read_spi();
ram_cs = 1;
return data;
}

void write_spi(unsigned char data)
{
SSPIF = 0;
SSPBUF = data;
while (!SSPIF)
{
}
delay_in_ms(5);
}

unsigned char read_spi(void)
{
unsigned char temp;
SSPIF = 0;
SSPBUF = 0xFF;
while (!SSPIF)
{
}
delay_in_ms(5);
temp=SSPBUF;
return temp;
}
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC