So I've tidied up the code a bit:
- Made the ASCII hex to value code a lot shorter
- Removed a lot of the repeated code into functions
I've found a bit of inconsistency - have a look at for the WHY IS THIS "00" comment towards the end.
I also think you should call bcm2835_init(); only once, but you seem to be calling it every time you send a byte,...
#include <stdio.h>
#include "bcm2835.h"
// bcm2835_delayMicroseconds()
// Blinks on RPi Plug P1 pin 11 (which is GPIO pin 17)
#define MOSI RPI_GPIO_P1_19 // MOSI DATA 8 bit
#define SCK RPI_GPIO_P1_23 // SCK Clock
#define SS_CS RPI_GPIO_P1_24 // CS Slave slect
#define CD RPI_GPIO_P1_21 // CD command select MISO
void DEF ()
{
bcm2835_init();
bcm2835_gpio_fsel(CD, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(SS_CS, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(MOSI, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(SCK, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(CD, LOW);
bcm2835_gpio_write(SS_CS, LOW);
bcm2835_gpio_write(MOSI, LOW);
bcm2835_gpio_write(SCK, LOW);
bcm2835_close();
}
void SPI8(char* hexdec)
{
int value, i;
int DATA [8]; //8 biten megy a kommmunikáció
fprintf(stderr,"Sending %s",hexdec);
if(hexdec[0] >= '0' && hexdec[0] <= '9') {
value = hexdec[0] - '0';
} else if(hexdec[0] >= 'A' && hexdec[0] <= 'Z') {
value = hexdec[0] - 'A' + 10;
} else if(hexdec[0] >= 'a' && hexdec[0] <= 'z') {
value = hexdec[0] - 'a' + 10;
} else {
fprintf(stderr,"Bad hexdec[0]\n");
return;
}
value *= 16;
if(hexdec[1] >= '0' && hexdec[1] <= '9') {
value += hexdec[1] - '0';
} else if(hexdec[1] >= 'A' && hexdec[1] <= 'Z') {
value += hexdec[1] - 'A' + 10;
} else if(hexdec[1] >= 'a' && hexdec[1] <= 'z') {
value += hexdec[1] - 'a' + 10;
} else {
fprintf(stderr,"Bad hexdec[1]\n");
return;
}
for(i=0; i < 8; i++) {
DATA[i] = (value & (1<<i)) ? 1 : 0;
}
bcm2835_init();
bcm2835_gpio_fsel(MOSI, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(SCK, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(SS_CS, BCM2835_GPIO_FSEL_OUTP);
for(int i = 0; i < 8; i++) {
bcm2835_gpio_write(MOSI, DATA[i]); //bcm2835_delayMicroseconds(1);
bcm2835_gpio_write(SCK, HIGH); //SCK UP
bcm2835_delayMicroseconds(1);
bcm2835_gpio_write(SCK, LOW); //SCK DOWN
}
bcm2835_gpio_write(MOSI, LOW);//defaul helyzet
bcm2835_close();
}
void ENABLE_H ()
{
bcm2835_init();
bcm2835_gpio_fsel(SS_CS, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(SS_CS, HIGH);
bcm2835_close();
}
void ENABLE_L ()
{
bcm2835_init();
bcm2835_gpio_fsel(SS_CS, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(SS_CS, LOW);
bcm2835_close();
}
void CD_H ()
{
bcm2835_init();
bcm2835_gpio_fsel(CD, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(CD, HIGH);
bcm2835_close();
}
void CD_L ()
{
bcm2835_init();
bcm2835_gpio_fsel(CD, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(CD, LOW);
bcm2835_close();
}
void send_data_block(char *a, char *b, char *c, char *d) {
ENABLE_L ();
SPI8 (a) ;
SPI8 (b) ;
SPI8 (c) ;
CD_H ();
// An idle pattern to waste some time????
for(int i = 0; i < 128; i++) {
SPI8 (d) ;
}
CD_L ();
ENABLE_H ();
}
void display_on(void) {
CD_L ();
ENABLE_L ();
SPI8 ("AF") ;
ENABLE_H ();
CD_L ();
}
int main()
{
DEF (); // all out to 0
ENABLE_H ();// if starts it the currect pos
ENABLE_L ();
CD_L ();
SPI8 ("AE") ;
SPI8 ("DC") ;
SPI8 ("00") ;
SPI8 ("81") ;
SPI8 ("2F") ;
SPI8 ("20") ;
SPI8 ("A0") ;
SPI8 ("C0") ;
SPI8 ("A8") ;
SPI8 ("7F") ;
SPI8 ("D5") ;
SPI8 ("50") ;
SPI8 ("D9") ;
SPI8 ("22") ;
SPI8 ("DB") ;
SPI8 ("35") ;
SPI8 ("B0") ;
SPI8 ("DA") ;
SPI8 ("12") ;
SPI8 ("A4") ;
SPI8 ("A6") ;
ENABLE_H ();
bcm2835_delay(1);
CD_L ();
// SCR INI KÉSZ
send_data_block("10","00","B0","FF");
send_data_block("10","00","B1","00"); // << WHY IS THIS "00"?
send_data_block("10","00","B2","FF");
send_data_block("10","00","B3","FF");
send_data_block("10","00","B4","FF");
send_data_block("10","00","B5","FF");
send_data_block("10","00","B6","FF");
send_data_block("10","00","B7","FF");
send_data_block("10","00","B8","FF");
send_data_block("10","00","B9","FF");
send_data_block("10","00","BA","FF");
send_data_block("10","00","BB","FF");
send_data_block("10","00","BC","FF");
send_data_block("10","00","BD","FF");
send_data_block("10","00","BE","FF");
send_data_block("10","00","BF","FF");
display_on();
display_on();
/// data
send_data_block("15","08","B0","FF");
send_data_block("10","00","B1","FF");
}