delete old files

rewrite_for_hd774
Eggert Jung 5 years ago
parent 76ced85569
commit 1fc729f8ce

65
adc.c

@ -1,65 +0,0 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "adc.h"
#include "buffer.h"
volatile buffer_t adc_buf[4];
volatile float adc_avrg[4];
void initADC()
{
ADMUX = 1 << REFS0 | 0 << REFS1; //Select external Vref
//ADC Status Register A
ADCSRA = 1 << ADEN //Enable ADC
| 1 << ADIE //Enable ISR after conversion complete
//| 1<<ADATE //Freerunning-Mode
//| 1<<ADLAR //2 results bits are left aligned
| 1 << ADPS2 //Set clock-prescaler to 128
| 1 << ADPS1 | 1 << ADPS0;
ADCSRA |= 1 << ADSC; //Start first Conversion for "warmup"
}
void set_ADC_Channel(uint8_t adr)
{
if (adr < 11)
{
ADMUX &= (0b11110000); //Clear MUX-Address
ADMUX |= adr; //Set new MUX-Address
}
}
ISR(ADC_vect)
{
static uint8_t init[4] = {0,0,0,0};
static uint8_t current_channel = 0;
//TODO write non broken adc code
//Reading 10bit conversion result
uint16_t ADC_reading = ADCL; //copy the first LSB bits
ADC_reading |= ADCH << 8; //copy remaing byte
ADC_reading *= 0.33;
insert_to_buffer(ADC_reading, &adc_buf[current_channel]);
//if(adc_buf[current_channel].position == BUFFER_SIZE-1){
// //if(init[current_channel]){
// //float tmp = (99*adc_avrg[current_channel]) + get_buffer_mean(&adc_buf[current_channel]);
// //tmp /= 100;
// adc_avrg[current_channel] = get_buffer_mean(&adc_buf[current_channel]);
// //}
// //else{
// // adc_avrg[current_channel] = get_buffer_mean(&adc_buf[current_channel]);
// // init[current_channel]=0;
// //}
//}
current_channel++;
if(current_channel == 4)
current_channel = 0;
set_ADC_Channel(current_channel);
ADCSRA |= (1 << ADSC); //Start next conversion
}

12
adc.h

@ -1,12 +0,0 @@
#ifndef _ADC_H_
#define _ADC_H_
#include "buffer.h"
extern volatile buffer_t adc_buf[4];
extern volatile float adc_avrg[4];
void initADC(void);
#endif//_ADC_H_

@ -1,42 +0,0 @@
#include <util/atomic.h>
#include "buffer.h"
void insert_to_buffer(uint16_t val, volatile buffer_t* buf){
ATOMIC_BLOCK(ATOMIC_FORCEON){
buf->position++;
if(buf->position == BUFFER_SIZE)
buf->position = 0;
buf->values[buf->position] = val;
}
}
float get_buffer_mean(volatile buffer_t* buf){
///* discard lowest and highest value */
//uint16_t low=0xFFFF;
//uint16_t high=0;
//uint16_t index_l = 0;
//uint16_t index_h = 0;
//for(uint16_t i=0; i<BUFFER_SIZE; i++){
// if(buf->values[i] < low){
// low=buf->values[i];
// index_l = i;
// }
// if(buf->values[i] > high){
// high=buf->values[i];
// index_h = i;
// }
//}
uint32_t sum = 0;
for(uint16_t i=0; i<BUFFER_SIZE; i++){
//if(i == index_h || i == index_l)
// continue;
sum += buf->values[i];
}
uint16_t res = sum/(BUFFER_SIZE/*-2*/);
return res;
}

@ -1,16 +0,0 @@
#ifndef _BUFFER_H_
#define _BUFFER_H_
#include <stdint.h>
#define BUFFER_SIZE 200
typedef struct {
uint16_t values[BUFFER_SIZE];
uint16_t position;
} buffer_t;
void insert_to_buffer(uint16_t val, volatile buffer_t* buf);
float get_buffer_mean(volatile buffer_t* buf);
#endif//_BUFFER_H_

34
i2c.c

@ -1,9 +1,9 @@
/*
* i2c.c
*
* Created: 20.01.2018 12:50:54
* Author: Ulrich
*/
/*
* i2c.c
*
* Created: 20.01.2018 12:50:54
* Author: Ulrich
*/
#include "i2c.h"
@ -41,17 +41,17 @@ uint8_t i2c_byte (uint8_t byte){
if((timeout++) > 1000) return 1;
}
return 0;
}
//***************************************************************************************
uint8_t i2c_readNak(void)
{
uint16_t timeout = 0;
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT))){
if((timeout++) > 1000) return 0;
}
return TWDR;
}
//***************************************************************************************
uint8_t i2c_readNak(void)
{
uint16_t timeout = 0;
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT))){
if((timeout++) > 1000) return 0;
}
return TWDR;
}
//***************************************************************************************

42
i2c.h

@ -1,26 +1,26 @@
/*
* i2c.h
*
* Created: 20.01.2018 12:51:11
* Author: Ulrich
*/
#ifndef I2C_H_
#define I2C_H_
#include <avr/io.h>
#include <util/delay.h>
/*
* i2c.h
*
* Created: 20.01.2018 12:51:11
* Author: Ulrich
*/
#ifndef I2C_H_
#define I2C_H_
#include <avr/io.h>
#include <util/delay.h>
//I2C
#define SDA_PIN PC4 //PIN am Atmel
#define SCL_PIN PC5 //PIN am Atmel
#define I2C_PORT PORTC //PORT am Atmel
#define SCL_CLOCK 100000UL
void i2c_init(void);
uint8_t i2c_start (uint8_t);
uint8_t i2c_byte (uint8_t);
uint8_t i2c_readNak(void);
void i2c_stop (void);
#define SCL_CLOCK 100000UL
void i2c_init(void);
uint8_t i2c_start (uint8_t);
uint8_t i2c_byte (uint8_t);
uint8_t i2c_readNak(void);
void i2c_stop (void);
#endif /* I2C_H_ */

