Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
ti-nspire / I2CClass.cpp
Last active Aug 25, 2020
I2C library for ATmega328P
View I2CClass.cpp
/*
I2C library for ATmega328P
*/
#include "I2CClass.h"
#include "set_SCL_freq.h"
void I2CClass::waitForComplete(){
loop_until_bit_is_set(TWCR, TWINT);
}
@ti-nspire
ti-nspire / DS3231.cpp
Last active Feb 4, 2020
DS3231_Library_for_ATmega328P
View DS3231.cpp
#include "DS3231.h"
uint8_t bcd2dec(uint8_t bcd){return ((bcd >> 4) * 10) + (bcd & 0x0f);}
uint8_t dec2bcd(uint8_t dec){return ((dec / 10) << 4) + (dec % 10) ;}
uint8_t contain(uint8_t var, uint8_t min, uint8_t max){
if (var < min){return min;}
else if(var > max){return max;}
else {return var;}
}
@ti-nspire
ti-nspire / LM75.cpp
Last active Feb 2, 2020
LM75_library_for_ATmega328P
View LM75.cpp
#include "LM75.h"
float LM75::getTemp(){
int16_t tempData;
myI2C.start();
myI2C.send(_ADDRESS_W);
myI2C.send(TEMP_REGISTER);
myI2C.start();
@ti-nspire
ti-nspire / myI2C.cpp
Created Jan 24, 2020
myI2C_library_for_ATmega328P
View myI2C.cpp
#include "myI2C.h"
void myI2CClass::waitForComplete(){
loop_until_bit_is_set(TWCR, TWINT);
}
void myI2CClass::enable(uint16_t div){
// i2cビットレート=システムクロック÷(16 + 2 * TWBR * PrescalerValue)。
// PrescalerValueは4にしておく。
TWSR &= ~(1 << TWPS1);
TWSR |= (1 << TWPS0);
@ti-nspire
ti-nspire / LM75_Example.cpp
Created Jan 24, 2020
LM75_Example_for_ATmega328P
View LM75_Example.cpp
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include "USART.h"
#include "myI2C.h"
#include "LM75.h"
int main(){
initUSART();
@ti-nspire
ti-nspire / mySPIv2.cpp
Last active Jan 22, 2020
SPI_library_for_ATmega328P_ver2
View mySPIv2.cpp
#include <avr/io.h>
#include "mySPIv2.h"
void mySPIClass::selectSlave(uint8_t port, uint8_t pin){
switch(port){
case 'B': PORTB &= ~(1 << pin); break;
case 'C': PORTC &= ~(1 << pin); break;
case 'D': PORTD &= ~(1 << pin); break;
default : PORTB &= ~(1 << PB2); break;
}
@ti-nspire
ti-nspire / AD8402.cpp
Last active Jan 18, 2020
AD8402_library_for_ATmega328P
View AD8402.cpp
#include <avr/io.h>
#include "AD8402.h"
#include "mySPIv2.h"
void AD8402::setChPos(uint8_t ch, uint8_t pos){
mySPI.selectSlave(_port, _pin);
mySPI.tradeByte(ch - 1);
mySPI.tradeByte(pos);
mySPI.deselectSlave(_port, _pin);
@ti-nspire
ti-nspire / AD8402_Example.cpp
Last active Jan 19, 2020
AD8402_Example_for_ATmega328P
View AD8402_Example.cpp
#include <avr/io.h>
#include <util/delay.h>
extern "C" {
#include "USART.h"
}
#include "mySPIv2.h"
#include "AD8402.h"
int main(){
@ti-nspire
ti-nspire / EEPROM25_256.cpp
Last active Jan 16, 2020
EEPROM25_256_library_for_ATmega328P
View EEPROM25_256.cpp
#include <avr/io.h>
#include "mySPI.h"
#include "EEPROM25_256.h"
uint8_t EEPROM25_256::isLastElement(uint16_t currentIndex, uint16_t numOfElements){
return (currentIndex == numOfElements - 1);
}
uint8_t EEPROM25_256::isfirstAddressOfPage(uint16_t address){
return address % BYTES_PER_PAGE == 0;
}
@ti-nspire
ti-nspire / EEPROM25_256_Example.cpp
Last active Jan 16, 2020
EEPROM25_256_Example_for_ATmega328P
View EEPROM25_256_Example.cpp
#include <avr/io.h>
#include <util/delay.h>
extern "C" {
#include "USART.h"
}
#include "mySPI.h"
#include "EEPROM25_256.h"
int main(){
initUSART();