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;
86
 
87
    /* main loop */
88
    while (1) {
89
        /* service the CAN routines */
90
        Can_Service();
91
 
92
        if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
93
            //här ska det egentligen vara en en kort delay innan man kollar 
94
            //om std är hög igen
95
 
96
            if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
97
                //testa om receviercounter har blivit för stor, error error
98
                if (receviercounter >= 16) {
99
                    receviercounter = 0;    //kasta paket
100
                } else {
101
                    //nu finns data tillgänglig på Qx
102
                    uint8_t datain = 0;
103
                    datain |= ((MT_Q1_PIN >> MT_Q1_BIT) & 0x1);
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;
135
            if ((receviercounter & 0x1) == 0) {
136
                txMsg.DataLength = (receviercounter>>1);
137
            } else {
138
                txMsg.DataLength = (receviercounter>>1)+1;
139
            }
140
 
141
            receviercounter = 0;
142
            Can_Send(&txMsg);
143
        }      
144
 
145
        /* send CAN message and check for CAN errors once every second */
146
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
147
            timeStamp = Timebase_CurrentTime();
148
            /* send txMsg */
149
            //txMsg.Id++;
150
            //Can_Send(&txMsg);
151
        }
152
 
153
        /* check if any messages have been received */
154
        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));
156
            printf("data={ ");
157
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
158
                printf("%x ", rxMsg.Data.bytes[i]);
159
            }
160
            printf("}\n");*/
161
        }
162
    }
163
 
164
    return 0;
165
}