|
@@ -18,35 +18,47 @@
|
|
|
#include "ds3231.h"
|
|
|
#include "rtos.h"
|
|
|
#include "event-system.h"
|
|
|
+#include "common.h"
|
|
|
#include "main.h"
|
|
|
|
|
|
/* Defines */
|
|
|
-// timeout in timer tiks, step 4 mks
|
|
|
-#define DHT_TIMEOUT 1500
|
|
|
-#define DHT_TOUT1 55
|
|
|
-#define DHT_MAX_COUNT 41
|
|
|
/* Timer2 settings */
|
|
|
#define TIMER2_HZ 400
|
|
|
#define TIMER2_PRESCALER 1024
|
|
|
#define TIMER2_CS (1<<CS22 | 1<<CS21 | 1<<CS20)
|
|
|
#define TIMER2_CNT (0x100 - (F_CPU / TIMER2_PRESCALER / TIMER2_HZ))
|
|
|
|
|
|
-#if (USE_BRIGHT_CONTROL == 1)
|
|
|
-/* Lamp brightness */
|
|
|
-#define BRIGHT_IDX_MAX 9
|
|
|
-#define FULL_BRIGHT_ON 0x06
|
|
|
-#define FULL_BRIGHT_OFF 0x22
|
|
|
-#endif // USE_BRIGHT_CONTROL
|
|
|
-
|
|
|
/* Display timeout, sec */
|
|
|
#define DISP_WDT_TIME 10
|
|
|
|
|
|
+#ifdef USE_UART
|
|
|
/* USART */
|
|
|
#define BAUD 19200UL
|
|
|
#define BAUD_PRESCALE (uint8_t)((F_CPU / BAUD / 16UL) - 1)
|
|
|
#define CHAR_NEWLINE '\n'
|
|
|
#define CHAR_RETURN '\r'
|
|
|
#define RETURN_NEWLINE "\r\n"
|
|
|
+#endif // USE_UART
|
|
|
+
|
|
|
+#ifdef USE_BRIGHT_CONTROL
|
|
|
+/* Lamp brightness */
|
|
|
+#define BRIGHT_IDX_MAX 4
|
|
|
+#define FULL_BRIGHT_ON 0x06
|
|
|
+#define FULL_BRIGHT_OFF 0x22
|
|
|
+static const uint8_t PROGMEM brightConv[BRIGHT_IDX_MAX+1] = {
|
|
|
+ 217, 219, 224, 236, 255
|
|
|
+};
|
|
|
+#endif // USE_BRIGHT_CONTROL
|
|
|
+
|
|
|
+#ifdef USE_DHT
|
|
|
+// timeout in timer tiks, step 4 mks
|
|
|
+#define DHT_TIMEOUT 1500
|
|
|
+#define DHT_TOUT1 55
|
|
|
+static struct {
|
|
|
+ uint8_t Humidity;
|
|
|
+ uint16_t Temperature;
|
|
|
+} dhtData;
|
|
|
+#endif // USE_DHT
|
|
|
|
|
|
|
|
|
/* Variables */
|
|
@@ -71,20 +83,6 @@ static volatile uint8_t DISP_WDT = 0;
|
|
|
static EEMEM uint8_t EEP_BrightIdx;
|
|
|
static uint8_t brightIdx;
|
|
|
static EEMEM uint8_t EEP_SummerTime;
|
|
|
-static uint8_t dhtBuf[42];
|
|
|
-static volatile struct {
|
|
|
- uint8_t Humidity;
|
|
|
- uint8_t Temperature;
|
|
|
-} dhtData;
|
|
|
-uint16_t Humidity;
|
|
|
-uint16_t Temperature;
|
|
|
-
|
|
|
-/* Constants */
|
|
|
-#if (USE_BRIGHT_CONTROL == 1)
|
|
|
-static const uint8_t PROGMEM brightConv[10] = {
|
|
|
- 195, 196, 197, 199, 203, 208, 216, 226, 239, 255
|
|
|
-};
|
|
|
-#endif // USE_BRIGHT_CONTROL
|
|
|
|
|
|
/* Function prototypes */
|
|
|
static void Board_Init(void);
|
|
@@ -94,14 +92,17 @@ static void valIncrease(uint8_t * val, uint8_t max);
|
|
|
static void valDecrease(uint8_t * val, uint8_t max);
|
|
|
static void blink(void);
|
|
|
static void setSummerWinterTime(void);
|
|
|
+#ifdef USE_DHT
|
|
|
static void dhtStart(void);
|
|
|
static void dhtProcess(void);
|
|
|
static void dhtEnd(void);
|
|
|
static void dhtTimeout(void);
|
|
|
static void dhtNoAck(void);
|
|
|
-//static void dhtErrorCRC(void);
|
|
|
+#endif // USE_DHT
|
|
|
+#ifdef USE_UART
|
|
|
void usart_putc (char send);
|
|
|
void usart_puts (const char *send);
|
|
|
+#endif // USE_UART
|
|
|
|
|
|
void main(void) {
|
|
|
/**
|
|
@@ -117,12 +118,12 @@ void main(void) {
|
|
|
Flag.blinkC = 0;
|
|
|
Flag.saveEEP = 0;
|
|
|
|
|
|
- #if (USE_BRIGHT_CONTROL == 1)
|
|
|
+#ifdef USE_BRIGHT_CONTROL
|
|
|
brightIdx = eeprom_read_byte(&EEP_BrightIdx);
|
|
|
if (brightIdx > BRIGHT_IDX_MAX) {
|
|
|
brightIdx = BRIGHT_IDX_MAX;
|
|
|
}
|
|
|
- #endif // USE_BRIGHT_CONTROL
|
|
|
+#endif // USE_BRIGHT_CONTROL
|
|
|
|
|
|
/**
|
|
|
* Инициализация, настройка...
|
|
@@ -143,7 +144,9 @@ void main(void) {
|
|
|
showTime();
|
|
|
|
|
|
RTOS_SetTask(btnProcess, 3, BTN_SCAN_PERIOD);
|
|
|
+#ifdef USE_DHT
|
|
|
RTOS_SetTask(dhtStart, 2000, 15000);
|
|
|
+#endif // USE_DHT
|
|
|
|
|
|
/** main loop */
|
|
|
do {
|
|
@@ -159,7 +162,7 @@ void main(void) {
|
|
|
RTC_ReadCalendar(&RTC);
|
|
|
ES_PlaceEvent(evRefreshCal);
|
|
|
}
|
|
|
-#if (USE_BRIGHT_CONTROL == 1)
|
|
|
+#ifdef USE_BRIGHT_CONTROL
|
|
|
if (RTC.Hr >= FULL_BRIGHT_ON && RTC.Hr < FULL_BRIGHT_OFF) {
|
|
|
OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
|
|
|
} else {
|
|
@@ -186,7 +189,25 @@ void main(void) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+#ifdef USE_DHT
|
|
|
+ switch(RTC.Sec) {
|
|
|
+ case 20:
|
|
|
+ case 50:
|
|
|
+ ES_PlaceEvent(evShTemp);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 22:
|
|
|
+ case 52:
|
|
|
+ ES_PlaceEvent(evShHum);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 24:
|
|
|
+ case 54:
|
|
|
+ ES_PlaceEvent(evShTime);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+#endif // USE_DHT
|
|
|
+ } // End of New Second
|
|
|
|
|
|
event = ES_GetEvent();
|
|
|
if (event) {
|
|
@@ -219,17 +240,17 @@ static void Board_Init(void) {
|
|
|
DDRC = DIGIT_PINS; // as output
|
|
|
DDRD = (DOT_PIN | ANOD_PINS); // as output
|
|
|
|
|
|
+#ifdef USE_DHT
|
|
|
/* Timer1, IC negative edge, CTC mode, 64 prescaler, 4 mks one tick */
|
|
|
TCCR1B = ((0<<ICES1) | (1<<CS11) | (1<<CS10));
|
|
|
+#endif // USE_DHT
|
|
|
|
|
|
/* Timer2 - refresh Nixie values */
|
|
|
TCCR2 = TIMER2_CS;
|
|
|
TCNT2 = TIMER2_CNT;
|
|
|
-
|
|
|
- /* Timer interrupts */
|
|
|
TIMSK = _BV(TOIE2);
|
|
|
|
|
|
-#if (USE_BRIGHT_CONTROL == 1)
|
|
|
+#ifdef USE_BRIGHT_CONTROL
|
|
|
OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
|
|
|
TIMSK |= _BV(OCIE2);
|
|
|
#endif // USE_BRIGHT_CONTROL
|
|
@@ -238,6 +259,7 @@ static void Board_Init(void) {
|
|
|
MCUCR = _BV(ISC11); // falling edge
|
|
|
GICR = _BV(INT1);
|
|
|
|
|
|
+#ifdef USE_UART
|
|
|
/* USART */
|
|
|
// Turn on USART hardware (no RX, TX)
|
|
|
UCSRB |= (0 << RXEN) | (1 << TXEN);
|
|
@@ -246,6 +268,7 @@ static void Board_Init(void) {
|
|
|
// Set baud rate
|
|
|
UBRRH = (BAUD_PRESCALE >> 8);
|
|
|
UBRRL = BAUD_PRESCALE;
|
|
|
+#endif // USE_UART
|
|
|
|
|
|
/* Enable Interrupts */
|
|
|
sei();
|
|
@@ -291,14 +314,15 @@ void dotOnPersistent(void) {
|
|
|
PORTD |= DOT_PIN;
|
|
|
}
|
|
|
|
|
|
+#ifdef USE_DHT
|
|
|
static void dhtStart(void) {
|
|
|
RTOS_SetTask(dhtProcess, 2, 0);
|
|
|
DHT_PIN_LOW;
|
|
|
}
|
|
|
|
|
|
static void dhtProcess(void) {
|
|
|
- uint8_t cnt1, cnt2, buf;//, parity;
|
|
|
- uint16_t tcnt_old;
|
|
|
+ uint8_t cnt1, cnt2, buf;
|
|
|
+ uint16_t tcnt_old, hmdt, tmprtr;
|
|
|
|
|
|
DHT_PIN_INPUT;
|
|
|
TCNT1 = 0;
|
|
@@ -315,13 +339,11 @@ static void dhtProcess(void) {
|
|
|
if (TCNT1 >= DHT_TIMEOUT) {
|
|
|
RTOS_SetTask(dhtTimeout, 0, 0);
|
|
|
}
|
|
|
- // begin of data
|
|
|
-// parity = 0;
|
|
|
for (cnt1=0; cnt1<32; cnt1+=8) { // 0 8 16 24 32
|
|
|
+ hmdt = 0; tmprtr = 0;
|
|
|
buf = 0;
|
|
|
for (cnt2=0; cnt2<8; cnt2++) {
|
|
|
buf <<= 1;
|
|
|
-// cli();
|
|
|
// "0", начало периода
|
|
|
while(bit_is_clear(PINB, PB0)); // ждём начало импульса
|
|
|
tcnt_old = TCNT1; // начало импульса
|
|
@@ -329,37 +351,29 @@ static void dhtProcess(void) {
|
|
|
if ((TCNT1 - tcnt_old) > 12) {
|
|
|
buf |= 1;
|
|
|
}
|
|
|
-// sei();
|
|
|
}
|
|
|
-// parity += buf;
|
|
|
switch (cnt1) {
|
|
|
case 0:
|
|
|
- Humidity = buf << 8;
|
|
|
+ hmdt = buf << 8;
|
|
|
break;
|
|
|
case 8:
|
|
|
- Humidity |= buf;
|
|
|
+ hmdt |= buf;
|
|
|
break;
|
|
|
case 16:
|
|
|
- Temperature = buf << 8;
|
|
|
+ tmprtr = buf << 8;
|
|
|
break;
|
|
|
case 24:
|
|
|
- Temperature |= buf;
|
|
|
+ tmprtr |= buf;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
if (TCNT1 >= DHT_TIMEOUT) {
|
|
|
RTOS_SetTask(dhtTimeout, 0, 0);
|
|
|
return;
|
|
|
}
|
|
|
-/*
|
|
|
- if (parity != buf) {
|
|
|
- dhtErrorCRC();
|
|
|
- return;
|
|
|
- }
|
|
|
-*/
|
|
|
- dhtData.Humidity = (uint8_t)((Humidity + 5) / 10);
|
|
|
- dhtData.Temperature = (uint8_t)((Temperature + 5) / 10);
|
|
|
+
|
|
|
+ dhtData.Humidity = (uint8_t)((hmdt + 5) / 10);
|
|
|
+ dhtData.Temperature = tmprtr;
|
|
|
|
|
|
RTOS_SetTask(dhtEnd, 0, 0);
|
|
|
}
|
|
@@ -371,65 +385,43 @@ static void dhtEnd(void) {
|
|
|
usart_puts("DHT22 data:\r\n");
|
|
|
|
|
|
usart_puts("Humidity: ");
|
|
|
- itoa(Humidity, buffer, 10);
|
|
|
- usart_puts(buffer);
|
|
|
- usart_puts(" %/% ");
|
|
|
itoa(dhtData.Humidity, buffer, 10);
|
|
|
usart_puts(buffer);
|
|
|
-
|
|
|
- usart_putc('\t');
|
|
|
+ usart_puts(" %\t\t");
|
|
|
|
|
|
usart_puts("Temperature: ");
|
|
|
- itoa(Temperature, buffer, 10);
|
|
|
- usart_puts(buffer);
|
|
|
- usart_puts(" C/C ");
|
|
|
- itoa(dhtData.Temperature, buffer, 10);
|
|
|
+ itoa(dhtData.Temperature/10, buffer, 10);
|
|
|
usart_puts(buffer);
|
|
|
- usart_puts(RETURN_NEWLINE);
|
|
|
-}
|
|
|
-/*
|
|
|
- itoa(dhtBuf[0], buffer, 10);
|
|
|
- dhtBuf[0] = 0;
|
|
|
+ usart_putc('.');
|
|
|
+ itoa(dhtData.Temperature%10, buffer, 10);
|
|
|
usart_puts(buffer);
|
|
|
- usart_puts(RETURN_NEWLINE);
|
|
|
-
|
|
|
- for (i=0; i<40; i+8) {
|
|
|
- for (k=1; k<9; k++) {
|
|
|
- itoa(dhtBuf[i+k], buffer, 10);
|
|
|
- dhtBuf[i+k] = 0;
|
|
|
- usart_puts(buffer);
|
|
|
- usart_putc('\t');
|
|
|
- }
|
|
|
- usart_puts(RETURN_NEWLINE);
|
|
|
- }
|
|
|
- usart_puts(RETURN_NEWLINE);
|
|
|
+ usart_puts("oC\r\n");
|
|
|
}
|
|
|
-*/
|
|
|
+
|
|
|
static void dhtNoAck(void) {
|
|
|
- usart_puts("DHT22 no ACK reached.\r\n");
|
|
|
+ usart_puts("DHT22 no ACK occurred.\r\n");
|
|
|
}
|
|
|
|
|
|
static void dhtTimeout(void) {
|
|
|
- usart_puts("DHT22 Timeout reached.\r\n");
|
|
|
- dhtEnd();
|
|
|
+ usart_puts("DHT22 Timeout occurred.\r\n");
|
|
|
}
|
|
|
-/*
|
|
|
-static void dhtErrorCRC(void) {
|
|
|
- char buffer[6];
|
|
|
|
|
|
- usart_puts("DHT22 CRC Error.\r\n");
|
|
|
+void showTemperature(void) {
|
|
|
+ uint8_t a = dhtData.Temperature / 100;
|
|
|
+ uint8_t b = dhtData.Temperature % 100;
|
|
|
+ Digit[0] = a / 10;
|
|
|
+ Digit[1] = a % 10;
|
|
|
+ Digit[2] = b;
|
|
|
+ Digit[3] = DIGIT_BLANK;
|
|
|
+}
|
|
|
|
|
|
- usart_puts("Humidity: ");
|
|
|
- itoa(Humidity, buffer, 10);
|
|
|
- usart_puts(buffer);
|
|
|
- usart_puts("\t\t");
|
|
|
+void showHumidity(void) {
|
|
|
+ Digit[0] = DIGIT_BLANK;
|
|
|
+ Digit[1] = DIGIT_BLANK;
|
|
|
+ Digit[2] = dhtData.Humidity / 10;
|
|
|
+ Digit[3] = dhtData.Humidity % 10;}
|
|
|
+#endif // USE_DHT
|
|
|
|
|
|
- usart_puts("Temperature: ");
|
|
|
- itoa(Temperature, buffer, 10);
|
|
|
- usart_puts(buffer);
|
|
|
- usart_puts(RETURN_NEWLINE);
|
|
|
-}
|
|
|
-*/
|
|
|
/**
|
|
|
* @brief Обработка кнопок.
|
|
|
* @param : None
|
|
@@ -516,16 +508,6 @@ void showYear(void) {
|
|
|
Digit[3] = RTC.Year & 0x0F;
|
|
|
}
|
|
|
|
|
|
-#if (USE_BRIGHT_CONTROL == 1)
|
|
|
-void showBright(void) {
|
|
|
- DISP_WDT = DISP_WDT_TIME;
|
|
|
- Digit[0] = DIGIT_BLANK;
|
|
|
- Digit[1] = DIGIT_BLANK;
|
|
|
- Digit[2] = brightIdx;
|
|
|
- Digit[3] = DIGIT_BLANK;
|
|
|
-}
|
|
|
-#endif // USE_BRIGHT_CONTROL
|
|
|
-
|
|
|
void incWDay(void) {
|
|
|
if (RTC.WD < 7) {
|
|
|
RTC.WD ++;
|
|
@@ -580,7 +562,15 @@ void decYear(void) {
|
|
|
Flag.saveCal = 1;
|
|
|
}
|
|
|
|
|
|
-#if (USE_BRIGHT_CONTROL == 1)
|
|
|
+#ifdef USE_BRIGHT_CONTROL
|
|
|
+void showBright(void) {
|
|
|
+ DISP_WDT = DISP_WDT_TIME;
|
|
|
+ Digit[0] = DIGIT_BLANK;
|
|
|
+ Digit[1] = DIGIT_BLANK;
|
|
|
+ Digit[2] = brightIdx;
|
|
|
+ Digit[3] = DIGIT_BLANK;
|
|
|
+}
|
|
|
+
|
|
|
void incBright(void) {
|
|
|
if (brightIdx < BRIGHT_IDX_MAX) {
|
|
|
brightIdx ++;
|
|
@@ -713,6 +703,7 @@ static void valDecrease(uint8_t * val, uint8_t max) {
|
|
|
*val = ((bin / 10 ) << 4) | (bin % 10);
|
|
|
}
|
|
|
|
|
|
+#ifdef USE_UART
|
|
|
void usart_putc (char send) {
|
|
|
// Do nothing for a bit if there is already
|
|
|
// data waiting in the hardware to be sent
|
|
@@ -726,7 +717,7 @@ void usart_puts (const char *send) {
|
|
|
usart_putc(*send++);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+#endif // USE_UART
|
|
|
|
|
|
/**
|
|
|
* П р е р ы в а н и я
|
|
@@ -743,6 +734,7 @@ ISR (INT1_vect) {
|
|
|
* @brief Refresh Nixie output
|
|
|
* @note Digit[] must be in range 0x00 - 0x0F
|
|
|
*/
|
|
|
+#pragma GCC optimize ("O3")
|
|
|
ISR(TIMER2_OVF_vect) {
|
|
|
static uint8_t idx = 0;
|
|
|
|
|
@@ -753,7 +745,7 @@ ISR(TIMER2_OVF_vect) {
|
|
|
uint8_t pd = PORTD & ~ANOD_PINS;
|
|
|
uint8_t pc = PORTC & ~DIGIT_PINS;
|
|
|
|
|
|
-#if (USE_BRIGHT_CONTROL != TRUE)
|
|
|
+#ifndef USE_BRIGHT_CONTROL
|
|
|
// power off lamps
|
|
|
PORTD = pd;
|
|
|
PORTC = pc;
|
|
@@ -808,11 +800,12 @@ ISR(TIMER2_OVF_vect) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#if (USE_BRIGHT_CONTROL == 1)
|
|
|
+#ifdef USE_BRIGHT_CONTROL
|
|
|
/**
|
|
|
* @brief Power Off Nixie output
|
|
|
* @note For Brightnes dimming
|
|
|
*/
|
|
|
+#pragma GCC optimize ("O3")
|
|
|
ISR(TIMER2_COMP_vect) {
|
|
|
// power off lamps
|
|
|
PORTD &= ~ANOD_PINS;
|
|
@@ -820,25 +813,6 @@ ISR(TIMER2_COMP_vect) {
|
|
|
}
|
|
|
#endif // USE_BRIGHT_CONTROL
|
|
|
|
|
|
-/**
|
|
|
- * @brief DHT input capture
|
|
|
- */
|
|
|
-ISR(TIMER1_CAPT_vect) {
|
|
|
- static uint8_t count = 0;
|
|
|
- static uint16_t icr_old = 0;
|
|
|
-
|
|
|
- uint16_t icr = ICR1;
|
|
|
-
|
|
|
- dhtBuf[count] = (uint8_t)(icr - icr_old);
|
|
|
- icr_old = icr;
|
|
|
- count ++;
|
|
|
- if (count >= DHT_MAX_COUNT) {
|
|
|
- icr_old = 0;
|
|
|
- count = 0;
|
|
|
- RTOS_SetTask(dhtEnd, 0, 0);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
/**
|
|
|
* @brief заглушка для неиспользуемых прерываний
|
|
|
*/
|