From e22453c44e0b88f47cd16f6dd1a769bd4ff90203 Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Sat, 20 May 2017 19:08:44 +0200 Subject: First prototype version: Just shift register usage. --- Makefile | 6 +++ main.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 Makefile create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e8d2720 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +all: + avr-gcc -Wall -mmcu=attiny85 -O3 -o main.elf main.c + avr-objcopy -j .text -j .data -O ihex main.elf main.hex + +program: all + avrdude -B100 -p t85 -c avrisp2 -P usb -U flash:w:main.hex diff --git a/main.c b/main.c new file mode 100644 index 0000000..7906126 --- /dev/null +++ b/main.c @@ -0,0 +1,164 @@ +#include // (1) +#include + +#ifndef F_CPU +#warning "F_CPU war noch nicht definiert, wird nun mit 3686400 definiert" +// #define F_CPU 3686400UL /* Quarz mit 3.6864 Mhz */ +#define F_CPU 1000000UL /* Quarz mit 3.6864 Mhz */ +#endif + +#include +#include + + +// set bit +static inline void BIT_SET(volatile uint8_t *target, uint8_t bit) __attribute__((always_inline)); +static inline void BIT_SET(volatile uint8_t *target, uint8_t bit){ + *target |= (1<SPI Blocksende Routine mit optimierungen auf Speed. +// * \param buffer Zeiger auf den Puffer der gesendet werden soll. +// * \param Datalenght Anzahl der Bytes die gesedet werden soll. +// */ +///* -----------------------------------------------------------------------------------------------------------*/ +//void SPI_FastMem2Write( unsigned char * buffer, unsigned int Datalenght ) +//{ +// unsigned int Counter = 0; +// unsigned char data; +// +// // erten Wert senden +// SPDR = buffer[ Counter++ ]; +// while( Counter < Datalenght ) +// { +// // Wert schon mal in Register holen, schneller da der Wert jetzt in einem Register steht und nicht mehr aus dem RAM geholt werden muss +// // nachdem das senden des vorherigen Wertes fertig ist, +// data = buffer[ Counter ]; +// // warten auf fertig +// while(!(SPSR & (1<>i); + + /* + + char data = 1; + for(; data<128; data++) + { + // clear register (reset) + print_data(data); + _delay_ms(1000); + } + + */ + + // spi_transfer(0x10101010b); + + + } + return 0; +} -- cgit v1.2.3 From ff7774def5479996e2acb77ddd942c036134453d Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Sat, 20 May 2017 20:59:52 +0200 Subject: Fix number format, coding style and improved test program --- Makefile | 4 +- main.c | 146 ++++++++++++--------------------------------------------------- 2 files changed, 29 insertions(+), 121 deletions(-) diff --git a/Makefile b/Makefile index e8d2720..e63d2c0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ +FLAGS=-Wall -I/usr/lib/gcc/avr/6.3.0/plugin/include/ -I/usr/avr/include -mmcu=attiny13 -O3 -DF_CPU=1200000 + all: - avr-gcc -Wall -mmcu=attiny85 -O3 -o main.elf main.c + avr-gcc $(FLAGS) -o main.elf main.c avr-objcopy -j .text -j .data -O ihex main.elf main.hex program: all diff --git a/main.c b/main.c index 7906126..2b5753d 100644 --- a/main.c +++ b/main.c @@ -1,164 +1,70 @@ -#include // (1) +// vim:set et sw=2 ts=2 tw=120: +#define __AVR_ATtiny85__ #include +#include // (1) #ifndef F_CPU #warning "F_CPU war noch nicht definiert, wird nun mit 3686400 definiert" -// #define F_CPU 3686400UL /* Quarz mit 3.6864 Mhz */ -#define F_CPU 1000000UL /* Quarz mit 3.6864 Mhz */ +#define F_CPU 1000000UL #endif -#include #include - +#include // set bit -static inline void BIT_SET(volatile uint8_t *target, uint8_t bit) __attribute__((always_inline)); -static inline void BIT_SET(volatile uint8_t *target, uint8_t bit){ - *target |= (1<SPI Blocksende Routine mit optimierungen auf Speed. -// * \param buffer Zeiger auf den Puffer der gesendet werden soll. -// * \param Datalenght Anzahl der Bytes die gesedet werden soll. -// */ -///* -----------------------------------------------------------------------------------------------------------*/ -//void SPI_FastMem2Write( unsigned char * buffer, unsigned int Datalenght ) -//{ -// unsigned int Counter = 0; -// unsigned char data; -// -// // erten Wert senden -// SPDR = buffer[ Counter++ ]; -// while( Counter < Datalenght ) -// { -// // Wert schon mal in Register holen, schneller da der Wert jetzt in einem Register steht und nicht mehr aus dem RAM geholt werden muss -// // nachdem das senden des vorherigen Wertes fertig ist, -// data = buffer[ Counter ]; -// // warten auf fertig -// while(!(SPSR & (1<>i); - - /* - - char data = 1; - for(; data<128; data++) + uint16_t data = 1; + for (; data < UINT16_MAX; data++) { // clear register (reset) - print_data(data); - _delay_ms(1000); + print_data(data | data << 6); } - */ - - // spi_transfer(0x10101010b); - - + _delay_ms(1000); // Eine Sekunde +/-1/10000 Sekunde warten... } return 0; } -- cgit v1.2.3 From e8c784a9502bbaa8d3c47b2fc60884311632d968 Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Thu, 25 May 2017 02:40:18 +0200 Subject: Simple counter (in seconds) --- .clang-format | 5 +++ .ycm_extra_conf.py | 26 ++++++++++++ Makefile | 17 ++++++-- main.c | 118 +++++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 136 insertions(+), 30 deletions(-) create mode 100644 .clang-format create mode 100644 .ycm_extra_conf.py diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b8ecd2e --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +BasedOnStyle: Webkit +IndentWidth: 2 +BreakBeforeBraces: Allman +AllowShortIfStatementsOnASingleLine: false +ColumnLimit: 120 diff --git a/.ycm_extra_conf.py b/.ycm_extra_conf.py new file mode 100644 index 0000000..42834fb --- /dev/null +++ b/.ycm_extra_conf.py @@ -0,0 +1,26 @@ +import os +import ycm_core + +def FlagsForFile( filename, **kwargs ): + return { + 'flags': [ + '-Wall', + '-Wextra', + '-Werror', + '-Wno-long-long', + '-Wno-variadic-macros', + '-fexceptions', + '-ferror-limit=10000', + '-DNDEBUG', + '-std=c99', + '-x', 'c', + '-D_GNU_SOURCE', + '-D__AVR_ATtiny85__', + '-D__AVR__', + '-I.', + '-I', '/usr/avr/include/' + ], + 'do_cache': True, + } + +# vim:set et sw=2 ts=2 tw=120: diff --git a/Makefile b/Makefile index e63d2c0..e81f5a1 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,17 @@ -FLAGS=-Wall -I/usr/lib/gcc/avr/6.3.0/plugin/include/ -I/usr/avr/include -mmcu=attiny13 -O3 -DF_CPU=1200000 +FLAGS=-Wall -O5 -Werror +FLAGS+=-mmcu=attiny85 +FLAGS+=-D__AVR_ATtiny85__=1 +# FLAGS+=-DF_CPU=120000UL +# FLAGS+=-DF_CPU=960000UL +FLAGS+=-DF_CPU=800000UL +FLAGS+=-I inc/int/ +FLAGS+=-I/usr/lib/gcc/avr/6.3.0/plugin/include/ +FLAGS+=-I/usr/avr/include + +program: all + avrdude -V -B100 -p t85 -c avrisp2 -P usb -U flash:w:main.hex all: - avr-gcc $(FLAGS) -o main.elf main.c + avr-gcc $(FLAGS) -o main.elf main.c inc/int/dcf77.c avr-objcopy -j .text -j .data -O ihex main.elf main.hex -program: all - avrdude -B100 -p t85 -c avrisp2 -P usb -U flash:w:main.hex diff --git a/main.c b/main.c index 2b5753d..2dd8058 100644 --- a/main.c +++ b/main.c @@ -1,15 +1,12 @@ // vim:set et sw=2 ts=2 tw=120: -#define __AVR_ATtiny85__ +#include +#include #include -#include // (1) +#include -#ifndef F_CPU -#warning "F_CPU war noch nicht definiert, wird nun mit 3686400 definiert" -#define F_CPU 1000000UL -#endif +#include "inc/int/dcf77.h" -#include -#include +#define CTC_MATCH_OVERFLOW ((F_CPU / 1000) / 8) // set bit static inline void BIT_SET(volatile uint8_t* target, uint8_t bit) __attribute__((always_inline)); @@ -19,52 +16,121 @@ static inline void BIT_SET(volatile uint8_t* target, uint8_t bit) { *target |= ( static inline void BIT_CLEAR(volatile uint8_t* target, uint8_t bit) __attribute__((always_inline)); static inline void BIT_CLEAR(volatile uint8_t* target, uint8_t bit) { *target &= ~(1 << bit); }; + +/* + * + * PB2 : connected to ShiftClock on Port 11 + * PB3 : connected to MemoryClock on Port 12 (shows content of memory) + * PB4 (PIN3) : connected to SERIAL_IN on pin 14 + * + */ + void pulse_sck() { PORTB |= (1 << PB2); - _delay_ms(10); // Eine Sekunde +/-1/10000 Sekunde warten... PORTB ^= (1 << PB2); + } void print_data(uint16_t data) { - BIT_CLEAR(&PORTB, PB4); - _delay_ms(50); - BIT_SET(&PORTB, PB4); + //BIT_CLEAR(&PORTB, PB4); + //BIT_SET(&PORTB, PB4); for (uint16_t i = 0; i < 16; i++) { - data & 0x1 << i ? BIT_SET(&PORTB, PB4) : BIT_CLEAR(&PORTB, PB4); + data & 0x1 << i + ? BIT_SET(&PORTB, PB4) + : BIT_CLEAR(&PORTB, PB4); pulse_sck(); } - + //BIT_CLEAR(&PORTB, PB4); + // pulse_sck(); BIT_CLEAR(&PORTB, PB3); - _delay_ms(50); BIT_SET(&PORTB, PB3); + } int main(void) { - DDRB = 0xFF; BIT_CLEAR(&PORTB, PB5); - _delay_ms(200); + _delay_ms(20); BIT_SET(&PORTB, PB5); BIT_CLEAR(&PORTB, PB5); - _delay_ms(200); + _delay_ms(20); BIT_SET(&PORTB, PB5); + + + // make all pins output pins... + DDRB = 0xFF; + DDRB &= ~(1 << PB0); // makes PB0 an input pin (INT0) + PORTB |= (1 << PB0); // activate input resistor + + // activate dcf77-receiver... + PORTB &= (1 << PB1); // sets PB1 to low + _delay_ms(20); + PORTB &= ~(1 << PB1); // sets PB1 to low + + + cli(); + + /* + GIMSK |= (1<= 1000) // one second + { + t_current.ms = 0; // restart + if (++t_current.s > 59) // one minute + { + t_current.s = 0; // restart + if (++t_current.m > 59) // one hour + { + t_current.m = 0; // restart + if (++t_current.h > 23) // one day + { + t_current.h = 0; // restart + t_current.dd++; + } + } + } + } +} + +ISR(INT0_vect) +{ +} + +ISR(PCINT0_vect) +{ + interval = 1; + /// stdout_put_int(interval, uart_putc); + /// stdout_put_string("INT0 call\n", uart_putc); + // cli(); + + // if(INT0_CONTROL == INT0_RISING_EDGE) + // if(MCUCR & (1< 1000 && interval < 2000) // 59th second - no rising edge at the beginning + { + /// if(dcf77_bit == 59) // check if every bit has been transfered + // set_dcf_value(); // completely received- apply new time data + + // 59th second: synchronize with receiver... + TCNT0 = 0; + dcf77_bit = 0; + t_current.s = 0; + t_current.ms = 0; + // t_current.us = 0; + // falling edge causes interrupt... + // MCUCR &= ~(1 << ISC00); + // MCUCR |= (1 << ISC01); + // INT0_CONTROL = INT0_FALLING_EDGE; + } + else // INT0_FALLING_EDGE + { + dcf77[dcf77_bit++].bit = interval >= 200 ? 1 : 0; + + // rising edge causes interrupt... + // MCUCR |= (1 << ISC00); + // MCUCR |= (1 << ISC01); + // INT0_CONTROL = INT0_RISING_EDGE; + // interval = 0; // reset interval to count next interval + } + + interval = 0; + // sei(); +} + +/* +void timer_init() +{ + // 7372800/8 + // >>>>>> DDRD &= ~((1< + #include + + #include + + #define INT0_CONTROL MCUCR + // #define INT0_FALLING_EDGE 0x02 + // #define INT0_RISING_EDGE 0x03 + #define INT0_FALLING_EDGE (1< 1000 && interval < 2000) // 59th second - no rising edge at the beginning { - /// if(dcf77_bit == 59) // check if every bit has been transfered - // set_dcf_value(); // completely received- apply new time data + if(dcf77_bit == 59) // check if every bit has been transfered + { + t_current.m = getBits(DCF_MIN, 7); + t_current.h = getBits(DCF_HOUR, 6); + t_current.dd = getBits(DCF_DAY, 6); + t_current.wd = getBits(DCF_WEEKDAY, 3); + t_current.mm = getBits(DCF_MONTH, 5); + t_current.yy = getBits(DCF_YEAR, 8); + } // 59th second: synchronize with receiver... - TCNT0 = 0; dcf77_bit = 0; t_current.s = 0; t_current.ms = 0; - // t_current.us = 0; + // falling edge causes interrupt... - // MCUCR &= ~(1 << ISC00); - // MCUCR |= (1 << ISC01); // INT0_CONTROL = INT0_FALLING_EDGE; } else // INT0_FALLING_EDGE { - dcf77[dcf77_bit++].bit = interval >= 200 ? 1 : 0; + if(interval > 50 && interval < 150) + dcf77[dcf77_bit++].bit = 0; + else if(interval > 150 && interval < 250) + dcf77[dcf77_bit++].bit = 1; // rising edge causes interrupt... - // MCUCR |= (1 << ISC00); - // MCUCR |= (1 << ISC01); // INT0_CONTROL = INT0_RISING_EDGE; - // interval = 0; // reset interval to count next interval } + TCNT0 = 0; interval = 0; - // sei(); } /* diff --git a/inc/int/dcf77.h b/inc/int/dcf77.h index e47ca4e..2d79cdd 100755 --- a/inc/int/dcf77.h +++ b/inc/int/dcf77.h @@ -9,7 +9,6 @@ */ #include #include - #include #define INT0_CONTROL MCUCR @@ -26,14 +25,14 @@ typedef struct { - uint16_t h ; // hour - uint16_t m ; // minute - uint16_t s ; // second + uint16_t h; // hour + uint16_t m; // minute + uint16_t s; // second uint16_t ms; // milli second - uint8_t yy; // year - uint8_t mm; // month - uint8_t dd; // day - uint8_t wd; // weekday + uint8_t yy; // year + uint8_t mm; // month + uint8_t dd; // day + uint8_t wd; // weekday } Time; typedef enum diff --git a/main.c b/main.c index 5c8d616..6e86c36 100644 --- a/main.c +++ b/main.c @@ -84,7 +84,7 @@ int main(void) // OCR0A |= (1 << WGM01); // CTC mode // OCR0A = 149; - OCR0A = 124; + OCR0A = 125; // OCR0A = CTC_MATCH_OVERFLOW; TIMSK |= (1 << OCIE0A); // if you want interrupt @@ -92,27 +92,25 @@ int main(void) while (1) { - // uint16_t t = 1; - // for (uint16_t i=0; i<16; i++) - // { - // print_data(t_current.s); - // uint16_t p = 0; - // for(int i=0; i