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 CCS Technical Support

SPI reading values to EEPROM register

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



Joined: 23 Apr 2017
Posts: 3

View user's profile Send private message

SPI reading values to EEPROM register
PostPosted: Sun Apr 23, 2017 4:53 pm     Reply with quote

Hi. This is my first post. I am working on a PIC16F1829 and having some issues (I am newer with C actually). I am reading a value from a master by SPI interruption and saving that value in a specific address of eeprom (works fine).

Problem is that I need to receive 16 bytes and save one by one in 16 specific address. I think I need a routine or something.

Part of code:
Code:

Spi int {
    Value= SSP1BUF;
     //write_eeprom(address, value);  << Routine??

}

Probably I need to create an array of int and them save them to all to address I need, but I don't know where to put it.

Thanks guys
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Mon Apr 24, 2017 12:48 am     Reply with quote

You don't exactly give us much to work on. However you already have the answer.
Your code shown is not syntactically correct.

The SPI interrupt says _one_ byte has arrived.

How does the master say 'start' of the data (chip select signal or something)?.
If so, then you need to have a counter, and reset this when this signal is seen.
Then you need a RAM array of 16 bytes (perhaps called 'temp_array'), and in your routine, use:

temp_array[counter++]= SSP1BUF;

Then when the counter says you have reached the end of the data, or when the master turns off the 'select', write this.

As a further comment, consider using the C routines, rather than accessing registers directly. Makes the code much more portable.
adolforp2



Joined: 23 Apr 2017
Posts: 3

View user's profile Send private message

PostPosted: Sat Apr 29, 2017 8:42 am     Reply with quote

This is my entire code.

Slave code:
Code:
#include <16f1829.h>

#fuses xt, nowdt   //xt para osc = 4 MHz, hs > a 4 MHz
#use delay(clock= 4M)

#define led_vida pin_c4
#define ss1 pin_c6


int8 v1 = 4, v2 = 3, value, i=0;
int8 mainRegister[6];

//Registers address for 16F1829
#byte SSP1BUF = 0x211
#byte APFCON0 = 0x11D
#byte APFCON1 = 0x11E

//SPI mode definitions
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

void writeMemoryEEPROM() {
   write_eeprom (0x10, mainRegister[0]); // address, value
   write_eeprom (0x11, mainRegister[1]);
   write_eeprom (0x12, mainRegister[2]);
   write_eeprom (0x13, mainRegister[3]);
   write_eeprom (0x14, mainRegister[4]);
   write_eeprom (0x15, mainRegister[5]);
   write_eeprom (0x16, 0x96);
   write_eeprom (0x17, 0xF4);
   write_eeprom (0x18, 0x31);
   write_eeprom (0x19, 0xD3);
   write_eeprom (0x20, 0x21);
   write_eeprom (0x21, 0x00);
   write_eeprom (0x30, 0x03);
   write_eeprom (0x31, 0x45);
   write_eeprom (0x40, 0x65);
   write_eeprom (0x41, 0x85);
}
#int_ssp

void ssp_isr() {
   while(spi_data_is_in()) {
   mainRegister[i] = SSP1BUF;
   i++;
   }
   writeMemoryEEPROM();
}

void main() {
   output_high(led_vida);
   
   //Initialize the hardware SPI for SPI master mode.
   setup_spi(SPI_SLAVE|SPI_L_TO_H);
   delay_ms(100);

   //Enable interrupts for the SPI slave.
    clear_interrupt(INT_SSP);
    enable_interrupts(INT_SSP);
    enable_interrupts(GLOBAL);
   
   while(true) {
   
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19561

View user's profile Send private message

PostPosted: Sat Apr 29, 2017 12:20 pm     Reply with quote

The big mistake is trying to write in the spi routine.

Understand that SPI is potentially very fast. Writing to the EEPROM takes longer than hundreds of bytes can be sent using SPI.

Your spi routine just needs:
Code:

#int_ssp
void ssp_isr(void)
{
   mainRegister[i++] = read_ssp();
}


Then in your main code, test for 'i' getting to the final count, and when this happens, write the data, and reset the count.

However 'caveat'. There really needs to be some checking, otherwise, even a single accidentally received clock, will result in everything going out of sync...
There also needs to be a pause long enough for the write, between the data 'blocks'.
adolforp2



Joined: 23 Apr 2017
Posts: 3

View user's profile Send private message

PostPosted: Sat Apr 29, 2017 1:18 pm     Reply with quote

Thanks a lot Ttelmah. I will try it
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