Skip to content

Instantly share code, notes, and snippets.

@eduardoaugustojulio
Created November 17, 2016 16:10
Show Gist options
  • Save eduardoaugustojulio/007c987515eeb1f5ef2a15a20fe24f26 to your computer and use it in GitHub Desktop.
Save eduardoaugustojulio/007c987515eeb1f5ef2a15a20fe24f26 to your computer and use it in GitHub Desktop.
c lib for mcp3201 circuit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mcp3201.h"
mcp3201 device;
void iniciaMCP2301 (volatile unsigned char* port, const char dout_mask, const char clk_mask, const char cs_mask)
{
device.SPI_PIN_PORT = (unsigned char*) port;
device.DOUT_PIN_MASK = dout_mask;
device.CLK_PIN_MASK = clk_mask;
device.CS_PIN_MASK = cs_mask;
setNthBit (device.SPI_PIN_PORT, device.CS_PIN_MASK, 1);
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 0);
}
unsigned char buscaMCP2301 ()
{
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 1);
NOP();
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 0);
NOP();
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 1);
NOP();
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 0);
NOP();
if(!isNthBitSet (device.SPI_PIN_PORT, device.DOUT_PIN_MASK))
return 1;
return 0;
}
unsigned int readSPIMCP3201 ()
{
unsigned int retval = 0;
setNthBit (device.SPI_PIN_PORT, device.CS_PIN_MASK, 0);
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 0);
if(buscaMCP2301 ())
{
for (int i = 0; i < 12; i++)
{
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 1);
setNthBit (device.SPI_PIN_PORT, device.CLK_PIN_MASK, 0);
retval <<=1;
if(isNthBitSet(device.SPI_PIN_PORT, device.DOUT_PIN_MASK))
retval |= 1;
}
}
setNthBit (device.SPI_PIN_PORT, device.CS_PIN_MASK, 1);
return retval;
}
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);
}
/*
* File: mcp3201.h
* Author: Eduardo
*
* Created on 18 de Julho de 2016, 01:40
*/
#ifndef MCP3201_H
#define MCP3201_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _mcp3201{
unsigned char * SPI_PIN_PORT;
unsigned char DOUT_PIN_MASK;
unsigned char CLK_PIN_MASK;
unsigned char CS_PIN_MASK;
} mcp3201;
void iniciaMCP2301(volatile unsigned char *port, const char dout_mask, const char clk_mask, const char cs_mask);
unsigned char buscaMCP2301();
unsigned int readSPIMCP3201();
unsigned char isNthBitSet(unsigned char *c, unsigned char n);
void setNthBit(unsigned char *c, unsigned char n ,unsigned char b);
#ifdef __cplusplus
}
#endif
#endif /* MCP3201_H */
@eduardoaugustojulio
Copy link
Author

eduardoaugustojulio commented Nov 17, 2016

This is a C library for reading via SPI (bit banging) the MCP3201 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(){
unsigned int temp;
/*Function call passing PORT pins and numeration of the respective DOUT, CLK, CS*/
iniciaMCP2301 (&PORTA,0,1,2);
while(true){temp = readSPIMCP3201 ();}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment