Subversion Repositories HomeAutomation

Rev

Rev 217 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 217 Rev 222
1
/**
1
/**
2
 * Description
2
 * Description
3
 *
3
 *
4
 * @date    2007-01-06
4
 * @date    2007-01-06
5
 * @author  Anders Runeson
5
 * @author  Anders Runeson
6
 *  
6
 *  
7
 */
7
 */
8
 
8
 
9
/*-----------------------------------------------------------------------------
9
/*-----------------------------------------------------------------------------
10
 * Includes
10
 * Includes
11
 *---------------------------------------------------------------------------*/
11
 *---------------------------------------------------------------------------*/
12
/* system files */
12
/* system files */
13
#include <avr/io.h>
13
#include <avr/io.h>
14
#include <avr/interrupt.h>
14
#include <avr/interrupt.h>
15
#include <avr/wdt.h>
15
#include <avr/wdt.h>
16
#include <stdio.h>
16
#include <stdio.h>
17
/* lib files */
17
/* lib files */
18
#include <can.h>
18
#include <can.h>
19
#include <serial.h>
19
#include <serial.h>
20
#include <timebase.h>
20
#include <timebase.h>
21
 
21
 
22
/*
22
/*
23
 * De få signaler som man behöver koppla in från MT8870 är
23
 * De få signaler som man behöver koppla in från MT8870 är
24
 * StD, Q1, Q2, Q3 och Q4
24
 * StD, Q1, Q2, Q3 och Q4
25
 */
25
 */
26
 
26
 
27
#define MT_StD_PIN  PIND
27
#define MT_StD_PIN  PIND
28
#define MT_StD_DDR  DDRD
28
#define MT_StD_DDR  DDRD
29
#define MT_StD_BIT  PD4
29
#define MT_StD_BIT  PD4
30
 
30
 
31
#define MT_Q1_PIN   PIND
31
#define MT_Q1_PIN   PIND
32
#define MT_Q1_DDR   DDRD
32
#define MT_Q1_DDR   DDRD
33
#define MT_Q1_BIT   PD5
33
#define MT_Q1_BIT   PD5
34
 
34
 
35
#define MT_Q2_PIN   PIND
35
#define MT_Q2_PIN   PIND
36
#define MT_Q2_DDR   DDRD
36
#define MT_Q2_DDR   DDRD
37
#define MT_Q2_BIT   PD6
37
#define MT_Q2_BIT   PD6
38
 
38
 
39
#define MT_Q3_PIN   PINB
39
#define MT_Q3_PIN   PINB
40
#define MT_Q3_DDR   DDRB
40
#define MT_Q3_DDR   DDRB
41
#define MT_Q3_BIT   PB0
41
#define MT_Q3_BIT   PB0
42
 
42
 
43
#define MT_Q4_PIN   PINB
43
#define MT_Q4_PIN   PINB
44
#define MT_Q4_DDR   DDRB
44
#define MT_Q4_DDR   DDRB
45
#define MT_Q4_BIT   PB1
45
#define MT_Q4_BIT   PB1
46
 
46
 
47
 
47
 
48
/*-----------------------------------------------------------------------------
48
/*-----------------------------------------------------------------------------
49
 * Main Program
49
 * Main Program
50
 *---------------------------------------------------------------------------*/
50
 *---------------------------------------------------------------------------*/
51
int main(void) {
51
int main(void) {
52
   
52
   
53
    Mcu_Init();
53
    Mcu_Init();
54
    Timebase_Init();
54
    Timebase_Init();
55
    Serial_Init();
55
    Serial_Init();
56
   
56
   
57
    sei();
57
    sei();
58
 
58
 
59
    printf("CanInit...");
59
    printf("CanInit...");
60
    if (Can_Init() != CAN_OK) {
60
    if (Can_Init() != CAN_OK) {
61
        printf("FAILED!\n");
61
        printf("FAILED!\n");
62
    }
62
    }
63
    else {
63
    else {
64
        printf("OK!\n");
64
        printf("OK!\n");
65
    }
65
    }
66
   
66
   
67
    uint32_t timeStamp = 0;
67
    uint32_t timeStamp = 0;
68
    uint32_t lastToneTimeStamp = 0;
68
    uint32_t lastToneTimeStamp = 0;
69
    uint8_t timerOn = 0;
69
    uint8_t timerOn = 0;
70
   
70
   
71
    Can_Message_t txMsg;
71
    Can_Message_t txMsg;
72
    Can_Message_t rxMsg;
72
    Can_Message_t rxMsg;
73
    txMsg.Id = 4;
73
    txMsg.Id = 4;
74
    txMsg.DataLength = 0;
74
    txMsg.DataLength = 0;
75
    txMsg.RemoteFlag = 0;
75
    txMsg.RemoteFlag = 0;
76
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
76
    txMsg.ExtendedFlag = 1; //DataLength and the databytes are just what happens to be in the memory. They are never set.
77
   
77
   
78
    //set ports to input
78
    //set ports to input
79
    MT_StD_DDR &= ~(1<<MT_StD_BIT);
79
    MT_StD_DDR &= ~(1<<MT_StD_BIT);
80
    MT_Q1_DDR &= ~(1<<MT_Q1_BIT);
80
    MT_Q1_DDR &= ~(1<<MT_Q1_BIT);
81
    MT_Q2_DDR &= ~(1<<MT_Q2_BIT);
81
    MT_Q2_DDR &= ~(1<<MT_Q2_BIT);
82
    MT_Q3_DDR &= ~(1<<MT_Q3_BIT);
82
    MT_Q3_DDR &= ~(1<<MT_Q3_BIT);
83
    MT_Q4_DDR &= ~(1<<MT_Q4_BIT);
83
    MT_Q4_DDR &= ~(1<<MT_Q4_BIT);
84
   
84
   
85
    uint8_t receviercounter = 0;
85
    uint8_t receviercounter = 0;
-
 
86
    uint8_t datain = 0;
86
   
87
   
87
    /* main loop */
88
    /* main loop */
88
    while (1) {
89
    while (1) {
89
        /* service the CAN routines */
90
        /* service the CAN routines */
90
        Can_Service();
91
        Can_Service();
91
       
92
 
92
        if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
93
        if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
93
            //här ska det egentligen vara en en kort delay innan man kollar 
94
            //här ska det egentligen vara en en kort delay innan man kollar 
94
            //om std är hög igen
95
            //om std är hög igen
95
 
96
 
96
            if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
97
            if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
97
                //testa om receviercounter har blivit för stor, error error
98
                //testa om receviercounter har blivit för stor, error error
98
                if (receviercounter >= 16) {
99
                if (receviercounter >= 16) {
99
                    receviercounter = 0;    //kasta paket
100
                    receviercounter = 0;    //kasta paket
100
                } else {
101
                } else {
101
                    //nu finns data tillgänglig på Qx
102
                    //nu finns data tillgänglig på Qx
102
                    uint8_t datain = 0;
-
 
103
                    datain |= ((MT_Q1_PIN >> MT_Q1_BIT) & 0x1);
103
                    datain = ((MT_Q1_PIN >> MT_Q1_BIT) & 0x1);
104
                    datain |= (((MT_Q2_PIN >> MT_Q2_BIT) & 0x1)<<1);
104
                    datain |= (((MT_Q2_PIN >> MT_Q2_BIT) & 0x1)<<1);
105
                    datain |= (((MT_Q3_PIN >> MT_Q3_BIT) & 0x1)<<2);
105
                    datain |= (((MT_Q3_PIN >> MT_Q3_BIT) & 0x1)<<2);
106
                    datain |= (((MT_Q4_PIN >> MT_Q4_BIT) & 0x1)<<3);
106
                    datain |= (((MT_Q4_PIN >> MT_Q4_BIT) & 0x1)<<3);
107
                    if (datain == 0) { datain = 0x1d; }     //DTMF0  = D    //this is temporary
107
                    if (datain == 0) { datain = 0x1d; }     //DTMF0  = D    //this is temporary
108
                    if (datain == 11) { datain = 0x1e; }    //DTMF12 = *    //this is temporary
108
                    if (datain == 11) { datain = 0x1e; }    //DTMF12 = *    //this is temporary
109
                    if (datain == 12) { datain = 0x1f; }    //DTMF11 = #    //this is temporary
109
                    if (datain == 12) { datain = 0x1f; }    //DTMF11 = #    //this is temporary
110
                    if (datain == 15) { datain = 0xc; }     //DTMF11 = C
110
                    if (datain == 15) { datain = 0xc; }     //DTMF11 = C
111
                    if (datain == 10) { datain = 0; }       //DTMF10 = number 0
111
                    if (datain == 10) { datain = 0; }       //DTMF10 = number 0
112
                    if (datain == 13) { datain = 0xa; }     //DTMF13 = A
112
                    if (datain == 13) { datain = 0xa; }     //DTMF13 = A
113
                    if (datain == 14) { datain = 0xb; }     //DTMF14 = B
113
                    if (datain == 14) { datain = 0xb; }     //DTMF14 = B
114
                    if (datain == 0x1d) { datain = 0xd; }   //DTMF0  = D    //now permanent
114
                    if (datain == 0x1d) { datain = 0xd; }   //DTMF0  = D    //now permanent
115
                    if (datain == 0x1e) { datain = 0xe; }   //DTMF12 = *    //now permanent
115
                    if (datain == 0x1e) { datain = 0xe; }   //DTMF12 = *    //now permanent
116
                    if (datain == 0x1f) { datain = 0xf; }   //DTMF11 = #    //now permanent
116
                    if (datain == 0x1f) { datain = 0xf; }   //DTMF11 = #    //now permanent
117
                   
117
                   
118
                    if ((receviercounter & 0x1) == 0) {
118
                    if ((receviercounter & 0x1) == 0) {
119
                        txMsg.Data.bytes[(receviercounter>>1)] = (datain<<4);
119
                        txMsg.Data.bytes[(receviercounter>>1)] = (datain<<4);
120
                    } else {
120
                    } else {
121
                        txMsg.Data.bytes[(receviercounter>>1)] |= datain;
121
                        txMsg.Data.bytes[(receviercounter>>1)] |= datain;
122
                    }
122
                    }
123
   
123
   
124
                    receviercounter++;
124
                    receviercounter++;
125
                     
125
                     
126
                    timerOn = 1;
126
                    timerOn = 1;
127
                }
127
                }
128
                while ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {}
128
                while ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {}
129
                lastToneTimeStamp = Timebase_CurrentTime();
129
                lastToneTimeStamp = Timebase_CurrentTime();
130
            }
130
            }
131
        }
131
        }