@ -1,178 +0,0 @@
#ifndef _I2CMASTER_H
#define _I2CMASTER_H
/*************************************************************************
* Title: C include file for the I2C master interface
* (i2cmaster.S or twimaster.c)
* Author: Peter Fleury <pfleury@gmx.ch>
* File: $Id: i2cmaster.h,v 1.12 2015/09/16 09:27:58 peter Exp $
* Software: AVR-GCC 4.x
* Target: any AVR device
* Usage: see Doxygen manual
**************************************************************************/
/**
@file
@defgroup pfleury_ic2master I2C Master library
@code #include <i2cmaster.h> @endcode
@brief I2C (TWI) Master Software Library
Basic routines for communicating with I2C slave devices. This single master
implementation is limited to one bus master on the I2C bus.
This I2c library is implemented as a compact assembler software implementation of the I2C protocol
which runs on any AVR (i2cmaster.S) and as a TWI hardware interface for all AVR with built-in TWI hardware (twimaster.c).
Since the API for these two implementations is exactly the same, an application can be linked either against the
software I2C implementation or the hardware I2C implementation.
Use 4.7k pull-up resistor on the SDA and SCL pin.
Adapt the SCL and SDA port and pin definitions and eventually the delay routine in the module
i2cmaster.S to your target when using the software I2C implementation !
Adjust the CPU clock frequence F_CPU in twimaster.c or in the Makfile when using the TWI hardware implementaion.
@note
The module i2cmaster.S is based on the Atmel Application Note AVR300, corrected and adapted
to GNU assembler and AVR-GCC C call interface.
Replaced the incorrect quarter period delays found in AVR300 with
half period delays.
@author Peter Fleury pfleury@gmx.ch http://tinyurl.com/peterfleury
@copyright (C) 2015 Peter Fleury, GNU General Public License Version 3
@par API Usage Example
The following code shows typical usage of this library, see example test_i2cmaster.c
@code
#include <i2cmaster.h>
#define Dev24C02 0xA2 // device address of EEPROM 24C02, see datasheet
int main(void)
{
unsigned char ret;
i2c_init(); // initialize I2C library
// write 0x75 to EEPROM address 5 (Byte Write)
i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
i2c_write(0x05); // write address = 5
i2c_write(0x75); // write value 0x75 to EEPROM
i2c_stop(); // set stop conditon = release bus
// read previously written value back from EEPROM address 5
i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
i2c_write(0x05); // write address = 5
i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode
ret = i2c_readNak(); // read one byte from EEPROM
i2c_stop();
for(;;);
}
@endcode
*/
/**@{*/
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
#endif
#include <avr/io.h>
/** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */
#define I2C_READ 1
/** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */
#define I2C_WRITE 0
/**
@brief initialize the I2C master interace. Need to be called only once
@return none
*/
extern void i2c_init(void);
/**
@brief Terminates the data transfer and releases the I2C bus
@return none
*/
extern void i2c_stop(void);
/**
@brief Issues a start condition and sends address and transfer direction
@param addr address and transfer direction of I2C device
@retval 0 device accessible
@retval 1 failed to access device
*/
extern unsigned char i2c_start(unsigned char addr);
/**
@brief Issues a repeated start condition and sends address and transfer direction
@param addr address and transfer direction of I2C device
@retval 0 device accessible
@retval 1 failed to access device
*/
extern unsigned char i2c_rep_start(unsigned char addr);
/**
@brief Issues a start condition and sends address and transfer direction
If device is busy, use ack polling to wait until device ready
@param addr address and transfer direction of I2C device
@return none
*/
extern void i2c_start_wait(unsigned char addr);
/**
@brief Send one byte to I2C device
@param data byte to be transfered
@retval 0 write successful
@retval 1 write failed
*/
extern unsigned char i2c_write(unsigned char data);
/**
@brief read one byte from the I2C device, request more data from device
@return byte read from I2C device
*/
extern unsigned char i2c_readAck(void);
/**
@brief read one byte from the I2C device, read is followed by a stop condition
@return byte read from I2C device
*/
extern unsigned char i2c_readNak(void);
/**
@brief read one byte from the I2C device
Implemented as a macro, which calls either @ref i2c_readAck or @ref i2c_readNak
@param ack 1 send ack, request more data from device<br>
0 send nak, read is followed by a stop condition
@return byte read from I2C device
*/
extern unsigned char i2c_read(unsigned char ack);
#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
/**@}*/
#endif

162
lcd.c

