Subversion Repositories HomeAutomation

Rev

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

Rev 189 Rev 191
1
/**
1
/**
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
2
 * CAN Test. This program sends a CAN message once every second. The ID of the
3
 * message is increased each time for testing purposes.
3
 * message is increased each time for testing purposes.
4
 *
4
 *
5
 * @date    2006-11-21
5
 * @date    2006-11-21
6
 * @author  Jimmy Myhrman
6
 * @author  Jimmy Myhrman
7
 *  
7
 *  
8
 */
8
 */
9
 
9
 
10
/*-----------------------------------------------------------------------------
10
/*-----------------------------------------------------------------------------
11
 * Includes
11
 * Includes
12
 *---------------------------------------------------------------------------*/
12
 *---------------------------------------------------------------------------*/
13
/* system files */
13
/* system files */
14
#include <avr/io.h>
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
15
#include <avr/interrupt.h>
16
#include <avr\pgmspace.h> //För pgm_Read_byte
16
#include <avr\pgmspace.h> //För pgm_Read_byte
17
#include <avr/wdt.h>
17
#include <avr/wdt.h>
18
#include <stdio.h>
18
#include <stdio.h>
19
#include <font.h>
19
#include <font.h>
20
/* lib files */
20
/* lib files */
21
#include <can.h>
21
#include <can.h>
22
#include <timebase.h>
22
#include <timebase.h>
23
#include <ks0108.h>
23
#include <ks0108.h>
24
 
24
 
25
 
25
 
26
uint8_t encoderPosition; //Bara för interrupttest. +/-1 beroende på riktning.;
26
uint8_t encoderPosition; //Bara för interrupttest. +/-1 beroende på riktning.;
-
 
27
 
-
 
28
//variabler för visning
-
 
29
    Can_Message_t rxMsg;
-
 
30
 
-
 
31
void draw_lcd_screen(void){
-
 
32
 
-
 
33
    switch (encoderPosition) {
-
 
34
        case 0:
-
 
35
            glcdSetXY(0,0);
-
 
36
            char buffert[21];
-
 
37
            glcdPutStr("Latest CAN-message:");
-
 
38
            glcdSetXY(0,1);
-
 
39
            snprintf(buffert,21,"ID=%lx L=%u E=%u RT=%u", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
-
 
40
            glcdPutStr(buffert);
-
 
41
 
-
 
42
            glcdSetXY(0,2);
-
 
43
 
-
 
44
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
-
 
45
                    snprintf(buffert,21,"%x ", rxMsg.Data.bytes[i]);
-
 
46
                glcdPutStr(buffert);
-
 
47
                }
-
 
48
        return;
-
 
49
 
-
 
50
        case 1:
-
 
51
            glcdSetXY(0,0);
-
 
52
            glcdPutStr("Muahaha, Har har vi nummer tva. Och detta kanske funkar!");
-
 
53
        return;
-
 
54
        case 2:
-
 
55
            glcdSetXY(0,0);
-
 
56
            glcdPutStr("cparune :P");
-
 
57
        return;
-
 
58
 
-
 
59
    }
-
 
60
 
-
 
61
}
-
 
62
 
-
 
63
 
27
 
64
 
28
SIGNAL (SIG_INTERRUPT1)
65
SIGNAL (SIG_INTERRUPT1)
29
{
66
{
30
    if ((PINB&(0b10000000))==0){
67
    if ((PINB&(0b10000000))==0){
31
        encoderPosition = encoderPosition - 1;
68
        encoderPosition = encoderPosition - 1;
32
    } else {
69
    } else {
33
        encoderPosition = encoderPosition + 1;
70
        encoderPosition = encoderPosition + 1;
34
    }
71
    }
35
   
72
   
36
 
73
 
37
    if (encoderPosition > 250) encoderPosition = 0;
74
    if (encoderPosition > 250) encoderPosition = 0;
38
    if (encoderPosition > 99) encoderPosition = 99;
75
    if (encoderPosition > 99) encoderPosition = 99;
-
 
76
    glcdClear(); //Nolla displayen
-
 
77
 
-
 
78
    draw_lcd_screen();
39
}
79
}
-
 
80
 
-
 
81
 
-
 
82
//Stöd för en massa olika sidor
-
 
83
 
40
 
84
 
41
 
85
 
42
 
86
 
43
/*-----------------------------------------------------------------------------
87
/*-----------------------------------------------------------------------------
44
 * Main Program
88
 * Main Program
45
 *---------------------------------------------------------------------------*/
