Skip to content

Commit

Permalink
makefile and spilib and tiltbuzz_3
Browse files Browse the repository at this point in the history
adding the source for the final phase of this project.
we allow the addition of a pentiometer through an MCP3008 ADC
which we display the value of to the 1602 LCD.
  • Loading branch information
RichardChambers committed Jan 9, 2018
1 parent 643b5cd commit 0672d26
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
14 changes: 14 additions & 0 deletions project11/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

clean:
rm tiltbuzz_1 tiltbuzz_2 tiltbuzz_3

all: tiltbuzz_1 tiltbuzz_2 tiltbuzz_3

tiltbuzz_1: tiltbuzz_1.c lcd1602.c pinreadwrite.c
cc -o tiltbuzz_1 tiltbuzz_1.c lcd1602.c pinreadwrite.c -lwiringPi

tiltbuzz_2: tiltbuzz_2.c lcd1602.c pinreadwrite.c
cc -o tiltbuzz_2 tiltbuzz_2.c lcd1602.c pinreadwrite.c -lwiringPi

tiltbuzz_3: tiltbuzz_3.c lcd1602.c spilib.c pinreadwrite.c
cc -o tiltbuzz_3 tiltbuzz_3.c lcd1602.c spilib.c pinreadwrite.c -lwiringPi
72 changes: 72 additions & 0 deletions project11/spilib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

#include <wiringPi.h>
#include <wiringPiSPI.h>

#include "spilib.h"

static int spiFD;

int spiAnalogRead(int spiChannel, int channelConfig, int analogChannel)
{
if(analogChannel < 1 || analogChannel > 8)
return -1;

analogChannel--;

// from URL:
// http://hertaville.com/interfacing-an-spi-adc-mcp3008-chip-to-the-raspberry-pi-using-c.html

// Raspberry Pi asserts the chip select signal connected to the MCP3008
// (CS0 in our case) by setting it to 0V. This is typically taken care
// of internally by the spidev driver whenever the proper ioctl() function is called.

// Raspberry Pi sends a byte containing a value of '1' to the MCP3008. This is a
// start bit. At the same time the MCP3008 sends back a 'don't care' byte to the Raspberry Pi.

// Raspberry Pi then sends a second byte whose most significant nibble
// (SGL/DIFF,D2,D1 & D0 bits) indicate the channel that we want to convert and
// whether we want single-ended or differential conversion (See Figure 5).
// For example if this nibble is "1000", the conversion will be single-ended
// and take place on channel 0 (CH0 pin). The least significant nibble is sent
// as 'don't care'. At the same time, the MCP3008 sends back the two most
// significant bits of the digital value (result) of the conversion (bits 8 and 9).

// Raspberry Pi sends another 'don't care' byte to the MCP3008. At the same time
// the MCP3008 send back a byte containing bits 7 through 0 0f the digital value
// (result) of the conversion.

// The Raspberry Pi then merges bits 8 & 9 from the second received byte with
// bits 7 through 0 from the third received byte to create the 10-bit digital
// value resulting from the conversion.

unsigned char buffer[3] = {1}; // start bit
buffer[1] = (channelConfig+analogChannel) << 4;

wiringPiSPIDataRW(spiChannel, buffer, 3);

return ( (buffer[1] & 3 ) << 8 ) + buffer[2]; // get last 10 bits
}

int spiClose (void)
{

close (spiFD) ;
}

// int spiChannel = 0; // default is to use SPI channel 0 on the Raspberry Pi
// int channelConfig = CHAN_CONFIG_SINGLE;
int spiOpen (int spiChannel, int channelConfig)
{

spiFD = wiringPiSPISetup (spiChannel, 10000);
if (spiFD < 0) {
return -1;
}

return 0;
}

9 changes: 9 additions & 0 deletions project11/spilib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


#define SPI_CHAN_CONFIG_SINGLE 8
#define SPI_CHAN_CONFIG_DIFF 0

int spiOpen (int spiChannel, int channelConfig);
int spiClose (void);
int spiAnalogRead(int spiChannel,int channelConfig,int analogChannel);

55 changes: 55 additions & 0 deletions project11/tiltbuzz_3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>

#include "lcd1602.h"
#include "spilib.h"
#include "pinreadwrite.h"

int main (int argc, char *argv[])
{
// BUZZPIN is wiringPi Pin #1 or physical pin #12 or GPIO #18
int BUZZPIN = 1;
int LEDPIN = 3;
int SW520DPIN = 3;

if (wiringPiSetup() == -1) {
printf ("Setup wiringPi Failed!\n");
return -1;
}

lcd1602Open (0x27);
spiOpen (0, SPI_CHAN_CONFIG_SINGLE);

pinSetupWrite (BUZZPIN, LOW);
pinSetupWrite (LEDPIN, LOW);
pinSetupRead (SW520DPIN);

lcd1602Write (0, 0, "Welcome to tiltbuzz_2");
delay (2000);
lcd1602Clear();

int iCount = 20;
for ( ; iCount > 0; iCount--) {
int iDelay = ((iCount % 5) + 1) * 400;
char buffLines[2][24];

int iSpiVal = spiAnalogRead (0, SPI_CHAN_CONFIG_SINGLE, 2);

sprintf (buffLines[0], "tiltbuzz_2 %2d", iCount);
lcd1602Write (0, 0, buffLines[0]);
sprintf (buffLines[1], "Pent val %4d", iSpiVal);
lcd1602Write (0, 1, buffLines[1]);
pinWriteHiLo (BUZZPIN, 1, iDelay, iDelay);
}

// clean up the GPIO resources we have been using.
// make sure that things are back to a good state before exit.
pinCleanup (BUZZPIN);
lcd1602Close();
spiClose();

delay (500);
return 0;
}

0 comments on commit 0672d26

Please sign in to comment.