132
       
132
       
133
        if (timerOn ==1 && Timebase_PassedTimeMillis(lastToneTimeStamp) >= 1800) {
133
        if (timerOn ==1 && Timebase_PassedTimeMillis(lastToneTimeStamp) >= 1800) {
134
            timerOn = 0;
134
            timerOn = 0;
-
 
135
           
-
 
136
            if (datain == 0xc) {    //om sista tonen var c
135
            if ((receviercounter & 0x1) == 0) {
137
                if ((receviercounter & 0x1) == 0) {
136
                txMsg.DataLength = (receviercounter>>1);
138
                    txMsg.DataLength = (receviercounter>>1);
137
            } else {
139
                } else {
138
                txMsg.DataLength = (receviercounter>>1)+1;
140
                    txMsg.DataLength = (receviercounter>>1)+1;
-
 
141
                    txMsg.Data.bytes[(receviercounter>>1)] |= 0xc; //spara ett extra c om udda antal
139
            }
142
                }
140
           
143
               
141
            receviercounter = 0;
144
                receviercounter = 0;
142
            Can_Send(&txMsg);
145
                Can_Send(&txMsg);
-
 
146
            } else {
-
 
147
                //den ska skicka felmeddelande? använda ett annat id till det, FUNCT/FUNCC?
-
 
148
            }
143
        }      
149
        }      
144
       
150
       
145
        /* send CAN message and check for CAN errors once every second */
151
        /* send CAN message and check for CAN errors once every second */
146
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
152
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
147
            timeStamp = Timebase_CurrentTime();
153
            timeStamp = Timebase_CurrentTime();
148
            /* send txMsg */
154
            /* send txMsg */
149
            //txMsg.Id++;
155
            //txMsg.Id++;
150
            //Can_Send(&txMsg);
156
            //Can_Send(&txMsg);
151
        }
157
        }
152
       
158
       
153
        /* check if any messages have been received */
159
        /* check if any messages have been received */
154
        while (Can_Receive(&rxMsg) == CAN_OK) {
160
        while (Can_Receive(&rxMsg) == CAN_OK) {
155
            /*printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
161
            /*printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
156
            printf("data={ ");
162
            printf("data={ ");
157
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
163
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
158
                printf("%x ", rxMsg.Data.bytes[i]);
164
                printf("%x ", rxMsg.Data.bytes[i]);
159
            }
165
            }
160
            printf("}\n");*/
166
            printf("}\n");*/
161
        }
167
        }
162
    }
168
    }
163
   
169
   
164
    return 0;
170
    return 0;
165
}
171
}
166
 
172