Skip to content

Commit

Permalink
Added multiple new drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 26, 2014
1 parent 9c2f598 commit bc5612b
Show file tree
Hide file tree
Showing 184 changed files with 27,548 additions and 0 deletions.
38 changes: 38 additions & 0 deletions CMakeLists.txt~
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 2.8)

set (CPU_FREQ "16000000UL")
set (CPU "atmega328p")
set (CPU_AVRDUDE "m328p")
#set (CPU_FREQ "18432000UL")
#set (CPU "atmega88p")
#set (CPU_AVRDUDE "m88")

set(TARGET drivers)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")

include_directories("../include/avr/" "./drivers")

file(GLOB Project_SOURCES drivers/*.c)
#file(GLOB Project_SOURCES test/hello.c)
file(GLOB Project_HEADER drivers/*.h)

set (CMAKE_C_COMPILER "avr-gcc")
set (CMAKE_C_FLAGS "-ffunction-sections -std=c99 -fdata-sections -O2 -Wl,--relax,--gc-sections -DF_CPU=${CPU_FREQ} -mmcu=${CPU}")

set (CMAKE_CXX_COMPILER "avr-g++")
set (CMAKE_CXX_FLAGS "-ffunction-sections -fpermissive -fdata-sections -std=c++11 -O2 -Wl,--relax,--gc-sections -DF_CPU=${CPU_FREQ} -mmcu=${CPU}")

#set (CMAKE_C_COMPILER "/usr/bin/avr-gcc")
#set (CMAKE_C_FLAGS "-ffunction-sections -fdata-sections -O2 -Wl,--relax,--gc-sections -DF_CPU=16000000UL -mmcu=atmega328p")
add_custom_target(install
COMMAND avr-objcopy -j .text -j .data -O ihex ${TARGET} ${TARGET}.hex
COMMAND avr-size -C -x ${TARGET}
COMMAND sudo avrdude -p ${CPU_AVRDUDE} -c usbasp -e -U flash:w:${TARGET}.hex
DEPENDS ${TARGET}
)

# Executable
add_library(${TARGET} ${Project_SOURCES} ${Project_HEADERS})

target_link_libraries(${TARGET})
40 changes: 40 additions & 0 deletions CMakeLists_old.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 2.8)

# THIS IS AN OLD CMAKE FILE! YOU DO NOT NEED IT!

set (CPU_FREQ "16000000UL")
set (CPU "atmega328p")
set (CPU_AVRDUDE "m328p")
#set (CPU_FREQ "18432000UL")
#set (CPU "atmega88p")
#set (CPU_AVRDUDE "m88")

set(TARGET drivers)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")

include_directories("../include/avr/" "./drivers")

file(GLOB Project_SOURCES drivers/*.c)
#file(GLOB Project_SOURCES test/hello.c)
file(GLOB Project_HEADER drivers/*.h)

set (CMAKE_C_COMPILER "avr-gcc")
set (CMAKE_C_FLAGS "-ffunction-sections -std=c99 -fdata-sections -O2 -Wl,--relax,--gc-sections -DF_CPU=${CPU_FREQ} -mmcu=${CPU}")

set (CMAKE_CXX_COMPILER "avr-g++")
set (CMAKE_CXX_FLAGS "-ffunction-sections -fpermissive -fdata-sections -std=c++11 -O2 -Wl,--relax,--gc-sections -DF_CPU=${CPU_FREQ} -mmcu=${CPU}")

#set (CMAKE_C_COMPILER "/usr/bin/avr-gcc")
#set (CMAKE_C_FLAGS "-ffunction-sections -fdata-sections -O2 -Wl,--relax,--gc-sections -DF_CPU=16000000UL -mmcu=atmega328p")
add_custom_target(install
COMMAND avr-objcopy -j .text -j .data -O ihex ${TARGET} ${TARGET}.hex
COMMAND avr-size -C -x ${TARGET}
COMMAND sudo avrdude -p ${CPU_AVRDUDE} -c usbasp -e -U flash:w:${TARGET}.hex
DEPENDS ${TARGET}
)

# Executable
add_library(${TARGET} ${Project_SOURCES} ${Project_HEADERS})

target_link_libraries(${TARGET})
41 changes: 41 additions & 0 deletions accel_docalibration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
accellerometer calibration functions 01
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/

//use this module in association with the python script provided

#if ACCEL_DOCALIBRATION == 1

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>

/*
* do calibration
*/
void accelcal_docalibrationclient(void(*cputc)(unsigned char), unsigned int (*cgetc)()) {
int16_t axraw = 0;
int16_t ayraw = 0;
int16_t azraw = 0;
for(;;) {
//wait for the input request
while(cgetc() != 0x20);
//get raw data
accelcal_getrawdata(&axraw, &ayraw, &azraw);
//send raw data
cputc(axraw);
cputc(axraw>>8);
cputc(ayraw);
cputc(ayraw>>8);
cputc(azraw);
cputc(azraw>>8);
cputc('\n');
}
}
#endif
22 changes: 22 additions & 0 deletions acs712.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
acs712 lib 0x01
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/


/*
* get the current
* current = (V - Vref/2) / sensitivity
*/

#include "acs712.h"

double acs712_getcurrent(double voltagein, double adcvref) {
return (double)((double)(voltagein - (double)(adcvref/(double)2)) / (double)ACS712_SENSITIVITY);
}


24 changes: 24 additions & 0 deletions acs712.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
acs712 lib 0x01
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/


#ifndef ACS712_H_
#define ACS712_H_

//defined sensitivity
#define ACS712_SENSITIVITY5 0.185
#define ACS712_SENSITIVITY20 0.100
#define ACS712_SENSITIVITY30 0.066

//setup sensitivity
#define ACS712_SENSITIVITY ACS712_SENSITIVITY30

extern double acs712_getcurrent(double voltagein, double adcvref);

#endif
192 changes: 192 additions & 0 deletions adc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
ADC Library 0x05
copyright (c) Davide Gironi, 2013
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/


#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#if defined (__AVR_ATtiny13A__)
#elif defined (__AVR_ATmega8__)
#elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
#elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
#else
#error "no definitions available for this AVR"
#endif

#include "adc.h"

//complete this if trigger is on
//call adc_setchannel()
//call sei()
//ISR(ADC_vect)
//{
//}

/*
* set an adc channel
*/
void adc_setchannel(uint8_t channel)
{
ADCSRA &= ~(1 << ADEN);
ADMUX = (ADMUX & 0xf8) | (channel & 0x07); //set channel
ADCSRA |= (1 << ADEN);
}

/*
* read from selected adc channel
*/
uint16_t adc_readsel(void)
{
ADCSRA |= (1 << ADSC); // Start conversion
while(ADCSRA & _BV(ADSC));
//while( !(ADCSRA & (1<<ADIF)) ); // Wait for conversion to complete
uint16_t adc = ADC;
//ADCSRA |= (1 << ADIF); // Clear ADIF by writing one to it
return(adc);
}

/*
* read from adc channel
*/
uint16_t adc_read(uint8_t channel)
{
adc_setchannel(channel);
return adc_readsel();
}

/*
* init adc
*/
void adc_init(void)
{
// Set ADC reference
#if defined (__AVR_ATtiny13A__)
#if ADC_REF == 0
ADMUX |= (0 << REFS0); // VCC used as analog reference
#elif ADC_REF == 1
ADMUX |= (1 << REFS0); // Internal Voltage Reference
#endif
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
#if ADC_REF == 0
ADMUX |= (0 << REFS1) | (0 << REFS0); // AREF, Internal Vref turned off
#elif ADC_REF == 1
ADMUX |= (0 << REFS1) | (1 << REFS0); // AVCC with external capacitor at AREF pin
#elif ADC_REF == 3
ADMUX |= (1 << REFS1) | (1 << REFS0); // Internal 2.56V Voltage Reference with external cap at AREF
#endif
#else
#error "No processor type defined!"
#endif

// Set ADC prescaler
#if ADC_PRESCALER == 2
ADCSRA |= (0 << ADPS2) | (0 << ADPS1) | (1 << ADPS0); // Prescaler 2
#elif ADC_PRESCALER == 4
ADCSRA |= (0 << ADPS2) | (1 << ADPS1) | (0 << ADPS0); // Prescaler 4
#elif ADC_PRESCALER == 8
ADCSRA |= (0 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Prescaler 8
#elif ADC_PRESCALER == 16
ADCSRA |= (1 << ADPS2) | (0 << ADPS1) | (0 << ADPS0); // Prescaler 16
#elif ADC_PRESCALER == 32
ADCSRA |= (1 << ADPS2) | (0 << ADPS1) | (1 << ADPS0); // Prescaler 32
#elif ADC_PRESCALER == 64
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0); // Prescaler 64
#elif ADC_PRESCALER == 128
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Prescaler 128
#endif

// Set ADC justify
#if ADC_JUSTIFY == 'L'
ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
#elif ADC_JUSTIFY == 'R'
ADMUX |= (0 << ADLAR); // Right adjust
#endif

// Set ADC trigger and mode
#if ADC_TRIGGERON == 1
#if defined (__AVR_ATtiny13A__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
ADCSRB |= (0 << ADTS2) | (0 << ADPS1) | (0 << ADPS0); // Free Running mode
ADCSRA |= (1 << ADATE); // Enable ADC Interrupt
#elif defined (__AVR_ATmega8__)
ADCSRA |= (1 << ADFR); // Set ADC to Free-Running Mode
#endif
ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
#else
ADCSRB |= (0 << ADTS2) | (0 << ADPS1) | (0 << ADPS0); // Free Running mode
#endif

// Enable ADC
ADCSRA |= (1 << ADEN);

//DIDR0 = 0xff;

#if ADC_TRIGGERON == 1
ADCSRA |= (1 << ADSC); // Start conversions
#endif
}

/*
* get reference voltage using bandgap voltage
*/
double acd_getrealvref(void)
{
double intvoltage = 0;
#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
//set bandgap voltage channel, and read value
adc_setchannel(14);
_delay_us(250);
uint16_t adc = adc_readsel();
//calculate internal voltage
intvoltage = ((ADC_BANDGAPVOLTAGE * ADC_REFRES) / adc) / 1000.0;
#endif
return intvoltage;
}

/*
* convert an adc value to a resistence value
*/
long adc_getresistence(uint16_t adcread, uint16_t adcbalanceresistor)
{
if(adcread == 0)
return 0;
else
return (long)((long)(ADC_REFRES*(long)adcbalanceresistor)/adcread-(long)adcbalanceresistor);
}

/*
* convert an adc value to a voltage value
*/
double adc_getvoltage(uint16_t adcread, double adcvref) {
if(adcread == 0)
return 0;
else
return (double)(adcread*adcvref/(double)ADC_REFRES);
}

/*
* exponential moving avarage filter
*
* "newvalue" new adc read value
* "value" old adc filtered value
* return a new filtered value
*
* References:
* Guillem Planissi: Measurement and filtering of temperatures with NTC
*/
#define ADC_EMAFILTERALPHA 30
unsigned int adc_emafilter(unsigned int newvalue, unsigned int value)
{
//use exponential moving avarate Y=(1-alpha)*Y + alpha*Ynew, alpha between 1 and 0
//in uM we use int math, so Y=(63-63alpha)*Y + 63alpha*Ynew and Y=Y/63 (Y=Y>>6)
value = (64-ADC_EMAFILTERALPHA)*value+ADC_EMAFILTERALPHA*newvalue;
value = (value>>6);
return value;
}

Loading

0 comments on commit bc5612b

Please sign in to comment.