Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bitbang MLX90316 on SPI1
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdint.h>
#include <math.h>
#include "pigpio.h"
/*
gcc -o bitbangspi bitbangspi.c -lpigpio -lrt -lpthread
sudo ./bitbangspi
*/
#define CE0 26
#define MISO 19
#define MOSI 20
#define SCLK 21
int main(int argc, char *argv[])
{
int i, count, set_val, read_val;
unsigned char inBuf[1];
char cmd1[] = {0x55};
char cmd2[] = {0};
uint16_t val;
if (gpioInitialise() < 0)
{
fprintf(stderr, "pigpio initialisation failed.\n");
return 1;
}
gpioWrite(18, 1);
bbSPIOpen(CE0, MISO, MOSI, SCLK, 250000, 0);
while (1) {
val = 0;
gpioWrite(18, 0);
usleep(2);
count = bbSPIXfer(CE0, cmd1, (char *)inBuf, 1);
usleep(1);
count = bbSPIXfer(CE0, cmd2, (char *)inBuf, 1);
usleep(1);
count = bbSPIXfer(CE0, cmd2, (char *)inBuf, 1);
usleep(1);
val = ( inBuf[0] << 8 );
count = bbSPIXfer(CE0, cmd2, (char *)inBuf, 1);
val = val | inBuf[0];
printf("0x%04x\n", val);
usleep(3);
gpioWrite(18, 1);
usleep(300);
}
bbSPIClose(CE0);
gpioTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.