Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
188 noddan 1
/**
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.
4
 *
5
 * @date    2006-11-21
6
 * @author  Jimmy Myhrman
7
 *  
8
 */
9
 
10
/*-----------------------------------------------------------------------------
11
 * Includes
12
 *---------------------------------------------------------------------------*/
13
/* system files */
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
16
#include <avr\pgmspace.h> //För pgm_Read_byte
17
#include <avr/wdt.h>
18
#include <stdio.h>
19
#include <font.h>
20
/* lib files */
21
#include <can.h>
22
#include <timebase.h>
23
#include <ks0108.h>
24
 
25
 
189 noddan 26
uint8_t encoderPosition; //Bara för interrupttest. +/-1 beroende på riktning.;
188 noddan 27
 
191 noddan 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
 
64
 
188 noddan 65
SIGNAL (SIG_INTERRUPT1)
66
{
67
    if ((PINB&(0b10000000))==0){
68
        encoderPosition = encoderPosition - 1;
69
    } else {
70
        encoderPosition = encoderPosition + 1;
71
    }
72
 
73
 
74
    if (encoderPosition > 250) encoderPosition = 0;
75
    if (encoderPosition > 99) encoderPosition = 99;
191 noddan 76
    glcdClear(); //Nolla displayen
77
 
78
    draw_lcd_screen();
188 noddan 79
}
80
 
81
 
191 noddan 82
//Stöd för en massa olika sidor
188 noddan 83
 
191 noddan 84
 
85
 
86
 
188 noddan 87
/*-----------------------------------------------------------------------------
88
 * Main Program
89
 *---------------------------------------------------------------------------*/
90
int main(void) {
189 noddan 91
    GICR|=0x80;     //Interrupt på
92
    MCUCR=0b00001000;   //Rising Edge
93
    GIFR=0x80;      //Något interruptigt :)
188 noddan 94
 
95
 
96
    Timebase_Init();
97
    sei();
98
    uint32_t timeStamp = 0;
99
 
100
 
189 noddan 101
    //Sätt en massa ut/ingångar.. kanske inte snygg lösning, men det får bli så sålänge.
102
    PORTC=0x00;
103
    DDRC |= (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5);
104
    PORTD=0x00;
105
    DDRD |= (1<<0)|(1<<1)|(1<<2)|(1<<4)|(1<<5)|(1<<6)|(1<<7);
106
    PORTB=0x00;
107
    DDRB |= (1<<0)|(1<<1);
108
    DDRD &= ~(1<<3);
109
    DDRB &= ~(1<<7);
188 noddan 110
 
189 noddan 111
 
112
    glcdPowerOn();
188 noddan 113
 
114
    glcdSetXY(0,0);
115
    glcdPutStr("CanInit...");
116
    if (Can_Init() != CAN_OK) {
117
        glcdPutStr("FAILED!");
118
    }
119
    else {
120
        glcdPutStr("OK!");
121
    }
122
 
123
 
124
    //Can-loopen
125
    while (1) {
126
        /* service the CAN routines */
127
        Can_Service();
128
 
129
        /* check if any messages have been received */
130
        while (Can_Receive(&rxMsg) == CAN_OK) {
191 noddan 131
            if (encoderPosition == 0){
132
                draw_lcd_screen();
133
            }
188 noddan 134
        }
135
 
136
        /* Här kollar vi klickvriden */    
191 noddan 137
//      glcdSetXY(115,0);
138
//      snprintf(buffert,21,"%u ",encoderPosition);
139
//      glcdPutStr(buffert);
188 noddan 140
    }
141
 
142
    return 0;
143
}