|
|
View previous topic :: View next topic |
Author |
Message |
adolforp2
Joined: 23 Apr 2017 Posts: 3
|
SPI reading values to EEPROM register |
Posted: Sun Apr 23, 2017 4:53 pm |
|
|
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: 19546
|
|
Posted: Mon Apr 24, 2017 12:48 am |
|
|
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
|
|
Posted: Sat Apr 29, 2017 8:42 am |
|
|
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: 19546
|
|
Posted: Sat Apr 29, 2017 12:20 pm |
|
|
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
|
|
Posted: Sat Apr 29, 2017 1:18 pm |
|
|
Thanks a lot Ttelmah. I will try it |
|
|
|
|
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
|