Rev 191 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 191 | Rev 389 | ||
|---|---|---|---|
| 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 | 27 | ||
| 28 | //variabler för visning |
28 | //variabler för visning |
| 29 | Can_Message_t rxMsg; |
29 | Can_Message_t rxMsg; |
| 30 | 30 | ||
| 31 | void draw_lcd_screen(void){ |
31 | void draw_lcd_screen(void){ |
| 32 | 32 | ||
| 33 | switch (encoderPosition) { |
33 | switch (encoderPosition) { |
| 34 | case 0: |
34 | case 0: |
| 35 | glcdSetXY(0,0); |
35 | glcdSetXY(0,0); |
| 36 | char buffert[21]; |
36 | char buffert[21]; |
| 37 | glcdPutStr("Latest CAN-message:"); |
37 | glcdPutStr("Latest CAN-message:"); |
| 38 | glcdSetXY(0,1); |
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)); |
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); |
40 | glcdPutStr(buffert); |
| 41 | 41 | ||
| 42 | glcdSetXY(0,2); |
42 | glcdSetXY(0,2); |
| 43 | 43 | ||
| 44 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
44 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
| 45 | snprintf(buffert,21,"%x ", rxMsg.Data.bytes[i]); |
45 | snprintf(buffert,21,"%x ", rxMsg.Data.bytes[i]); |
| 46 | glcdPutStr(buffert); |
46 | glcdPutStr(buffert); |
| 47 | } |
47 | } |
| 48 | return; |
48 | return; |
| 49 | 49 | ||
| 50 | case 1: |
50 | case 1: |
| 51 | glcdSetXY(0,0); |
51 | glcdSetXY(0,0); |
| 52 | glcdPutStr("Muahaha, Har har vi nummer tva. Och detta kanske funkar!"); |
52 | glcdPutStr("Muahaha, Har har vi nummer tva. Och detta kanske funkar!"); |
| 53 | return; |
53 | return; |
| 54 | case 2: |
54 | case 2: |
| 55 | glcdSetXY(0,0); |
55 | glcdSetXY(0,0); |
| 56 | glcdPutStr("cparune :P"); |
56 | glcdPutStr("cparune :P"); |
| 57 | return; |
57 | return; |
| 58 | 58 | ||
| 59 | } |
59 | } |
| 60 | 60 | ||
| 61 | } |
61 | } |
| 62 | 62 | ||
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | SIGNAL (SIG_INTERRUPT1) |
65 | SIGNAL (SIG_INTERRUPT1) |
| 66 | { |
66 | { |
| 67 | if ((PINB&(0b10000000))==0){ |
67 | if ((PINB&(0b10000000))==0){ |
| 68 | encoderPosition = encoderPosition - 1; |
68 | encoderPosition = encoderPosition - 1; |
| 69 | } else { |
69 | } else { |
| 70 | encoderPosition = encoderPosition + 1; |
70 | encoderPosition = encoderPosition + 1; |
| 71 | } |
71 | } |
| 72 | 72 | ||
| 73 | 73 | ||
| 74 | if (encoderPosition > 250) encoderPosition = 0; |
74 | if (encoderPosition > 250) encoderPosition = 0; |
| 75 | if (encoderPosition > 99) encoderPosition = 99; |
75 | if (encoderPosition > 99) encoderPosition = 99; |
| 76 | glcdClear(); //Nolla displayen |
76 | glcdClear(); //Nolla displayen |
| 77 | 77 | ||
| 78 | draw_lcd_screen(); |
78 | draw_lcd_screen(); |
| 79 | } |
79 | } |
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | //Stöd för en massa olika sidor |
82 | //Stöd för en massa olika sidor |
| 83 | 83 | ||
| 84 | 84 | ||
| 85 | 85 | ||
| 86 | 86 | ||
| 87 | /*----------------------------------------------------------------------------- |
87 | /*----------------------------------------------------------------------------- |
| 88 | * Main Program |
88 | * Main Program |
| 89 | *---------------------------------------------------------------------------*/ |
89 | *---------------------------------------------------------------------------*/ |
| 90 | int main(void) { |
90 | int main(void) { |
| 91 | GICR|=0x80; //Interrupt på |
91 | GICR|=0x80; //Interrupt på |
| 92 | MCUCR=0b00001000; //Rising Edge |
92 | MCUCR=0b00001000; //Rising Edge |
| 93 | GIFR=0x80; //Något interruptigt :) |
93 | GIFR=0x80; //Något interruptigt :) |
| 94 | 94 | ||
| 95 | 95 | ||
| 96 | Timebase_Init(); |
96 | Timebase_Init(); |
| 97 | sei(); |
97 | sei(); |
| 98 | uint32_t timeStamp = 0; |
98 | uint32_t timeStamp = 0; |
| 99 | 99 | ||
| 100 | 100 | ||
| 101 | //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. |
| 102 | PORTC=0x00; |
102 | PORTC=0x00; |
| 103 | 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); |
| 104 | PORTD=0x00; |
104 | PORTD=0x00; |
| 105 | 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); |
| 106 | PORTB=0x00; |
106 | PORTB=0x00; |
| 107 | DDRB |= (1<<0)|(1<<1); |
107 | DDRB |= (1<<0)|(1<<1); |
| 108 | DDRD &= ~(1<<3); |
108 | DDRD &= ~(1<<3); |
| 109 | DDRB &= ~(1<<7); |
109 | DDRB &= ~(1<<7); |
| 110 | 110 | ||
| 111 | 111 | ||
| 112 | glcdPowerOn(); |
112 | glcdPowerOn(); |
| 113 | 113 | ||
| 114 | glcdSetXY(0,0); |
114 | glcdSetXY(0,0); |
| 115 | glcdPutStr("CanInit..."); |
115 | glcdPutStr("CanInit..."); |
| 116 | if (Can_Init() != CAN_OK) { |
116 | if (Can_Init() != CAN_OK) { |
| 117 | glcdPutStr("FAILED!"); |
117 | glcdPutStr("FAILED!"); |
| 118 | } |
118 | } |
| 119 | else { |
119 | else { |
| 120 | glcdPutStr("OK!"); |
120 | glcdPutStr("OK!"); |
| 121 | } |
121 | } |
| 122 | 122 | ||
| 123 | 123 | ||
| 124 | //Can-loopen |
124 | //Can-loopen |
| 125 | while (1) { |
125 | while (1) { |
| 126 | /* service the CAN routines */ |
126 | /* service the CAN routines */ |
| 127 | Can_Service(); |
127 | Can_Service(); |
| 128 | 128 | ||
| 129 | /* check if any messages have been received */ |
129 | /* check if any messages have been received */ |
| 130 | while (Can_Receive(&rxMsg) == CAN_OK) { |
130 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 131 | if (encoderPosition == 0){ |
131 | if (encoderPosition == 0){ |
| 132 | draw_lcd_screen(); |
132 | draw_lcd_screen(); |
| 133 | } |
133 | } |
| 134 | } |
134 | } |
| 135 | 135 | ||
| 136 | /* Här kollar vi klickvriden */ |
136 | /* Här kollar vi klickvriden */ |
| 137 | // glcdSetXY(115,0); |
137 | // glcdSetXY(115,0); |
| 138 | // snprintf(buffert,21,"%u ",encoderPosition); |
138 | // snprintf(buffert,21,"%u ",encoderPosition); |
| 139 | // glcdPutStr(buffert); |
139 | // glcdPutStr(buffert); |
| 140 | } |
140 | } |
| 141 | 141 | ||
| 142 | return 0; |
142 | return 0; |
| 143 | } |
143 | } |
| 144 | 144 | ||