1 | /*
|
2 | * MD25_24_02_15.c
|
3 | *
|
4 | * Created: 24.02.2015 13:57:49
|
5 | * Author: andreas
|
6 | */
|
7 |
|
8 |
|
9 | #include <avr/io.h>
|
10 | #define TAKT 1000000UL
|
11 | #define EINAD 0xB0 //Write
|
12 | #define AUSAD 0xB1 //Read
|
13 | #define FAKTOR 10 //möglicherweise ändern
|
14 | #define TEILER 1 //-||-
|
15 | #define MOTOR1 0x00
|
16 | #define MOTOR2 0x01
|
17 | #define MOTOR1_STROM 0x0B
|
18 | #define MOTOR2_STROM 0x0C
|
19 |
|
20 | //////////////////////////////////////
|
21 | // //
|
22 | // Funktionen Definieren //
|
23 | // //
|
24 | //////////////////////////////////////
|
25 | //TWI INITALISIEREN
|
26 | void init(unsigned char faktor, unsigned char teiler)
|
27 | {
|
28 | TWBR = faktor; //Bitrate
|
29 | TWSR = teiler; //Prescaler
|
30 | }
|
31 | //TWI INITALISIEREN END
|
32 | //TWI SENDEN
|
33 | void send(unsigned char adres,unsigned char reg, unsigned char daten)
|
34 | {
|
35 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); //Startbedingung
|
36 | loop_until_bit_is_set(TWCR, TWINT); //warte bis fertig
|
37 | TWDR = adres;
|
38 | TWCR = (1<<TWINT) | (1<<TWEN);
|
39 | loop_until_bit_is_set(TWCR, TWINT);
|
40 | TWDR = reg;
|
41 | TWCR = (1<<TWINT)|(1<<TWEN);
|
42 | loop_until_bit_is_set(TWCR, TWINT);
|
43 | TWDR = daten;
|
44 | TWCR = (1<<TWINT)|(1<<TWEN);
|
45 | loop_until_bit_is_set(TWCR, TWINT);
|
46 | TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
|
47 | }
|
48 | //TWI SENDEN END
|
49 | //TWI EMPFANGEN
|
50 | unsigned char empf(unsigned char adres, unsigned char reg)
|
51 | {
|
52 | unsigned char daten;
|
53 | TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
|
54 | loop_until_bit_is_set(TWCR, TWINT);
|
55 | TWDR = adres;
|
56 | TWCR = (1<<TWINT)|(1<<TWEN);
|
57 | loop_until_bit_is_set(TWCR,TWINT);
|
58 | TWDR = reg;
|
59 | TWCR = (1<<TWINT)|(1<<TWEN);
|
60 | loop_until_bit_is_set(TWCR,TWINT);
|
61 | TWCR=(1<<TWINT)|(1<<TWEN);
|
62 | loop_until_bit_is_set(TWCR,TWINT);
|
63 | daten = TWDR;
|
64 | TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
|
65 | loop_until_bit_is_set(TWCR,TWINT);
|
66 | TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
|
67 | return daten;
|
68 | }
|
69 | //TWI EMPFANGEN END
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | int main(void)
|
76 | {
|
77 | unsigned char ausl;
|
78 | init(FAKTOR,TEILER);
|
79 | while(1)
|
80 | {
|
81 |
|
82 | send(EINAD,MOTOR1,0x80);
|
83 | send(EINAD,MOTOR2,0x80);
|
84 | ausl = empf(AUSAD, MOTOR1);
|
85 | PORTB = ausl;
|
86 |
|
87 |
|
88 | }
|
89 | }
|