@ -1,8 +1,8 @@
/*
* lcd.c
*
* Created: 20.01.2018 12:51:11
* Author: Ulrich
/*
* lcd.c
*
* Created: 20.01.2018 12:51:11
* Author: Ulrich
*/
#include "lcd.h"
@ -108,80 +108,80 @@ void lcd_print_str(char *str) {
}
}
//***************************************************************************************
void lcd_write_P (const char *Buffer,...)
{
va_list ap;
va_start (ap, Buffer);
int format_flag;
char str_buffer[10];
char str_null_buffer[10];
char move = 0;
char Base = 0;
int tmp = 0;
char by;
char *ptr;
//Ausgabe der Zeichen
for(;;){
by = pgm_read_byte(Buffer++);
if(by==0) break; // end of format string
if (by == '%'){
by = pgm_read_byte(Buffer++);
if (isdigit(by)>0){
str_null_buffer[0] = by;
str_null_buffer[1] = '\0';
move = atoi(str_null_buffer);
by = pgm_read_byte(Buffer++);
}
switch (by){
case 's':
ptr = va_arg(ap,char *);
while(*ptr) { lcd_write_char(*ptr++); }
break;
case 'b':
Base = 2;
goto ConversionLoop;
case 'c':
//Int to char
format_flag = va_arg(ap,int);
lcd_write_char (format_flag++);
break;
case 'i':
Base = 10;
goto ConversionLoop;
case 'o':
Base = 8;
goto ConversionLoop;
case 'x':
Base = 16;
//****************************
ConversionLoop:
//****************************
itoa(va_arg(ap,int),str_buffer,Base);
int b=0;
while (str_buffer[b++] != 0){};
b--;
if (b<move){
move -=b;
for (tmp = 0;tmp<move;tmp++){
str_null_buffer[tmp] = '0';
}
//tmp ++;
str_null_buffer[tmp] = '\0';
strcat(str_null_buffer,str_buffer);
strcpy(str_buffer,str_null_buffer);
}
lcd_print_str (str_buffer);
move =0;
break;
}
}else{
lcd_write_char (by);
}
}
va_end(ap);
//***************************************************************************************
void lcd_write_P (const char *Buffer,...)
{
va_list ap;
va_start (ap, Buffer);
int format_flag;
char str_buffer[10];
char str_null_buffer[10];
char move = 0;
char Base = 0;
int tmp = 0;
char by;
char *ptr;
//Ausgabe der Zeichen
for(;;){
by = pgm_read_byte(Buffer++);
if(by==0) break; // end of format string
if (by == '%'){
by = pgm_read_byte(Buffer++);
if (isdigit(by)>0){
str_null_buffer[0] = by;
str_null_buffer[1] = '\0';
move = atoi(str_null_buffer);
by = pgm_read_byte(Buffer++);
}
switch (by){
case 's':
ptr = va_arg(ap,char *);
while(*ptr) { lcd_write_char(*ptr++); }
break;
case 'b':
Base = 2;
goto ConversionLoop;
case 'c':
//Int to char
format_flag = va_arg(ap,int);
lcd_write_char (format_flag++);
break;
case 'i':
Base = 10;
goto ConversionLoop;
case 'o':
Base = 8;
goto ConversionLoop;
case 'x':
Base = 16;
//****************************
ConversionLoop:
//****************************
itoa(va_arg(ap,int),str_buffer,Base);
int b=0;
while (str_buffer[b++] != 0){};
b--;
if (b<move){
move -=b;
for (tmp = 0;tmp<move;tmp++){
str_null_buffer[tmp] = '0';
}
//tmp ++;
str_null_buffer[tmp] = '\0';
strcat(str_null_buffer,str_buffer);
strcpy(str_buffer,str_null_buffer);
}
lcd_print_str (str_buffer);
move =0;
break;
}
}else{
lcd_write_char (by);
}
}
va_end(ap);
}

14
lcd.h

@ -1,8 +1,8 @@
/*
* lcd.h
*
* Created: 20.01.2018 12:51:11
* Author: Ulrich
/*
* lcd.h
*
* Created: 20.01.2018 12:51:11
* Author: Ulrich
*/
#ifndef __LCD_H__
@ -11,8 +11,8 @@
#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <util/delay.h>

@ -1,479 +0,0 @@
/*
lcdpcf8574 lib 0x03
copyright (c) Davide Gironi, 2013
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "pcf8574.h"
#include "lcdpcf8574.h"
#define lcd_e_delay() __asm__ __volatile__( "rjmp 1f\n 1:" );
#define lcd_e_toggle() toggle_e()
#if LCD_LINES==1
#define LCD_FUNCTION_DEFAULT LCD_FUNCTION_4BIT_1LINE
#else
#define LCD_FUNCTION_DEFAULT LCD_FUNCTION_4BIT_2LINES
#endif
volatile uint8_t dataport = 0;
/*
** function prototypes
*/
static void toggle_e(void);
/*
** local functions
*/
/*************************************************************************
delay loop for small accurate delays: 16-bit counter, 4 cycles/loop
*************************************************************************/
static inline void _delayFourCycles(unsigned int __count)
{
if ( __count == 0 )
__asm__ __volatile__( "rjmp 1f\n 1:" ); // 2 cycles
else
__asm__ __volatile__ (
"1: sbiw %0,1" "\n\t"
"brne 1b" // 4 cycles/loop
: "=w" (__count)
: "0" (__count)
);
}
/*************************************************************************
delay for a minimum of <us> microseconds
the number of loops is calculated at compile-time from MCU clock frequency
*************************************************************************/
#define delay(us) _delayFourCycles( ( ( 1*(F_CPU/4000) )*us)/1000 )
/* toggle Enable Pin to initiate write */
static void toggle_e(void)
{
pcf8574_setoutputpinhigh(LCD_PCF8574_DEVICEID, LCD_E_PIN);
lcd_e_delay();
pcf8574_setoutputpinlow(LCD_PCF8574_DEVICEID, LCD_E_PIN);
}
/*************************************************************************
Low-level function to write byte to LCD controller
Input: data byte to write to LCD
rs 1: write data
0: write instruction
Returns: none
*************************************************************************/
static void lcd_write(uint8_t data,uint8_t rs)
{
if (rs) /* write data (RS=1, RW=0) */
dataport |= _BV(LCD_RS_PIN);
else /* write instruction (RS=0, RW=0) */
dataport &= ~_BV(LCD_RS_PIN);
dataport &= ~_BV(LCD_RW_PIN);
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
/* output high nibble first */
dataport &= ~_BV(LCD_DATA3_PIN);
dataport &= ~_BV(LCD_DATA2_PIN);
dataport &= ~_BV(LCD_DATA1_PIN);
dataport &= ~_BV(LCD_DATA0_PIN);
if(data & 0x80) dataport |= _BV(LCD_DATA3_PIN);
if(data & 0x40) dataport |= _BV(LCD_DATA2_PIN);
if(data & 0x20) dataport |= _BV(LCD_DATA1_PIN);
if(data & 0x10) dataport |= _BV(LCD_DATA0_PIN);
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
lcd_e_toggle();
/* output low nibble */
dataport &= ~_BV(LCD_DATA3_PIN);
dataport &= ~_BV(LCD_DATA2_PIN);
dataport &= ~_BV(LCD_DATA1_PIN);
dataport &= ~_BV(LCD_DATA0_PIN);
if(data & 0x08) dataport |= _BV(LCD_DATA3_PIN);
if(data & 0x04) dataport |= _BV(LCD_DATA2_PIN);
if(data & 0x02) dataport |= _BV(LCD_DATA1_PIN);
if(data & 0x01) dataport |= _BV(LCD_DATA0_PIN);
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
lcd_e_toggle();
/* all data pins high (inactive) */
dataport |= _BV(LCD_DATA0_PIN);
dataport |= _BV(LCD_DATA1_PIN);
dataport |= _BV(LCD_DATA2_PIN);
dataport |= _BV(LCD_DATA3_PIN);
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
}
/*************************************************************************
Low-level function to read byte from LCD controller
Input: rs 1: read data
0: read busy flag / address counter
Returns: byte read from LCD controller
*************************************************************************/
static uint8_t lcd_read(uint8_t rs)
{
uint8_t data;
if (rs) /* write data (RS=1, RW=0) */
dataport |= _BV(LCD_RS_PIN);
else /* write instruction (RS=0, RW=0) */
dataport &= ~_BV(LCD_RS_PIN);
dataport |= _BV(LCD_RW_PIN);
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
/* read high nibble first */
pcf8574_setoutputpinhigh(LCD_PCF8574_DEVICEID, LCD_E_PIN);
lcd_e_delay();
data = 0;
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA0_PIN)) data |= 0x10;
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA1_PIN)) data |= 0x20;
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA2_PIN)) data |= 0x40;
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA3_PIN)) data |= 0x80;
pcf8574_setoutputpinlow(LCD_PCF8574_DEVICEID, LCD_E_PIN);
/* Enable 500ns low */
lcd_e_delay();
/* read low nibble */
pcf8574_setoutputpinhigh(LCD_PCF8574_DEVICEID, LCD_E_PIN);
lcd_e_delay();
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA0_PIN)) data |= 0x01;
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA1_PIN)) data |= 0x02;
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA2_PIN)) data |= 0x04;
if(!pcf8574_getinputpin(LCD_PCF8574_DEVICEID, LCD_DATA3_PIN)) data |= 0x08;
pcf8574_setoutputpinlow(LCD_PCF8574_DEVICEID, LCD_E_PIN);
return data;
}
/*************************************************************************
loops while lcd is busy, returns address counter
*************************************************************************/
static uint8_t lcd_waitbusy(void)
{
register uint8_t c;
/* wait until busy flag is cleared */
while ( (c=lcd_read(0)) & (1<<LCD_BUSY)) {}
/* the address counter is updated 4us after the busy flag is cleared */
delay(2);
/* now read the address counter */
return (lcd_read(0)); // return address counter
}/* lcd_waitbusy */
/*************************************************************************
Move cursor to the start of next line or to the first line if the cursor
is already on the last line.
*************************************************************************/
static inline void lcd_newline(uint8_t pos)
{
register uint8_t addressCounter;
#if LCD_LINES==1
addressCounter = 0;
#endif
#if LCD_LINES==2
if ( pos < (LCD_START_LINE2) )
addressCounter = LCD_START_LINE2;
else
addressCounter = LCD_START_LINE1;
#endif
#if LCD_LINES==4
if ( pos < LCD_START_LINE3 )
addressCounter = LCD_START_LINE2;
else if ( (pos >= LCD_START_LINE2) && (pos < LCD_START_LINE4) )
addressCounter = LCD_START_LINE3;
else if ( (pos >= LCD_START_LINE3) && (pos < LCD_START_LINE2) )
addressCounter = LCD_START_LINE4;
else
addressCounter = LCD_START_LINE1;
#endif
lcd_command((1<<LCD_DDRAM)+addressCounter);
}/* lcd_newline */
/*
** PUBLIC FUNCTIONS
*/
/*************************************************************************
Send LCD controller instruction command
Input: instruction to send to LCD controller, see HD44780 data sheet
Returns: none
*************************************************************************/
void lcd_command(uint8_t cmd)
{
lcd_waitbusy();
lcd_write(cmd,0);
}
/*************************************************************************
Send data byte to LCD controller
Input: data to send to LCD controller, see HD44780 data sheet
Returns: none
*************************************************************************/
void lcd_data(uint8_t data)
{
lcd_waitbusy();
lcd_write(data,1);
}
/*************************************************************************
Clear CGRAM
*************************************************************************/
void lcd_clear_CGRAM() {
register uint8_t addressCounter = 0;
for (; addressCounter < 64; addressCounter++) {
lcd_command((1 << LCD_CGRAM) + addressCounter);
lcd_putc(0x00);
}
}
/*************************************************************************
Define custom character in CGRAM
Inputs: charnum: Character position in CGRAM. You can define maximum 8 chars.
values[]: Custom character descriptor pointer.
Returns: 0 - The custom character successfully created
1 - If the charnum greater than 7. You can define maximum 8 chars.
*************************************************************************/
uint8_t lcd_create_custom_char(uint8_t charnum, const uint8_t * values) {
register uint8_t j = 0;
const uint8_t *p = values;
if (charnum > 7) {
return 1;
}
lcd_command((1 << LCD_CGRAM) + charnum * 8); // set CGRAM address charnum * 8 byte
for (; j < 8; j++) {
lcd_putc (*(p + j)); // write 8 byte data (one character) to CGRAM
}
return (0);
}
/*************************************************************************
Set cursor to specified position
Input: x horizontal position (0: left most position)
y vertical position (0: first line)
Returns: none
*************************************************************************/
void lcd_gotoxy(uint8_t x, uint8_t y)
{
#if LCD_LINES==1
lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);
#endif
#if LCD_LINES==2
if ( y==0 )
lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);
else
lcd_command((1<<LCD_DDRAM)+LCD_START_LINE2+x);
#endif
#if LCD_LINES==4
if ( y==0 )
lcd_command((1<<LCD_DDRAM)+LCD_START_LINE1+x);
else if ( y==1)
lcd_command((1<<LCD_DDRAM)+LCD_START_LINE2+x);
else if ( y==2)
lcd_command((1<<LCD_DDRAM)+LCD_START_LINE3+x);
else /* y==3 */
lcd_command((1<<LCD_DDRAM)+LCD_START_LINE4+x);
#endif
}/* lcd_gotoxy */
/*************************************************************************
*************************************************************************/
int lcd_getxy(void)
{
return lcd_waitbusy();
}
/*************************************************************************
Clear display and set cursor to home position
*************************************************************************/
void lcd_clrscr(void)
{
lcd_command(1<<LCD_CLR);
}
/*************************************************************************
Set illumination pin
*************************************************************************/
void lcd_led(uint8_t onoff)
{
if(onoff)
dataport &= ~_BV(LCD_LED_PIN);
else
dataport |= _BV(LCD_LED_PIN);
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
}
/*************************************************************************
Set cursor to home position
*************************************************************************/
void lcd_home(void)
{
lcd_command(1<<LCD_HOME);
}
/*************************************************************************
Display character at current cursor position
Input: character to be displayed
Returns: none
*************************************************************************/
void lcd_putc(char c)
{
uint8_t pos;
pos = lcd_waitbusy(); // read busy-flag and address counter
if (c=='\n')
{
lcd_newline(pos);
}
else
{
#if LCD_WRAP_LINES==1
#if LCD_LINES==1
if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {
lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
}
#elif LCD_LINES==2
if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {
lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0);
}else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH ){
lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
}
#elif LCD_LINES==4
if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH ) {
lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0);
}else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH ) {
lcd_write((1<<LCD_DDRAM)+LCD_START_LINE3,0);
}else if ( pos == LCD_START_LINE3+LCD_DISP_LENGTH ) {
lcd_write((1<<LCD_DDRAM)+LCD_START_LINE4,0);
}else if ( pos == LCD_START_LINE4+LCD_DISP_LENGTH ) {
lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
}
#endif
lcd_waitbusy();
#endif
lcd_write(c, 1);
}
}/* lcd_putc */
/*************************************************************************
Display string without auto linefeed
Input: string to be displayed
Returns: none
*************************************************************************/
void lcd_puts(const char *s)
/* print string on lcd (no auto linefeed) */
{
register char c;
while ( (c = *s++) ) {
lcd_putc(c);
}
}/* lcd_puts */
/*************************************************************************
Display string from program memory without auto linefeed
Input: string from program memory be be displayed
Returns: none
*************************************************************************/
void lcd_puts_p(const char *progmem_s)
/* print string from program memory on lcd (no auto linefeed) */
{
register char c;
while ( (c = pgm_read_byte(progmem_s++)) ) {
lcd_putc(c);
}
}/* lcd_puts_p */
/*************************************************************************
Initialize display and select type of cursor
Input: dispAttr LCD_DISP_OFF display off
LCD_DISP_ON display on, cursor off
LCD_DISP_ON_CURSOR display on, cursor on
LCD_DISP_CURSOR_BLINK display on, cursor on flashing
Returns: none
*************************************************************************/
void lcd_init(uint8_t dispAttr)
{
#if LCD_PCF8574_INIT == 1
//init pcf8574
pcf8574_init();
#endif
dataport = 0;
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
delay(16000); /* wait 16ms or more after power-on */
/* initial write to lcd is 8bit */
dataport |= _BV(LCD_DATA1_PIN); // _BV(LCD_FUNCTION)>>4;
dataport |= _BV(LCD_DATA0_PIN); // _BV(LCD_FUNCTION_8BIT)>>4;
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
lcd_e_toggle();
delay(4992); /* delay, busy flag can't be checked here */
/* repeat last command */
lcd_e_toggle();
delay(64); /* delay, busy flag can't be checked here */
/* repeat last command a third time */
lcd_e_toggle();
delay(64); /* delay, busy flag can't be checked here */
/* now configure for 4bit mode */
dataport &= ~_BV(LCD_DATA0_PIN);
pcf8574_setoutput(LCD_PCF8574_DEVICEID, dataport);
lcd_e_toggle();
delay(64); /* some displays need this additional delay */
/* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */
lcd_command(LCD_FUNCTION_DEFAULT); /* function set: display lines */
lcd_command(LCD_DISP_OFF); /* display off */
lcd_clrscr(); /* display clear */
lcd_command(LCD_MODE_DEFAULT); /* set entry mode */
lcd_command(dispAttr); /* display/cursor control */
}/* lcd_init */

