Guest User

Untitled

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