Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 171 | eqlazer | 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 | * Added LCD output |
||
| 5 | * |
||
| 6 | * @date 2006-11-21, LCD addition 2006-12-11 |
||
| 7 | * @author Jimmy Myhrman, Erik Larsson |
||
| 8 | * |
||
| 9 | * Observera! Applikationen är helt otestad |
||
| 10 | */ |
||
| 11 | |||
| 12 | /*----------------------------------------------------------------------------- |
||
| 13 | * Includes |
||
| 14 | *---------------------------------------------------------------------------*/ |
||
| 15 | /* system files */ |
||
| 16 | #include <avr/io.h> |
||
| 17 | #include <avr/interrupt.h> |
||
| 18 | #include <avr/wdt.h> |
||
| 19 | #include <stdio.h> |
||
| 20 | /* lib files */ |
||
| 21 | #include <can.h> |
||
| 22 | #include <serial.h> |
||
| 23 | #include <timebase.h> |
||
| 24 | |||
| 25 | #define RELAY_PORT PORTC |
||
| 26 | #define RELAY_PIN PC1 |
||
| 27 | #define OFF 0x00 |
||
| 28 | #define ON 0x01 |
||
| 29 | |||
| 30 | /*----------------------------------------------------------------------------- |
||
| 31 | * Main Program |
||
| 32 | *---------------------------------------------------------------------------*/ |
||
| 33 | int main(void) { |
||
| 34 | |||
| 35 | uint8_t relayStatus; |
||
| 36 | |||
| 37 | Timebase_Init(); |
||
| 38 | Serial_Init(); |
||
| 39 | sei(); |
||
| 40 | |||
| 41 | /* Enable RELAY_PIN as output */ |
||
| 42 | DDRC |= (1<<DDC1); |
||
| 43 | /* Turn relay off */ |
||
| 44 | RELAY_PORT &= ~(1<<RELAY_PIN); |
||
| 45 | relayStatus = OFF; |
||
| 46 | |||
| 47 | // TODO initiera temperatursensor |
||
| 48 | |||
| 49 | printf("\n------------------------------------------------------------\n"); |
||
| 50 | printf( " CAN: Relay\n"); |
||
| 51 | printf( "------------------------------------------------------------\n"); |
||
| 52 | |||
| 53 | |||
| 54 | printf("CanInit..."); |
||
| 55 | if (Can_Init() != CAN_OK) { |
||
| 56 | printf("FAILED!\n"); |
||
| 57 | } |
||
| 58 | else { |
||
| 59 | printf("OK!\n"); |
||
| 60 | } |
||
| 61 | |||
| 62 | uint32_t timeStamp = 0; |
||
| 63 | |||
| 64 | Can_Message_t txMsg; |
||
| 65 | Can_Message_t rxMsg; |
||
| 66 | txMsg.Id = 0; |
||
| 67 | txMsg.RemoteFlag = 0; |
||
| 68 | txMsg.ExtendedFlag = 1; |
||
| 69 | |||
| 70 | |||
| 71 | /* main loop */ |
||
| 72 | while (1) { |
||
| 73 | /* service the CAN routines */ |
||
| 74 | Can_Service(); |
||
| 75 | |||
| 76 | /* check if any messages have been received */ |
||
| 77 | while (Can_Receive(&rxMsg) == CAN_OK) { |
||
| 78 | /* This relay is adressed*/ |
||
| 79 | if( rxMsg.Id == 0x1200 ){ |
||
| 80 | |||
| 81 | if( rxMsg.Data.bytes[0] == 0x01 ){ |
||
| 82 | /* Turn relay on */ |
||
| 83 | |||
| 84 | RELAY_PORT |= (1<<RELAY_PIN); |
||
| 85 | relayStatus = ON; |
||
| 86 | |||
| 87 | }else if(rxMsg.Data.bytes[0] == 0x02){ |
||
| 88 | /* Turn relay off */ |
||
| 89 | |||
| 90 | RELAY_PORT &= ~(1<<RELAY_PIN); |
||
| 91 | relayStatus = OFF; |
||
| 92 | |||
| 93 | }else if(rxMsg.Data.bytes[0] == 0x03){ |
||
| 94 | /* Toggle relay */ |
||
| 95 | |||
| 96 | if(relayStatus == ON){ |
||
| 97 | RELAY_PORT &= ~(1<<RELAY_PIN); |
||
| 98 | relayStatus = OFF; |
||
| 99 | }else{ |
||
| 100 | RELAY_PORT |= (1<<RELAY_PIN); |
||
| 101 | relayStatus = ON; |
||
| 102 | } |
||
| 103 | |||
| 104 | }else if(rxMsg.Data.bytes[0] == 0x04){ |
||
| 105 | /* Get relay status (ON/OFF?, temperature) */ |
||
| 106 | // TODO läsa värde från temperatursensor |
||
| 107 | |||
| 108 | txMsg.Id = 0x1201; |
||
| 109 | txMsg.Data.bytes[0] = relayStatus; |
||
| 110 | txMsg.DataLength = 3; |
||
| 111 | |||
| 112 | Can_Send( &txMsg ); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | // TODO |
||
| 117 | // ska status på relä och temperatur skickas periodiskt? |
||
| 118 | // tätare intervall om temperaturen kommer över en viss gräns? |
||
| 119 | |||
| 120 | if( Timebase_PassedTimeMillis(timeStamp) >=5000){ |
||
| 121 | timeStamp = Timebase_CurrentTime(); |
||
| 122 | /* Get relay status (ON/OFF?, temperature) */ |
||
| 123 | // TODO läsa värde från temperatursensor |
||
| 124 | |||
| 125 | txMsg.Id = 0x1201; |
||
| 126 | txMsg.Data.bytes[0] = relayStatus; |
||
| 127 | txMsg.DataLength = 3; |
||
| 128 | |||
| 129 | Can_Send( &txMsg ); |
||
| 130 | |||
| 131 | } |
||
| 132 | |||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | return 0; |
||
| 137 | } |