ソースを参照

Finish IN-4x6.

Vladimir N. Shilov 4 年 前
コミット
bc28331f5e
7 ファイル変更72 行追加22 行削除
  1. 7 0
      ReadMe.txt
  2. 1 7
      SNC.cbp
  3. BIN
      hw/cpu.dch
  4. BIN
      hw/disp-in4x6.dch
  5. 6 1
      inc/common.h
  6. 9 5
      src/i2c.c
  7. 49 9
      src/main.c

+ 7 - 0
ReadMe.txt

@@ -25,3 +25,10 @@ PWR: MAX771
 Пофиксил "меню" установки времени и даты под 6 ламп.
 
 Пока без регулировки яркости по времени.
+---
+2020.12.28
+
+По сутит всё работает, сделал даже обработку фоторезистора, но в реале оно 
+как-то не работает :-)
+
+На этом всё.

+ 1 - 7
SNC.cbp

@@ -29,9 +29,6 @@
 					<Add option="-g" />
 					<Add option="-std=gnu99" />
 				</Compiler>
-				<Environment>
-					<Variable name="USE_BRIGHT_CONTROL" value="0" />
-				</Environment>
 			</Target>
 			<Target title="Release">
 				<Option output="build/SNC.elf" prefix_auto="1" extension_auto="0" />
@@ -54,7 +51,7 @@
 			</Target>
 			<Environment>
 				<Variable name="MCU" value="atmega8" />
-				<Variable name="USE_BRIGHT_CONTROL" value="0" />
+				<Variable name="USE_BRIGHT_CONTROL" value="1" />
 				<Variable name="USE_UART" value="0" />
 			</Environment>
 		</Build>
@@ -435,9 +432,6 @@
 			<Option compilerVar="CC" />
 		</Unit>
 		<Extensions>
-			<code_completion />
-			<envvars />
-			<debugger />
 			<lib_finder disable_auto="1" />
 		</Extensions>
 	</Project>

BIN
hw/cpu.dch


BIN
hw/disp-in4x6.dch


+ 6 - 1
inc/common.h

@@ -5,8 +5,13 @@
 /**
  * Global defines
  */
-//#define USE_BRIGHT_CONTROL
+#ifndef F_CPU
+  #define F_CPU               16000000
+#endif // F_CPU
+
+#define USE_BRIGHT_CONTROL  1
 //#define USE_DHT
 //#define USE_UART
+//#define LAMP_TEST  1
 
 #endif /* _COMMON_H */

+ 9 - 5
src/i2c.c

@@ -11,15 +11,19 @@ COPYRIGHT (C) 2008-2009 EXTREME ELECTRONICS INDIA
 #include "i2c.h"
 
 /* Private defines */
-// twi_br must be 3
-#define TWI_SPEED       400000UL
-#define TWI_PRESCALER   4
-#define TWI_BR          ((F_CPU / TWI_SPEED) - 16) / (2 * TWI_PRESCALER)
 #define TWPS_1          ((0<<TWPS1) | (0<<TWPS0))
 #define TWPS_4          ((0<<TWPS1) | (1<<TWPS0))
 #define TWPS_16         ((1<<TWPS1) | (0<<TWPS0))
 #define TWPS_64         ((1<<TWPS1) | (1<<TWPS0))
-#define TWI_PS          TWPS_4
+#define TWI_SPEED       400000UL
+#if (F_CPU == 16000000)
+  #define TWI_PRESCALER   4
+  #define TWI_PS          TWPS_4
+#elif (F_CPU == 8000000)
+  #define TWI_PRESCALER   1
+  #define TWI_PS          TWPS_1
+#endif // F_CPU
+#define TWI_BR          ((F_CPU / TWI_SPEED) - 16) / (2 * TWI_PRESCALER)
 
 #define TWI_WDT_TIMEOUT 10
 

+ 49 - 9
src/main.c

@@ -18,14 +18,19 @@
 #include "ds3231.h"
 #include "rtos.h"
 #include "event-system.h"
-#include "common.h"
 #include "main.h"
+#include "common.h"
 
 /* Defines */
 /* Timer2 settings */
-#define TIMER2_HZ         400
-#define TIMER2_PRESCALER  1024
-#define TIMER2_CS         (1<<CS22 | 1<<CS21 | 1<<CS20)
+#define TIMER2_HZ         600
+#if F_CPU == 16000000
+  #define TIMER2_PRESCALER  256
+  #define TIMER2_CS         (1<<CS22 | 0<<CS21 | 0<<CS20)
+#elif F_CPU == 8000000
+  #define TIMER2_PRESCALER  64
+  #define TIMER2_CS         (0<<CS22 | 1<<CS21 | 1<<CS20)
+#endif // F_CPU
 #define TIMER2_CNT        (0x100 - (F_CPU / TIMER2_PRESCALER / TIMER2_HZ))
 
 /* Display timeout, sec */
