Guest User

Untitled

a guest
Dec 12th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.93 KB | None | 0 0
  1. #ifndef __AVR_ATmega8__
  2.     #define __AVR_ATmega8__
  3. #endif
  4.  
  5. //#define F_CPU 12000000
  6. #define F_CPU 3686400
  7. //#define F_CPU 8000000
  8. const char _ID = 0x2;
  9. const char _ALL = 0xff;
  10.  
  11. const int STEPS = 100;
  12. #include <avr/io.h>
  13. #include <avr/interrupt.h>
  14. #include <util/delay.h>
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include "Enums.h"
  18.  
  19. const float stepRatio = 1.0220;
  20. const char STARTBYTE = 0x7E;
  21. const int BUFFER_SIZE = 40;
  22. char buff[BUFFER_SIZE];
  23. volatile int buffCount=0;
  24. volatile char receivedByte;
  25. bool startReceived = true;
  26. bool byteHandled = true;
  27. volatile bool dataFinished = false;
  28. bool forMe = false;
  29. volatile char data = 0x0;
  30.  
  31. /*************************************************************************
  32.     Enable RS485 transmissionin
  33. **************************************************************************/
  34. inline void RS485_TE()
  35. {
  36.     PORTD |= _BV(PD2);
  37. }
  38.  
  39. /*************************************************************************
  40.     Enable RS485 reception
  41. **************************************************************************/
  42. inline void RS485_RE()
  43. {
  44.     PORTD &= ~_BV(PD2);
  45. }
  46.  
  47. /*************************************************************************
  48.     Functions
  49. **************************************************************************/
  50. void SetR(unsigned char value )
  51. {
  52.     if (value > 255)
  53.     {
  54.         value = 255;
  55.     }
  56.     if (value >= 0)
  57.     {
  58.         OCR1A = 255-value;
  59.     }
  60. }
  61.  
  62. void SetG(unsigned char value )
  63. {
  64.     if (value > 255)
  65.     {
  66.         value = 255;
  67.     }
  68.     if (value >= 0)
  69.     {
  70.         OCR1B = 255-value;
  71.     }
  72. }
  73.  
  74. void SetB(unsigned char value )
  75. {
  76.     if (value > 255)
  77.     {
  78.         value = 255;
  79.     }
  80.     if (value >= 0)
  81.     {
  82.         OCR2 = 255-value;
  83.     }
  84. }
  85.  
  86. void USART_Transmit(short int data )
  87. {
  88.     //Wait for empty transmit buffer
  89.     while ( !( UCSRA & (1<<UDRE)) )
  90.     {
  91.     }
  92.     RS485_TE();
  93.     UDR = data;
  94. }
  95.  
  96. void SendString(char *str)
  97. {
  98.     while(*str)
  99.     {
  100.         char c = *str++;
  101.         USART_Transmit(c);
  102.     }
  103. }
  104.  
  105. void SendString(int val)
  106. {
  107.     char* str;
  108.     itoa(val, str, 10);
  109.     SendString(str);
  110. }
  111.  
  112. char xtod(char c)
  113. {
  114.     if (c>='0' && c<='9') return c-'0';
  115.     if (c>='A' && c<='F') return c-'A'+10;
  116.     if (c>='a' && c<='f') return c-'a'+10;
  117.     return c=0;        // not Hex digit
  118. }
  119.  
  120. int xstrtoi(char *hex)      // hex string to integer
  121. {
  122.     return xtod(hex[0])*16 + xtod(hex[1]);
  123. }
  124.  
  125. int ConvertValue(int value)
  126. {
  127.     if (value == 0)
  128.     {
  129.         return 0;
  130.     }
  131.     else
  132.     {
  133.         return (int)roundf(pow(stepRatio, (value - 1) ));
  134.     }
  135. }
  136.  
  137.  
  138. /*************************************************************************
  139.     Interrupts
  140. **************************************************************************/
  141. // Data received
  142. ISR(USART_RXC_vect)
  143. {
  144.     while ( !(UCSRA & (1<<RXC)) );
  145.     if((UCSRA & (1<<FE))||(UCSRA & (1<<PE)))  // If frame error or parity error
  146.     {
  147.         char garbage = UDR;                          // UDR -> Garbage
  148.     }
  149.     else
  150.     {
  151.         char data = UDR;
  152.         if (data == STARTBYTE)
  153.         {
  154.             startReceived = true;
  155.         }
  156.         else if (startReceived)
  157.         {
  158.             if (forMe)
  159.             {
  160.                 buff[buffCount] = data;
  161.                 buffCount++;
  162.                 if (buffCount > BUFFER_SIZE)
  163.                 {
  164.                     buff[0] = '\0';
  165.                     dataFinished = false;
  166.                     startReceived = false;
  167.                     forMe = false;
  168.                     buffCount = 0;
  169.                 }
  170.             }
  171.             else
  172.             {
  173.                 if (data == _ID)
  174.                 {
  175.                     forMe = true;
  176.                     buffCount = 0;
  177.                 }
  178.                 else
  179.                 {
  180.                     startReceived = false;
  181.                 }
  182.             }
  183.             if (data == '\0' || data == '\n')
  184.             {
  185.                 dataFinished = true;
  186.                 startReceived = false;
  187.                 forMe = false;
  188.             }
  189.         }
  190.     }
  191. }
  192.  
  193. ISR(USART_TXC_vect)
  194. {
  195.     RS485_RE(); //RS485 is receiving
  196. }
  197.  
  198. /*************************************************************************
  199.     Initialize
  200. **************************************************************************/
  201. void PWM_Init()
  202. {
  203.     DDRB = (1 << PINB1) | (1 << PINB2) | (1 << PINB3);
  204.     TCNT1 = 0x00;
  205.  
  206.     TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM12) | (1 << WGM10);
  207.     TCCR1B = (1 << CS10);
  208.     TCCR2 = (1 << COM21) | (1 << WGM20) | (1 << CS20);
  209. }
  210.  
  211. void USART_Init(int baudrate)
  212. {
  213.     // Set baud rate
  214.     int rate = F_CPU/16/baudrate - 1;
  215.  
  216.     // Set baud rate
  217.     UBRRH = (short int)(rate>>8);
  218.     UBRRL = (short int)rate;
  219.  
  220.     UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXCIE)|(1<<TXEN);
  221.  
  222.     // Set frame format to 8 data bits, no parity, 1 stop bit
  223.     UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
  224.  
  225.     RS485_RE();
  226. }
  227.  
  228. /*************************************************************************
  229.     Main
  230. **************************************************************************/
  231. int main()
  232. {
  233.     _delay_ms(500);
  234.     unsigned char oldR = 0;
  235.     unsigned char oldG = 0;
  236.     unsigned char oldB = 0;
  237.     unsigned char requestedRed = 0;
  238.     unsigned char requestedGreen = 0;
  239.     unsigned char requestedBlue = 0;
  240.     float stepR = 0;
  241.     float stepG = 0;
  242.     float stepB = 0;
  243.     int stepCount = 0;
  244.     int delayCount = 0;
  245.     PWM_Init();
  246.     USART_Init(4800);
  247.     sei();
  248.  
  249.     SendString("Device ready:");
  250.     _delay_ms(100);
  251.     char* id = '\0';
  252.     itoa(_ID, id, 10);
  253.     SendString( id );
  254.  
  255.     while (true)
  256.     {
  257.         if (dataFinished)
  258.         {
  259.             dataFinished = false;
  260.             buffCount = 0;
  261.             char cmd = buff[0];
  262.             switch (cmd)
  263.             {
  264.                 case Status:
  265.                 {
  266.                     char str[11];
  267.                     str[0] = STARTBYTE;
  268.                     str[1] = _ID;
  269.                     str[2] = 0x1;
  270.                     char valR[3];
  271.                     char valG[3];
  272.                     char valB[3];
  273.                     itoa(requestedRed, valR, 16);
  274.                     itoa(requestedGreen, valG, 16);
  275.                     itoa(requestedBlue, valB, 16);
  276.                     str[3] = valR[0];
  277.                     str[4] = valR[1];
  278.                     str[5] = valG[0];
  279.                     str[6] = valG[1];
  280.                     str[7] = valB[0];
  281.                     str[8] = valB[1];
  282.                     str[9] = '\n';
  283.                     str[10] = '\0';
  284.                     SendString(str);
  285.                     break;
  286.                 }
  287.                 case Set:
  288.                 {
  289.                     USART_Transmit(STARTBYTE);
  290.                     USART_Transmit(_ID);
  291.                     SendString("ACK\n");
  292.                     oldR = requestedRed;
  293.                     oldG = requestedGreen;
  294.                     oldB = requestedBlue;
  295.  
  296.                     char rStr[3];
  297.                     char gStr[3];
  298.                     char bStr[3];
  299.  
  300.                     rStr[0] = buff[1];
  301.                     rStr[1] = buff[2];
  302.                     rStr[2] = '\0';
  303.  
  304.                     gStr[0] = buff[3];
  305.                     gStr[1] = buff[4];
  306.                     gStr[2] = '\0';
  307.  
  308.                     bStr[0] = buff[5];
  309.                     bStr[1] = buff[6];
  310.                     bStr[2] = '\0';
  311.  
  312.                     int red = xstrtoi(rStr);
  313.                     int green = xstrtoi(gStr);
  314.                     int blue = xstrtoi(bStr);
  315.                     requestedRed = ConvertValue( red );
  316.                     requestedGreen = ConvertValue( green );
  317.                     requestedBlue = ConvertValue( blue );
  318.  
  319.                     stepR = (float)(requestedRed - oldR) / (float)STEPS;
  320.                     stepG = (float)(requestedGreen - oldG) / (float)STEPS;
  321.                     stepB = (float)(requestedBlue - oldB) / (float)STEPS;
  322.                     stepCount = 0;
  323.                     break;
  324.                 }
  325.                 default:
  326.                 {
  327.                     SendString("~NACK\n");
  328.                     break;
  329.                 }
  330.             }
  331.         }
  332.  
  333.         if (stepCount < STEPS)
  334.         {
  335.             if (delayCount >= 4000)
  336.             {
  337.                 delayCount = 0;
  338.                 stepCount++;
  339.                 unsigned char newR = oldR + (int)roundf(stepR * (float)stepCount);
  340.                 unsigned char newG = oldG + (int)roundf(stepG * (float)stepCount);
  341.                 unsigned char newB = oldB + (int)roundf(stepB * (float)stepCount);
  342.                 SetR(newR);
  343.                 SetG(newG);
  344.                 SetB(newB);
  345.             }
  346.             delayCount++;
  347.         }
  348.     }
  349. }
  350.  
Advertisement
Add Comment
Please, Sign In to add comment