Add spark fun fuel gauge back ; use qwiic as interface instread of other pins

This commit is contained in:
KemoNine 2019-05-13 22:40:59 -04:00
parent 3dc58c308d
commit 3a83bc5b43
31 changed files with 3847 additions and 22 deletions

View File

@ -0,0 +1,86 @@
/*! \file avrlibdefs.h \brief AVRlib global defines and macros. */
//*****************************************************************************
//
// File Name : 'avrlibdefs.h'
// Title : AVRlib global defines and macros include file
// Author : Pascal Stang
// Created : 7/12/2001
// Revised : 9/30/2002
// Version : 1.1
// Target MCU : Atmel AVR series
// Editor Tabs : 4
//
// Description : This include file is designed to contain items useful to all
// code files and projects, regardless of specific implementation.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#ifndef AVRLIBDEFS_H
#define AVRLIBDEFS_H
//#define F_CPU 4000000
#define MEM_TYPE 1
// Code compatibility to new AVR-libc
// outb(), inb(), inw(), outw(), BV(), sbi(), cbi(), sei(), cli()
#ifndef outb
#define outb(addr, data) addr = (data)
#endif
#ifndef inb
#define inb(addr) (addr)
#endif
#ifndef outw
#define outw(addr, data) addr = (data)
#endif
#ifndef inw
#define inw(addr) (addr)
#endif
#ifndef BV
#define BV(bit) (1<<(bit))
#endif
//#ifndef cbi
// #define cbi(reg,bit) reg &= ~(BV(bit))
//#endif
//#ifndef sbi
// #define sbi(reg,bit) reg |= (BV(bit))
//#endif
#ifndef cli
#define cli() __asm__ __volatile__ ("cli" ::)
#endif
#ifndef sei
#define sei() __asm__ __volatile__ ("sei" ::)
#endif
// support for individual port pin naming in the mega128
// see port128.h for details
#ifdef __AVR_ATmega128__
// not currently necessary due to inclusion
// of these defines in newest AVR-GCC
// do a quick test to see if include is needed
#ifndef PD0
//#include "port128.h"
#endif
#endif
// use this for packed structures
// (this is seldom necessary on an 8-bit architecture like AVR,
// but can assist in code portability to AVR)
#define GNUC_PACKED __attribute__((packed))
// port address helpers
#define DDR(x) ((x)-1) // address of data direction register of port x
#define PIN(x) ((x)-2) // address of input register of port x
// MIN/MAX/ABS macros
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a>b)?(a):(b))
#define ABS(x) ((x>0)?(x):(-x))
// constants
#define PI 3.14159265359
#endif

View File

@ -0,0 +1,175 @@
// This library provides the high-level functions needed to use the I2C
// serial interface supported by the hardware of several AVR processors.
#include <avr/io.h>
#include <avr/interrupt.h>
#include "types.h"
#include "defs.h"
// TWSR values (not bits)
// (taken from avr-libc twi.h - thank you Marek Michalkiewicz)
// Master
#define TW_START 0x08
#define TW_REP_START 0x10
// Master Transmitter
#define TW_MT_SLA_ACK 0x18
#define TW_MT_SLA_NACK 0x20
#define TW_MT_DATA_ACK 0x28
#define TW_MT_DATA_NACK 0x30
#define TW_MT_ARB_LOST 0x38
// Master Receiver
#define TW_MR_ARB_LOST 0x38
#define TW_MR_SLA_ACK 0x40
#define TW_MR_SLA_NACK 0x48
#define TW_MR_DATA_ACK 0x50
#define TW_MR_DATA_NACK 0x58
// Slave Transmitter
#define TW_ST_SLA_ACK 0xA8
#define TW_ST_ARB_LOST_SLA_ACK 0xB0
#define TW_ST_DATA_ACK 0xB8
#define TW_ST_DATA_NACK 0xC0
#define TW_ST_LAST_DATA 0xC8
// Slave Receiver
#define TW_SR_SLA_ACK 0x60
#define TW_SR_ARB_LOST_SLA_ACK 0x68
#define TW_SR_GCALL_ACK 0x70
#define TW_SR_ARB_LOST_GCALL_ACK 0x78
#define TW_SR_DATA_ACK 0x80
#define TW_SR_DATA_NACK 0x88
#define TW_SR_GCALL_DATA_ACK 0x90
#define TW_SR_GCALL_DATA_NACK 0x98
#define TW_SR_STOP 0xA0
// Misc
#define TW_NO_INFO 0xF8
#define TW_BUS_ERROR 0x00
// defines and constants
#define TWCR_CMD_MASK 0x0F
#define TWSR_STATUS_MASK 0xF8
// return values
#define I2C_OK 0x00
#define I2C_ERROR_NODEV 0x01
#define WRITE_sda() DDRC = DDRC | 0b00010000 //SDA must be output when writing
#define READ_sda() DDRC = DDRC & 0b11101111 //SDA must be input when reading - don't forget the resistor on SDA!!
#define sbi(var, mask) ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask) ((var) &= (uint8_t)~(1 << mask))
// functions
//! Initialize I2C (TWI) interface
void i2cInit(void);
//! Set the I2C transaction bitrate (in KHz)
void i2cSetBitrate(unsigned short bitrateKHz);
// Low-level I2C transaction commands
//! Send an I2C start condition in Master mode
void i2cSendStart(void);
//! Send an I2C stop condition in Master mode
void i2cSendStop(void);
//! Wait for current I2C operation to complete
void i2cWaitForComplete(void);
//! Send an (address|R/W) combination or a data byte over I2C
void i2cSendByte(unsigned char data);
//! Receive a data byte over I2C
// ackFlag = TRUE if recevied data should be ACK'ed
// ackFlag = FALSE if recevied data should be NACK'ed
void i2cReceiveByte(unsigned char ackFlag);
//! Pick up the data that was received with i2cReceiveByte()
unsigned char i2cGetReceivedByte(void);
//! Get current I2c bus status from TWSR
unsigned char i2cGetStatus(void);
// high-level I2C transaction commands
//! send I2C data to a device on the bus (non-interrupt based)
unsigned char i2cMasterSendNI(unsigned char deviceAddr, unsigned char length, unsigned char* data);
//! receive I2C data from a device on the bus (non-interrupt based)
unsigned char i2cMasterReceiveNI(unsigned char deviceAddr, unsigned char length, unsigned char *data);
/*********************
****I2C Functions****
*********************/
void i2cInit(void)
{
// set i2c bit rate to 40KHz
i2cSetBitrate(100);
// enable TWI (two-wire interface)
sbi(TWCR, TWEN);
}
void i2cSetBitrate(unsigned short bitrateKHz)
{
unsigned char bitrate_div;
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
// calculate bitrate division
bitrate_div = ((F_CPU/4000l)/bitrateKHz);
if(bitrate_div >= 16)
bitrate_div = (bitrate_div-16)/2;
outb(TWBR, bitrate_div);
}
void i2cSendStart(void)
{
WRITE_sda();
// send start condition
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
}
void i2cSendStop(void)
{
// transmit stop condition
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
void i2cWaitForComplete(void)
{
int i = 0; //time out variable
// wait for i2c interface to complete operation
while ((!(TWCR & (1<<TWINT))) && (i < 90))
i++;
}
void i2cSendByte(unsigned char data)
{
WRITE_sda();
// save data to the TWDR
TWDR = data;
// begin send
TWCR = (1<<TWINT)|(1<<TWEN);
}
void i2cReceiveByte(unsigned char ackFlag)
{
// begin receive over i2c
if( ackFlag )
{
// ackFlag = TRUE: ACK the recevied data
outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
}
else
{
// ackFlag = FALSE: NACK the recevied data
outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));
}
}
unsigned char i2cGetReceivedByte(void)
{
// retieve received data byte from i2c TWDR
return( inb(TWDR) );
}
unsigned char i2cGetStatus(void)
{
// retieve current i2c status from i2c TWSR
return( inb(TWSR) );
}

View File

