clear unnecessary files
clock now roughly works (no Timezone support) also setting led brightness via encoder works (every 2nd step works, overflow at FF)
This commit is contained in:
@@ -3,17 +3,18 @@
|
||||
#include <avr/io.h>
|
||||
#include "i2cmaster.h"
|
||||
|
||||
uint32_t DS3231_read(){
|
||||
void DS3231_read(uint8_t dest[3]){
|
||||
// set address to 0
|
||||
i2c_start_wait(0xD0);
|
||||
i2c_write(0x00);
|
||||
i2c_stop();
|
||||
|
||||
// read addresses 0, 1 and 2
|
||||
i2c_start_wait(0xD1);
|
||||
uint8_t sec = i2c_readAck();
|
||||
uint8_t min = i2c_readAck();
|
||||
uint8_t std = i2c_readNak();
|
||||
dest[0] = i2c_readAck();
|
||||
dest[1] = i2c_readAck();
|
||||
dest[2] = i2c_readNak();
|
||||
i2c_stop();
|
||||
return ((uint32_t)std<<16) | ((uint32_t)min<<8) | sec;
|
||||
}
|
||||
|
||||
void DS3231_write(uint8_t inbuffer[3]){
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
#include "adc.h"
|
||||
#include "pinout.h"
|
||||
#include <avr/io.h>
|
||||
|
||||
void adc_setup(void){
|
||||
ADMUX = (1 << REFS0); //Versorgungsspannung definieren
|
||||
ADCSRA|= (1 << ADEN) //enable bit für adc
|
||||
| (1 << ADPS0) //setze Prescaler für Abtastfrequenz
|
||||
| (1 << ADPS1) //setze Prescaler für Abtastfrequenz
|
||||
| (0 << ADPS2); //setze Prescaler für Abtastfrequenz
|
||||
}
|
||||
|
||||
|
||||
|
||||
int adc_read(unsigned char adc_pin) {
|
||||
ADMUX &= 0b11110000; //Alte Port-Select Flags löschen
|
||||
ADMUX|=adc_pin; //Neuen Eingang setzen
|
||||
ADCSRA |= (1 << ADSC); // eine Wandlung "single conversion"
|
||||
while (ADCSRA & (1 << ADSC) ) { // auf Abschluss der Konvertierung warten
|
||||
}
|
||||
int ADCresult = ADCL;
|
||||
ADCresult += (ADCH << 8);
|
||||
return ADCresult;
|
||||
}
|
||||
|
||||
288
code/src/main.c
288
code/src/main.c
@@ -1,44 +1,56 @@
|
||||
//#define F_CPU 16000000L
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h> //interrupts
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "pinout.h"
|
||||
#include "print.h"
|
||||
#include "adc.h"
|
||||
#include "uart.h"
|
||||
#include "i2cmaster.h"
|
||||
#include "DS3231.h"
|
||||
|
||||
#define RX_INPUT_BUFFER_SIZE 92
|
||||
|
||||
/*README
|
||||
DIP1 aktiviert Röhren-Schaltregler
|
||||
*/
|
||||
|
||||
volatile int counter = 0;
|
||||
//char text[22] = {'F','R','o','h','e',' ',' ','W','e','i','h','n','a','c','h','t','e', 'n', ' ',' ', ' ', ' '};
|
||||
char text[18] = {'1','2','3','4','5','6','7','8','9','t','s','c','a','f','e',' ', ' ', ' '};
|
||||
int offset = 0;
|
||||
int animationtimer = 0;
|
||||
int stringlength = 22;
|
||||
int duty = 125;
|
||||
|
||||
|
||||
void check_dip_switches(void);
|
||||
void check_buttons(void);
|
||||
void setup(void);
|
||||
void set_PWM_duty(int dutycycle);
|
||||
|
||||
uint8_t parse_nmea_gps(char input_string[RX_INPUT_BUFFER_SIZE], uint8_t output_buffer[3]){
|
||||
unsigned char SR_Buffer[6];
|
||||
const unsigned char number_Font[10] = {
|
||||
0b11011110, //0
|
||||
0b00000110, //1
|
||||
0b11101010, //2
|
||||
0b01101110, //3
|
||||
0b00110110, //4
|
||||
0b01111100, //5
|
||||
0b11111100, //6
|
||||
0b00001110, //7
|
||||
0b11111110, //8
|
||||
0b01111110 //9
|
||||
};
|
||||
|
||||
void clear_SR_Buffer(void) {
|
||||
for (uint8_t i = 0; i < 6; i++) {
|
||||
SR_Buffer[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void print_SR_Buffer(void) {
|
||||
PORTC &= ~(LATCH_SR);
|
||||
|
||||
for (uint8_t i = 0; i < 6; i++) {
|
||||
SPDR = SR_Buffer[i];
|
||||
while (!(SPSR & (1 << SPIF))); //warten auf transferabschluss
|
||||
}
|
||||
PORTC |= LATCH_SR;
|
||||
}
|
||||
|
||||
int8_t parse_nmea_gps(char input_string[RX_INPUT_BUFFER_SIZE], uint8_t output_buffer[3]){
|
||||
char* token = strsep(&input_string, ",");
|
||||
if(!strcmp("$GPRMC", token)){
|
||||
char* time = strsep(&input_string, ",");
|
||||
if(!strlen(time)){
|
||||
// no valid time available
|
||||
return -1;
|
||||
return -2;
|
||||
}
|
||||
|
||||
// craft bcd values
|
||||
@@ -53,199 +65,129 @@ uint8_t parse_nmea_gps(char input_string[RX_INPUT_BUFFER_SIZE], uint8_t output_b
|
||||
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
setup();
|
||||
setup();
|
||||
|
||||
while(1){
|
||||
_delay_ms(500);
|
||||
|
||||
check_dip_switches();
|
||||
check_buttons();
|
||||
while(1){
|
||||
_delay_ms(200);
|
||||
|
||||
uint32_t clock = DS3231_read();
|
||||
for(uint8_t i=0; i < 6; i++){
|
||||
SR_Buffer[5-i] = number_Font[clock & 0x0F];
|
||||
clock = clock >> 4;
|
||||
check_dip_switches();
|
||||
|
||||
uint8_t clock[3];
|
||||
DS3231_read(clock);
|
||||
for(uint8_t i=0; i < 3; i++){
|
||||
SR_Buffer[5-(2*i)] = number_Font[clock[i] & 0x0F];
|
||||
SR_Buffer[5-(2*i)-1] = number_Font[(clock[i] & 0xF0)>>4];
|
||||
}
|
||||
print_SR_Buffer();
|
||||
}
|
||||
print_SR_Buffer();
|
||||
}
|
||||
}
|
||||
|
||||
void check_dip_switches() {
|
||||
if (PINB & DIP_1) {
|
||||
PORTC |= ENABLE_TUBE_PSU;
|
||||
PORTA &= ~ENABLE_TUBE_SUPPLY;
|
||||
// PORTB |= STATUS_LED_B;
|
||||
} else {
|
||||
PORTC &= ~(ENABLE_TUBE_PSU);
|
||||
PORTA |= (ENABLE_TUBE_SUPPLY);
|
||||
// PORTB &= ~STATUS_LED_B;
|
||||
}
|
||||
if (PINB & DIP_1) {
|
||||
PORTC |= ENABLE_TUBE_PSU;
|
||||
PORTA &= ~ENABLE_TUBE_SUPPLY;
|
||||
// PORTB |= STATUS_LED_B;
|
||||
} else {
|
||||
PORTC &= ~(ENABLE_TUBE_PSU);
|
||||
PORTA |= (ENABLE_TUBE_SUPPLY);
|
||||
// PORTB &= ~STATUS_LED_B;
|
||||
}
|
||||
}
|
||||
|
||||
void check_buttons(){
|
||||
if((PINC & BUTTON_A)){
|
||||
// PORTB &= ~STATUS_LED_C;
|
||||
}
|
||||
};
|
||||
|
||||
void setup(){
|
||||
|
||||
DDRA = ENABLE_TUBE_SUPPLY;
|
||||
|
||||
DDRA = ENABLE_TUBE_SUPPLY;
|
||||
|
||||
DDRB = STATUS_LED_B
|
||||
| STATUS_LED_C
|
||||
| MOSI
|
||||
| CLK;
|
||||
| STATUS_LED_C
|
||||
| MOSI
|
||||
| CLK;
|
||||
|
||||
DDRC = LATCH_SR
|
||||
| ENABLE_SR
|
||||
| DOT
|
||||
| ENABLE_TUBE_PSU;
|
||||
| ENABLE_SR
|
||||
| DOT
|
||||
| ENABLE_TUBE_PSU;
|
||||
|
||||
DDRD = RESET_SR
|
||||
| TUBE_LED;
|
||||
| TUBE_LED;
|
||||
|
||||
|
||||
//--------------------------
|
||||
// SPI
|
||||
//--------------------------
|
||||
//--------------------------
|
||||
// SPI
|
||||
//--------------------------
|
||||
SPCR = (1 << SPE) | //enable SPI
|
||||
(1 << MSTR) | //SPI als Master starten
|
||||
(1 << SPR0) | //clock-prescaler LSB
|
||||
(1 << SPR1) | //clock-prescaler MSB
|
||||
(0 << SPIE) | //complete transfer/ recive interrupt
|
||||
(0 << CPOL) |
|
||||
(0 << CPHA) |
|
||||
(0 << DORD); //MSB first = 0
|
||||
(1 << MSTR) | //SPI als Master starten
|
||||
(1 << SPR0) | //clock-prescaler LSB
|
||||
(1 << SPR1) | //clock-prescaler MSB
|
||||
(0 << SPIE) | //complete transfer/ recive interrupt
|
||||
(0 << CPOL) |
|
||||
(0 << CPHA) |
|
||||
(0 << DORD); //MSB first = 0
|
||||
SPSR = (1 << SPI2X);// | //clock- prescaler double speed
|
||||
|
||||
PORTC &= ~LATCH_SR; //latch low, sonst startet SPI nicht
|
||||
PORTC |= LATCH_SR;
|
||||
PORTD |= RESET_SR; //Setzte Reset auf HIGH (active LOW)
|
||||
PORTC &= ~(ENABLE_SR); //Aktiviert Shiftregister (active LOW)
|
||||
|
||||
|
||||
|
||||
//--------------------------
|
||||
// Externe Interrupts
|
||||
//--------------------------
|
||||
//External Interrupt Mask Register
|
||||
|
||||
EIMSK |= (1 << INT0) | //Aktiviere Interrupt INT0
|
||||
(1 << INT1); //Aktiviere Interrupt INT1
|
||||
|
||||
//External Interrupt Control Register A
|
||||
EICRA |= (0 << ISC10) | //Interrupt bei jedem Pegelwechsel auf INT1
|
||||
(1 << ISC11) |
|
||||
(1 << ISC00) | //Interrupt bei jedem Pegelwechsel auf INT0
|
||||
(1 << ISC01);
|
||||
//Pin Change Interrupt Control Register
|
||||
PCICR |= (1 << PCIE2); //Enabled PCINT16-23 als mögliche Signalquelle für Interrupt
|
||||
|
||||
//Pin Change Mask Register 2
|
||||
//PCMSK2 |= (1 << PCINT18) | //PCINT18 als Signalquelle wählen
|
||||
// (1 << PCINT19); //PCINT19 als Signalquelle wählen
|
||||
|
||||
|
||||
//--------------------------
|
||||
// Timer + Zeitinterrupts
|
||||
//--------------------------
|
||||
//--------------------------
|
||||
// Externe Interrupts
|
||||
//--------------------------
|
||||
//External Interrupt Mask Register
|
||||
|
||||
//PWM-Timer
|
||||
//OC2A set when upcounting, clear when down counting
|
||||
TCCR2A = (1<<COM1A1)
|
||||
| (0<<COM1A0);
|
||||
|
||||
TCCR2B=(1<<WGM20) //Wave form generartion mode
|
||||
|(1<<WGM21) // Wave form generation mode
|
||||
|(1<<COM2A1); //Compare Output Mode
|
||||
|
||||
OCR2A = duty; // Compare value 125
|
||||
|
||||
EIMSK |= (1 << INT0) | //Aktiviere Interrupt INT0
|
||||
(1 << INT1); //Aktiviere Interrupt INT1
|
||||
|
||||
//External Interrupt Control Register A
|
||||
EICRA |= (0 << ISC10) | //Interrupt bei jedem Pegelwechsel auf INT1
|
||||
(1 << ISC11) |
|
||||
(1 << ISC00) | //Interrupt bei jedem Pegelwechsel auf INT0
|
||||
(1 << ISC01);
|
||||
|
||||
|
||||
//--------------------------
|
||||
// Timer + Zeitinterrupts
|
||||
//--------------------------
|
||||
|
||||
//PWM-Timer
|
||||
//OC2A set when upcounting, clear when down counting
|
||||
TCCR2A = (1<<COM2A1)
|
||||
|(0<<COM2A0);
|
||||
|
||||
//Timer/Counter control Register A&B
|
||||
//set normal-Mode and waveform
|
||||
TCCR1A = (0 << WGM01) |
|
||||
(0 << WGM00);
|
||||
TCCR1B = (0 << WGM02);
|
||||
TCCR2A |= (1<<WGM20) //Wave form generartion mode
|
||||
|(0<<WGM21); // Wave form generation mode
|
||||
|
||||
TCCR2B = (1 << CS22)
|
||||
|(0 << CS21)
|
||||
|(0 << CS20);
|
||||
OCR2A = 0; // Compare value 125
|
||||
|
||||
////Prescaler von 1024
|
||||
//TCCR1B |= (1 << CS02) |
|
||||
// (0 << CS01) |
|
||||
// (1 << CS00) ;
|
||||
uart_init();
|
||||
i2c_init();
|
||||
|
||||
|
||||
//Output compare Regsiter (Setzt Zeit bei der Interrupt auslösen soll)
|
||||
OCR1A = (1 << 7) | //MSB
|
||||
(1 << 6) |
|
||||
(1 << 5) |
|
||||
(1 << 4) |
|
||||
(1 << 3) |
|
||||
(1 << 2) |
|
||||
(1 << 1) |
|
||||
(1 << 0) ; //LSB
|
||||
|
||||
//TIMSK1 |= (1 << OCIE0A); //Interruptvector hinzufügen
|
||||
TIMSK1 |= (1 << ICIE1) | //Interrupt Counter Enabled)
|
||||
(1 << TOIE1); //Timer Overflow Enabled
|
||||
|
||||
uart_init();
|
||||
i2c_init();
|
||||
|
||||
sei(); //Enable Interrupts
|
||||
//adc_setup();
|
||||
//i2c_init();
|
||||
//USART_Init(MYUBRR);
|
||||
sei(); //Enable Interrupts
|
||||
clear_SR_Buffer();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void set_PWM_duty(int dutycycle){
|
||||
OCR2A = dutycycle;
|
||||
}
|
||||
|
||||
|
||||
//Timer 1 Interrupt
|
||||
ISR (TIMER1_OVF_vect) // Timer1 ISR
|
||||
{
|
||||
//PORTD ^=TUBE_LED;
|
||||
TCNT1 = 63974; //63974; // for 1 sec at 16 MHz
|
||||
animationtimer ++;
|
||||
if(animationtimer > 10){
|
||||
animationtimer= 0;
|
||||
offset++;
|
||||
PORTB ^= STATUS_LED_B;
|
||||
if(offset > 15){
|
||||
offset = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Encoder rotate
|
||||
ISR(INT0_vect) {
|
||||
if (PIND & ENCODER_B) {
|
||||
// set_PWM_duty(duty--);
|
||||
} else {
|
||||
// set_PWM_duty(duty++);
|
||||
}
|
||||
if (PIND & ENCODER_B) {
|
||||
printf("step-\n\r");
|
||||
OCR2A--;
|
||||
} else {
|
||||
OCR2A++;
|
||||
printf("step+\n\r");
|
||||
}
|
||||
}
|
||||
|
||||
//Encoder Button
|
||||
ISR(INT1_vect) {
|
||||
|
||||
printf("button\n\r");
|
||||
}
|
||||
|
||||
|
||||
ISR(USART0_RX_vect)
|
||||
{
|
||||
static char async_input_buffer[RX_INPUT_BUFFER_SIZE];
|
||||
@@ -259,15 +201,15 @@ ISR(USART0_RX_vect)
|
||||
else if(async_buffer_pointer > 0){
|
||||
async_input_buffer[async_buffer_pointer] = 0; // Null terminate string
|
||||
async_buffer_pointer=0;
|
||||
uint8_t bcd_time[3];
|
||||
|
||||
/* write to RTC if time is valid */
|
||||
if(!parse_nmea_gps(async_input_buffer, bcd_time)){
|
||||
DS3231_write(bcd_time);
|
||||
uint8_t gps_time[3];
|
||||
int8_t err = parse_nmea_gps(async_input_buffer, gps_time);
|
||||
if(!err){
|
||||
PORTB |= STATUS_LED_C;
|
||||
} else {
|
||||
PORTB &= ~STATUS_LED_C;
|
||||
//if(rtc_time[0] != gps_time[0] || rtc_time[1] != gps_time[1] || rtc_time[2] != gps_time[2])
|
||||
DS3231_write(gps_time);
|
||||
}
|
||||
else if(err == -2)
|
||||
PORTB &= ~STATUS_LED_C;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
124
code/src/print.c
124
code/src/print.c
@@ -1,124 +0,0 @@
|
||||
#include "pinout.h"
|
||||
#include "print.h"
|
||||
#include <stdlib.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
const unsigned char number_Font[10] = { 0b11011110, //0
|
||||
0b00000110, //1
|
||||
0b11101010, //2
|
||||
0b01101110, //3
|
||||
0b00110110, //4
|
||||
0b01111100, //5
|
||||
0b11111100, //6
|
||||
0b00001110, //7
|
||||
0b11111110, //8
|
||||
0b01111110 //9
|
||||
};
|
||||
const unsigned char character_Font[26] = {0b10111110, //A
|
||||
0b11110100, //b
|
||||
0b11100000, //c
|
||||
0b11100110, //d
|
||||
0b11111000, //e
|
||||
0b10111000, //F
|
||||
0b11011100, //G
|
||||
0b10110100, //h
|
||||
0b00000100, //i
|
||||
0b01000110, //J
|
||||
0b10111100, //K
|
||||
0b11010000, //L
|
||||
0b10011110, //M
|
||||
0b10100100, //n
|
||||
0b11100100, //o
|
||||
0b10111010, //P
|
||||
0b00111110, //q
|
||||
0b10100000, //r
|
||||
0b01111100, //S
|
||||
0b11110000, //t
|
||||
0b11000100, //u
|
||||
0b11000100, //v
|
||||
0b11010110, //W
|
||||
0b10110110, //X
|
||||
0b01110110, //Y
|
||||
0b11100110 //Z
|
||||
};
|
||||
const unsigned char blank = 0b00000000; //zeigt nichts an
|
||||
const unsigned char minus = 0b00100000; //zeigt '-' an
|
||||
const unsigned char dot = 0b00000001; //aktiviert Dezimalpunkt
|
||||
unsigned char SR_Buffer[6] = {0b11100110,
|
||||
0b11100110,
|
||||
0b11100110,
|
||||
0b11100110,
|
||||
0b11100110,
|
||||
0b11100110};
|
||||
|
||||
|
||||
|
||||
void clear_SR_Buffer() {
|
||||
for (uint8_t i = 0; i < 6; i++) {
|
||||
SR_Buffer[i] = blank;
|
||||
}
|
||||
}
|
||||
|
||||
void add_Decimal_Point_to_SR_Buffer(int pos){
|
||||
SR_Buffer[pos-1] += dot;
|
||||
}
|
||||
|
||||
void print_SR_Buffer() {
|
||||
PORTC &= ~(LATCH_SR);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
SPDR = SR_Buffer[i];
|
||||
//SPDR = 0b11100110;
|
||||
//_delay_us(100);
|
||||
while (!(SPSR & (1 << SPIF))); //warten auf transferabschluss
|
||||
}
|
||||
PORTC |= LATCH_SR;
|
||||
//_delay_ms(1);
|
||||
//PORTC &= ~(LATCH_SR);
|
||||
}
|
||||
|
||||
void print_String(char* string){
|
||||
for(int i = 0; i < 6; i++){
|
||||
add_ASCII_to_SR_Buffer(string[i],i);
|
||||
}
|
||||
print_SR_Buffer();
|
||||
}
|
||||
|
||||
|
||||
void add_ASCII_to_SR_Buffer(unsigned char character, int pos) {
|
||||
int ascii_offset = 0;
|
||||
|
||||
if((character > 46) && (character < 57)){ //Char ist Zahl
|
||||
ascii_offset = 48;
|
||||
SR_Buffer[pos] = number_Font[character - ascii_offset]; //Zieht ASCII-Offset ab, sodass Zahl mit Font Index übereinstimmen
|
||||
}else if (character > 95 && character < 122) { //Buchstabe ist kleiner Buchstabe
|
||||
ascii_offset = 97;
|
||||
SR_Buffer[pos] = character_Font[character - ascii_offset]; //Zieht ASCII-Offset ab, sodass Buchstaben mit Font Index übereinstimmen
|
||||
} else if (character > 64 && character < 91){ //Buchstabe groß Buchstabe
|
||||
ascii_offset = 65;
|
||||
SR_Buffer[pos] = character_Font[character - ascii_offset]; //Zieht ASCII-Offset ab, sodass Buchstaben mit Font Index übereinstimmen
|
||||
} else if (character == 32){ //Charakter ist Blank ' '
|
||||
SR_Buffer[pos] = blank;
|
||||
} else if (character == 45){ //Charakter ist Minus -
|
||||
SR_Buffer[pos] = minus;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void add_Character_to_SR_Buffer(unsigned char character, int pos) {
|
||||
int ascii_offset = 0;
|
||||
|
||||
if (character > 96 && character < 122) { //Buchstabe ist kleiner Buchstabe
|
||||
ascii_offset = 97;
|
||||
SR_Buffer[pos] = character_Font[character - ascii_offset]; //Zieht ASCII-Offset ab, sodass Buchstaben mit Font Index übereinstimmen
|
||||
} else if (character > 65 && character < 91){ //Buchstabe groß Buchstabe
|
||||
ascii_offset = 65;
|
||||
SR_Buffer[pos] = character_Font[character - ascii_offset]; //Zieht ASCII-Offset ab, sodass Buchstaben mit Font Index übereinstimmen
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void add_Number_to_SR_Buffer(int number, int pos) {
|
||||
SR_Buffer[pos-1] = number_Font[number];
|
||||
}
|
||||
Reference in New Issue
Block a user