Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 285 | 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/wdt.h> |
||
| 17 | #include <stdio.h> |
||
| 18 | /* lib files */ |
||
| 19 | #include <can.h> |
||
| 20 | #include <serial.h> |
||
| 21 | #include <timebase.h> |
||
| 22 | #include <math.h> |
||
| 23 | |||
| 24 | /*----------------------------------------------------------------------------- |
||
| 25 | * Variables |
||
| 26 | *---------------------------------------------------------------------------*/ |
||
| 27 | |||
| 28 | uint8_t currRed; //Current Red PWM dutycycle |
||
| 29 | uint8_t currGreen; //Current Green PWM dutycycle |
||
| 30 | uint8_t currBlue; //Current Blue PWM dutycycle |
||
| 31 | |||
| 32 | uint8_t destRed; //Destination Red PWM dutycycle |
||
| 33 | uint8_t destGreen; //Destination Green PWM dutycycle |
||
| 34 | uint8_t destBlue; //Destination Blue PWM dutycycle |
||
| 35 | |||
| 36 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading |
||
| 37 | uint8_t program; //This variable keeps track of what program is currently running. 0 means no program. |
||
| 38 | uint8_t programstate; //This variable keeps track of where in a program we currently are. |
||
| 39 | |||
| 40 | /*---------------------------------------------------------------------------- |
||
| 41 | * Functions |
||
| 42 | * -------------------------------------------------------------------------*/ |
||
| 43 | void updateLED(); |
||
| 44 | |||
| 45 | |||
| 46 | /*----------------------------------------------------------------------------- |
||
| 47 | * Main Program |
||
| 48 | *---------------------------------------------------------------------------*/ |
||
| 49 | int main(void) { |
||
| 50 | Timebase_Init(); |
||
| 51 | Serial_Init(); |
||
| 52 | |||
| 53 | //Setting up OC1A, 16 bit counter used as 8 bit. |
||
| 54 | /* Use OC1A */ |
||
| 55 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
||
| 56 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
||
| 57 | ICR1 = 256; //8 bit precision to match the other two counters. |
||
| 58 | OCR1A = 128; //50% as standard |
||
| 59 | DDRB |= (1<<PB1); /* Enable pin as output */ |
||
| 60 | |||
| 61 | //Initialize variables |
||
| 62 | destRed = 128; |
||
| 63 | fadeSpeed = 10; |
||
| 64 | uint32_t timeStamp = 0; |
||
| 65 | |||
| 66 | Can_Message_t rxMsg; |
||
| 67 | |||
| 68 | sei(); |
||
| 69 | |||
| 70 | /* main loop */ |
||
| 71 | while (1) { |
||
| 72 | /* service the CAN routines */ |
||
| 73 | Can_Service(); |
||
| 74 | |||
| 75 | /* Time to see if we want to update our values */ |
||
| 76 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
||
| 77 | timeStamp = Timebase_CurrentTime(); |
||
| 78 | updateLED(); |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /* This code really needs cleaning up. Support for fade-programs */ |
||
| 83 | /* Maybe create a struct for one program-step? TargetRED, TargetGREEN, TargetBLUE, Speed, NextAction? */ |
||
| 84 | if (program != 0){ |
||
| 85 | switch(program){ |
||
| 86 | case 1: |
||
| 87 | if (destRed != 255){ |
||
| 88 | destRed = destRed + 1; |
||
| 89 | } |
||
| 90 | break; |
||
| 91 | case 2: |
||
| 92 | if (destRed != 0){ |
||
| 93 | destRed = destRed - 1; |
||
| 94 | } |
||
| 95 | break; |
||
| 96 | case 3: |
||
| 97 | switch(programstate){ |
||
| 98 | case 0: |
||
| 99 | destRed = 0; |
||
| 100 | fadeSpeed = 10; |
||
| 101 | if (currRed == 0){ |
||
| 102 | programstate = 1; |
||
| 103 | } |
||
| 104 | break; |
||
| 105 | case 1: |
||
| 106 | destRed = 255; |
||
| 107 | fadeSpeed = 100; |
||
| 108 | if (currRed == 255){ |
||
| 109 | programstate = 2; |
||
| 110 | } |
||
| 111 | break; |
||
| 112 | case 2: |
||
| 113 | destRed = 60; |
||
| 114 | fadeSpeed = 5; |
||
| 115 | if (currRed == 60){ |
||
| 116 | programstate = 3; |
||
| 117 | } |
||
| 118 | break; |
||
| 119 | case 3: |
||
| 120 | destRed = 170; |
||
| 121 | fadeSpeed = 20; |
||
| 122 | if (currRed == 170){ |
||
| 123 | programstate = 4; |
||
| 124 | } |
||
| 125 | break; |
||
| 126 | case 4: |
||
| 127 | destRed = 0; |
||
| 128 | fadeSpeed = 40; |
||
| 129 | if (currRed == 40){ |
||
| 130 | programstate = 0; |
||
| 131 | program = 0; |
||
| 132 | } |
||
| 133 | break; |
||
| 134 | } |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | /* check if any messages have been received */ |
||
| 142 | /* A lot of remote control crap going on here now for testing */ |
||
| 143 | while (Can_Receive(&rxMsg) == CAN_OK) { |
||
| 144 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
||
| 145 | program = 1; |
||
| 146 | fadeSpeed = 0; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
||
| 150 | program = 2; |
||
| 151 | fadeSpeed = 0; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x20)){ |
||
| 155 | program = 0; |
||
| 156 | |||
| 157 | } |
||
| 158 | |||
| 159 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x21)){ |
||
| 160 | program = 0; |
||
| 161 | } |
||
| 162 | |||
| 163 | |||
| 164 | //Ready-made programs! |
||
| 165 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x01)){ |
||
| 166 | destRed = 255; |
||
| 167 | } |
||
| 168 | |||
| 169 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x02)){ |
||
| 170 | destRed = 0; |
||
| 171 | } |
||
| 172 | |||
| 173 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x04)){ |
||
| 174 | destRed = 0; |
||
| 175 | fadeSpeed = 0; |
||
| 176 | } |
||
| 177 | |||
| 178 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x05)){ |
||
| 179 | destRed = 255; |
||
| 180 | fadeSpeed = 10; |
||
| 181 | } |
||
| 182 | |||
| 183 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x07)){ |
||
| 184 | program = 3; |
||
| 185 | programstate = 0; |
||
| 186 | } |
||
| 187 | |||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | } |
||
| 192 | |||
| 193 | return 0; |
||
| 194 | } |
||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | void updateLED(){ |
||
| 201 | //Red |
||
| 202 | if (currRed < destRed){ |
||
| 203 | currRed = currRed + 1; |
||
| 204 | OCR1A = currRed; |
||
| 205 | } else if (currRed > destRed){ |
||
| 206 | currRed = currRed - 1; |
||
| 207 | OCR1A = currRed; |
||
| 208 | } |
||
| 209 | //Green |
||
| 210 | //Blue |
||
| 211 | } |
||
| 212 |