@@ -41,12 +46,19 @@
 #endif // USE_UART
 
 #ifdef USE_BRIGHT_CONTROL
+//static void startADC(void);
+//volatile uint8_t resultADC = 0;
 /* Lamp brightness */
 #define BRIGHT_IDX_MAX    4
+#define LIGHT_LEVEL       100
 #define FULL_BRIGHT_ON    0x06
 #define FULL_BRIGHT_OFF   0x22
 static const uint8_t PROGMEM brightConv[BRIGHT_IDX_MAX+1] = {
-  218, 225, 230, 240, 255
+#if F_CPU == 16000000
+  155, 164, 181, 211, 255
+#elif F_CPU == 8000000
+	54, 71, 106, 165, 255
+#endif // F_CPU
 };
 #endif // USE_BRIGHT_CONTROL
 
@@ -129,9 +141,12 @@ void main(void) {
   /* Initialize Event State Machine */
   ES_Init(stShowTime);
 
-  showTime();
-
   RTOS_SetTask(btnProcess, 3, BTN_SCAN_PERIOD);
+#ifdef USE_BRIGHT_CONTROL
+//  RTOS_SetTask(startADC, 10, 500);
+#endif // USE_BRIGHT_CONTROL
+
+  showTime();
 
   /** main loop */
   do {
@@ -150,6 +165,7 @@ void main(void) {
         }
 #ifdef USE_BRIGHT_CONTROL
         if (RTC.Hr >= FULL_BRIGHT_ON && RTC.Hr < FULL_BRIGHT_OFF) {
+//        if (resultADC < LIGHT_LEVEL) {
           OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
         } else {
           OCR2 = pgm_read_byte(&brightConv[brightIdx]);
@@ -210,6 +226,10 @@ static void Board_Init(void) {
   TIMSK = _BV(TOIE2);
 
 #ifdef USE_BRIGHT_CONTROL
+  /* ADC init */
+//  ADMUX = (1<<REFS0 | 1<<ADLAR | 1<<MUX2 | 1<<MUX1 | 1<<MUX0); // Vref = AVcc, channel ADC7, Left adjusted result
+//  ADCSRA = (1<<ADEN | 1<<ADSC | 1<<ADIE | 1<<ADPS2 | 1<<ADPS1 | 1<<ADPS0); // enable ADC, prescaler = 128
+
   OCR2 = pgm_read_byte(&brightConv[BRIGHT_IDX_MAX]);
   TIMSK |= _BV(OCIE2);
 #endif // USE_BRIGHT_CONTROL
@@ -309,6 +329,14 @@ static void btnProcess(void) {
 }
 
 void showTime(void) {
+/*
+  Digit[0] = DIGIT_BLANK;
+  Digit[1] = DIGIT_BLANK;
+  Digit[2] = DIGIT_BLANK;
+  Digit[3] = resultADC / 100;
+  Digit[4] = (resultADC % 100) / 10;
+  Digit[5] = resultADC % 10;
+*/
   dotOn();
   RTOS_SetTask(dotOff, 500, 0);
 
@@ -352,8 +380,10 @@ void showBright(void) {
 
   Digit[0] = DIGIT_BLANK;
   Digit[1] = DIGIT_BLANK;
-  Digit[2] = brightIdx;
-  Digit[3] = DIGIT_BLANK;
+  Digit[2] = DIGIT_BLANK;
+  Digit[3] = brightIdx;
+  Digit[4] = DIGIT_BLANK;
+  Digit[5] = DIGIT_BLANK;
 }
 
 void incBright(void) {
@@ -371,6 +401,11 @@ void decBright(void) {
     Flag.saveEEP = 1;
   }
 }
+/*
+static void startADC(void) {
+  // enable interrupt and start conversion
+  ADCSRA |= ((1<<ADSC) | (1<<ADIE));
+}*/
 #endif // USE_BRIGHT_CONTROL
 
 static void blink(void) {
@@ -809,6 +844,11 @@ ISR(TIMER2_COMP_vect) {
   PORTD &= ~ANODD_PINS;
   PORTC &= ~DIGIT_PINS;
 }
+/*
+ISR(ADC_vect) {
+  resultADC = ADCH;
+  ADCSRA &= ~(1<<ADIE); // disable interrupt
+}*/
 #endif // USE_BRIGHT_CONTROL
 
 /**