Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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