@ -0,0 +1,359 @@
/*
6-22-11
Aaron Weiss
aaron at sparkfun dot com
OSHW 1.0 License, http://freedomdefined.org/OSHW
LiPo Fuel Gauge Example Code
Uses the MAX17043
ATmega328@3.3V w/ external 8MHz resonator
HF: 0xDA, LF: 0xFF, EF: 0xF8
PIN CONNECTIONS:
VCC - No connect or connect to system power
GND - GND
SDA - PC4
SCL - PC5
ALT - PD2
QST - GND or a hardware reset pin
Default Baud Rate: 9600bps 8N1
Firmware: v10
Hardware: v10
Usage: Displays the raw A/D value in mV and percentage of
charge every second
*/
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "types.h"
#include "defs.h"
#include "i2c.h"
#define FOSC 8000000
#define BAUD 9600
#define sbi(var, mask) ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask) ((var) &= (uint8_t)~(1 << mask))
#define STATUS_LED 5
///===========Main Prototypes=============================///////////////////////
void config(void);
void power_on_reset(void);
long read_ad(void);
long read_config(void);
void read_percent(void);
void quick_start_reset(void);
/////=====================================================///////////////////////
///============Initialize Prototypes=====================///////////////////////
void ioinit(void); // initializes IO
void UART_Init(unsigned int ubrr);
static int uart_putchar(char c, FILE *stream);
uint8_t uart_getchar(void);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void delay(uint16_t x); // general purpose delay
/////===================================================////////////////////////
///============I2C Prototypes=============//////////////////
void i2cSendStart(void);
void i2cSendStop(void);
void i2cWaitForComplete(void);
void i2cSendByte(unsigned char data);
void i2cInit(void);
void i2cHz(long uP_F, long scl_F);
///===========Global Vars=============================//////////////////////////
volatile long i=0; //counts per second
volatile double percent_decimal;
/////===================================================////////////////////////
//=========MAIN================/////////////////////////////////////////////////
ISR (INT0_vect)
{
//when charge drops below 32% turn on status LED
sbi(PORTB, STATUS_LED);
}
ISR(TIMER1_OVF_vect)
{
//one second timer, prints values every second
TCNT1 = 34286;
//printf("config=%5ld, ", read_config());
printf("a/d=%5ld, ", read_ad());
read_percent();
}
int main(void)
{
ioinit(); //Setup IO pins and defaults
power_on_reset(); // reset MAX module upon MCU reset
config();
while(1)
{
//put your additional code here
}
return (0);
}
void config(void)
{
/////////write config register///////////////
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6C); //write MAX
i2cWaitForComplete();
i2cSendByte(0x0C); //mode register
i2cWaitForComplete();
i2cSendByte(0x97);
i2cWaitForComplete();
i2cSendByte(0x00); //set alert to 32%
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
}
void power_on_reset(void)
{
/////////write command register to reset///////////////
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6C); //write MAX
i2cWaitForComplete();
i2cSendByte(0xFE); //mode register
i2cWaitForComplete();
i2cSendByte(0x54);
i2cWaitForComplete();
i2cSendByte(0x00);
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
}
long read_ad(void)
{
//0x6C write, 0x6D read
uint16_t xm, xl;
long temp, xo;
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6C); //write MAX
i2cWaitForComplete();
i2cSendByte(0x02); //mode register
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6D); //read from MAX
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xm = i2cGetReceivedByte(); //high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); //high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cSendStop();
i2cSendStart();
temp = ((xl|(xm << 8)) >> 4);
xo = 1.25* temp;
//returns A/D value in mV
return xo;
}
long read_config(void)
{
///should read 38656 or 0x9700 before alert
///should read 38752 or 0x9760 after alert
//0x6C write, 0x6D read
uint16_t xm, xl;
long xo;
//////////read command register/////////////
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6C); //write MAX
i2cWaitForComplete();
i2cSendByte(0x0C); //mode register
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6D); //read from MAX
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xm = i2cGetReceivedByte(); //high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); //high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cSendStop();
i2cSendStart();
xo = xl|(xm << 8);
return xo;
}
void read_percent(void)
{
//0x6C write, 0x6D read
uint8_t xm, xl;
//uint8_t xmh, xml, xlh, xll;
//long xo;
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6C); //write MAX
i2cWaitForComplete();
i2cSendByte(0x04); //SOC register
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6D); //read from MAX
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xm = i2cGetReceivedByte(); //high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); //high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cSendStop();
i2cSendStart();
//percent_decimal = (0.003906)*xl + xm;
//xo = xl|(xm << 8);
printf("percent=%d\n\r", xm);
//printf("percent=%4.2f\n\r", percent_decimal);
}
void quick_start_reset(void)
{
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x6C); //write MAX
i2cWaitForComplete();
i2cSendByte(0x06); //mode register
i2cWaitForComplete();
i2cSendByte(0x40);
i2cWaitForComplete();
i2cSendByte(0x00);
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
}
////////////////////////////////////////////////////////////////////////////////
///==============Initializations=======================================/////////
////////////////////////////////////////////////////////////////////////////////
void ioinit (void)
{
//1 = output, 0 = input
DDRB = 0b11101111; //PB4 = MISO
DDRC = 0b11111111; //Output on PORTC0, PORTC4 (SDA), PORTC5 (SCL), all others are inputs
DDRD = 0b11110010; //PORTD (RX on PD0), input on PD2 ,out on PD5 for ATtiny motor enable
PORTD |= (1<<PORTD2); //pullup on PD2
PORTC = (1<<PORTC4)|(1<<PORTC5); //i2c pullups
cbi(PORTB, STATUS_LED);
UART_Init((unsigned int)((1000000)/(BAUD*2)-1));
i2cInit();
//pin change interrupt on INT0
EICRA = (1<<ISC01);//falling edge generates interrupt
EIMSK = (1<<INT0);
// Setting Timer 1:
// normal mode
TCCR1A = 0x00;
// Set to clk/256
TCCR1B |= (1<<CS12);
//enable overflow interrupt
TIMSK1 |= (1<<TOIE1);
//load timer with a value to optimize for 1 second,
//(256/8MHz)*(65536bits-34286) = 1s
TCNT1 = 34286;
sei(); //turn on global interrupts
}
void UART_Init(unsigned int ubrr)
{
// Set baud rate
UBRR0H = ubrr>>8;
UBRR0L = ubrr;
// Enable receiver and transmitter
UCSR0A = (0<<U2X0);
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
// Set frame format: 8 bit, no parity, 1 stop bit,
UCSR0C = (1<<UCSZ00)|(1<<UCSZ01);
stdout = &mystdout; //Required for printf init
delay(2000);
}
static int uart_putchar(char c, FILE *stream)
{
if (c == '\n') uart_putchar('\r', stream);
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
uint8_t uart_getchar(void)
{
while( !(UCSR0A & (1<<RXC0)) );
return(UDR0);
}
//General short delays
void delay(uint16_t x)
{
uint8_t y;
for ( ; x > 50 ; x--){
for ( y = 0 ; y < 200 ; y++){
asm volatile ("nop");
}
}
}

View File

@ -0,0 +1,379 @@
:100000000C94B5000C9429010C94D2000C94D200ED
:100010000C94D2000C94D2000C94D2000C94D20018
:100020000C94D2000C94D2000C94D2000C94D20008
:100030000C94D2000C94EC040C94D2000C94D200DA
:100040000C94D2000C94D2000C94D2000C94D200E8
:100050000C94D2000C94D2000C94D2000C94D200D8
:100060000C94D2000C94D2006E616E00696E660032
:1000700000407A10F35A00A0724E18090010A5D45F
:10008000E80000E87648170000E40B54020000CABC
:100090009A3B000000E1F505000080969800000002
:1000A00040420F000000A086010000001027000061
:1000B0000000E803000000006400000000000A00E7
:1000C000000000000100000000002C76D888DC67EA
:1000D0004F0823DFC1DFAE59E1B1B796E5E3E45342
:1000E000C63AE651997696E8E6C28426EB898C9B5F
:1000F00062ED407C6FFCEFBC9C9F40F2BAA56FA5FF
:10010000F490055A2AF75C936B6CF9676DC11BFC80
:10011000E0E40D47FEF520E6B500D0ED902E03009B
:10012000943577050080841E080000204E0A0000E8
:1001300000C80C333333330F986E12831141EF8DA7
:100140002114893BE65516CFFEE6DB18D1844B38E7
:100150001BF77C1D901DA4BBE424203284725E2218
:100160008100C9F124ECA1E53D2711241FBECFEF8A
:10017000D8E0DEBFCDBF11E0A0E0B1E0E4E6F7E1FA
:1001800002C005900D92A632B107D9F711E0A6E2A0
:10019000B1E001C01D92A433B107E1F70E942D0424
:1001A0000C94B00B0C940000BC018091B9008E7FC0
:1001B0008093B9008091B9008D7F8093B90080ED64
:1001C00097E00E94D809603130F0862F90E0409788
:1001D00095958795682F6093B800089584E690E020
:1001E0000E94D400ECEBF0E080818460808308956D
:1001F0003C9A84EA8093BC00089584E98093BC0013
:10020000089520E030E002C02F5F3F4F8091BC0096
:1002100087FD03C02A353105B9F708953C9A8093CC
:10022000BB0084E88093BC000895882329F0809166
:10023000BC008F70806C04C08091BC008F7080689F
:100240008093BC0008958091BB0008958091B9000F
:1002500008951F920F920FB60F9211242D9A0F90AE
:100260000FBE0F901F9018953C9A84EA8093BC00B3
:1002700020E030E002C02F5F3F4F8091BC0087FD3F
:1002800003C02A353105B9F73C9A8CE68093BB0050
:1002900084E88093BC0020E030E002C02F5F3F4F35
:1002A0008091BC0087FD03C02A353105B9F73C9A1F
:1002B0008CE08093BB0084E88093BC0020E030E0B9
:1002C00002C02F5F3F4F8091BC0087FD03C02A35DD
:1002D0003105B9F73C9A87E98093BB0084E88093A5
:1002E000BC0020E030E002C02F5F3F4F8091BC0097
:1002F00087FD03C02A353105B9F73C9A1092BB003F
:1003000084E88093BC0020E030E002C02F5F3F4FC4
:100310008091BC0087FD03C02A353105B9F784E917
:100320008093BC003C9A84EA8093BC0008953C9A78
:1003300084EA8093BC0020E030E002C02F5F3F4F92
:100340008091BC0087FD03C02A353105B9F73C9A7E
:100350008CE68093BB0084E88093BC0020E030E012
:1003600002C02F5F3F4F8091BC0087FD03C02A353C
:100370003105B9F73C9A8EEF8093BB0084E88093F7
:10038000BC0020E030E002C02F5F3F4F8091BC00F6
:1003900087FD03C02A353105B9F73C9A84E580937F
:1003A000BB0084E88093BC0020E030E002C02F5FF7
:1003B0003F4F8091BC0087FD03C02A353105B9F756
:1003C0003C9A1092BB0084E88093BC0020E030E0AF
:1003D00002C02F5F3F4F8091BC0087FD03C02A35CC
:1003E0003105B9F784E98093BC003C9A84EA809394
:1003F000BC0008950F931F933C9A84EA8093BC003D
:1004000020E030E002C02F5F3F4F8091BC0087FDAD
:1004100003C02A353105B9F73C9A8CE68093BB00BE
:1004200084E88093BC0020E030E002C02F5F3F4FA3
:100430008091BC0087FD03C02A353105B9F73C9A8D
:1004400082E08093BB0084E88093BC0020E030E031
:1004500002C02F5F3F4F8091BC0087FD03C02A354B
:100460003105B9F784E98093BC003C9A84EA809313
:10047000BC0020E030E002C02F5F3F4F8091BC0005
:1004800087FD03C02A353105B9F73C9A8DE6809384
:10049000BB0084E88093BC0020E030E002C02F5F06
:1004A0003F4F8091BC0087FD03C02A353105B9F765
:1004B0008FEF0E94150120E030E002C02F5F3F4F18
:1004C0008091BC0087FD03C02A353105B9F7009142
:1004D000BB0020E030E002C02F5F3F4F8091BC00A6
:1004E00087FD03C02A353105B9F78FEF0E9415014A
:1004F00020E030E002C02F5F3F4F8091BC0087FDBD
:1005000003C02A353105B9F71091BB0020E030E077
:1005100002C02F5F3F4F8091BC0087FD03C02A358A
:100520003105B9F78FEF0E94150184E98093BC0073
:100530003C9A84EA8093BC00702F60E0212F30E069
:10054000622B732B34E0769567953A95E1F780E05E
:1005500090E00E94E90820E030E040EA5FE30E947A
:100560004D090E94B6089B01AC01CA011F910F9171
:1005700008950F931F933C9A84EA8093BC0020E077
:1005800030E002C02F5F3F4F8091BC0087FD03C069
:100590002A353105B9F73C9A8CE68093BB0084E894
:1005A0008093BC0020E030E002C02F5F3F4F80917D
:1005B000BC0087FD03C02A353105B9F73C9A8CE0B1
:1005C0008093BB0084E88093BC0020E030E002C050
:1005D0002F5F3F4F8091BC0087FD03C02A35310556
:1005E000B9F784E98093BC003C9A84EA8093BC000C
:1005F00020E030E002C02F5F3F4F8091BC0087FDBC
:1006000003C02A353105B9F73C9A8DE68093BB00CB
:1006100084E88093BC0020E030E002C02F5F3F4FB1
:100620008091BC0087FD03C02A353105B9F78FEFF3
:100630000E94150120E030E002C02F5F3F4F809103
:10064000BC0087FD03C02A353105B9F70091BB0016
:1006500020E030E002C02F5F3F4F8091BC0087FD5B
:1006600003C02A353105B9F78FEF0E94150120E04C
:1006700030E002C02F5F3F4F8091BC0087FD03C078
:100680002A353105B9F71091BB0020E030E002C0F7
:100690002F5F3F4F8091BC0087FD03C02A35310595
:1006A000B9F78FEF0E94150184E98093BC003C9A52
:1006B00084EA8093BC00902F80E0212F30E0822BD1
:1006C000932BBC0180E090E01F910F9108953C9A1C
:1006D00084EA8093BC0020E030E002C02F5F3F4FEF
:1006E0008091BC0087FD03C02A353105B9F73C9ADB
:1006F0008CE68093BB0084E88093BC0020E030E06F
:1007000002C02F5F3F4F8091BC0087FD03C02A3598
:100710003105B9F73C9A86E08093BB0084E880936A
:10072000BC0020E030E002C02F5F3F4F8091BC0052
:1007300087FD03C02A353105B9F73C9A80E48093E0
:10074000BB0084E88093BC0020E030E002C02F5F53
:100750003F4F8091BC0087FD03C02A353105B9F7B2
:100760003C9A1092BB0084E88093BC0020E030E00B
:1007700002C02F5F3F4F8091BC0087FD03C02A3528
:100780003105B9F784E98093BC003C9A84EA8093F0
:10079000BC0008951F93182F8A3019F48DE00E9431
:1007A000CA038091C00085FFFCCF1093C60080E093
:1007B00090E01F9108958091C00087FFFCCF809149
:1007C000C600089506C020E000002F5F283CE1F736
:1007D000019783339105B8F708959093C5008093EE
:1007E000C4001092C00088E18093C10086E080932D
:1007F000C20088E191E0909331018093300180ED57
:1008000097E00E94E20308958FEE84B98FEF87B9D5
:1008100082EF8AB95A9A80E388B92D9883E390E0F1
:100820000E94ED030E94EE0082E08093690081E067
:100830008DBB10928000E1E8F0E0808184608083CD
:10084000EFE6F0E08081816080838EEE95E8909302
:10085000850080938400789408950E9404040E9487
:1008600097010E943401FFCF1F933C9A84EA809342
:10087000BC0020E030E002C02F5F3F4F8091BC0001
:1008800087FD03C02A353105B9F73C9A8CE6809381
:10089000BB0084E88093BC0020E030E002C02F5F02
:1008A0003F4F8091BC0087FD03C02A353105B9F761
:1008B0003C9A84E08093BB0084E88093BC0020E0F5
:1008C00030E002C02F5F3F4F8091BC0087FD03C026
:1008D0002A353105B9F784E98093BC003C9A84EA53
:1008E0008093BC0020E030E002C02F5F3F4F80913A
:1008F000BC0087FD03C02A353105B9F73C9A8DE667
:100900008093BB0084E88093BC0020E030E002C00C
:100910002F5F3F4F8091BC0087FD03C02A35310512
:10092000B9F78FEF0E94150120E030E002C02F5F81
:100930003F4F8091BC0087FD03C02A353105B9F7D0
:100940001091BB0020E030E002C02F5F3F4F80914C
:10095000BC0087FD03C02A353105B9F78FEF0E942F
:10096000150120E030E002C02F5F3F4F8091BC00B6
:1009700087FD03C02A353105B9F78091BB0020E01F
:1009800030E002C02F5F3F4F8091BC0087FD03C065
:100990002A353105B9F78FEF0E94150184E980935C
:1009A000BC003C9A84EA8093BC0000D000D0EDB734
:1009B000FEB7319680E091E0ADB7BEB712969C933A
:1009C0008E931197128313820E943D0B0F900F900C
:1009D0000F900F901F9108951F920F920FB60F92D4
:1009E00011242F933F934F935F936F937F938F9334
:1009F0009F93AF93BF93EF93FF938EEE95E8909301
:100A00008500809384000E94FA0100D000D000D0BD
:100A10002DE031E0EDB7FEB7328321836383748329
:100A2000858396830E943D0B8DB79EB706960FB6C1
:100A3000F8949EBF0FBE8DBF0E943404FF91EF91CA
:100A4000BF91AF919F918F917F916F915F914F91E6
:100A50003F912F910F900FBE0F901F901895A1E11D
:100A6000B0E0E5E3F5E00C94EC093C017F876E878C
:100A70006A01FC0117821682838181FD03C06FEF3A
:100A80007FEF6FC39E012F5F3F4F398B288BF301A0
:100A90002381EE85FF8523FD859123FF8191FF87CB
:100AA000EE87882309F45AC3853251F4EE85FF8519
:100AB00023FD859123FF8191FF87EE87853229F4FD
:100AC00090E0B3010E94110BE2CF982F10E0882430
:100AD00099241032B0F49B3269F09C3228F49032A1
:100AE00051F0933271F40BC09D3239F0903349F4D8
:100AF000116028C01260146025C0186023C0106106
:100B000021C017FD2AC0892F80538A3078F416FF40
:100B100006C0FAE09F9E902C1124980E13C03AE074
:100B2000839E802C1124880E10620CC09E3221F40A
:100B300016FD14C3106406C09C3611F4106802C080
:100B4000983659F4EE85FF8523FD959123FF919109
:100B5000FF87EE87992309F0BCCF892F8554833016
:100B600020F4812F8061905E07C0892F85568330E5
:100B700008F09FC1812F8F7E86FD02C076E0972E00
:100B80006FE3F62EF822953619F4F0E4FF2A07C039
:100B9000963619F420E8F22A02C091109A94F7FED2
:100BA0000AC03BE3391518F45CE3B52E02C0B92C3A
:100BB000B39427E009C047E0491520F4BB2447E07F
:100BC000942EF7CF292DBB24C60104969D878C87D0
:100BD000F6016081718182819381AE014F5F5F4F29
:100BE0000B2D0E94230A6C010981202E332400FF63
:100BF00004C003FD02C01DE209C0F1FE02C01BE2F9
:100C000005C0F2FC02C010E001C010E2C1018C700E
:100C10009070892BB9F1112311F483E001C084E0B5
:100C2000881510F088240AC0881AF3FC07C080E2F7
:100C300090E0B3010E94110B8A94C9F7112329F0A7
:100C4000812F90E0B3010E94110B23FE03C008E640
:100C500010E00EC00CE610E00BC0E114F10409F046
:100C6000805290E0B3010E94110B0F5F1F4F05C02F
:100C7000EF2CFF24F0E1EF22FF24F8018491882378
:100C800061F714C1F7FE12C0BC0C24FE04C08A81B7
:100C9000813309F4BA941B141CF0BB24B3942DC007
:100CA000F8E0FB1550F538E0B32E27C0F6FC25C060
:100CB000892D90E08C159D054CF02CEFC2162FEF7E
:100CC000D20624F030E8F32A01C09A94992049F022
:100CD000E2E0F0E0EC0FFD1FE90DF11D80818033B3
:100CE000A1F3F7FE0AC0B92CB394892D90E0C81681
:100CF000D90614F0992401C09C18F7FC03C025E024
:100D000030E009C01C141D041CF021E030E003C0D9
:100D100096012F5F3F4F112311F02F5F3F4F992016
:100D200029F0892D90E00196280F391F882D90E039
:100D30002817390714F0882401C0821A4F2C552433
:100D4000C20189709070892B39F008C080E290E070
:100D5000B3010E94110B8A948820C1F7112329F056
:100D6000812F90E0B3010E94110B43FE07C008C021
:100D700080E390E0B3010E94110B8A948820C1F7B0
:100D8000F7FE46C08601D7FE02C000E010E0760103
:100D90000894E11CF11CE01AF10A41E050E04C0F0C
:100DA0005D1FE40EF51E26014B185108892D90E0B9
:100DB000AA24BB24A81AB90A5FEF0F3F150729F42C
:100DC0008EE290E0B3010E94110BC016D10634F000
:100DD000401651061CF4F701808101C080E30150E8
:100DE00010400894E11CF11C0A151B052CF090E042
:100DF000B3010E94110BE0CF0C151D0539F49A8147
:100E0000963318F4953311F424FE81E390E04BC03F
:100E10008A81813309F00F7E90E0B3010E94110BAB
:100E20009920A1F08EE290E0B3010E94110B12E034
:100E3000E1E0F0E0EC0FFD1FE10FF11D1F5F80818D
:100E400090E0B3010E94110B9A9491F744FC03C007
:100E500085E690E002C085E490E0B3010E94110BAA
:100E6000D7FC05C0C114D10441F404FF06C0D094DE
:100E7000C194D108D3948DE201C08BE290E0B3011C
:100E80000E94110B80E305C08F5F26EF3FEFC20E7B
:100E9000D31E3AE0C316D104BCF790E0B3010E9420
:100EA000110BC601C096B3010E94110BCC84DD84E6
:100EB00052C1933631F0933799F0933509F059C008
:100EC00023C0F601808189835E010894A11CB11CB6
:100ED00022E030E0C20ED31E21E0E22EF12C12C03F
:100EE000F601A080B18016FD03C06FEF7FEF02C056
:100EF000692D70E022E030E0C20ED31EC5010E94D1
:100F0000060B7C011F7713C0F601A080B18016FD8F
:100F100003C06FEF7FEF02C0692D70E022E030E088
:100F2000C20ED31EC5010E94FB0A7C01106813FF8C
:100F300007C01BC080E290E0B3010E94110B8A94AD
:100F4000882D90E0E816F906A8F30FC0F50117FD0B
:100F5000859117FF81915F0190E0B3010E94110B11
:100F600081108A940894E108F108E114F10471F702
:100F7000F2C0943611F0993689F517FF08C0F601D2
:100F8000208131814281538184E090E00AC0F601E2
:100F9000808191819C01442737FD4095542F82E048
:100FA00090E0C80ED91E9FE6F92EF12257FF09C026
:100FB00050954095309521953F4F4F4F5F4F90E8AA
:100FC000F92ACA01B901AE014F5F5F4F2AE030E054
:100FD0000E94520BE82EE889EE1A41C0953721F4A1
:100FE0001F7E2AE030E01CC0197F9F3661F09037E9
:100FF00020F4983509F0B2C00FC0903739F0983717
:1010000009F0ACC004C028E030E00AC0106114FD53
:10101000146020E130E004C014FD166020E132E0ED
:1010200017FF08C0F601608171818281938144E0DD
:1010300050E008C0F60180819181BC0180E090E021
:1010400042E050E0C40ED51EAE014F5F5F4F0E94DC
:10105000520BE82E5889E51A8FE7F82EF122F6FE9A
:101060000BC08EEFF822E91438F4F4FE07C0F2FC4E
:1010700005C09FEEF92202C01E2D01C0192DF4FEFD
:101080000DC0FE01EE0DF11D8081803319F4E9EEF3
:10109000FE2208C01F5FF2FE05C003C08F2D8678B8
:1010A00009F01F5F0F2DF3FC14C0F0FE0FC01815E0
:1010B00010F09E2C0BC09E2C980C911A182D06C077
:1010C00080E290E0B3010E94110B1F5F1815C0F37E
:1010D00004C0181510F4811A01C0882404FF0FC041
:1010E00080E390E0B3010E94110B02FF1DC001FDDF
:1010F00003C088E790E00EC088E590E00BC0802F29
:10110000867891F001FF02C08BE201C080E2F7FC1B
:101110008DE290E0B3010E94110B06C080E390E0E5
:10112000B3010E94110B9A94E914C0F3EA94E1E030
:10113000F0E0EC0FFD1FEE0DF11D808190E0B3019A
:101140000E94110BEE2091F706C080E290E0B301FF
:101150000E94110B8A948820C1F799CCF301668113
:101160007781CB016196E2E10C94080A04D068947F
:10117000B1118DC0089570D088F09F5790F0B92FAD
:101180009927B751A0F0D1F0660F771F881F991FDC
:101190001AF0BA95C9F712C0B13081F077D0B1E03A
:1011A000089574C0672F782F8827B85F39F0B93F4A
:1011B000CCF3869577956795B395D9F73EF49095DE
:1011C0008095709561957F4F8F4F9F4F0895E8945C
:1011D00009C097FB3EF490958095709561957F4F7F
:1011E0008F4F9F4F9923A9F0F92F96E9BB2793952D
:1011F000F695879577956795B795F111F8CFFAF43D
:10120000BB0F11F460FF1BC06F5F7F4F8F4F9F4F6D
:1012100016C0882311F096E911C0772321F09EE8CB
:10122000872F762F05C0662371F096E8862F70E031
:1012300060E02AF09A95660F771F881FDAF7880F0B
:101240009695879597F9089557FD9058440F551F27
:1012500059F05F3F71F04795880F97FB991F61F038
:101260009F3F79F087950895121613061406551FAF
:10127000F2CF4695F1DF08C0161617061806991F1B
:10128000F1CF86957105610508940895E894BB2710
:1012900066277727CB0197F908950BD078C069D0DE
:1012A00028F06ED018F0952309F05AC05FC01124C1
:1012B000EECFCADFA0F3959FD1F3950F50E0551FF5
:1012C000629FF001729FBB27F00DB11D639FAA279B
:1012D000F00DB11DAA1F649F6627B00DA11D661FEA
:1012E000829F2227B00DA11D621F739FB00DA11D0B
:1012F000621F839FA00D611D221F749F3327A00DC5
:10130000611D231F849F600D211D822F762F6A2F60
:1013100011249F5750408AF0E1F088234AF0EE0FE5
:10132000FF1FBB1F661F771F881F91505040A9F7F2
:101330009E3F510570F014C0AACF5F3FECF3983E7A
:10134000DCF3869577956795B795F795E7959F5F59
:10135000C1F7FE2B880F911D9695879597F90895F3
:1013600097F99F6780E870E060E008959FEF80EC58
:10137000089500240A9416161706180609060895FB
:1013800000240A9412161306140605060895092E61
:101390000394000C11F4882352F0BB0F40F4BF2BD0
:1013A00011F460FF04C06F5F7F4F8F4F9F4F089510
:1013B000AA1BBB1B51E107C0AA1FBB1FA617B7077B
:1013C00010F0A61BB70B881F991F5A95A9F7809597
:1013D0009095BC01CD0108952F923F924F925F925C
:1013E0006F927F928F929F92AF92BF92CF92DF9235
:1013F000EF92FF920F931F93CF93DF93CDB7DEB79A
:10140000CA1BDB0B0FB6F894DEBF0FBECDBF09942D
:101410002A88398848885F846E847D848C849B8484
:10142000AA84B984C884DF80EE80FD800C811B8192
:10143000AA81B981CE0FD11D0FB6F894DEBF0FBEC1
:10144000CDBFED010895283008F027E03327DA01F9
:10145000990F311D87FD916000966105710539F482
:1014600032602E5F3D9330E32A95E1F708959F3F68
:1014700030F080387105610509F03C5F3C5F3D93B9
:10148000913008F08068911DDF93CF931F930F93E5
:10149000FF92EF92192F987F9695E92F9695969542
:1014A000E90FFF27E653FF4F99273327EE24FF2448
:1014B000A701E70105900894079428F4360FE71E6A
:1014C000F81E491F511D660F771F881F991F06942C
:1014D000A1F70590079428F4E70EF81E491F561F40
:1014E000C11D770F881F991F661F0694A1F70590ED
:1014F000079428F4F80E491F561FC71FD11D880FE7
:10150000991F661F771F0694A1F70590079420F492
:10151000490F561FC71FD81F990F661F771F881FB7
:101520000694A9F784911095177041F0D695C79548
:1015300057954795F794E7941A95C1F7E0E7F0E0DF
:1015400068941590159135916591959105907FE27C
:101550007395E118F10A430B560BC90BD009C0F77C
:10156000E10CF11E431F561FC91FD01D7EF47033BE
:1015700011F48A95E6CFE894015030F0080F0AF490
:101580000027021708F4202F2395022F7A3328F022
:1015900079E37D932A95E9F710C07D932A9589F622
:1015A000069497956795379517951794E118F10A62
:1015B000430B560BC90BD00998F023957E91739578
:1015C0007A3308F070E37C932013B8F77E91706152
:1015D0007D9330F0839571E37D9370E32A95E1F775
:1015E0001124EF90FF900F911F91CF91DF919927D8
:1015F00087FD90950895FC010590615070400110A1
:10160000D8F7809590958E0F9F1F0895FC0161502B
:10161000704001900110D8F7809590958E0F9F1F14
:1016200008950F931F93CF93DF938C01EB018B8170
:1016300081FF1BC082FF0DC02E813F818C819D8167
:101640002817390764F4E881F9810193F983E88365
:1016500006C0E885F985802F0995892B31F48E81A4
:101660009F8101969F838E8302C00FEF1FEFC801F9
:10167000DF91CF911F910F910895A0E0B0E0E3E4D6
:10168000FBE00C94FC09FE0135966191719180910B
:10169000300190913101AF010E942F052096E2E0C8
:1016A0000C94180AFA01AA27283051F1203181F14F
:1016B000E8946F936E7F6E5F7F4F8F4F9F4FAF4F5A
:1016C000B1E03ED0B4E03CD0670F781F891F9A1F6D
:1016D000A11D680F791F8A1F911DA11D6A0F711D21
:1016E000811D911DA11D20D009F468943F912AE02D
:1016F000269F11243019305D3193DEF6CF01089515
:10170000462F4770405D4193B3E00FD0C9F7F6CF45
:10171000462F4F70405D4A3318F0495D31FD40520D
:10172000419302D0A9F7EACFB4E0A69597958795A3
:1017300077956795BA95C9F7009761057105089582
:101740009B01AC010A2E069457954795379527952E
:10175000BA95C9F7620F731F841F951FA01D0895C6
:04176000F894FFCF2B
:1017640070657263656E743D25640A0D00612F64B3
:101774003D25356C642C20000000000200000000B0
:06178400CA030000000092
:00000001FF

View File

@ -0,0 +1,555 @@
# Hey Emacs, this is a -*- makefile -*-
#----------------------------------------------------------------------------
# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
#
# Released to the Public Domain
#
# Additional material for this makefile was written by:
# Peter Fleury
# Tim Henigan
# Colin O'Flynn
# Reiner Patommel
# Markus Pfaff
# Sander Pool
# Frederik Rouleau
#
#----------------------------------------------------------------------------
# On command line:
#
# make all = Make software.
#
# make clean = Clean out built project files.
#
# make coff = Convert ELF to AVR COFF.
#
# make extcoff = Convert ELF to AVR Extended COFF.
#
# make program = Download the hex file to the device, using avrdude.
# Please customize the avrdude settings below first!
#
# make debug = Start either simulavr or avarice as specified for debugging,
# with avr-gdb or avr-insight as the front end for debugging.
#
# make filename.s = Just compile filename.c into the assembler code only.
#
# make filename.i = Create a preprocessed source file for use in submitting
# bug reports to the GCC project.
#
# To rebuild project do "make clean" then "make all".
#----------------------------------------------------------------------------
# MCU name
MCU = atmega328p
# Processor frequency.
# This will define a symbol, F_CPU, in all source code files equal to the
# processor frequency. You can then use this symbol in your source code to
# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
# automatically to create a 32-bit value in your source code.
F_CPU = 8000000
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# Target file name (without extension).
TARGET = main
# List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c
# List Assembler source files here.
# Make them always end in a capital .S. Files ending in a lowercase .s
# will not be considered source files but generated files (assembler
# output from the compiler), and will be deleted upon "make clean"!
# Even though the DOS/Win* filesystem matches both .s and .S the same,
# it will preserve the spelling of the filenames, and gcc itself does
# care about how the name is spelled on its command-line.
ASRC =
# Optimization level, can be [0, 1, 2, 3, s].
# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s
# Debugging format.
# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
# AVR Studio 4.10 requires dwarf-2.
# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
DEBUG = dwarf-2
# List any extra directories to look for include files here.
# Each directory must be seperated by a space.
# Use forward slashes for directory separators.
# For a directory that has spaces, enclose it in quotes.
EXTRAINCDIRS =
# Compiler flag to set the C Standard level.
# c89 = "ANSI" C
# gnu89 = c89 plus GCC extensions
# c99 = ISO C99 standard (not yet fully implemented)
# gnu99 = c99 plus GCC extensions
CSTANDARD = -std=gnu99
# Place -D or -U options here
CDEFS = -DF_CPU=$(F_CPU)UL
# Place -I options here
CINCS =
#---------------- Compiler Options ----------------
# -g*: generate debugging information
# -O*: optimization level
# -f...: tuning, see GCC manual and avr-libc documentation
# -Wall...: warning level
# -Wa,...: tell GCC to pass this to the assembler.
# -adhlns...: create assembler listing
CFLAGS = -g$(DEBUG)
CFLAGS += $(CDEFS) $(CINCS)
CFLAGS += -O$(OPT)
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -Wall -Wstrict-prototypes
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
CFLAGS += $(CSTANDARD)
#---------------- Assembler Options ----------------
# -Wa,...: tell GCC to pass this to the assembler.
# -ahlms: create listing
# -gstabs: have the assembler create line number information; note that
# for use in COFF files, additional information about filenames
# and function names needs to be present in the assembler source
# files -- see avr-libc docs [FIXME: not yet described there]
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
#---------------- Library Options ----------------
# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
# If this is left blank, then it will use the Standard printf version.
#PRINTF_LIB =
#PRINTF_LIB = $(PRINTF_LIB_MIN)
PRINTF_LIB = $(PRINTF_LIB_FLOAT)
# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
# If this is left blank, then it will use the Standard scanf version.
SCANF_LIB =
#SCANF_LIB = $(SCANF_LIB_MIN)
#SCANF_LIB = $(SCANF_LIB_FLOAT)
MATH_LIB = -lm
#---------------- External Memory Options ----------------
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# used for variables (.data/.bss) and heap (malloc()).
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# only used for heap (malloc()).
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
EXTMEMOPTS =
#---------------- Linker Options ----------------
# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += $(EXTMEMOPTS)
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
#---------------- Programming Options (avrdude serial bootloader) ----------------
#"C:\arduino\hardware\tools\avr\bin\avrdude" -PCOM3 -c stk500v1 -patmega168 -b19200 -Uflash:w:Simon-PTH-v1.hex -V -F -C"C:\arduino\hardware\tools\avr\etc\avrdude.conf"
SERIAL_AVRDUDE = "C:\Documents and Settings\aweiss\My Documents\arduino-0022\arduino-0022\hardware/tools/avr/bin/avrdude"
SERIAL_AVRDUDE_CONFIG = "C:\Documents and Settings\aweiss\My Documents\arduino-0022\arduino-0022\hardware/tools/avr/etc/avrdude.conf"
SERIAL_AVRDUDE_PORT = COM4
SERIAL_AVRDUDE_SPEED = 57600
SERIAL_AVRDUDE_PROGRAMMER = stk500v1
SERIAL_AVRDUDE_FLAGS = -p $(MCU) -P $(SERIAL_AVRDUDE_PORT) -c $(SERIAL_AVRDUDE_PROGRAMMER) -b $(SERIAL_AVRDUDE_SPEED)
SERIAL_AVRDUDE_FLAGS += -C$(SERIAL_AVRDUDE_CONFIG)
SERIAL_AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
SERIAL_AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
SERIAL_AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
#---------------- Programming Options (avrdude) ----------------
# Programming hardware: alf avr910 avrisp bascom bsd
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
#
# Type: avrdude -c ?
# to get a full listing.
AVRDUDE_PROGRAMMER = stk500v1
#AVRDUDE_PROGRAMMER = ponyser
# com1 = serial port. Use lpt1 to connect to parallel port.
#AVRDUDE_PORT = lpt1
AVRDUDE_PORT = COM5
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
# Uncomment the following if you want avrdude's erase cycle counter.
# Note that this counter needs to be initialized first using -Yn,
# see avrdude manual.
#AVRDUDE_ERASE_COUNTER = -y
# Uncomment the following if you do /not/ wish a verification to be
# performed after programming the device.
#AVRDUDE_NO_VERIFY = -V
# Increase verbosity level. Please use this when submitting bug
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
# to submit bug reports.
#AVRDUDE_VERBOSE = -v -v
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
#---------------- Programming Options (STK500) ----------------
# Programming hardware: stk500 (the AVR MKII ISP version)
STK500 = stk500
# Location of STK500.exe - no trailing '\'
STK500_PATH = C:\Program Files\Atmel\AVR Tools\STK500
# The STK500 AVR ISP MKII is USB. The USB drivers must already be installed.
# Do this normally by installing AVR Studio.
STK500_PORT = USB
#-erase chip -Program Flash -Verify Flash -File name -Serial programing(ISP)
STK500_WRITE_FLASH = -e -pf -vf -if$(TARGET).hex -ms
STK500_FLAGS = -d$(MCU) -c$(STK500_PORT)
#-Set ISP frequency to 250kHz. Limit is 1/4 of internal osc which is default 1MHz
#Reduce this to 100kHz if you run into flash verification problems in low-voltage systems
STK500_FLAGS += -I250kHz
#---------------- Debugging Options ----------------
# For simulavr only - target MCU frequency.
DEBUG_MFREQ = $(F_CPU)
# Set the DEBUG_UI to either gdb or insight.
# DEBUG_UI = gdb
DEBUG_UI = insight
# Set the debugging back-end to either avarice, simulavr.
DEBUG_BACKEND = avarice
#DEBUG_BACKEND = simulavr
# GDB Init Filename.
GDBINIT_FILE = __avr_gdbinit
# When using avarice settings for the JTAG
JTAG_DEV = /dev/com1
# Debugging port used to communicate between GDB / avarice / simulavr.
DEBUG_PORT = 4242
# Debugging host used to communicate between GDB / avarice / simulavr, normally
# just set to localhost unless doing some sort of crazy debugging when
# avarice is running on a different computer.
DEBUG_HOST = localhost
#============================================================================
# Define programs and commands.
SHELL = sh
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm -f
COPY = cp
WINSHELL = cmd
# Define Messages
# English
MSG_ERRORS_NONE = Errors: none
MSG_BEGIN = -------- begin --------
MSG_END = -------- end --------
MSG_SIZE_BEFORE = Size before:
MSG_SIZE_AFTER = Size after:
MSG_COFF = Converting to AVR COFF:
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cleaning project:
# Define all object files.
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
# Define all listing files.
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst)
# Compiler flags to generate dependency files.
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
# Default target.
all: begin gccversion sizebefore build sizeafter end
build: elf hex eep lss sym
elf: $(TARGET).elf
hex: $(TARGET).hex
eep: $(TARGET).eep
lss: $(TARGET).lss
sym: $(TARGET).sym
# Eye candy.
# AVR Studio 3.x does not check make's exit code but relies on
# the following magic strings to be generated by the compile job.
begin:
@echo
@echo $(MSG_BEGIN)
end:
@echo $(MSG_END)
@echo
# Display size of file.
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
#New
ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
#Old
#ELFSIZE = $(SIZE) -A $(TARGET).elf
AVRMEM = avr-mem.sh $(TARGET).elf $(MCU)
sizebefore:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
$(AVRMEM) 2>/dev/null; echo; fi
sizeafter:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
$(AVRMEM) 2>/dev/null; echo; fi
# Display compiler version information.
gccversion :
@$(CC) --version
# Program the device.
program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
program_stk500: $(TARGET).hex $(TARGET).eep
$(STK500_PATH)\$(STK500) $(STK500_FLAGS) $(STK500_WRITE_FLASH)
program_serial: $(TARGET).hex $(TARGET).eep
$(SERIAL_AVRDUDE) $(SERIAL_AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)
# Generate avr-gdb config/init file which does the following:
# define the reset signal, load the target file, connect to target, and set
# a breakpoint at main().
gdb-config:
@$(REMOVE) $(GDBINIT_FILE)
@echo define reset >> $(GDBINIT_FILE)
@echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
@echo end >> $(GDBINIT_FILE)
@echo file $(TARGET).elf >> $(GDBINIT_FILE)
@echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
ifeq ($(DEBUG_BACKEND),simulavr)
@echo load >> $(GDBINIT_FILE)
endif
@echo break main >> $(GDBINIT_FILE)
debug: gdb-config $(TARGET).elf
ifeq ($(DEBUG_BACKEND), avarice)
@echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
@$(WINSHELL) /c pause
else
@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
$(DEBUG_MFREQ) --port $(DEBUG_PORT)
endif
@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000
coff: $(TARGET).elf
@echo
@echo $(MSG_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
extcoff: $(TARGET).elf
@echo
@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
%.eep: %.elf
@echo
@echo $(MSG_EEPROM) $@
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
# Create extended listing file from ELF output file.
%.lss: %.elf
@echo
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S $< > $@
# Create a symbol table from ELF output file.
%.sym: %.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@
# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
@echo
@echo $(MSG_LINKING) $@
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
# Compile: create object files from C source files.
%.o : %.c
@echo
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS) $< -o $@
# Compile: create assembler files from C source files.
%.s : %.c
$(CC) -S $(ALL_CFLAGS) $< -o $@
# Assemble: create object files from assembler source files.
%.o : %.S
@echo
@echo $(MSG_ASSEMBLING) $<
$(CC) -c $(ALL_ASFLAGS) $< -o $@
# Create preprocessed source for use in sending a bug report.
%.i : %.c
$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
# Target: clean project.
clean: begin clean_list end
clean_list :
@echo
@echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).eep
$(REMOVE) $(TARGET).cof
$(REMOVE) $(TARGET).elf
$(REMOVE) $(TARGET).map
$(REMOVE) $(TARGET).sym
$(REMOVE) $(TARGET).lss
$(REMOVE) $(OBJ)
$(REMOVE) $(LST)
$(REMOVE) $(SRC:.c=.s)
$(REMOVE) $(SRC:.c=.d)
$(REMOVE) .dep/*
# Include the dependency files.
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter gccversion \
build elf hex eep lss sym coff extcoff \
clean clean_list program debug gdb-config

View File

@ -0,0 +1,65 @@
//useful things to include in code
#ifndef TYPES_H
#define TYPES_H
#ifndef WIN32
// true/false defines
#define FALSE 0
#define TRUE -1
#endif
// datatype definitions macros
typedef unsigned char u08;
typedef signed char s08;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned long u32;
typedef signed long s32;
typedef unsigned long long u64;
typedef signed long long s64;
/* use inttypes.h instead
// C99 standard integer type definitions
typedef unsigned char uint8_t;
typedef signed char int8_t;
typedef unsigned short uint16_t;
typedef signed short int16_t;
typedef unsigned long uint32_t;
typedef signed long int32_t;
typedef unsigned long uint64_t;
typedef signed long int64_t;
*/
// maximum value that can be held
// by unsigned data types (8,16,32bits)
#define MAX_U08 255
#define MAX_U16 65535
#define MAX_U32 4294967295
// maximum values that can be held
// by signed data types (8,16,32bits)
#define MIN_S08 -128
#define MAX_S08 127
#define MIN_S16 -32768
#define MAX_S16 32767
#define MIN_S32 -2147483648
#define MAX_S32 2147483647
#ifndef WIN32
// more type redefinitions
typedef unsigned char BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef unsigned long DWORD;
typedef unsigned char UCHAR;
typedef unsigned int UINT;
typedef unsigned short USHORT;
typedef unsigned long ULONG;
typedef char CHAR;
typedef int INT;
typedef long LONG;
#endif
#endif

