Subversion Repositories HomeAutomation

Rev

Rev 188 | Blame | Last modification | View Log | SVN | RSS feed

  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.  
  26. uint8_t encoderPosition; //Bara för interrupttest. +/-1 beroende på riktning.;
  27.  
  28. SIGNAL (SIG_INTERRUPT1)
  29. {
  30.     if ((PINB&(0b10000000))==0){
  31.         encoderPosition = encoderPosition - 1;
  32.     } else {
  33.         encoderPosition = encoderPosition + 1;
  34.     }
  35.    
  36.  
  37.     if (encoderPosition > 250) encoderPosition = 0;
  38.     if (encoderPosition > 99) encoderPosition = 99;
  39. }
  40.  
  41.  
  42.  
  43. /*-----------------------------------------------------------------------------
  44.  * Main Program
  45.  *---------------------------------------------------------------------------*/
  46. int main(void) {
  47.     GICR|=0x80;     //Interrupt på
  48.     MCUCR=0b00001000;   //Rising Edge
  49.     GIFR=0x80;      //Något interruptigt :)
  50.  
  51.  
  52.     Timebase_Init();
  53.     sei();
  54.     uint32_t timeStamp = 0;
  55.  
  56.  
  57.     //Sätt en massa ut/ingångar.. kanske inte snygg lösning, men det får bli så sålänge.
  58.     PORTC=0x00;
  59.     DDRC |= (1<<0)|(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5);
  60.     PORTD=0x00;
  61.     DDRD |= (1<<0)|(1<<1)|(1<<2)|(1<<4)|(1<<5)|(1<<6)|(1<<7);
  62.     PORTB=0x00;
  63.     DDRB |= (1<<0)|(1<<1);
  64.     DDRD &= ~(1<<3);
  65.     DDRB &= ~(1<<7);
  66.  
  67.    
  68.     glcdPowerOn();
  69.  
  70.     Can_Message_t rxMsg;
  71.     char buffert[21];   //Buffert för snprintf
  72.  
  73.  
  74.     glcdSetXY(0,0);
  75.     glcdPutStr("CanInit...");
  76.     if (Can_Init() != CAN_OK) {
  77.         glcdPutStr("FAILED!");
  78.     }
  79.     else {
  80.         glcdPutStr("OK!");
  81.     }
  82.  
  83.  
  84.     //Can-loopen
  85.     while (1) {
  86.         /* service the CAN routines */
  87.         Can_Service();
  88.        
  89.         /* check if any messages have been received */
  90.         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);
  96.  
  97.             glcdSetXY(0,3);
  98.  
  99.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  100.                     snprintf(buffert,21,"%x ", rxMsg.Data.bytes[i]);
  101.                 glcdPutStr(buffert);
  102.                 }
  103.         }
  104.        
  105.         /* Här kollar vi klickvriden */    
  106.         glcdSetXY(115,0);
  107.         snprintf(buffert,21,"%u ",encoderPosition);
  108.         glcdPutStr(buffert);
  109.  
  110.  
  111.     }
  112.  
  113.     return 0;
  114. }
  115.