@ -1,223 +0,0 @@
/*
lcdpcf8574 lib 0x03
copyright (c) Davide Gironi, 2013
Released under GPLv3.
Please refer to LICENSE file for licensing information.
References:
+ based on lcd library by Peter Fleury
http://jump.to/fleury
+ CGRAM functions by Péter Papp
*/
#ifndef LCD_H
#define LCD_H
#include <inttypes.h>
#include <avr/pgmspace.h>
#define LCD_PCF8574_INIT 1 //init pcf8574
#define LCD_PCF8574_DEVICEID 0 //device id, addr = pcf8574 base addr + LCD_PCF8574_DEVICEID
/**
* @name Definitions for Display Size
* Change these definitions to adapt setting to your display
*/
#define LCD_LINES 2 /**< number of visible lines of the display */
#define LCD_DISP_LENGTH 16 /**< visibles characters per line of the display */
#define LCD_LINE_LENGTH 0x40 /**< internal line length of the display */
#define LCD_START_LINE1 0x00 /**< DDRAM address of first char of line 1 */
#define LCD_START_LINE2 0x40 /**< DDRAM address of first char of line 2 */
#define LCD_START_LINE3 0x14 /**< DDRAM address of first char of line 3 */
#define LCD_START_LINE4 0x54 /**< DDRAM address of first char of line 4 */
#define LCD_WRAP_LINES 1 /**< 0: no wrap, 1: wrap at end of visibile line */
#define LCD_DATA0_PIN 4 /**< pin for 4bit data bit 0 */
#define LCD_DATA1_PIN 5 /**< pin for 4bit data bit 1 */
#define LCD_DATA2_PIN 6 /**< pin for 4bit data bit 2 */
#define LCD_DATA3_PIN 7 /**< pin for 4bit data bit 3 */
#define LCD_RS_PIN 0 /**< pin for RS line */
#define LCD_RW_PIN 1 /**< pin for RW line */
#define LCD_E_PIN 2 /**< pin for Enable line */
#define LCD_LED_PIN 3 /**< pin for Led */
/**
* @name Definitions for LCD command instructions
* The constants define the various LCD controller instructions which can be passed to the
* function lcd_command(), see HD44780 data sheet for a complete description.
*/
/* instruction register bit positions, see HD44780U data sheet */
#define LCD_CLR 0 /* DB0: clear display */
#define LCD_HOME 1 /* DB1: return to home position */
#define LCD_ENTRY_MODE 2 /* DB2: set entry mode */
#define LCD_ENTRY_INC 1 /* DB1: 1=increment, 0=decrement */
#define LCD_ENTRY_SHIFT 0 /* DB2: 1=display shift on */
#define LCD_ON 3 /* DB3: turn lcd/cursor on */
#define LCD_ON_DISPLAY 2 /* DB2: turn display on */
#define LCD_ON_CURSOR 1 /* DB1: turn cursor on */
#define LCD_ON_BLINK 0 /* DB0: blinking cursor ? */
#define LCD_MOVE 4 /* DB4: move cursor/display */
#define LCD_MOVE_DISP 3 /* DB3: move display (0-> cursor) ? */
#define LCD_MOVE_RIGHT 2 /* DB2: move right (0-> left) ? */
#define LCD_FUNCTION 5 /* DB5: function set */
#define LCD_FUNCTION_8BIT 4 /* DB4: set 8BIT mode (0->4BIT mode) */
#define LCD_FUNCTION_2LINES 3 /* DB3: two lines (0->one line) */
#define LCD_FUNCTION_10DOTS 2 /* DB2: 5x10 font (0->5x7 font) */
#define LCD_CGRAM 6 /* DB6: set CG RAM address */
#define LCD_DDRAM 7 /* DB7: set DD RAM address */
#define LCD_BUSY 7 /* DB7: LCD is busy */
/* set entry mode: display shift on/off, dec/inc cursor move direction */
#define LCD_ENTRY_DEC 0x04 /* display shift off, dec cursor move dir */
#define LCD_ENTRY_DEC_SHIFT 0x05 /* display shift on, dec cursor move dir */
#define LCD_ENTRY_INC_ 0x06 /* display shift off, inc cursor move dir */
#define LCD_ENTRY_INC_SHIFT 0x07 /* display shift on, inc cursor move dir */
/* display on/off, cursor on/off, blinking char at cursor position */
#define LCD_DISP_OFF 0x08 /* display off */
#define LCD_DISP_ON 0x0C /* display on, cursor off */
#define LCD_DISP_ON_BLINK 0x0D /* display on, cursor off, blink char */
#define LCD_DISP_ON_CURSOR 0x0E /* display on, cursor on */
#define LCD_DISP_ON_CURSOR_BLINK 0x0F /* display on, cursor on, blink char */
/* move cursor/shift display */
#define LCD_MOVE_CURSOR_LEFT 0x10 /* move cursor left (decrement) */
#define LCD_MOVE_CURSOR_RIGHT 0x14 /* move cursor right (increment) */
#define LCD_MOVE_DISP_LEFT 0x18 /* shift display left */
#define LCD_MOVE_DISP_RIGHT 0x1C /* shift display right */
/* function set: set interface data length and number of display lines */
#define LCD_FUNCTION_4BIT_1LINE 0x20 /* 4-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_4BIT_2LINES 0x28 /* 4-bit interface, dual line, 5x7 dots */
#define LCD_FUNCTION_8BIT_1LINE 0x30 /* 8-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_8BIT_2LINES 0x38 /* 8-bit interface, dual line, 5x7 dots */
#define LCD_MODE_DEFAULT ((1<<LCD_ENTRY_MODE) | (1<<LCD_ENTRY_INC) )
/**
* @name Functions
*/
/**
@brief Initialize display and select type of cursor
@param dispAttr \b LCD_DISP_OFF display off\n
\b LCD_DISP_ON display on, cursor off\n
\b LCD_DISP_ON_CURSOR display on, cursor on\n
\b LCD_DISP_ON_CURSOR_BLINK display on, cursor on flashing
@return none
*/
extern void lcd_init(uint8_t dispAttr);
/**
@brief Clear display and set cursor to home position
@param void
@return none
*/
extern void lcd_clrscr(void);
/**
@brief Set cursor to home position
@param void
@return none
*/
extern void lcd_home(void);
/**
@brief Set cursor to specified position
@param x horizontal position\n (0: left most position)
@param y vertical position\n (0: first line)
@return none
*/
extern void lcd_gotoxy(uint8_t x, uint8_t y);
/**
@brief Set illumination pin
@param void
@return none
*/
extern void lcd_led(uint8_t onoff);
/**
@brief Display character at current cursor position
@param c character to be displayed
@return none
*/
extern void lcd_putc(char c);
/**
@brief Display string without auto linefeed
@param s string to be displayed
@return none
*/
extern void lcd_puts(const char *s);
/**
@brief Display string from program memory without auto linefeed
@param s string from program memory be be displayed
@return none
@see lcd_puts_P
*/
extern void lcd_puts_p(const char *progmem_s);
/**
@brief Send LCD controller instruction command
@param cmd instruction to send to LCD controller, see HD44780 data sheet
@return none
*/
extern void lcd_command(uint8_t cmd);
/**
@brief Send data byte to LCD controller
Similar to lcd_putc(), but without interpreting LF
@param data byte to send to LCD controller, see HD44780 data sheet
@return none
*/
extern void lcd_data(uint8_t data);
/**
@brief Clear CGRAM
@param void
@return none
*/
extern void lcd_clear_CGRAM (void);
/**
@brief Create custom character in CGRAM
@param charnum: Character position in CGRAM. You can define maximum 8 chars.
@param values[]: Custom character descriptor pointer.
@return 0 - The custom character successfully created
@return 1 - If the charnum greater than 7. You can define maximum 8 chars.
*/
extern uint8_t lcd_create_custom_char (uint8_t charnum, const uint8_t * values);
/**
@brief macros for automatically storing string constant in program memory
*/
#define lcd_puts_P(__s) lcd_puts_p(PSTR(__s))
/*@}*/
#endif //LCD_H

