CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Data storage by SPI on 18F452s

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

Data storage by SPI on 18F452s
PostPosted: Mon May 21, 2007 8:25 pm     Reply with quote

Friends,
I am trying to save data in the EPROM memory of 18f452. The code is very simple which I copied from CCS examples.
One 18F452 is acting as a master and the other 18F452 is emulating 9356 EPROM.
I am trying to write to an address and then read from the same address.
Later, I would put the data on a port for display/debug purpose.
The problem, I am having in Proteus is the following message.

[PIC18] PC=0X0000 $MCLR$ is Low. Processor is in Reset U2(SLAVE)
[PIC18] PC=0X0000 $MCLR$ is Low. Processor is in Reset U2(MASTER)
Does it mean that I have to power the MCLR pin.

Both the codes, the master/slave is pasted below.

'===============U2(SLAVE)====================
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C1, rcv=PIN_C0)

int8 memory[80], data, instr, address;

/*
void write_data(void)
PURPOSE: Reads data from SPI and writes it to memory
PARAMS: none
RETURNS: None
*/
void write_data(void)
{
while(!spi_data_is_in());

data = spi_read();

if(address >= 0 || address <= 80)
{
memory[address] = data;
}
}

/*
BYTE read_data(void)
PURPOSE: To read the memory
PARAMS: none
RETURNS: Data stored in memory
*/
BYTE read_data(void)
{
return (memory[address]);
}

/*
void main(void)
PURPOSE: Peripheral Initialization
*/
void main(void)
{
setup_spi(spi_slave | spi_h_to_l | spi_ss_disabled);

while(true)
{
while(!spi_data_is_in());
instr = spi_read();

while(!spi_data_is_in());
address = spi_read();

if (instr == 0x18)
{
instr = spi_read(memory[address]);
}
else if(instr == 0xa)
{
write_data();
}
}
}


'==============U1(Master)==========================

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_A0, rcv=PIN_A1)

#include <9356spi.c>
BYTE PORT_A;

void main()
{
int8 value;
EEPROM_ADDRESS address;

address=1;

init_ext_eeprom();

do
{
WRITE_EXT_EEPROM( address, 35 );
value=READ_EXT_EEPROM( address );
PORT_A=value;
delay_ms(500);
PORT_A=0;

} while (TRUE);
}
'======================Code End==================
Thanks.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 22, 2007 1:01 am     Reply with quote

When posting code please use the 'code' buttons to preserve formatting.

Quote:
The problem, I am having in Proteus is the following message.

[PIC18] PC=0X0000 $MCLR$ is Low. Processor is in Reset U2(SLAVE)
[PIC18] PC=0X0000 $MCLR$ is Low. Processor is in Reset U2(MASTER)
Does it mean that I have to power the MCLR pin.
Use the search function of this forum and type 'proteus MCLR', select the 'search for all terms'option. This will give you 5 hits. Read http://www.ccsinfo.com/forum/viewtopic.php?t=28419&highlight=proteus+mclr

Short: Yes, you have to connect MCLR to the positive power supply.

Your check for memory buffer overflow is incorrect.
Code:
if(address >= 0 || address <= 80)
Change this to
Code:
if(address >= 0 || address < 80)
AdamkT1



Joined: 21 Apr 2007
Posts: 44

View user's profile Send private message

SPI between two 18F452
PostPosted: Wed May 23, 2007 9:16 am     Reply with quote

Hi friends,

Still having troubles with SPI between two 18F452s. The slave is emulating 9356 EEPROM.
Below is the modified code. I hope somebody would help me solve this problem.
The error(s):
1) In Proteus, the error is : [PIC18 MSSP] PC=0X0052 MSSP slave mode with no SS pin control and CKE==1 is ambiguous U2(SLAVE)
2) Data does not show up on PORTB as I intend to. In fact nothing happen to the PORT.

Code:

=========SLAVE==========

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

int8 memory[80], data, instr, address;

/*
void write_data(void)
PURPOSE: Reads data from SPI and writes it to memory
PARAMS: none
RETURNS: None
*/
void write_data(void)
{
   while(!spi_data_is_in());
   
   data = spi_read();
   
   if(address >= 0 && address <= 80) // AND operator seems logical here.(atleast to me)
   {
      memory[address] = data;
   }
}

/*
BYTE read_data(void)
PURPOSE: To read the memory
PARAMS: none
RETURNS: Data stored in memory
*/
BYTE read_data(void)
{
    return (memory[address]);
}

/*
void main(void)
PURPOSE: Peripheral Initialization
*/
void main(void)
{
   setup_spi(spi_slave | spi_h_to_l | spi_ss_disabled);

   while(true)
   {
      while(!spi_data_is_in());
      instr = spi_read();
     
      while(!spi_data_is_in());
      address = spi_read();

      if (instr == 0x18)
      {
         instr = spi_read(memory[address]);
      }
      else if(instr == 0xa)
      {
         write_data();
      }
   }
}

=====================Master================

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <9356spi.c>


void main()
{
   byte value;
   EEPROM_ADDRESS address;
   SET_TRIS_B(0); // make PORTB all outputs.
   PORTB=0;
   address=1;
   value=35;
   init_ext_eeprom();

   do
   {   
         PORTB=0;
         delay_ms(500);
         WRITE_EXT_EEPROM( address, value );
         PORTB=READ_EXT_EEPROM(address);
         delay_ms(500);
   } while (TRUE);
}
==========9365SPI.C=========

#define EEPROM_SELECT PIN_B0
#define EEPROM_DI     PIN_C5
#define EEPROM_DO     PIN_C4
#define EEPROM_CLK    PIN_C3

#define EEPROM_ADDRESS BYTE
#define EEPROM_SIZE    256


void init_ext_eeprom()
{
   short int i;

   output_low(EEPROM_DI);
   output_low(EEPROM_CLK);
   output_low(EEPROM_SELECT);
   i=input(EEPROM_DO);

   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);

   output_high(EEPROM_SELECT);
   spi_write(0x9);
   spi_write(0x80);
   output_low(EEPROM_SELECT);
}


void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data)
{

   output_high(EEPROM_SELECT);
   spi_write(0xa);
   spi_write(address);
   spi_write(data);
   output_low(EEPROM_SELECT);
   delay_ms(11);
}


BYTE read_ext_eeprom(EEPROM_ADDRESS address)
{
   BYTE data;

   output_high(EEPROM_SELECT);
   spi_write(0x18);
   spi_write(address);
   data=spi_read(0);
   output_low(EEPROM_SELECT);

   return(data);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group