Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interfacing PIC16F690 with external serial EEPROM

Status
Not open for further replies.

Emil09

New Member
So I have an spi_main.c file which needs an spi.c file. The spi.c file will have the read and write functions and some delay functions in it. I have some of the functions written in the spi.c file but I stopped because I'm not sure if I'm wasting my time by doing it completely wrong. Maybe someone can give it a look?

Code:
//spi_main.c
#include <stdio.h>
#include <pic.h>
#include <htc.h>
#include "spi.c"
	__CONFIG(INTIO & WDTDIS	& PWRTEN & BORDIS);

void init (void);

/*******************************************/
// MAIN - SPI COMMUNICATION
/*******************************************/
#define Databyte 0x05
#define Datapos 0x03

void main (void)
	{
	char temp;	
	init();
	short_delay(10000);
	while(1)
		{
		// Write and read back...
		write_SPI(Databyte,Datapos); // Write one byte
		delay(100000);
		temp = read_SPI(Datapos); // Read one byte
		
		// Is our SPI interface working...?
		if(temp == Databyte) 
			flash_led_ok(); 
		else
			flash_led_error();

		// Job done, Relax some clock cycles...
			while(1);
		}
	}

/*******************************************/
// INIT - RUN AT STARTUP
/*******************************************/
void init (void)
	{			
		ANSEL=0;
		
		// Port directions
		TRISC0 = 0; //-> Chip select (CS)
		TRISC1 = 1; //-> Serial input to microcontroller
		TRISC2 = 0; //-> Serial clock (SCK)
		TRISC3 = 0; //-> Serial output from microcontroller

		// Init values
		RC0 = 1; // CS high
		RC2 = 0; // SCK low
	}


Code:
// spi.c

/********************************/
// Prototypes
/********************************/
write_SPI(char,int);
read_SPI(int);
void short_delay(int);
void delay(int);
void long_delay(void);
void flash_led_ok(void); 
void flash_led_error(void); 

/********************************/
// Functions
/********************************/
write_SPI(char Databyte, int Datapos)
	{
	char instr,i; 
	RC0 = 0;
	instr = 0x03;
	
	for()i=0;i=<7;i++)
	{
		RC3 = (instr>>(7-i)) & 0x01;	
		toggle_clk();
	}
	  
	
	}

read_SPI(int Datapos)
	{

	}


/*******************************************/
// short delay
/*******************************************/
void short_delay(int)
	{
		unsigned int del;
		for(del=0;del<2000;del++){;}
	}
void delay(int)
	{
		unsigned int del;
		for(del=0;del<2000;del++){;}
	}

/*******************************************/
// long delay, used at init...
/*******************************************/
void long_delay(void)
	{
		unsigned int j;
		for(j=0;j<60000;j++){;}
	}

void flash_led_ok(void)	
	{
	
	}

void flash_led_error(void)	
	{
	
	}

The idea is just to a basic read and write between the microcontroller and the memory module.
 
Last edited:
Feels weird posting again in the thread I started but I think I finished the code needed to demonstrate a read and write to an external EEPROM and I noticed that there are others trying to do the same or similar things.

So the first file contains the main() and init() functions, and the second file contains the write_SPI1 and read_SPI1 functions as well as a couple of functions that flash an LED to indicate if the data read from the EEPROM is the same as the data sent. If the data is in fact the same (if(temp == Databyte)), the LED flashes slowly. If it's not the same, it flashes quickly.

I'd really welcome any comments.

Code:
// spi-main.c
#include <stdio.h>
#include <pic.h>
#include <htc.h>
#include "spi.c"
	__CONFIG(INTIO & WDTDIS	& PWRTEN & BORDIS);

void init (void);

/*******************************************/
// MAIN - SPI COMMUNICATION
/*******************************************/
#define Databyte 0x05
#define Datapos 0x03

int main (void)
	{
	char temp;	
	init();
	//short_delay(10000);
	while(1)
		{
		//Write and read back...
		write_SPI1(Databyte,Datapos); // Write one byte
	//	delay(100000);
       	_delay(100000);
		temp = read_SPI1(Datapos); // Read one byte
		
		// Is our SPI interface working...?
		if(temp == Databyte) 	// Change to != to demonstrate functionality
			flash_led_ok(); 

		else
			flash_led_error();
			
			

		// Job done, Relax some clock cycles...
			while(1);
		

	}
}

/*******************************************/
// INIT - RUN AT STARTUP
/*******************************************/
void init (void)
	{			
		ANSEL=0;
		
		// Port directions
		TRISC0 = 0; //-> Chip select (CS)
		TRISC3 = 1; //-> Serial input to microcontroller
		TRISC2 = 0; //-> Serial clock (SCK)
		TRISC1 = 0; //-> Serial output from microcontroller

		// Init values
		RC0 = 1; // CS high
		RC2 = 0; // SCK low
	}

Code:
// spi.c

/********************************/
// Prototypes
/********************************/
write_SPI1(char,int);
read_SPI1(int);
void short_delay(int);
void delay(int);
void long_delay(void);
void flash_led_ok(void); 
void flash_led_error(void); 

/********************************/
// Functions
/********************************/
 write_SPI1(char Databyte, int Datapos)
	{
    char i;
    RC0 = 0;

for(i=0;i<8;i++)
    {
    RC2 = 1;
    if(i == 6)
    RC1 = 1;
    else
    RC1 = 0;
	RC2 = 0;
	}
for(i=0;i<16;i++)
   {
    RC2 = 1;
    RC1 = (Datapos>>(15-i)) & 0x01;
    RC2 = 0;
   }

for (i=0;i<8;i++)
   {
    RC2 = 1;
    RC1 = (Databyte>>(7-i)) & 0x01;
    RC2 = 0;

   }
}


 read_SPI1(int Datapos)
   {
    char i;
    char t;
    RC0 = 0;

for(i=0;i<8;i++)
    {
    RC2 = 1;
    if(i == 6)
    RC1 = 1;
    else
    RC1 = 0;
	RC2 = 0;
	}
for(i=0;i<16;i++)
   {
    RC2 = 1;
    RC1 = (Datapos>>(15-i)) & 0x01;
    RC2 = 0;
   }

for (i=0;i<8;i++)
   {
    RC2 = 1;
    t = RC3<<i|t;
    RC2 = 0;
			}
   return t;
   
}

	


/*******************************************/
// Just a short delay
/*******************************************/
//void short_delay(int)
//	{
//		unsigned int del;
//		for(del=0;del<2000;del++){;}
//	}
//void delay(int)
//	{
//		unsigned int del;
//		for(del=0;del<2000;del++){;}
//	}

/*******************************************/
// Just a long delay, used at init...
/*******************************************/
//void long_delay(void)
//	{
//		unsigned int j;
//		for(j=0;j<60000;j++){;}
//	}

void flash_led_ok(void)	
	{
	// LED 1 flashes slowly indicating correct function
	TRISC = 0 ; // Make PORTC output
while(1)
{
	PORTC = 0x00; // turn all LEDS off 

	_delay(100000);
	_delay(100000);
	_delay(100000);
	_delay(100000);

	PORTC = 0x01; // turn LED on

	_delay(100000);
	_delay(100000);
	_delay(100000);
	_delay(100000);

}
	}

void flash_led_error(void)	
	{
	// LED 1 flashes quickly indicating an error
	TRISC = 0 ; // Make PORTC output
while(1)
{
	PORTC = 0x00; // turn all LEDS off 

	_delay(100000);

	PORTC = 0x01; // turn LED on

	_delay(100000);
}
	}

**broken link removed**
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top