@ -1,161 +0,0 @@
/*
pcf8574 lib 0x02
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "pcf8574.h"
//path to i2c fleury lib
#include PCF8574_I2CFLEURYPATH
/*
* initialize
*/
void pcf8574_init() {
#if PCF8574_I2CINIT == 1
//init i2c
i2c_init();
_delay_us(10);
#endif
//reset the pin status
uint8_t i = 0;
for(i=0; i<PCF8574_MAXDEVICES; i++)
pcf8574_pinstatus[i] = 0;
}
/*
* get output status
*/
int8_t pcf8574_getoutput(uint8_t deviceid) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES)) {
data = pcf8574_pinstatus[deviceid];
}
return data;
}
/*
* get output pin status
*/
int8_t pcf8574_getoutputpin(uint8_t deviceid, uint8_t pin) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pin >= 0 && pin < PCF8574_MAXPINS)) {
data = pcf8574_pinstatus[deviceid];
data = (data >> pin) & 0b00000001;
}
return data;
}
/*
* set output pins
*/
int8_t pcf8574_setoutput(uint8_t deviceid, uint8_t data) {
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES)) {
pcf8574_pinstatus[deviceid] = data;
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_WRITE);
i2c_write(data);
i2c_stop();
return 0;
}
return -1;
}
/*
* set output pins, replace actual status of a device from pinstart for pinlength with data
*/
int8_t pcf8574_setoutputpins(uint8_t deviceid, uint8_t pinstart, uint8_t pinlength, int8_t data) {
//example:
//actual data is 0b01101110
//want to change ---
//pinstart 4
//data 101 (pinlength 3)
//result 0b01110110
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pinstart - pinlength + 1 >= 0 && pinstart - pinlength + 1 >= 0 && pinstart < PCF8574_MAXPINS && pinstart > 0 && pinlength > 0)) {
uint8_t b = 0;
b = pcf8574_pinstatus[deviceid];
uint8_t mask = ((1 << pinlength) - 1) << (pinstart - pinlength + 1);
data <<= (pinstart - pinlength + 1);
data &= mask;
b &= ~(mask);
b |= data;
pcf8574_pinstatus[deviceid] = b;
//update device
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_WRITE);
i2c_write(b);
i2c_stop();
return 0;
}
return -1;
}
/*
* set output pin
*/
int8_t pcf8574_setoutputpin(uint8_t deviceid, uint8_t pin, uint8_t data) {
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pin >= 0 && pin < PCF8574_MAXPINS)) {
uint8_t b = 0;
b = pcf8574_pinstatus[deviceid];
b = (data != 0) ? (b | (1 << pin)) : (b & ~(1 << pin));
pcf8574_pinstatus[deviceid] = b;
//update device
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_WRITE);
i2c_write(b);
i2c_stop();
return 0;
}
return -1;
}
/*
* set output pin high
*/
int8_t pcf8574_setoutputpinhigh(uint8_t deviceid, uint8_t pin) {
return pcf8574_setoutputpin(deviceid, pin, 1);
}
/*
* set output pin low
*/
int8_t pcf8574_setoutputpinlow(uint8_t deviceid, uint8_t pin) {
return pcf8574_setoutputpin(deviceid, pin, 0);
}
/*
* get input data
*/
int8_t pcf8574_getinput(uint8_t deviceid) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES)) {
i2c_start(((PCF8574_ADDRBASE+deviceid)<<1) | I2C_READ);
data = ~i2c_readNak();
i2c_stop();
}
return data;
}
/*
* get input pin (up or low)
*/
int8_t pcf8574_getinputpin(uint8_t deviceid, uint8_t pin) {
int8_t data = -1;
if((deviceid >= 0 && deviceid < PCF8574_MAXDEVICES) && (pin >= 0 && pin < PCF8574_MAXPINS)) {
data = pcf8574_getinput(deviceid);
if(data != -1) {
data = (data >> pin) & 0b00000001;
}
}
return data;
}

