RTC init and timer
This commit is contained in:
parent
df3d0c6ce6
commit
88415ddec1
@ -14,17 +14,18 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/cpufunc.h>
|
||||||
int fan_speed(float value);
|
int RTC_init();
|
||||||
|
int fan_speed(float voltage_value);
|
||||||
|
inline void fan_speed_toggle();
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,32 +1,76 @@
|
|||||||
|
|
||||||
#include "fan speeeed.h"
|
#include "fan speeeed.h"
|
||||||
|
RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm;
|
||||||
|
|
||||||
int fan_speed(float voltage_value){
|
int RTC_init() {
|
||||||
init_millis()
|
uint8_t temp;
|
||||||
sei();
|
/* Initialize 32.768kHz Oscillator: */
|
||||||
float fan_speed;
|
/* Disable oscillator: */
|
||||||
float t = 0;
|
temp = CLKCTRL.XOSC32KCTRLA;
|
||||||
float past_time = 0;
|
temp &= ~CLKCTRL_ENABLE_bm;
|
||||||
float start_time = 0;
|
/* Writing to protected register */
|
||||||
int old_value = 0;
|
ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp);
|
||||||
int f;
|
|
||||||
if (voltage_value > 300 && old_value < 300){
|
while (CLKCTRL.MCLKSTATUS & CLKCTRL_XOSC32KS_bm) {
|
||||||
start_time = init_millis()
|
; /* Wait until XOSC32KS becomes 0 */
|
||||||
t =(start_time - past_time);
|
|
||||||
past_time = start_time;
|
|
||||||
}
|
|
||||||
voltage_value = old_value;
|
|
||||||
f = ((1/t)*1000);
|
|
||||||
fan_speed = (f*60)/2;
|
|
||||||
return fan_speed;
|
|
||||||
}
|
}
|
||||||
|
/* SEL = 0 (Use External Crystal): */
|
||||||
|
temp = CLKCTRL.XOSC32KCTRLA;
|
||||||
|
temp &= ~CLKCTRL_SEL_bm;
|
||||||
|
/* Writing to protected register */
|
||||||
|
ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp);
|
||||||
|
|
||||||
|
/* Enable oscillator: */
|
||||||
|
temp = CLKCTRL.XOSC32KCTRLA;
|
||||||
|
temp |= CLKCTRL_ENABLE_bm;
|
||||||
|
/* Writing to protected register */
|
||||||
|
ccp_write_io((void*) &CLKCTRL.XOSC32KCTRLA, temp);
|
||||||
|
|
||||||
|
/* Initialize RTC: */
|
||||||
|
while (RTC.STATUS > 0) {
|
||||||
|
; /* Wait for all register to be synchronized */
|
||||||
|
}
|
||||||
|
/* 32.768kHz External Crystal Oscillator (XOSC32K) */
|
||||||
|
RTC.CLKSEL = RTC_CLKSEL_TOSC32K_gc;
|
||||||
|
/* Run in debug: enabled */
|
||||||
|
RTC.DBGCTRL = RTC_DBGRUN_bm;
|
||||||
|
|
||||||
|
RTC.PITINTCTRL = RTC_PI_bm; /* Periodic Interrupt: enabled */
|
||||||
|
|
||||||
|
RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc /* RTC Clock Cycles 32768 */
|
||||||
|
| RTC_PITEN_bm; /* Enable: enabled */
|
||||||
|
|
||||||
ISR(PORTB_PORT_vect)
|
}
|
||||||
{
|
|
||||||
static int counter = 0;
|
int fan_speed(float voltage_value) {
|
||||||
if (counter >=5){
|
int counter = 0;
|
||||||
PORTB.OUT ^= 0x0F;
|
int old_value = 0;
|
||||||
counter = 0;
|
|
||||||
}
|
if (voltage_value > 300 && old_value < 300) {
|
||||||
PORTB.INFLAFS = PIN2_bm;
|
counter++;
|
||||||
|
}
|
||||||
|
voltage_value = old_value;
|
||||||
|
return old_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void fan_speed_toggle() {
|
||||||
|
float fan_speed;
|
||||||
|
float t = 0;
|
||||||
|
f = ((1 / t)*1000);
|
||||||
|
fan_speed = (f * 60) / 2;
|
||||||
|
return fan_speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(RTC_PIT_vect) {
|
||||||
|
RTC.PITINTFLAGS = RTC_PI_bm;
|
||||||
|
fan_speed_toggle();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
fan_speed();
|
||||||
|
RTC_init();
|
||||||
|
sei();
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user