Last active
November 17, 2016 22:03
-
-
Save eduardoaugustojulio/dc948c7b4cdf7fc69e9def1193d5bfdc to your computer and use it in GitHub Desktop.
c lib for max6675 circuit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "max6675.h" | |
max6675 device; | |
void iniciaMAX6675(volatile unsigned char *port, const char so_mask, const char sck_mask, const char cs_mask) | |
{ | |
device.SPI_PIN_PORT = (unsigned char*) port; | |
device.SO_PIN_MASK = so_mask; | |
device.SCK_PIN_MASK = sck_mask; | |
device.CS_PIN_MASK = cs_mask; | |
setNthBit (device.SPI_PIN_PORT, device.CS_PIN_MASK, 1); | |
setNthBit (device.SPI_PIN_PORT, device.SCK_PIN_MASK, 0); | |
} | |
unsigned int readSPIMAX6675 () | |
{ | |
unsigned int retval = 0; | |
setNthBit (device.SPI_PIN_PORT, device.CS_PIN_MASK, 0); | |
for(int i = 0; i < 16; i++) | |
{ | |
setNthBit (device.SPI_PIN_PORT, device.SCK_PIN_MASK, 1); | |
setNthBit (device.SPI_PIN_PORT, device.SCK_PIN_MASK, 0); | |
retval <<=1; | |
if(isNthBitSet(device.SPI_PIN_PORT, device.SO_PIN_MASK)) | |
retval |= 1; | |
} | |
setNthBit (device.SPI_PIN_PORT, device.CS_PIN_MASK, 1); | |
return (retval); | |
} | |
void readTempMAX6675 (float* temp) | |
{ | |
*temp = (readTwelveBits (readSPIMAX6675 ()) / 4); | |
} | |
unsigned int readTwelveBits(unsigned int raw_value) | |
{ | |
return(0x0FFF & (raw_value>>3)); | |
} | |
unsigned char isNthBitSet(unsigned char *c, unsigned char n) | |
{ | |
const unsigned char mask[] = {1, 2, 4, 8, 16, 32, 64, 128}; | |
return (*c & mask[n]); | |
} | |
void setNthBit(unsigned char *c, unsigned char n ,unsigned char b) | |
{ | |
if(b) | |
*c |= 1 << n; | |
else | |
*c &= ~(1 << n); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: max6675.h | |
* Author: eduardo | |
* | |
* Created on November 15, 2016, 8:11 AM | |
*/ | |
#ifndef MAX6675_H | |
#define MAX6675_H | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
#define setValue(value, mask) (value << mask) | |
typedef struct _max6675{ | |
unsigned char * SPI_PIN_PORT; | |
unsigned char SO_PIN_MASK; | |
unsigned char SCK_PIN_MASK; | |
unsigned char CS_PIN_MASK; | |
}max6675; | |
void iniciaMAX6675(volatile unsigned char *port, const char so_mask, const char sck_mask, const char cs_mask); | |
unsigned int readSPIMAX6675(); | |
void readTempMAX6675(float *temp); | |
unsigned int readTwelveBits(unsigned int raw_value); | |
unsigned char isNthBitSet(unsigned char *c, unsigned char n); | |
void setNthBit(unsigned char *c, unsigned char n ,unsigned char b); | |
void delay(int value); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif /* MAX6675_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a C library for reading via SPI (bit banging) the MAX6675 ADC integrated circuit.
Designed to be unlinked from hardware. Implementation in 3 lines of code. The library do not initialize pin direction
Example:
void main(){
float temp;
/*Function call passing PORT pins and numeration of the respective SO, SCK, CS*/
iniciaMAX6675 (&PORTA,0,1,2);
while(true){readTempMAX6675 (&temp);}
}