Advertisement
abdullahkahraman

Untitled

Jun 8th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.58 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: abdullah
  4.  *
  5.  * Created on 14 Mayıs 2014 Çarşamba, 17:47
  6.  */
  7. #include <xc.h>
  8. #include "init.h"
  9.  
  10. #define COM_TIME_OUT   100 // Communications timeout in ms
  11.  
  12. volatile unsigned char red_duty = 1;
  13. volatile unsigned char green_duty = 85;
  14. volatile unsigned char blue_duty = 127;
  15.  
  16. void interrupt myISR(void)
  17. {
  18.     static unsigned char duty_ctr = 0;
  19.     if (PIR1bits.TMR2IF == 1)
  20.     {
  21.         duty_ctr++;
  22.         if (duty_ctr > red_duty)
  23.         {
  24.             PWM_red = 0;
  25.         }
  26.         if (duty_ctr > green_duty)
  27.         {
  28.             PWM_green = 0;
  29.         }
  30.         if (duty_ctr > blue_duty)
  31.         {
  32.             PWM_blue = 0;
  33.         }
  34.         if (duty_ctr == 0xFF)
  35.         {
  36.             if (red_duty != 0) PWM_red = 1;
  37.             if (green_duty != 0) PWM_green = 1;
  38.             if (blue_duty != 0) PWM_blue = 1;
  39.             duty_ctr = 0;
  40.         }
  41.         PIR1bits.TMR2IF = 0;
  42.     }
  43. }
  44.  
  45. void main(void)
  46. {
  47.     unsigned char ctr;
  48.     unsigned char buffer;
  49.  
  50.     init_MPU();
  51.     init_EUSART();
  52.     while (1)
  53.     {
  54. reset:
  55.         CLRWDT();
  56.         ctr = 0;
  57.         buffer = 0;
  58.  
  59.         if (PIR1bits.RCIF == 1) // If the receive buffer is full
  60.         {
  61.             buffer = RCREG; // Put the contents of receiver
  62.             // register into a buffer register.
  63.  
  64.             if (buffer == 'R') // If the received character is 'R',
  65.             {
  66.                 while (PIR1bits.RCIF == 0) // Wait until a new character.
  67.                 {
  68.                     CLRWDT(); // Feed the dog in the mean time.
  69.                     __delay_ms(1); // If there is a wait more than timeout,
  70.                     if (ctr++ > COM_TIME_OUT)
  71.                         goto reset; // reset the state machine.
  72.                 }
  73.                 buffer = RCREG; // Put the new character into buffer.
  74.                 if (buffer != 'R' && buffer != 'G' && buffer != 'B')
  75.                 {
  76.                     red_duty = buffer; // Set the PWM value.
  77.                     TXREG = 'K';
  78.                 }
  79.             }
  80.             else if (buffer == 'G') // If the received character is 'G',
  81.             {
  82.                 while (PIR1bits.RCIF == 0) // Wait until a new character.
  83.                 {
  84.                     CLRWDT(); // Feed the dog in the mean time.
  85.                     __delay_ms(1); // If there is a wait more than timeout,
  86.                     if (ctr++ > COM_TIME_OUT)
  87.                         goto reset; // reset the state machine.
  88.                 }
  89.                 buffer = RCREG; // Put the new character into buffer.
  90.                 if (buffer != 'R' && buffer != 'G' && buffer != 'B')
  91.                 {
  92.                     green_duty = buffer; // Set the PWM value.
  93.                     TXREG = 'K';
  94.                 }
  95.             }
  96.             else if (buffer == 'B') // If the received character is 'B',
  97.             {
  98.                 while (PIR1bits.RCIF == 0) // Wait until a new character.
  99.                 {
  100.                     CLRWDT(); // Feed the dog in the mean time.
  101.                     __delay_ms(1); // If there is a wait more than timeout,
  102.                     if (ctr++ > COM_TIME_OUT)
  103.                         goto reset; // reset the state machine.
  104.                 }
  105.                 buffer = RCREG; // Put the new character into buffer.
  106.                 if (buffer != 'R' && buffer != 'G' && buffer != 'B')
  107.                 {
  108.                     blue_duty = buffer; // Set the PWM value.
  109.                     TXREG = 'K';
  110.                 }
  111.             }
  112.         }
  113.     } // End of big old while loop
  114. } // End of main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement