master.c


1
// 
2
// Anpassungen im makefile:
3
//    ATMega8 => MCU=atmega32 im makefile einstellen
4
//    lcd-routines.c in SRC = ... Zeile anhängen
5
// 
6
#include <stdlib.h>
7
8
#include <avr/io.h>
9
10
 #include <avr/delay.h>
11
12
13
#define SS PB4
14
#define MOSI PB5
15
#define MISO PB6
16
#define SCK PB7
17
18
int steuerwort;
19
void SPI_init() ;
20
void SPI_TX(int a);
21
int main(void)
22
{
23
_delay_ms(2);
24
25
  SPI_init();
26
27
    _delay_us(2000);
28
  steuerwort=0b00100001;
29
  SPI_TX(steuerwort);
30
  steuerwort=0b00000000;
31
  SPI_TX(steuerwort);
32
33
34
35
  
36
  _delay_us(1000);
37
  steuerwort=0b01010000;
38
  SPI_TX(steuerwort);
39
  steuerwort=0b11000111;
40
  SPI_TX(steuerwort);
41
42
43
44
45
  _delay_us(1000);
46
  steuerwort=0b01000000;
47
  SPI_TX(steuerwort);
48
  steuerwort=0b00000000;
49
  SPI_TX(steuerwort);
50
51
52
53
54
  _delay_us(1000);
55
  steuerwort=0b11000000;
56
  SPI_TX(steuerwort);
57
  steuerwort=0b00000000;
58
  SPI_TX(steuerwort);
59
60
61
62
  
63
64
  _delay_us(1000);
65
  steuerwort=0b00100000;
66
  SPI_TX(steuerwort);
67
  steuerwort=0b00000000;
68
  SPI_TX(steuerwort);
69
  
70
  _delay_us(0100);
71
   while(1){
72
  
73
    
74
75
  }
76
  return 0;
77
}
78
79
80
81
82
void SPI_init() //SPI initialization
83
{
84
DDRB = (1 << SS) | (1 << MOSI) | (1 << SCK);
85
  PORTB|=(1<<SS);
86
  SPCR|=(1<<MSTR);
87
  SPCR&=~(1<<SPR1);
88
  SPCR&=~(1<<SPR0);  
89
  SPSR|=(1<<SPI2X);
90
  SPCR|=(1<<SPE);
91
  SPCR|=(1<<CPOL);
92
  SPCR&=~(1<<CPHA);
93
}
94
95
void SPI_TX(int a)
96
{
97
98
SPDR=a; 
99
while(!(SPSR &(1<<SPIF))); //wait until SPIF get high
100
}