Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
ti-nspire / Fehlberg.py
Last active December 9, 2022 09:33
Python による常微分方程式の数値解法 / Fehlberg 法
# Python による常微分方程式の数値解法 / Fehlberg 法
from math import *
class Fehlberg:
def __init__(self, funcs, t0, inits, h, tol=1e-3):
self.funcs = funcs
self.t0 = t0
self.inits = inits
self.h = h
self.numOfDiv = None
@ti-nspire
ti-nspire / RK4.py
Last active September 23, 2020 00:04
Python による常微分方程式の数値解法 / 古典的 Runge-Kutta 法
# Python による常微分方程式の数値解法 / 古典的 Runge-Kutta 法
class RK4:
def __init__(self, funcs, t0, inits, h, numOfDiv=1):
self.funcs = funcs
self.t0 = t0
self.inits = inits
self.dim = len(funcs)
self.numOfDiv = numOfDiv
self.h = h / self.numOfDiv
self.f = [[None for i in range(self.dim)] for i in range(4)]
@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_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 / 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);