View File

@ -0,0 +1,32 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app

View File

@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

View File

@ -0,0 +1,123 @@
# MAX1704X
Arduino library for the MAX17043 and MAX17044 LiPo Battery Fuel Gauge. For an example of this device on a breakout board see the [SparkFun LiPo Fuel Gauge](https://www.sparkfun.com/products/10617).
![](https://github.com/porrey/max1704x/blob/master/extras/SparkFunLiPoFuelGauge-small.png)
This device is also available on breakout boards from [Amazon](https://www.amazon.com/s/ref=nb_sb_noss_1?url=search-alias%3Daps&field-keywords=max17043&rh=i%3Aaps%2Ck%3Amax17043).
## Usage ##
The step is to include the library in your sketch. Include MAX17043.h if you are suing the single cell chip or MAX17044.h if you are using the two cell chip.
#include "MAX17043.h"
Next, in the **`setup()`** routine call begin on the Fuel Gauge. Initialize the Serial interface to display the device properties to the serial output.
void setup()
{
// Initialize the serial interface.
Serial.begin(115200);
// Initialize the fuel gauge.
FuelGauge.begin();
}
In the loop, use the properties to show the state of the battery and the fuel gauge.
void loop()
{
// Get the voltage, battery percent
// and other properties.
Serial.print("Version: "); Serial.println(FuelGauge.version());
Serial.print("ADC: "); Serial.println(FuelGauge.adc());
Serial.print("Voltage: "); Serial.print(FuelGauge.voltage()); Serial.println(" v");
Serial.print("Percent: "); Serial.print(FuelGauge.percent()); Serial.println("%");
Serial.print("Is Sleeping: "); Serial.println(FuelGauge.isSleeping() ? "Yes" : "No");
Serial.print("Alert: "); Serial.println(FuelGauge.alertIsActive() ? "Yes" : "No");
Serial.print("Threshold: "); Serial.println(FuelGauge.getThreshold());
Serial.print("Compensation: 0x"); Serial.println(FuelGauge.compensation(), HEX);
}
## Sleep Mode ##
Holding both SDA and SCL logic-low forces the MAX17043/MAX17044 into Sleep mode. While in Sleep mode, all IC operations are halted and power drain of the IC is greatly reduced. After exiting Sleep mode, fuel-gauge operation continues from the point it was halted. SDA and SCL must be held low for at least 2.5s to guarantee transition into Sleep mode. Afterwards, a rising edge on either SDA or SCL immediately transitions the IC out of Sleep mode.
> Entering Sleep mode does not clear the interrupt.
Alternatively, Sleep mode can be calling the **`sleep()`** method to put the device into sleep mode via software and the **`wake()`** method to take it out of sleep mode. The **`isSleeping()`** method can be used to get the current state of device.
### Entering Seep Mode ###
The sample code below demonstrates how to check if the device is in sleep mode and then puts it in sleep mode if it is not.
if (!FuelGauge.isSleeping())
{
FuelGauge.sleep();
if (FuelGauge.isSleeping())
{
Serial.println("Fuel Gauge put in sleep mode.");
}
else
{
Serial.println("Fuel Gauge failed to be put in sleep mode.");
}
}
else
{
Serial.println("Fuel Gauge is already in sleep mode.");
}
### Exiting Seep Mode ###
The sample code below demonstrates how to check if the device is in sleep mode and then wakes it up if it is.
if (FuelGauge.isSleeping())
{
FuelGauge.wake();
if (!FuelGauge.isSleeping())
{
Serial.println("Fuel Gauge is now awake.");
}
else
{
Serial.println("Failed to wake Fuel Gauge.");
}
}
else
{
Serial.println("Fuel Gauge is already awake.");
}
## Resetting the Device ##
Calling the **`reset()`** method causes the MAX17043/MAX17044 to completely reset as if power had been removed. The reset occurs when the last bit has been clocked in. The IC does not respond with an I2C ACK after this command sequence.
FuelGauge.reset();
## Quick-Start
A quick-start allows the MAX17043/MAX17044 to restart fuel-gauge calculations in the same manner as initial power-up of the IC. For example, if an applications power-up sequence is exceedingly noisy such that excess error is introduced into the ICs “first guess” of SOC, the host can issue a quick-start to reduce the error. A quick-start is initiated by a rising edge on the QSTRT pin, or through software by calling the **`quickstart()`** method.
FuelGauge.quickstart();
## Alert Threshold ##
The MAX17043/MAX17044 have an interrupt feature that alerts a host microprocessor whenever the cell's state of charge, as defined by the SOC register, falls below a predefined alert threshold set at address 0Dh of the CONFIG register. When an alert is triggered, the IC drives the ALRT pin to logic-low and sets the ALRT bit in the CONFIG register to logic 1. The ALRT pin remains logic-low until the host software writes the ALRT bit to logic 0 to clear the interrupt. Clearing the ALRT bit while SOC is below the alert threshold does not generate another interrupt. The SOC register must first rise above and then fall below the alert threshold value before another interrupt is generated. Note that the alert function is not disabled at IC powerup. If the first SOC calculation is below the threshold setting, an interrupt is generated. Entering Sleep mode does not clear the interrupt.
### Check the Alert Status ###
The current status of the alert interrupt can be checked via software. This is done by calling the **`alertIsActive()`** method. See next section for an example of using this method.
### Clear the Alert Status ###
The **`clearAlert()`** method can be called to clear the current alert bit in the config register.
if (FuelGauge.alertIsActive())
{
FuelGauge.clearAlert();
}
### Change the Alert Threshold ###
The alert threshold is a 5-bit value that sets the state of charge level where an interrupt is generated on the **ALRT** pin. The alert threshold has an LSb weight of 1% and can be programmed from 1% up to 32%. The power-up default value for **ATHD** is 4%.
The **`getThreshold()`** and **`setThresold()`** methods can be used to get the current threshold setting and to change the threshold setting.
// Decrease the threshold by 1
uint8_t threshold = FuelGauge.getThreshold();
FuelGauge.setThreshold(--threshold);
Portions of this document are taken directly from the Maxim data-sheet for the MAX17043/MAX17044. To see the full details the data sheet can be found at [https://datasheets.maximintegrated.com/en/ds/MAX17043-MAX17044.pdf](https://datasheets.maximintegrated.com/en/ds/MAX17043-MAX17044.pdf).

View File

@ -0,0 +1,88 @@
/*
MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
Version 1.0.0
Copyright © 2018 Daniel Porrey. All Rights Reserved.
https://github.com/porrey/max1704x
This file is part of the MAX1704X Arduino Library.
The MAX1704X Arduino Library is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The MAX1704X Arduino Library is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the MAX1704X Arduino Library. If not,
see http://www.gnu.org/licenses/.
*/
#include "MAX1704X.h"
// ***
// *** The MAX17043 is a one-cell device with a
// *** a voltage measurement range of 0 to 5 V.
// ***
MAX1704X _fuelGauge1 = MAX1704X(5);
// ***
// *** The MAX17044 is a two-cell device with a
// *** a voltage measurement range of 0 to 10 V.
// ***
MAX1704X _fuelGauge2 = MAX1704X(10);
void setup()
{
// ***
// *** Initialize the serial interface.
// ***
Serial.begin(115200);
// ***
// *** Initialize the fuel gauges.
// ***
_fuelGauge1.begin();
_fuelGauge2.begin();
}
void loop()
{
// ***
// *** Display properties from the MAX17043.
// ***
Serial.println("MAX17043:");
displayFuelGauge(_fuelGauge1);
Serial.println();
// ***
// *** Display properties from the MAX17044.
// ***
Serial.println("MAX17044:");
displayFuelGauge(_fuelGauge2);
Serial.println();
// ***
// *** Delay 5 seconds.
// ***
delay(5000);
}
void displayFuelGauge(MAX1704X fuelGauge)
{
// ***
// *** Get the voltage, battery percent
// *** and other properties.
// ***
Serial.print("Version: "); Serial.println(fuelGauge.version());
Serial.print("ADC: "); Serial.println(fuelGauge.adc());
Serial.print("Voltage: "); Serial.print(fuelGauge.voltage()); Serial.println(" v");
Serial.print("Percent: "); Serial.print(fuelGauge.percent()); Serial.println("%");
Serial.print("Is Sleeping: "); Serial.println(fuelGauge.isSleeping() ? "Yes" : "No");
Serial.print("Alert: "); Serial.println(fuelGauge.alertIsActive() ? "Yes" : "No");
Serial.print("Threshold: "); Serial.println(fuelGauge.getThreshold());
Serial.print("Compensation: 0x"); Serial.println(fuelGauge.compensation(), HEX);
}

View File

@ -0,0 +1,202 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#include "MAX17043.h"
// ***
// *** Connections:
// *** 1) Conect SDA pin to A4 on Uno
// *** 2) Connect SCL pin to A5 on Uno
// *** 3) Connect the GND pin to ground on the Uno.
// ***
// *** For other devices lookup the correct i2C
// *** (SDA and SCL) pins.
// ***
void setup()
{
// ***
// *** Initialize the serial interface.
// ***
Serial.begin(115200);
// ***
// *** Initialize the fuel gauge.
// ***
FuelGauge.begin();
// ***
// *** Display an initial reading.
// ***
displayReading();
Serial.println();
displayMenu();
}
void loop()
{
Serial.println("Enter an option in the serial input (M for menu):");
while (Serial.available() == 0)
{
delay(25);
}
char c = Serial.read();
Serial.println();
switch (c)
{
case 'M':
displayMenu();
break;
case 'D':
displayReading();
break;
case 'S':
sleepMode();
break;
case 'W':
wakeMode();
break;
case 'Q':
quickStart();
break;
case 'C':
clearAlert();
break;
case 'R':
reset();
break;
case '+':
incrementThreshold();
break;
case '-':
decrementThreshold();
break;
}
Serial.println();
}
void displayMenu()
{
Serial.println("D => Display a reading.");
Serial.println("S => Enter sleep mode.");
Serial.println("W => Wake.");
Serial.println("Q => Quick start.");
Serial.println("C => Clear alert.");
Serial.println("R => Reset.");
Serial.println("+ => Increment threshold.");
Serial.println("- => Decrement threshold.");
}
void displayReading()
{
// ***
// *** Get the voltage, battery percent
// *** and other properties.
// ***
Serial.print("Version: "); Serial.println(FuelGauge.version());
Serial.print("ADC: "); Serial.println(FuelGauge.adc());
Serial.print("Voltage: "); Serial.print(FuelGauge.voltage()); Serial.println(" v");
Serial.print("Percent: "); Serial.print(FuelGauge.percent()); Serial.println("%");
Serial.print("Is Sleeping: "); Serial.println(FuelGauge.isSleeping() ? "Yes" : "No");
Serial.print("Alert: "); Serial.println(FuelGauge.alertIsActive() ? "Yes" : "No");
Serial.print("Threshold: "); Serial.println(FuelGauge.getThreshold());
Serial.print("Compensation: 0x"); Serial.println(FuelGauge.compensation(), HEX);
}
void sleepMode()
{
if (!FuelGauge.isSleeping())
{
FuelGauge.sleep();
if (FuelGauge.isSleeping())
{
Serial.println("Fuel Gauge put in sleep mode.");
}
else
{
Serial.println("Fuel Gauge failed to be put in sleep mode.");
}
}
else
{
Serial.println("Fuel Gauge is already in sleep mode.");
}
}
void wakeMode()
{
if (FuelGauge.isSleeping())
{
FuelGauge.wake();
if (!FuelGauge.isSleeping())
{
Serial.println("Fuel Gauge is now awake.");
}
else
{
Serial.println("Failed to wake Fuel Gauge.");
}
}
else
{
Serial.println("Fuel Gauge is already awake.");
}
}
void reset()
{
FuelGauge.reset();
Serial.println("Fuel Gauge has been reset/rebooted.");
}
void quickStart()
{
FuelGauge.quickstart();
Serial.println("Quick start has been initiated on the Fuel Gauge.");
}
void clearAlert()
{
FuelGauge.clearAlert();
Serial.println("The alert has been cleared on the Fuel Gauge.");
}
void incrementThreshold()
{
uint8_t threshold = FuelGauge.getThreshold();
FuelGauge.setThreshold(++threshold);
Serial.print("The alert threshold has been incremented to "); Serial.print(FuelGauge.getThreshold()); Serial.println(",");
}
void decrementThreshold()
{
uint8_t threshold = FuelGauge.getThreshold();
FuelGauge.setThreshold(--threshold);
Serial.print("The alert threshold has been decremented to "); Serial.print(FuelGauge.getThreshold()); Serial.println(",");
}

View File

@ -0,0 +1,202 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#include "MAX17044.h"
// ***
// *** Connections:
// *** 1) Conect SDA pin to A4 on Uno
// *** 2) Connect SCL pin to A5 on Uno
// *** 3) Connect the GND pin to ground on the Uno.
// ***
// *** For other devices lookup the correct i2C
// *** (SDA and SCL) pins.
// ***
void setup()
{
// ***
// *** Initialize the serial interface.
// ***
Serial.begin(115200);
// ***
// *** Initialize the fuel gauge.
// ***
FuelGauge.begin();
// ***
// *** Display an initial reading.
// ***
displayReading();
Serial.println();
displayMenu();
}
void loop()
{
Serial.println("Enter an option in the serial input (M for menu):");
while (Serial.available() == 0)
{
delay(25);
}
char c = Serial.read();
Serial.println();
switch (c)
{
case 'M':
displayMenu();
break;
case 'D':
displayReading();
break;
case 'S':
sleepMode();
break;
case 'W':
wakeMode();
break;
case 'Q':
quickStart();
break;
case 'C':
clearAlert();
break;
case 'R':
reset();
break;
case '+':
incrementThreshold();
break;
case '-':
decrementThreshold();
break;
}
Serial.println();
}
void displayMenu()
{
Serial.println("D => Display a reading.");
Serial.println("S => Enter sleep mode.");
Serial.println("W => Wake.");
Serial.println("Q => Quick start.");
Serial.println("C => Clear alert.");
Serial.println("R => Reset.");
Serial.println("+ => Increment threshold.");
Serial.println("- => Decrement threshold.");
}
void displayReading()
{
// ***
// *** Get the voltage, battery percent
// *** and other properties.
// ***
Serial.print("Version: "); Serial.println(FuelGauge.version());
Serial.print("ADC: "); Serial.println(FuelGauge.adc());
Serial.print("Voltage: "); Serial.print(FuelGauge.voltage()); Serial.println(" v");
Serial.print("Percent: "); Serial.print(FuelGauge.percent()); Serial.println("%");
Serial.print("Is Sleeping: "); Serial.println(FuelGauge.isSleeping() ? "Yes" : "No");
Serial.print("Alert: "); Serial.println(FuelGauge.alertIsActive() ? "Yes" : "No");
Serial.print("Threshold: "); Serial.println(FuelGauge.getThreshold());
Serial.print("Compensation: 0x"); Serial.println(FuelGauge.compensation(), HEX);
}
void sleepMode()
{
if (!FuelGauge.isSleeping())
{
FuelGauge.sleep();
if (FuelGauge.isSleeping())
{
Serial.println("Fuel Gauge put in sleep mode.");
}
else
{
Serial.println("Fuel Gauge failed to be put in sleep mode.");
}
}
else
{
Serial.println("Fuel Gauge is already in sleep mode.");
}
}
void wakeMode()
{
if (FuelGauge.isSleeping())
{
FuelGauge.wake();
if (!FuelGauge.isSleeping())
{
Serial.println("Fuel Gauge is now awake.");
}
else
{
Serial.println("Failed to wake Fuel Gauge.");
}
}
else
{
Serial.println("Fuel Gauge is already awake.");
}
}
void reset()
{
FuelGauge.reset();
Serial.println("Fuel Gauge has been reset/rebooted.");
}
void quickStart()
{
FuelGauge.quickstart();
Serial.println("Quick start has been initiated on the Fuel Gauge.");
}
void clearAlert()
{
FuelGauge.clearAlert();
Serial.println("The alert has been cleared on the Fuel Gauge.");
}
void incrementThreshold()
{
uint8_t threshold = FuelGauge.getThreshold();
FuelGauge.setThreshold(++threshold);
Serial.print("The alert threshold has been incremented to "); Serial.print(FuelGauge.getThreshold()); Serial.println(",");
}
void decrementThreshold()
{
uint8_t threshold = FuelGauge.getThreshold();
FuelGauge.setThreshold(--threshold);
Serial.print("The alert threshold has been decremented to "); Serial.print(FuelGauge.getThreshold()); Serial.println(",");
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

View File

@ -0,0 +1,40 @@
#######################################
# Syntax Coloring Map
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
FuelGauge KEYWORD1
MAX1704X KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
adc KEYWORD2
voltage KEYWORD2
percent KEYWORD2
version KEYWORD2
compensation KEYWORD2
sleep KEYWORD2
isSleeping KEYWORD2
wake KEYWORD2
reset KEYWORD2
quickstart KEYWORD2
alertIsActive KEYWORD2
clearAlert KEYWORD2
getThreshold KEYWORD2
setThreshold KEYWORD2
######################################
# Constants (LITERAL1)
#######################################
REGISTER_VCELL LITERAL1
REGISTER_SOC LITERAL1
REGISTER_MODE LITERAL1
REGISTER_VERSION LITERAL1
REGISTER_CONFIG LITERAL1
REGISTER_COMMAND LITERAL1

View File

@ -0,0 +1,9 @@
name=MAX1704X
version=1.0.0
author=Daniel Porrey <daniel.porrey@hotmail.com>
maintainer=Daniel Porrey <daniel.porrey@hotmail.com>
sentence=Arduino library for MAX17043/MAX17044 lithium ion battery fuel gauge.
paragraph=Provides a simple interface for monitoring battery charge levels. Works with any device using the Maxmim MAX17043 or MAX17044 chip such as the SparkFun LiPo Fuel Gauge.
category=Sensors
url=https://github.com/porrey/MAX1704X
architectures=*

View File

@ -0,0 +1,37 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#ifndef MAX17043_H
#define MAX17043_H
#include "MAX1704X.h"
extern MAX1704X FuelGauge;
// ***
// *** The MAX17043 is a one-cell device with a
// *** a voltage measurement range of 0 to 5 V.
// ***
MAX1704X FuelGauge = MAX1704X(5);
#endif

View File

@ -0,0 +1,37 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#ifndef MAX17044_H
#define MAX17044_H
#include "MAX1704X.h"
extern MAX1704X FuelGauge;
// ***
// *** The MAX17044 is a two-cell device with a
// *** a voltage measurement range of 0 to 10 V.
// ***
MAX1704X FuelGauge = MAX1704X(10);
#endif

View File

@ -0,0 +1,229 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#include "MAX1704X.h"
MAX1704X::MAX1704X(float maxVoltage)
{
this->_maxVoltage = maxVoltage;
}
void MAX1704X::begin()
{
Wire.begin();
}
uint16_t MAX1704X::adc()
{
// ***
// *** 12-bit ADC; remove the 4 MSB's
// ***
return (uint16_t)(this->readRegister16(REGISTER_VCELL) >> 4);
}
float MAX1704X::voltage()
{
// ***
// *** The MAX1704X is a 12-bit ADC measuring 0 to 5 volts.
// ***
return (float)(this->adc() / 4096.0 * this->_maxVoltage);
}
float MAX1704X::percent()
{
// ***
// *** Read the 16-bit register value
// ***
uint16_t value = this->readRegister16(REGISTER_SOC);
// ***
// *** The high byte is the percentage.
// ***
float percentage = (float)toHighByte(value);
// ***
// *** The low byte contains additional resolution of 1/256%.
// ***
float fraction = (float)(toLowByte(value) / 256.f);
return percentage + fraction;
}
uint16_t MAX1704X::version()
{
return this->readRegister16(REGISTER_VERSION);
}
uint8_t MAX1704X::compensation()
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
return toHighByte(value);
}
void MAX1704X::compensation(uint8_t data)
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
uint8_t lowByte = toLowByte(value);
uint16_t newValue = toUint16(data, lowByte);
this->writeRegister16(REGISTER_CONFIG, newValue);
}
bool MAX1704X::sleep()
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
setBit(value, 7);
this->writeRegister16(REGISTER_CONFIG, value);
}
bool MAX1704X::isSleeping()
{
// ***
// *** The sleep bit is bit 7 in the config byte.
// ***
uint16_t value = this->readRegister16(REGISTER_CONFIG);
return (value & 0x80);
}
bool MAX1704X::wake()
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
clearBit(value, 7);
this->writeRegister16(REGISTER_CONFIG, value);
}
void MAX1704X::reset()
{
// ***
// *** Write the command 0x5400 to the command register.
// ***
this->writeRegister16(REGISTER_COMMAND, 0x5400);
}
void MAX1704X::quickstart()
{
// ***
// *** Write the command 0x4000 to the mode register.
// ***
this->writeRegister16(REGISTER_MODE, 0x4000);
}
bool MAX1704X::alertIsActive()
{
// ***
// *** The alert bit is bit 5 in the config byte.
// ***
uint16_t value = this->readRegister16(REGISTER_CONFIG);
return (value & 0x20);
}
void MAX1704X::clearAlert()
{
// ***
// *** Get the config register value. Bit 5
// *** contains the alert value.
// ***
uint16_t value = this->readRegister16(REGISTER_CONFIG);
clearBit(value, 5);
this->writeRegister16(REGISTER_CONFIG, value);
}
uint8_t MAX1704X::getThreshold()
{
// ***
// *** Get the config register value. The last 5 bits
// *** contains the threshold in 2's complement form.
// ***
uint16_t config = this->readRegister16(REGISTER_CONFIG);
// ***
// *** Use only the last 5 bits of the config.
// ***
return configToThreshold(config & 0x001F);
}
void MAX1704X::setThreshold(uint8_t threshold)
{
// ***
// *** Get the config register value. The last 5 bits
// *** contains the threshold in 2's complement form.
// ***
uint16_t config = this->readRegister16(REGISTER_CONFIG);
// ***
// *** Set the last 5 bits to 0.
// ***
config &= 0xFFE0;
// ***
// *** The value of threshold can be between 1% and 32%.
// ***
uint8_t configThreshold = this->thresholdToConfig(threshold);
// ***
// *** Combine the threshold with the config.
// ***
config = config + configThreshold;
// ***
// *** Write the new register value.
// ***
this->writeRegister16(REGISTER_CONFIG, config);
}
uint8_t MAX1704X::thresholdToConfig(uint8_t threshold)
{
return 31 - (constrain(threshold, 1, 32) - 1);
}
uint8_t MAX1704X::configToThreshold(uint8_t config)
{
return (32 - constrain(config, 0, 31));
}
uint16_t MAX1704X::readRegister16(uint8_t registerId)
{
this->writeRegisterId(registerId);
Wire.requestFrom(I2C_ADDRESS, 2);
uint8_t highByte = Wire.read();
uint8_t lowByte = Wire.read();
uint16_t data = toUint16(highByte, lowByte);
return data;
}
void MAX1704X::writeRegisterId(uint8_t registerId)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(registerId);
Wire.endTransmission(false);
}
void MAX1704X::writeRegister16(uint8_t registerId, uint16_t data)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(registerId);
Wire.write(toHighByte(data));
Wire.write(toLowByte(data));
Wire.endTransmission(true);
}

