aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Christian Pohle2017-05-20 20:59:52 +0200
committerMax Christian Pohle2017-05-20 20:59:52 +0200
commitff7774def5479996e2acb77ddd942c036134453d (patch)
tree9905ba010f540ddcae6249649125fd79f7b4ae7e
parente22453c44e0b88f47cd16f6dd1a769bd4ff90203 (diff)
downloadbinwatch-mini-ff7774def5479996e2acb77ddd942c036134453d.tar.bz2
binwatch-mini-ff7774def5479996e2acb77ddd942c036134453d.zip
Fix number format, coding style and improved test program
-rw-r--r--Makefile4
-rw-r--r--main.c146
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 @@
1FLAGS=-Wall -I/usr/lib/gcc/avr/6.3.0/plugin/include/ -I/usr/avr/include -mmcu=attiny13 -O3 -DF_CPU=1200000
2
1all: 3all:
2 avr-gcc -Wall -mmcu=attiny85 -O3 -o main.elf main.c 4 avr-gcc $(FLAGS) -o main.elf main.c
3 avr-objcopy -j .text -j .data -O ihex main.elf main.hex 5 avr-objcopy -j .text -j .data -O ihex main.elf main.hex
4 6
5program: all 7program: all
diff --git a/main.c b/main.c
index 7906126..2b5753d 100644
--- a/main.c
+++ b/main.c
@@ -1,164 +1,70 @@
1#include <avr/io.h> // (1) 1// vim:set et sw=2 ts=2 tw=120:
2#define __AVR_ATtiny85__
2#include <avr/interrupt.h> 3#include <avr/interrupt.h>
4#include <avr/io.h> // (1)
3 5
4#ifndef F_CPU 6#ifndef F_CPU
5#warning "F_CPU war noch nicht definiert, wird nun mit 3686400 definiert" 7#warning "F_CPU war noch nicht definiert, wird nun mit 3686400 definiert"
6// #define F_CPU 3686400UL /* Quarz mit 3.6864 Mhz */ 8#define F_CPU 1000000UL
7#define F_CPU 1000000UL /* Quarz mit 3.6864 Mhz */
8#endif 9#endif
9 10
10#include <util/delay.h>
11#include <stdint.h> 11#include <stdint.h>
12 12#include <util/delay.h>
13 13
14// set bit 14// set bit
15static inline void BIT_SET(volatile uint8_t *target, uint8_t bit) __attribute__((always_inline)); 15static inline void BIT_SET(volatile uint8_t* target, uint8_t bit) __attribute__((always_inline));
16static inline void BIT_SET(volatile uint8_t *target, uint8_t bit){ 16static inline void BIT_SET(volatile uint8_t* target, uint8_t bit) { *target |= (1 << bit); };
17 *target |= (1<<bit);
18};
19 17
20// set clear 18// set clear
21static inline void BIT_CLEAR(volatile uint8_t *target, uint8_t bit) __attribute__((always_inline)); 19static inline void BIT_CLEAR(volatile uint8_t* target, uint8_t bit) __attribute__((always_inline));
22static inline void BIT_CLEAR(volatile uint8_t *target, uint8_t bit){ 20static inline void BIT_CLEAR(volatile uint8_t* target, uint8_t bit) { *target &= ~(1 << bit); };
23 *target &= ~(1<<bit);
24};
25
26///* -----------------------------------------------------------------------------------------------------------*/
27///*! Eine schnelle MEM->SPI Blocksende Routine mit optimierungen auf Speed.
28// * \param buffer Zeiger auf den Puffer der gesendet werden soll.
29// * \param Datalenght Anzahl der Bytes die gesedet werden soll.
30// */
31///* -----------------------------------------------------------------------------------------------------------*/
32//void SPI_FastMem2Write( unsigned char * buffer, unsigned int Datalenght )
33//{
34// unsigned int Counter = 0;
35// unsigned char data;
36//
37// // erten Wert senden
38// SPDR = buffer[ Counter++ ];
39// while( Counter < Datalenght )
40// {
41// // Wert schon mal in Register holen, schneller da der Wert jetzt in einem Register steht und nicht mehr aus dem RAM geholt werden muss
42// // nachdem das senden des vorherigen Wertes fertig ist,
43// data = buffer[ Counter ];
44// // warten auf fertig
45// while(!(SPSR & (1<<SPIF)));
46// // Wert aus Register senden
47// SPDR = data;
48// Counter++;
49// }
50// while(!(SPSR & (1<<SPIF)));
51// return;
52//}
53uint8_t spi_transfer(uint8_t data) {
54 USIDR = data;
55 USISR = _BV(USIOIF); // clear flag
56
57 while ( (USISR & _BV(USIOIF)) == 0 ) {
58 USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
59 }
60 return USIDR;
61}
62 21
63void pulse_sck() 22void pulse_sck()
64{ 23{
65 PORTB |= ( 1 << PB2 ); 24 PORTB |= (1 << PB2);
66 _delay_ms(10); // Eine Sekunde +/-1/10000 Sekunde warten... 25 _delay_ms(10); // Eine Sekunde +/-1/10000 Sekunde warten...
67 PORTB ^= ( 1 << PB2 ); 26 PORTB ^= (1 << PB2);
68} 27}
69 28
70void print_data(uint16_t data) 29void print_data(uint16_t data)
71{ 30{
72 BIT_CLEAR(&PORTB, PB4); 31 BIT_CLEAR(&PORTB, PB4);
73 _delay_ms(100); 32 _delay_ms(50);
74 BIT_SET(&PORTB, PB4); 33 BIT_SET(&PORTB, PB4);
75 34
76 for(int i=0; i<16; i++) 35 for (uint16_t i = 0; i < 16; i++)
77 { 36 {
78 if(data<<i & 0b1000000000000000) 37 data & 0x1 << i ? BIT_SET(&PORTB, PB4) : BIT_CLEAR(&PORTB, PB4);
79 BIT_SET(&PORTB, PB4);
80 pulse_sck(); 38 pulse_sck();
81 BIT_CLEAR(&PORTB, PB4);
82 //PORTB &= (1 << PB4);
83 } 39 }
84 //pulse_sck();
85
86 40
87 BIT_CLEAR(&PORTB, PB3); 41 BIT_CLEAR(&PORTB, PB3);
88 _delay_ms(50); 42 _delay_ms(50);
89 BIT_SET(&PORTB, PB3); 43 BIT_SET(&PORTB, PB3);
90} 44}
91 45
92 46int main(void)
93int main (void) { 47{
94 48 DDRB = 0xFF;
95 //DDRB |= _BV(PB4); // as output (latch)
96 //DDRB |= _BV(PB6); // as output (DO) - data out
97 //DDRB |= _BV(PB7); // as output (USISCK) - clock
98 _delay_ms(1000);
99 // DDRB = (1<<PB0) | (1<<PB2) | (1<<PB3) | (1<<PB4) | (1<<PB5); // PB0 an PORTB als Ausgang setzen
100 DDRB = 0xFF;
101
102 49
103 BIT_CLEAR(&PORTB, PB5); 50 BIT_CLEAR(&PORTB, PB5);
104 _delay_ms(500); 51 _delay_ms(200);
105 BIT_SET(&PORTB, PB5); 52 BIT_SET(&PORTB, PB5);
106 BIT_CLEAR(&PORTB, PB5); 53 BIT_CLEAR(&PORTB, PB5);
107 _delay_ms(500); 54 _delay_ms(200);
108 BIT_SET(&PORTB, PB5); 55 BIT_SET(&PORTB, PB5);
109 //DDRB &= ~_BV(PB5); // as input (DI) - data in
110 //PORTB |= _BV(PB5); // pullup on (DI)
111
112 while( 1 )
113 { // Endlosschleife
114
115 _delay_ms(1000); // Eine Sekunde +/-1/10000 Sekunde warten...
116
117
118 // char data = 0b10101010;
119 // char data = 0b11001101;
120 // char data = 0b10101100;
121 //
122 //
123 //
124 //
125 //
126 //
127 //
128 //
129 56
130 print_data(0x0001); 57 while (1)
131 print_data(0x0002); 58 { // Endlosschleife
132 print_data(0x0004);
133 print_data(0x0008);
134 print_data(0x000F);
135 print_data(0xFF00);
136 print_data(0x00FF);
137 print_data(0xFF00);
138 print_data(0x00FF);
139 print_data(0xFF00);
140 59
141 uint16_t data; 60 uint16_t data = 1;
142 for(int i=0; i<16; i++) 61 for (; data < UINT16_MAX; data++)
143 print_data(1<<i);
144 for(int i=0; i<16; i++)
145 print_data(0X8000>>i);
146
147 /*
148
149 char data = 1;
150 for(; data<128; data++)
151 { 62 {
152 // clear register (reset) 63 // clear register (reset)
153 print_data(data); 64 print_data(data | data << 6);
154 _delay_ms(1000);
155 } 65 }
156 66
157 */ 67 _delay_ms(1000); // Eine Sekunde +/-1/10000 Sekunde warten...
158
159 // spi_transfer(0x10101010b);
160
161
162 } 68 }
163 return 0; 69 return 0;
164} 70}
..