@ -1,39 +0,0 @@
/*
pcf8574 lib 0x02
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#ifndef PCF8574_H_
#define PCF8574_H_
#define PCF8574_ADDRBASE (0x20) //device base address
#define PCF8574_I2CINIT 1 //init i2c
#define PCF8574_MAXDEVICES 1 //max devices, depends on address (3 bit)
#define PCF8574_MAXPINS 8 //max pin per device
//settings
#define PCF8574_I2CFLEURYPATH "i2cmaster.h" //define the path to i2c fleury lib
//pin status
volatile uint8_t pcf8574_pinstatus[PCF8574_MAXDEVICES];
//functions
void pcf8574_init();
extern int8_t pcf8574_getoutput(uint8_t deviceid);
extern int8_t pcf8574_getoutputpin(uint8_t deviceid, uint8_t pin);
extern int8_t pcf8574_setoutput(uint8_t deviceid, uint8_t data);
extern int8_t pcf8574_setoutputpins(uint8_t deviceid, uint8_t pinstart, uint8_t pinlength, int8_t data);
extern int8_t pcf8574_setoutputpin(uint8_t deviceid, uint8_t pin, uint8_t data);
extern int8_t pcf8574_setoutputpinhigh(uint8_t deviceid, uint8_t pin);
extern int8_t pcf8574_setoutputpinlow(uint8_t deviceid, uint8_t pin);
extern int8_t pcf8574_getinput(uint8_t deviceid);
extern int8_t pcf8574_getinputpin(uint8_t deviceid, uint8_t pin);
#endif

34
pid.c

@ -1,34 +0,0 @@
#include <util/atomic.h>
#include "pid.h"
float pid_step(volatile struct pid* controller, float dt, float error) {
//TODO convert to integer; floats cause trouble ...
// Calculate p term
float p = error * controller->kP;
// Calculate i term
ATOMIC_BLOCK(ATOMIC_FORCEON){
controller->integral += error * dt * controller->kI;
if(controller->integral > 80)
controller->integral = 80;
if(controller->integral < -80)
controller->integral = -80;
}
// Calculate d term, taking care to not divide by zero
float d = dt == 0 ? 0 : ((error - controller->lastError) / dt) * controller->kD;
controller->lastError = error;
return p + controller->integral + d;
}
void init_pid(volatile struct pid* controller, float p, float i, float d){
*controller = (struct pid){
.kP = p,
.kI = i,
.kD = d,
.lastError = 0,
.integral = 0
};
}

14
pid.h

@ -1,14 +0,0 @@
struct pid{
// Controller gains
float kP;
float kI;
float kD;
// State variables
float lastError;
float integral;
};
float pid_step(volatile struct pid* controller, float dt, float error);
void init_pid(volatile struct pid* controller, float p, float i, float d);

@ -1,202 +0,0 @@
/*************************************************************************
* Title: I2C master library using hardware TWI interface
* Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
* File: $Id: twimaster.c,v 1.4 2015/01/17 12:16:05 peter Exp $
* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
* Target: any AVR device with hardware TWI
* Usage: API compatible with I2C Software Library i2cmaster.h
**************************************************************************/
#include <inttypes.h>
#include <compat/twi.h>
#include "i2cmaster.h"
/* define CPU frequency in hz here if not defined in Makefile */
#ifndef F_CPU
#define F_CPU 4000000UL
#endif
/* I2C clock in Hz */
#define SCL_CLOCK 100000L
/*************************************************************************
Initialization of the I2C bus interface. Need to be called only once
*************************************************************************/
void i2c_init(void)
{
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
TWSR = (1<<TWPS1) | (1<<TWPS1); /* 64 prescaler */
TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
}/* i2c_init */
/*************************************************************************
Issues a start condition and sends address and transfer direction.
return 0 = device accessible, 1= failed to access device
*************************************************************************/
unsigned char i2c_start(unsigned char address)
{
uint8_t twst;
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
return 0;
}/* i2c_start */
/*************************************************************************
Issues a start condition and sends address and transfer direction.
If device is busy, use ack polling to wait until device is ready
Input: address and transfer direction of I2C device
*************************************************************************/
void i2c_start_wait(unsigned char address)
{
uint8_t twst;
while ( 1 )
{
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
{
/* device busy, send stop condition to terminate write operation */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
while(TWCR & (1<<TWSTO));
continue;
}
//if( twst != TW_MT_SLA_ACK) return 1;
break;
}
}/* i2c_start_wait */
/*************************************************************************
Issues a repeated start condition and sends address and transfer direction
Input: address and transfer direction of I2C device
Return: 0 device accessible
1 failed to access device
*************************************************************************/
unsigned char i2c_rep_start(unsigned char address)
{
return i2c_start( address );
}/* i2c_rep_start */
/*************************************************************************
Terminates the data transfer and releases the I2C bus
*************************************************************************/
void i2c_stop(void)
{
/* send stop condition */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
while(TWCR & (1<<TWSTO));
}/* i2c_stop */
/*************************************************************************
Send one byte to I2C device
Input: byte to be transfered
Return: 0 write successful
1 write failed
*************************************************************************/
unsigned char i2c_write( unsigned char data )
{
uint8_t twst;
// send data to the previously addressed device
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits
twst = TW_STATUS & 0xF8;
if( twst != TW_MT_DATA_ACK) return 1;
return 0;
}/* i2c_write */
/*************************************************************************
Read one byte from the I2C device, request more data from device
Return: byte read from I2C device
*************************************************************************/
unsigned char i2c_readAck(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
while(!(TWCR & (1<<TWINT)));
return TWDR;
}/* i2c_readAck */
/*************************************************************************
Read one byte from the I2C device, read is followed by a stop condition
Return: byte read from I2C device
*************************************************************************/
unsigned char i2c_readNak(void)
{
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT)));
return TWDR;
}/* i2c_readNak */
Loading…
Cancel
Save