Hallo Forum, ich habe eine datei lm75.h die ich nutze um einen lm75
auszulesen. Ich habe jetzt das Problem, das sich der Controller leider
des öfteren aufhängt. Da ich einen Watchdog einprogrammiert habe resetet
er also. Kann mir jemand sagen was an dem Code nicht stimmt??
1 | unsigned char temperatur=0x00, temphalf;
|
2 | unsigned char adr_temp_sens = 0x91;
|
3 | unsigned char twcr_byte=0x00,receivebuffer=0x00;
|
4 |
|
5 | #include <avr/signal.h>
|
6 | #define ACK 1
|
7 | #define NACK 0
|
8 |
|
9 | SIGNAL(SIG_2WIRE_SERIAL) // signal handler fuer iic interrupt
|
10 | {
|
11 | // nichts tun
|
12 | }
|
13 |
|
14 | void IIC_INIT(void)
|
15 | {
|
16 |
|
17 | // IIC INIT (100kHz)
|
18 | TWSR &= 0xfc; // Vorteiler im IIC STATUS-REGISTER auf "1" ( TWPS1=TWPS0 = 0 )
|
19 | TWBR = 0x0c; // IIC BIT-RATE-Register Manual Seite 165 ( doc2486.pdf ) TWBR = ( CPU_Clock/SCL_Clock - 16) / 2
|
20 | // TWBR = ( 4MHz/100kHz - 16) / 2 = 12 -> 0x0c
|
21 | }
|
22 |
|
23 | void IIC_START(void)
|
24 | {
|
25 |
|
26 | TWSR &= 0x02; // Statusregister ruecksetzen
|
27 | twcr_byte= 0x00;
|
28 | twcr_byte = ((1<<TWINT)|(1<<TWSTA)|(1<<TWEN));
|
29 | TWCR=twcr_byte;
|
30 | while (!(TWCR & (1<<TWINT)));
|
31 | }
|
32 |
|
33 | void IIC_STOP(void)
|
34 | {
|
35 |
|
36 | TWSR &= 0x02;
|
37 | twcr_byte= 0x00;
|
38 | twcr_byte = ((1<<TWINT)|(1<<TWEN)|(1<<TWSTO));
|
39 | TWCR=twcr_byte;
|
40 | }
|
41 |
|
42 | void IIC_SEND(unsigned char sendbyte)
|
43 | {
|
44 |
|
45 | TWSR &= 0x02;
|
46 | TWDR = sendbyte;
|
47 | twcr_byte= 0x00;
|
48 | twcr_byte = ((1<<TWINT) | (1<<TWEN));
|
49 | TWCR=twcr_byte;
|
50 | while (!(TWCR & (1<<TWINT)));
|
51 | }
|
52 |
|
53 | void IIC_REC(unsigned char receive)
|
54 | {
|
55 |
|
56 | TWSR &= 0x02;
|
57 | twcr_byte= 0x00;
|
58 | switch(receive)
|
59 | {
|
60 | case 0: twcr_byte=((1<<TWINT) | (1<<TWEN));
|
61 | break;
|
62 | case 1: twcr_byte=((1<<TWINT) | (1<<TWEN) | (1<<TWEA));
|
63 | break;
|
64 | default: break;
|
65 | }
|
66 | TWCR=twcr_byte;
|
67 | while (!(TWCR & (1<<TWINT)));
|
68 | receivebuffer=TWDR;
|
69 | }
|
70 |
|
71 | int get_temp(uint8_t sens_id)
|
72 | {
|
73 | IIC_START();
|
74 | IIC_SEND((sens_id|0x01));
|
75 | IIC_REC(ACK); // erstes Byte holen
|
76 | temperatur = receivebuffer; // erstes Byte speichern
|
77 | IIC_REC(NACK); // zweites Byte holen
|
78 | temphalf = (receivebuffer); // zweites Byte speichern
|
79 | IIC_STOP();
|
80 | return temperatur;
|
81 | }
|