Академический Документы
Профессиональный Документы
Культура Документы
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <string.h>
#include "xitoa.h"
/*------------------------------------------------*/
/* Global variables */
uint32_t Gain[2];
uint16_t Ofs20;
/*------------------------------------------------*/
/* Interval Timer (800Hz) */
SIGNAL(SIG_OVERFLOW1)
{
static uint8_t bltimer; // LED blink timer
static uint8_t Col; // Dynamic scan pointer
bltimer++;
if((bltimer & 7) == 0) { // Check power button every 8 (100/sec)
PORTA |= _BV(3);
DDRA &= ~_BV(3);
DDRA &= ~_BV(3);
DDRA &= ~_BV(3);
if(PINA & _BV(3)) {
flags |= F_PB;
} else {
if(flags & F_PB)
flags |= F_POW;
flags &= ~F_PB;
}
PORTA &= 0b00000100;
DDRA |= _BV(3);
}
if((bltimer & 0xC0) || !(flags & F_LVD)) // Blink control
PORTB = Leds[Col];
PORTA |= (0b00001000 << Col);
flags |= F_800;
}
void putled(char c)
{
static uint8_t wp; // Write index
static const prog_uint8_t seg7[] // Segment pattern
= {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B, 0};
/*------------------------------------------------*/
/* Main Process */
int main(void)
{
uint16_t n, d;
uint32_t v, u;
/* Initialize ports */
PORTA = 0b00000100;
DDRA = 0b11111110;
PORTB = 0b00000101;
DDRB = 0b01111111;
eeprom_read_block(Gain, 0, sizeof(Gain));
sei();
/* Lamp test */
memset(Leds, 0x7F, sizeof(Leds));
delay(400);
delay(10);
Ofs20 = adconv(0x80 + 13);
delay(10);
cli();
PORTA &= 0b00000100;
PORTB = 0b00000101;
DDRB = 0b01111010;
DDRB = 0b01111010;
DDRB = 0b01111010;
/* MOSI is tied to GND. Low range calibration (1250 lux at -100 mV) */
if((PINB & _BV(0)) == 0) {
adconv(0x80 + 11);
Gain[0] = 1250 * 65536 / (uint32_t)(adconv(0x80 + 11) - Ofs20);
eeprom_write_block(Gain, 0, sizeof(Gain));
Leds[0] = 0;
}
DDRB = 0b01111111;
sei();
/* Power off */
memset(Leds, 0, sizeof(Leds)); // Clear display
PORTA &= ~_BV(2); // Release power hold
for(;;);
}