View File

@ -0,0 +1,75 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#ifndef MAX1704X_H
#define MAX1704X_H
#include <Arduino.h>
#include <Wire.h>
#define toLowByte(w) ((uint8_t) ((w) & 0xff))
#define toHighByte(w) ((uint8_t) ((w) >> 8))
#define toUint16(highB, lowB) ((uint16_t)((uint8_t)highB << 8) + (uint8_t)lowB)
#define BV(bit) (1 <<(bit))
#define setBit(byte, bit) (byte |= BV(bit))
#define clearBit(byte, bit) (byte &= ~BV(bit))
#define toggleBit(byte, bit) (byte ^= BV(bit))
#define I2C_ADDRESS 0x36
#define REGISTER_VCELL 0x02
#define REGISTER_SOC 0x04
#define REGISTER_MODE 0x06
#define REGISTER_VERSION 0x08
#define REGISTER_CONFIG 0x0C
#define REGISTER_COMMAND 0xFE
class MAX1704X
{
public:
MAX1704X(float);
void begin();
uint16_t adc();
float voltage();
float percent();
uint16_t version();
uint8_t compensation();
void compensation(uint8_t data);
bool sleep();
bool isSleeping();
bool wake();
void reset();
void quickstart();
bool alertIsActive();
void clearAlert();
uint8_t getThreshold();
void setThreshold(uint8_t threshold);
protected:
float _maxVoltage;
uint8_t thresholdToConfig(uint8_t threshold);
uint8_t configToThreshold(uint8_t config);
uint16_t readRegister16(uint8_t registerId);
void writeRegisterId(uint8_t registerId);
void writeRegister16(uint8_t registerId, uint16_t data);
};
#endif

