Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
ti-nspire / I2CClass.cpp
Last active August 25, 2020 05:01
I2C library for ATmega328P
/*
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 February 4, 2020 00:05
DS3231_Library_for_ATmega328P
#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 February 2, 2020 21:57
LM75_library_for_ATmega328P
#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 January 24, 2020 04:10
myI2C_library_for_ATmega328P
#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 January 24, 2020 04:09
LM75_Example_for_ATmega328P
#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 January 22, 2020 07:03
SPI_library_for_ATmega328P_ver2
#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 January 18, 2020 01:55
AD8402_library_for_ATmega328P
#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 January 19, 2020 19:24
AD8402_Example_for_ATmega328P
#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 January 16, 2020 07:45
EEPROM25_256_library_for_ATmega328P
#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 January 16, 2020 11:49
EEPROM25_256_Example_for_ATmega328P
#include <avr/io.h>
#include <util/delay.h>
extern "C" {
#include "USART.h"
}
#include "mySPI.h"
#include "EEPROM25_256.h"
int main(){
initUSART();