121 lines
2.1 KiB
C++
121 lines
2.1 KiB
C++
|
/*
|
||
|
* util.cpp
|
||
|
*
|
||
|
* Created: 17-10-17 21:21:20
|
||
|
* Author: Adrien
|
||
|
*/
|
||
|
#include <avr/io.h>
|
||
|
#include <avr/interrupt.h>
|
||
|
|
||
|
#include "util.h"
|
||
|
#include "global.h"
|
||
|
|
||
|
unsigned long tmpMillis = 0UL;
|
||
|
|
||
|
|
||
|
uint8_t uint8ToBCD(uint8_t val)
|
||
|
{
|
||
|
return (val%10)|((val/10)<<4);
|
||
|
}
|
||
|
|
||
|
void intTo4DigitArray(unsigned int d, uint8_t * array, uint8_t arrayMax)
|
||
|
{
|
||
|
int i;
|
||
|
for(i = 0; d && i<arrayMax ; i++)
|
||
|
{
|
||
|
array[i] = d%10;
|
||
|
d/=10;
|
||
|
}
|
||
|
for(;i<arrayMax;i++)
|
||
|
array[i] = 0;
|
||
|
}
|
||
|
|
||
|
volatile uint8_t *pinToPINX(uint8_t pin)
|
||
|
{
|
||
|
if(pin<=7)
|
||
|
return &PIND;
|
||
|
else if(pin<=13)
|
||
|
return &PINB;
|
||
|
else
|
||
|
return &PINC;
|
||
|
}
|
||
|
uint8_t pinArduinoToPINXX(uint8_t pin)
|
||
|
{
|
||
|
if(pin <= 13)
|
||
|
return pin%8;
|
||
|
else
|
||
|
return pin-13;
|
||
|
}
|
||
|
|
||
|
void setupMilis()
|
||
|
{
|
||
|
TCCR0A = 0x00;
|
||
|
TCCR0B = _BV(CS11)|_BV(CS10); //Divide 16Mhz by 64
|
||
|
TIMSK0 = _BV(OCIE1A);
|
||
|
OCR0A = 250;
|
||
|
//Compare at 250, every ms
|
||
|
sei();
|
||
|
}
|
||
|
|
||
|
unsigned long millis()
|
||
|
{
|
||
|
return tmpMillis;
|
||
|
}
|
||
|
|
||
|
unsigned int millisToHHMM()
|
||
|
{
|
||
|
unsigned long seconde = millis()/1000;
|
||
|
unsigned long minute = seconde/60;
|
||
|
unsigned long heure = minute/60;
|
||
|
minute %= 60;
|
||
|
heure %= 24;
|
||
|
return (heure*100)+minute;
|
||
|
}
|
||
|
void millisToHH_MM(uint8_t * HH, uint8_t *MM)
|
||
|
{
|
||
|
unsigned long seconde = millis()/1000;
|
||
|
unsigned long minute = seconde/60;
|
||
|
unsigned long heure = minute/60;
|
||
|
minute %= 60;
|
||
|
heure %= 24;
|
||
|
*MM = (uint8_t)minute;
|
||
|
*HH = (uint8_t)(heure);
|
||
|
}
|
||
|
void millisToHH_MM_Decalage(uint8_t * HH, uint8_t *MM, unsigned long decalage)
|
||
|
{
|
||
|
unsigned long seconde = (millis()+decalage)/1000;
|
||
|
unsigned long minute = seconde/60;
|
||
|
unsigned long heure = minute/60;
|
||
|
minute %= 60;
|
||
|
heure %= 24;
|
||
|
*MM = (uint8_t)minute;
|
||
|
*HH = (uint8_t)(heure);
|
||
|
}
|
||
|
unsigned long HHMMToMillis(unsigned int HHMM)
|
||
|
{
|
||
|
unsigned long minute = (HHMM%100)+((HHMM/100)*60);
|
||
|
unsigned long ms = minute*60*1000;
|
||
|
return ms;
|
||
|
}
|
||
|
unsigned int HH_MMtoHHMM(uint8_t HH, uint8_t MM)
|
||
|
{
|
||
|
return (unsigned int)(HH*100)+MM;
|
||
|
}
|
||
|
void HHMMtoHH_MM(uint8_t HHMM, uint8_t * HH, uint8_t *MM)
|
||
|
{
|
||
|
*HH = HHMM/100;
|
||
|
*MM = HHMM%100;
|
||
|
}
|
||
|
|
||
|
uint8_t VerifappuisBP()
|
||
|
{
|
||
|
return !(PINB&_BV(__BP_MOIN__) && PINB&_BV(__BP_PLUS__) && PINB&_BV(__BP_START__) && PINB&_BV(__BP_CONF__));
|
||
|
}
|
||
|
|
||
|
|
||
|
ISR(TIMER0_COMPA_vect)
|
||
|
{
|
||
|
++tmpMillis;
|
||
|
TCNT0 = 0;
|
||
|
}
|