View File

@ -0,0 +1,37 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#ifndef MAX17043_H
#define MAX17043_H
#include "MAX1704X.h"
extern MAX1704X FuelGauge;
// ***
// *** The MAX17043 is a one-cell device with a
// *** a voltage measurement range of 0 to 5 V.
// ***
MAX1704X FuelGauge = MAX1704X(5);
#endif

View File

@ -0,0 +1,229 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#include "MAX1704X.h"
MAX1704X::MAX1704X(float maxVoltage)
{
this->_maxVoltage = maxVoltage;
}
void MAX1704X::begin()
{
Wire.begin();
}
uint16_t MAX1704X::adc()
{
// ***
// *** 12-bit ADC; remove the 4 MSB's
// ***
return (uint16_t)(this->readRegister16(REGISTER_VCELL) >> 4);
}
float MAX1704X::voltage()
{
// ***
// *** The MAX1704X is a 12-bit ADC measuring 0 to 5 volts.
// ***
return (float)(this->adc() / 4096.0 * this->_maxVoltage);
}
float MAX1704X::percent()
{
// ***
// *** Read the 16-bit register value
// ***
uint16_t value = this->readRegister16(REGISTER_SOC);
// ***
// *** The high byte is the percentage.
// ***
float percentage = (float)toHighByte(value);
// ***
// *** The low byte contains additional resolution of 1/256%.
// ***
float fraction = (float)(toLowByte(value) / 256.f);
return percentage + fraction;
}
uint16_t MAX1704X::version()
{
return this->readRegister16(REGISTER_VERSION);
}
uint8_t MAX1704X::compensation()
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
return toHighByte(value);
}
void MAX1704X::compensation(uint8_t data)
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
uint8_t lowByte = toLowByte(value);
uint16_t newValue = toUint16(data, lowByte);
this->writeRegister16(REGISTER_CONFIG, newValue);
}
bool MAX1704X::sleep()
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
setBit(value, 7);
this->writeRegister16(REGISTER_CONFIG, value);
}
bool MAX1704X::isSleeping()
{
// ***
// *** The sleep bit is bit 7 in the config byte.
// ***
uint16_t value = this->readRegister16(REGISTER_CONFIG);
return (value & 0x80);
}
bool MAX1704X::wake()
{
uint16_t value = this->readRegister16(REGISTER_CONFIG);
clearBit(value, 7);
this->writeRegister16(REGISTER_CONFIG, value);
}
void MAX1704X::reset()
{
// ***
// *** Write the command 0x5400 to the command register.
// ***
this->writeRegister16(REGISTER_COMMAND, 0x5400);
}
void MAX1704X::quickstart()
{
// ***
// *** Write the command 0x4000 to the mode register.
// ***
this->writeRegister16(REGISTER_MODE, 0x4000);
}
bool MAX1704X::alertIsActive()
{
// ***
// *** The alert bit is bit 5 in the config byte.
// ***
uint16_t value = this->readRegister16(REGISTER_CONFIG);
return (value & 0x20);
}
void MAX1704X::clearAlert()
{
// ***
// *** Get the config register value. Bit 5
// *** contains the alert value.
// ***
uint16_t value = this->readRegister16(REGISTER_CONFIG);
clearBit(value, 5);
this->writeRegister16(REGISTER_CONFIG, value);
}
uint8_t MAX1704X::getThreshold()
{
// ***
// *** Get the config register value. The last 5 bits
// *** contains the threshold in 2's complement form.
// ***
uint16_t config = this->readRegister16(REGISTER_CONFIG);
// ***
// *** Use only the last 5 bits of the config.
// ***
return configToThreshold(config & 0x001F);
}
void MAX1704X::setThreshold(uint8_t threshold)
{
// ***
// *** Get the config register value. The last 5 bits
// *** contains the threshold in 2's complement form.
// ***
uint16_t config = this->readRegister16(REGISTER_CONFIG);
// ***
// *** Set the last 5 bits to 0.
// ***
config &= 0xFFE0;
// ***
// *** The value of threshold can be between 1% and 32%.
// ***
uint8_t configThreshold = this->thresholdToConfig(threshold);
// ***
// *** Combine the threshold with the config.
// ***
config = config + configThreshold;
// ***
// *** Write the new register value.
// ***
this->writeRegister16(REGISTER_CONFIG, config);
}
uint8_t MAX1704X::thresholdToConfig(uint8_t threshold)
{
return 31 - (constrain(threshold, 1, 32) - 1);
}
uint8_t MAX1704X::configToThreshold(uint8_t config)
{
return (32 - constrain(config, 0, 31));
}
uint16_t MAX1704X::readRegister16(uint8_t registerId)
{
this->writeRegisterId(registerId);
Wire.requestFrom(I2C_ADDRESS, 2);
uint8_t highByte = Wire.read();
uint8_t lowByte = Wire.read();
uint16_t data = toUint16(highByte, lowByte);
return data;
}
void MAX1704X::writeRegisterId(uint8_t registerId)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(registerId);
Wire.endTransmission(false);
}
void MAX1704X::writeRegister16(uint8_t registerId, uint16_t data)
{
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(registerId);
Wire.write(toHighByte(data));
Wire.write(toLowByte(data));
Wire.endTransmission(true);
}

