1 | #include <stdlib.h>
|
2 | #include <avr/io.h>
|
3 | #include "lcd.h"
|
4 | #include <avr/signal.h>
|
5 | |
6 | #define DDR_SPI DDRB
|
7 | #define DD_SCK PB5
|
8 | #define DD_MISO PB4
|
9 | #define DD_MOSI PB3
|
10 | #define DD_SS PB2
|
11 | |
12 | #define akku1 0b10010100 //10010111
|
13 | |
14 | |
15 | uint8_t sek=0; //sek werden mit 0 initialisiert
|
16 | uint8_t hilfs=0; //hilfswert mit dem das lcd aktualisiert wird
|
17 | uint16_t spannung1=0;
|
18 | int i=0;
|
19 | |
20 | void timerconfig(); //prototyp: der timer0 wird konfiguriert s.u.
|
21 | uint16_t readADC(uint8_t channel); //Prototyp: adc wird konfiguriert und ausgelsen
|
22 | void SPI_MasterInit(void);
|
23 | void SPI_MasterTransmit(char);
|
24 | uint16_t SPI_MasterRecive(void);
|
25 | |
26 | int main(void) //hauptprogramm
|
27 | {
|
28 | |
29 | timerconfig(); //timer config aufruf für lcd aktualisierung
|
30 | lcd_init(LCD_DISP_ON); // initialize display, cursor off
|
31 | SPI_MasterInit();
|
32 | //sei ();
|
33 |
|
34 | for (;;) { //endlosschleife
|
35 | |
36 |
|
37 |
|
38 | |
39 | lcd_gotoxy(0,0); //zu anfangskoordinaten des lcd springen
|
40 | if(sek!=hilfs) {
|
41 | hilfs=sek;
|
42 | lcd_clrscr(); //lcd wird gelöscht
|
43 |
|
44 | lcd_gotoxy(0,0);
|
45 | char text[77]; //wird für die übergabe für sprintf benötigt
|
46 | sprintf(text,"SPI Test %d",spannung1); //ausgabe adc0 und akt.intervall via sprintf
|
47 | lcd_puts(text); //text aus sprintf wird auf dem lcd ausgegeben
|
48 | }
|
49 |
|
50 | PORTB |= (1<<DD_SS);
|
51 | PORTB &= ~(1<<DD_SS);
|
52 |
|
53 | // PORTB &= ~(1<<DD_SCK);
|
54 | // SPI_MasterTransmit(0b0);
|
55 | SPI_MasterTransmit(akku1);
|
56 |
|
57 |
|
58 |
|
59 | //SPI_MasterTransmit(0x00); //dummy transmit für conversion
|
60 |
|
61 | spannung1= SPI_MasterRecive();
|
62 | //SPDR=0;
|
63 | // PORTB &= ~(1<<DD_SCK);
|
64 |
|
65 | // PORTB = (1<<DD_SS);
|
66 | //PORTB &= ~(1<<DD_MOSI);
|
67 | |
68 | PORTB |= (1<<DD_SS);
|
69 | //PORTB &= ~(1<<DD_SS);
|
70 | |
71 | PORTB &= ~(1<<PB3);
|
72 |
|
73 | //}
|
74 | }
|
75 | } //main ende
|
76 | |
77 | |
78 | |
79 | void SPI_MasterInit(void){
|
80 | DDR_SPI=(1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS);//MOSI & SCK = output alles andere eingang
|
81 | SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0);//enable spi, master, clk/16
|
82 | PORTB |=(1<<DD_SS);
|
83 | }
|
84 | |
85 | void SPI_MasterTransmit(char cData){
|
86 | SPDR=cData;
|
87 | while(!(SPSR&(1<<SPIF)));
|
88 | PORTB &= ~(1<<DD_MOSI);
|
89 | }
|
90 | |
91 | uint16_t SPI_MasterRecive(void){ //empfangsroutine
|
92 | SPDR=0;
|
93 | //PORTB &= ~(1<<DD_MOSI);
|
94 | while(!(SPSR & (1<<SPIF))); //warten bis alle daten übertragen sind
|
95 | return SPDR;
|
96 | //SPDR=0;
|
97 | }
|
98 | |
99 | void timerconfig() { // timer config für 85µsek
|
100 | TIMSK |= (1<<TOIE0); //Timer 0 Timer/Counter0 Overflow Interrupt Enable
|
101 | TCCR0 |= (1<<CS00) | (1<<CS02); //1024Teiler
|
102 | sei (); //
|
103 | }
|
104 | |
105 | |
106 | ISR(TIMER0_OVF_vect) {
|
107 | sek++; //"pseudo"sek (85µsek) wird erhöht
|
108 | }
|