|
|
@ -2,9 +2,9 @@ |
|
|
|
#include <avr/pgmspace.h> // storing data in program memory |
|
|
|
#include <avr/pgmspace.h> // storing data in program memory |
|
|
|
#include <avr/interrupt.h> // interrupt vectors |
|
|
|
#include <avr/interrupt.h> // interrupt vectors |
|
|
|
#include <util/delay.h> // delay functions |
|
|
|
#include <util/delay.h> // delay functions |
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h> // C header for int types like uint8_t |
|
|
|
#include <stdint.h> // C header for int types like uint8_t |
|
|
|
#include <stdbool.h> // C header for the bool type |
|
|
|
#include <stdbool.h> // C header for the bool type |
|
|
|
|
|
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
|
|
|
|
// Include stuff from the library
|
|
|
|
// Include stuff from the library
|
|
|
|
#include "lib/iopins.h" |
|
|
|
#include "lib/iopins.h" |
|
|
@ -14,6 +14,9 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "pinout.h" |
|
|
|
#include "pinout.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Configure pins |
|
|
|
|
|
|
|
*/ |
|
|
|
void setup_io(void) |
|
|
|
void setup_io(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
as_output(PIN_DISP_CP); |
|
|
|
as_output(PIN_DISP_CP); |
|
|
@ -35,35 +38,56 @@ void setup_io(void) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// --- LED display brightness control ---
|
|
|
|
// --- LED display brightness control ---
|
|
|
|
#define DISP_BRIGHTNESS OCR2B |
|
|
|
volatile uint8_t disp_brightness; |
|
|
|
|
|
|
|
#define LIGHT_ADC_CHANNEL 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* PWM for LED display dimming |
|
|
|
|
|
|
|
*/ |
|
|
|
void setup_pwm(void) |
|
|
|
void setup_pwm(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// PWM for LED display dimming
|
|
|
|
OCR2B = disp_brightness = 0xFF; |
|
|
|
TCCR2A |= (1 << WGM20) | (1 << WGM21) | (1 << COM2B1); |
|
|
|
TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2B1); |
|
|
|
TCCR2B |= (1 << CS20); |
|
|
|
TIMSK2 |= _BV(TOIE2); |
|
|
|
|
|
|
|
TCCR2B |= _BV(CS20); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
adc_start_conversion(LIGHT_ADC_CHANNEL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** ISR that writes the PWM register - to avoid glitches */ |
|
|
|
|
|
|
|
ISR(TIMER2_OVF_vect) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// convert in background
|
|
|
|
|
|
|
|
if (adc_ready()) { |
|
|
|
|
|
|
|
disp_brightness = 255 - adc_read_8bit(); |
|
|
|
|
|
|
|
adc_start_conversion(LIGHT_ADC_CHANNEL); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
DISP_BRIGHTNESS = 0x7F; |
|
|
|
OCR2B = disp_brightness; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Let's gooo |
|
|
|
|
|
|
|
*/ |
|
|
|
void main() |
|
|
|
void main() |
|
|
|
{ |
|
|
|
{ |
|
|
|
usart_init(BAUD_115200); |
|
|
|
usart_init(BAUD_115200); |
|
|
|
usart_isr_rx_enable(true); // enable RX interrupt handler
|
|
|
|
//usart_isr_rx_enable(true); // enable RX interrupt handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
adc_init(ADC_PRESC_128); |
|
|
|
setup_io(); |
|
|
|
setup_io(); |
|
|
|
setup_pwm(); |
|
|
|
setup_pwm(); |
|
|
|
adc_init(ADC_PRESC_128); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SPI conf
|
|
|
|
// SPI conf
|
|
|
|
spi_init_master(SPI_LSB_FIRST, |
|
|
|
// TODO verify the cpha and cpol. those seem to work, but it's a guess
|
|
|
|
CPOL_1, CPHA_0, |
|
|
|
spi_init_master(SPI_LSB_FIRST, CPOL_1, CPHA_0, SPI_DIV_2); |
|
|
|
SPI_DIV_2); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// globally enable interrupts
|
|
|
|
// globally enable interrupts
|
|
|
|
sei(); |
|
|
|
sei(); |
|
|
|
|
|
|
|
|
|
|
|
uint8_t cnt = 0; |
|
|
|
uint8_t cnt = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char buf[100]; |
|
|
|
while (1) { |
|
|
|
while (1) { |
|
|
|
pin_down(PIN_DISP_STR); |
|
|
|
pin_down(PIN_DISP_STR); |
|
|
|
spi_send(cnt); |
|
|
|
spi_send(cnt); |
|
|
@ -73,15 +97,7 @@ void main() |
|
|
|
|
|
|
|
|
|
|
|
_delay_ms(100); |
|
|
|
_delay_ms(100); |
|
|
|
|
|
|
|
|
|
|
|
// Brightness directly proportional to light level
|
|
|
|
sprintf(buf, "%d\n", disp_brightness); |
|
|
|
DISP_BRIGHTNESS = 255 - adc_read_byte(6); |
|
|
|
usart_puts(buf); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// UART receive handler
|
|
|
|
|
|
|
|
ISR(USART_RX_vect) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// "ECHO" function:
|
|
|
|
|
|
|
|
uint8_t b = usart_rx(); |
|
|
|
|
|
|
|
usart_tx(b); // send back
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|