View File

@ -0,0 +1,75 @@
/*
* MAX1704X Arduino Library for MAX17043 and MAX17044 Fuel Gauge.
*
* Version 1.0.0
* Copyright © 2018 Daniel Porrey. All Rights Reserved.
* https://github.com/porrey/max1704x
*
* This file is part of the MAX1704X Arduino Library.
*
* The MAX1704X Arduino Library is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The MAX1704X Arduino Library is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the MAX1704X Arduino Library. If not,
* see http://www.gnu.org/licenses/.
*/
#ifndef MAX1704X_H
#define MAX1704X_H
#include <Arduino.h>
#include <Wire.h>
#define toLowByte(w) ((uint8_t) ((w) & 0xff))
#define toHighByte(w) ((uint8_t) ((w) >> 8))
#define toUint16(highB, lowB) ((uint16_t)((uint8_t)highB << 8) + (uint8_t)lowB)
#define BV(bit) (1 <<(bit))
#define setBit(byte, bit) (byte |= BV(bit))
#define clearBit(byte, bit) (byte &= ~BV(bit))
#define toggleBit(byte, bit) (byte ^= BV(bit))
#define I2C_ADDRESS 0x36
#define REGISTER_VCELL 0x02
#define REGISTER_SOC 0x04
#define REGISTER_MODE 0x06
#define REGISTER_VERSION 0x08
#define REGISTER_CONFIG 0x0C
#define REGISTER_COMMAND 0xFE
class MAX1704X
{
public:
MAX1704X(float);
void begin();
uint16_t adc();
float voltage();
float percent();
uint16_t version();
uint8_t compensation();
void compensation(uint8_t data);
bool sleep();
bool isSleeping();
bool wake();
void reset();
void quickstart();
bool alertIsActive();
void clearAlert();
uint8_t getThreshold();
void setThreshold(uint8_t threshold);
protected:
float _maxVoltage;
uint8_t thresholdToConfig(uint8_t threshold);
uint8_t configToThreshold(uint8_t config);
uint16_t readRegister16(uint8_t registerId);
void writeRegisterId(uint8_t registerId);
void writeRegister16(uint8_t registerId, uint16_t data);
};
#endif