89
 *---------------------------------------------------------------------------*/
46
int main(void) {
90
int main(void) {
47
    GICR|=0x80;     //Interrupt på
91
    GICR|=0x80;     //Interrupt på
48
    MCUCR=0b00001000;   //Rising Edge
92
    MCUCR=0b00001000;   //Rising Edge
49
    GIFR=0x80;      //Något interruptigt :)
93
    GIFR=0x80;      //Något interruptigt :)
50
 
94
 
51
 
95
 
52
    Timebase_Init();
96
    Timebase_Init();
53
    sei();
97
    sei();
54
    uint32_t timeStamp = 0;
98
    uint32_t timeStamp = 0;
55
 
99
 
56
 
100
 
57
    //Sätt en massa ut/ingångar.. kanske inte snygg lösning, men det får bli så sålänge.
101
    //Sätt en massa ut/ingångar.. kanske inte snygg lösning, men det får bli så sålänge.
58
    PORTC=0x00;
102
    PORTC=0x00;
59
    DDRC |= (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5);
103
    DDRC |= (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5);
60
    PORTD=0x00;
104
    PORTD=0x00;
61
    DDRD |= (1<<0)|(1<<1)|(1<<2)|(1<<4)|(1<<5)|(1<<6)|(1<<7);
105
    DDRD |= (1<<0)|(1<<1)|(1<<2)|(1<<4)|(1<<5)|(1<<6)|(1<<7);
62
    PORTB=0x00;
106
    PORTB=0x00;
63
    DDRB |= (1<<0)|(1<<1);
107
    DDRB |= (1<<0)|(1<<1);
64
    DDRD &= ~(1<<3);
108
    DDRD &= ~(1<<3);
65
    DDRB &= ~(1<<7);
109
    DDRB &= ~(1<<7);
66
 
110
 
67
   
111
   
68
    glcdPowerOn();
112
    glcdPowerOn();
69
 
-
 
70
    Can_Message_t rxMsg;
-
 
71
    char buffert[21];   //Buffert för snprintf
-
 
72
 
-
 
73
 
113
 
74
    glcdSetXY(0,0);
114
    glcdSetXY(0,0);
75
    glcdPutStr("CanInit...");
115
    glcdPutStr("CanInit...");
76
    if (Can_Init() != CAN_OK) {
116
    if (Can_Init() != CAN_OK) {
77
        glcdPutStr("FAILED!");
117
        glcdPutStr("FAILED!");
78
    }
118
    }
79
    else {
119
    else {
80
        glcdPutStr("OK!");
120
        glcdPutStr("OK!");
81
    }
121
    }
82
 
122
 
83
 
123
 
84
    //Can-loopen
124
    //Can-loopen
85
    while (1) {
125
    while (1) {
86
        /* service the CAN routines */
126
        /* service the CAN routines */
87
        Can_Service();
127
        Can_Service();
88
       
128
       
89
        /* check if any messages have been received */
129
        /* check if any messages have been received */
90
        while (Can_Receive(&rxMsg) == CAN_OK) {
130
        while (Can_Receive(&rxMsg) == CAN_OK) {
91
            glcdSetXY(0,1);
-
 
92
            glcdPutStr("MSG Recieved!");
-
 
93
            glcdSetXY(0,2);
-
 
94
            snprintf(buffert,21,"ID=%lx L=%u E=%u RT=%u", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
-
 
95
            glcdPutStr(buffert);
131
            if (encoderPosition == 0){
96
 
-
 
97
            glcdSetXY(0,3);
132
                draw_lcd_screen();
98
 
133
            }
99
            for (uint8_t i=0; i<rxMsg.DataLength; i++) {
-
 
100
                    snprintf(buffert,21,"%x ", rxMsg.Data.bytes[i]);
-
 
101
                glcdPutStr(buffert);
-
 
102
                }
134
        }
103
        }
-
 
104
       
135
       
105
        /* Här kollar vi klickvriden */    
136
        /* Här kollar vi klickvriden */    
106
        glcdSetXY(115,0);
137
//      glcdSetXY(115,0);
107
        snprintf(buffert,21,"%u ",encoderPosition);
138
//      snprintf(buffert,21,"%u ",encoderPosition);
108
        glcdPutStr(buffert);
139
//      glcdPutStr(buffert);
109
 
-
 
110
 
-
 
111
    }
140
    }
112
 
141
 
113
    return 0;
142
    return 0;
114
}
143
}
115
 
144