10 years, 12 months ago.

How to write Hex-Variable via SPI?

Hi @ all, since a few weeks im kinda struggling to write a Variable on the SPI Bus, adressing a Shift Register. For that i need to write in HEx to have better controll, of what the Register does. Now , ive been checking the forum since quiet a while and either im totally blind, or there hasnt anyone ever had that trouble (which is kind of hard to believe). anyhow herre is my code:

writing on the SPI with SPI

#include "mbed.h"

DigitalIn tast(p20);

unsigned char a=0x00;

int main()
{
    send(0xff);
    while(1) {
        if (tast==1) {
            send(a);
            a++;
       } 
    }
}

What its supposed to be is: When I first supply the board, all Pins are to be high on the Shift Register. When i now press the Button, the bus has to send the Content of a, and ad 1 to a. So the first steps of the pattern would be like 0x00, 0x01, 0x02, 0x03 with the Shift register showing the same)

What it actually does is: When i supply the Voltage all Ports on the register go high. If i now press the button the pattern of the Shift Register looks like this: 0x00, 0x80, 0x01, 0x81, 0x02 etc. In other words the Shift register reacts as if the 7th pin is the first, the first acting as second, and so on.

One might argue now, that its the fault of how the register is internal circuied, but a friend of mine did check that one with simple shifts of 1's. The order was the way it was supposed to be. (0x00, 0x01, 0x02, 0x03 ...)

However my actuall question is: Do i really need to send my whole bunch of ones and zeros via shifts, before i send it, or is there another way to send it in the actuall way?

Would be thankfull for some advice. Chibbot

Question relating to:

3 Answers

Ralle Wabe
poster
10 years, 12 months ago.

:/ Sorry Wim, ure right, i totally overlooked that one x.x Here is the Sendfunction for the SPI

Send Function for SPI

#include "mbed.h"
#include <I2C.h>
#include <SPI.h>

SPI spi(p5,p6,p7);
DigitalOut led1(LED1);


int send(unsigned int i)
{int n=0;
    spi.format(8,3);
    spi.frequency(10000);
    led1=1;
    spi.write(i);
    wait_ms(500);
    led1=0; 
}

I dont have a debounced button avaible at the moment, but just as you said Wim the delay function is helping me out big deal there. Its also that if i keep the button pressed, the Function will be called repeatedly, and the LED's shift is quiet nice visible. the Shift Register type its a 74HC595N. http://www.nxp.com/documents/data_sheet/74HC_HCT595.pdf

As for the question of the Pin connection: Pin5 of the Mbed, declared as Mosi goes directly to Pin14 (Datashift). SCK of the mbed(Pin 7) is serving simultaniously as Register and shift clock (Pins 11 and 12 of the SR) The Master Reset is on VCC to gurantee full access to the Register, likewise the Output enable is COnnected to Ground for the Same Reason (Pins 10 and 13) For the POrts, Q0 to Q7 (Pins 15 + 1 to 7) are connected to ground via 1k Resistors and LED's And not to tell the supply of the sr is3.3V from the mbed, as well as the gnd (pins 16 and 8) All Outputs of the register are fully accessble and do react if checked by programm, or manually. The only thing is that with the programm the pattern looks quiet odd.(see upper description)

The Format...well: i want to write 8Bit, so ive set it to 8 there, and tsince i need same polarity, and phase i set it 3. Any mistakes so far? o.o

Accepted Answer

The problem is that you connect shiftclock (pin 11) and regclock/latch (pin 12) together. They should be separated: pin11 shiftclock needs to connect to mbed p7 sck, pin12 regclock needs to go to an mbed digital output like p8. This output should go low before you send data via spi, and go high after it has been send to latch it in the shiftregister. The separate latch activation will also prevent random bits appearing on the registeroutputs while you shift in the new value. Checkout my schematic and software in the cookbook under 'starburst led display'.

Regarding format, I use format(8,0) for the 74595. Also note that you dont need to call the format and freq with every send. You have to do that only once as part of initialisation.

posted by Wim Huiskamp 28 Apr 2013

thanks a lot for that one:D works verry well.

posted by Ralle Wabe 01 May 2013
10 years, 12 months ago.

Please provide code for your spi declaration and send function. Are you sure the spi format is set correctly? Are you sure you are measuring the correct pins of the shiftregister, which device is that, a portexpander like mcp23xxx or a 74595 or something else?

Seems to me that code above could generate whole bunch of outputs as long as you press button. A long delay inside the loop is a quick fix, but you should use debouncing and edgedetection.

10 years, 9 months ago.

/media/uploads/masi87/project.txt

hi all i have a small problem. i need make to communicate a master uPc(picMicro) with a slave rfid EM4325,for write the eeprom of EM4325 but i don't know which are the memory location where i must write, and i don't know which are the string of command to send for write a byte. you can help me please? i attach code here

  1. include <Project.h>
  2. USE SPI (MASTER, SPI1, MODE=0, BITS=8, STREAM=SPI_1, MSB_FIRST)
  3. int_EXT
  4. define SS PIN_A3
  5. define EEPROM_CLK PIN_A1
  6. define EEPROM_DO PIN_A0
  7. define EEPROM_ADDRESS BYTE int SWE=0xE8; void EXT_isr(void) {

}

void EXT_isr(void) {

}

  1. int_TIMER0 void TIMER0_isr(void) {

} routine write memory rfid

/* write_to_rfid(a,d) riceve il l'indirizzo in cui scrivere e il dato*/

void write_to_rfid(EEPROM_ADDRESS address, BYTE data) {

output_low(SS);

delay_us(15); minimo ritardo dal datasheet dato un clk da 8Mhz

spi_write(SWE);

spi_write(address);

spi_write(data); delay_us(0.15);

output_low(SS);

SWE=0;

}

void main() {

Setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_bit); 128 us overflow setup_comparator(NC_NC_NC_NC); This device COMP currently not supported by the PICWizard enable_interrupts(INT_EXT); enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); int i=0;

while(1){

for(i=0;i<50;i++)

write_to_rfid(i,1);

}

}