View File

@ -0,0 +1,11 @@
static const unsigned char battery_0[513] PROGMEM = {
// 'battery_0', 32x32px
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff,
0xff, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff
};

View File

@ -0,0 +1,11 @@
static const unsigned char battery_100[513] PROGMEM = {
// 'battery_100', 32x32px
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xff, 0xf0, 0x1f, 0xff,
0xfe, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xfc, 0x7f, 0xfc, 0xff, 0xfc, 0x7f, 0xfc, 0xff,
0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x7f, 0xfc, 0xff,
0xfc, 0x7f, 0xfc, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff,
0xfc, 0x40, 0x04, 0xff, 0xfc, 0x7f, 0xfc, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff,
0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x7f, 0xfc, 0xff, 0xfc, 0x40, 0x0c, 0xff,
0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x40, 0x04, 0xff, 0xfc, 0x7f, 0xfc, 0xff,
0xfc, 0x7f, 0xfc, 0xff, 0xfc, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff
};

View File

@ -0,0 +1,11 @@
static const unsigned char battery_25[513] PROGMEM = {
// 'battery_25', 32x32px
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff,
0xff, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff
};

View File

@ -0,0 +1,11 @@
static const unsigned char battery_50[513] PROGMEM = {
// 'battery_50', 32x32px
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff,
0xff, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f,
0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x60, 0x06, 0x7f,
0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff
};

View File

@ -0,0 +1,11 @@
static const unsigned char battery_75[513] PROGMEM = {
// 'battery_75', 32x32px
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xf8, 0x1f, 0xff,
0xff, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f,
0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f,
0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x60, 0x06, 0x7f,
0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x60, 0x06, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff
};

View File

@ -6,6 +6,12 @@
#include "humidity.h" // humidity
#include "temp_humidity.h" // temp_humidity
#include "temperature.h" // temperature
#include "battery_0.h" // 0% battery
#include "battery_25.h" // 25% battery
#include "battery_50.h" // 50% battery
#include "battery_75.h" // 75% battery
#include "battery_100.h" // 100% battery
#include "warning.h" // warning (filled in)
// Adafruit temp/humidity sensor
// We are using the SHT10
@ -36,6 +42,10 @@ SHT1x th_sensor(dataPin, sckPin);
//GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT_3C(GxEPD2_290c)> display(GxEPD2_290c(/*CS=77*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS*/ 27, /*DC=*/ 15, /*RST=*/ 14, /*BUSY=*/ 21));
// Fuel Gauge
#include "MAX17043.h"
float batt_voltage;
float batt_percent;
// Delay cheats
const unsigned long SECOND = 1000;
@ -47,6 +57,9 @@ float humid;
void setup()
{
// Initialize Fuel Gauge
FuelGauge.begin();
// Initialize display
display.init(115200);
display.setFullWindow();
@ -64,6 +77,65 @@ void loop()
display.firstPage();
display.setCursor(0, 0);
// Wake Fuel Gauge
if (FuelGauge.isSleeping()) {
FuelGauge.wake();
}
// Battery Level
display.println();
batt_voltage = FuelGauge.voltage();
batt_percent = FuelGauge.percent();
if (batt_percent > 95) {
display.drawInvertedBitmap(0, 0, battery_100, 32, 32, GxEPD_BLACK);
}
else if (batt_percent > 75) {
display.drawInvertedBitmap(0, 0, battery_75, 32, 32, GxEPD_BLACK);
}
else if (batt_percent > 50) {
display.drawInvertedBitmap(0, 0, battery_50, 32, 32, GxEPD_BLACK);
}
else if (batt_percent > 25) {
display.drawInvertedBitmap(0, 0, battery_25, 32, 32, GxEPD_RED);
}
else {
display.drawInvertedBitmap(0, 0, battery_0, 32, 32, GxEPD_RED);
}
display.print(" ");
display.print(batt_percent);
display.print("% / ");
display.print(batt_voltage);
display.println("V");
// Show battery alert if active
if (FuelGauge.alertIsActive()) {
display.drawInvertedBitmap(265, 0, warning, 32, 32, GxEPD_RED);
}
// Sleep Fuel Gauge to save power
FuelGauge.sleep();
// Read values from the sensor
humid = th_sensor.readHumidity();
// Since the humidity reading requires the temperature we simply
// retrieve the reading capture from the readHumidity() call. See the lib.
temp_c = th_sensor.retrieveTemperatureC();
display.drawInvertedBitmap(0, 35, temperature, 32, 32, GxEPD_BLACK);
display.println();
display.print(" ");
display.print(temp_c * 9 / 5 + 32);
display.print("F");
display.print(" / ");
display.print(temp_c);
display.println("C");
display.drawInvertedBitmap(0, 67, humidity, 32, 32, GxEPD_BLACK);
display.println();
display.print(" ");
display.print(humid);
display.println("%");
// Draw icons for plant status
display.drawInvertedBitmap(0, 96, cat_grass, 32, 32, GxEPD_BLACK);
display.drawInvertedBitmap(37, 96, catnip, 32, 32, GxEPD_BLACK);
@ -74,27 +146,6 @@ void loop()
display.drawInvertedBitmap(222, 96, tarragon, 32, 32, GxEPD_RED);
display.drawInvertedBitmap(259, 96, christmas_cactus, 32, 32, GxEPD_RED);
// Read values from the sensor
humid = th_sensor.readHumidity();
// Since the humidity reading requires the temperature we simply
// retrieve the reading capture from the readHumidity() call. See the lib.
temp_c = th_sensor.retrieveTemperatureC();
display.drawInvertedBitmap(0, 0, temperature, 32, 32, GxEPD_BLACK);
display.println();
display.print(" ");
display.print(temp_c * 9/5 + 32);
display.print("F");
display.print(" / ");
display.print(temp_c);
display.println("C");
display.drawInvertedBitmap(0, 35, humidity, 32, 32, GxEPD_BLACK);
display.println();
display.print(" ");
display.print(humid);
display.println("%");
// Flush display output
while (display.nextPage());

View File

@ -0,0 +1,11 @@
static const unsigned char warning[513] PROGMEM = {
// 'warning_full', 32x32px
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0x80, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x7f,
0xfc, 0x00, 0x00, 0x3f, 0xf8, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x0f, 0xe0, 0x01, 0x80, 0x07,
0xe0, 0x03, 0xc0, 0x07, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0x80, 0x03, 0xc0, 0x01,
0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01,
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, 0x01,
0x80, 0x00, 0x00, 0x01, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xc0, 0x03, 0xe0, 0x03, 0xc0, 0x07,
0xe0, 0x03, 0xc0, 0x07, 0xf0, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x1f, 0xfc, 0x00, 0x00, 0x3f,
0xfe, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xfe, 0x7f, 0xff
};