Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: abdullah
- *
- * Created on 14 Mayıs 2014 Çarşamba, 17:47
- */
- #include <xc.h>
- #include "init.h"
- #define COM_TIME_OUT 100 // Communications timeout in ms
- volatile unsigned char red_duty = 1;
- volatile unsigned char green_duty = 85;
- volatile unsigned char blue_duty = 127;
- void interrupt myISR(void)
- {
- static unsigned char duty_ctr = 0;
- if (PIR1bits.TMR2IF == 1)
- {
- duty_ctr++;
- if (duty_ctr > red_duty)
- {
- PWM_red = 0;
- }
- if (duty_ctr > green_duty)
- {
- PWM_green = 0;
- }
- if (duty_ctr > blue_duty)
- {
- PWM_blue = 0;
- }
- if (duty_ctr == 0xFF)
- {
- if (red_duty != 0) PWM_red = 1;
- if (green_duty != 0) PWM_green = 1;
- if (blue_duty != 0) PWM_blue = 1;
- duty_ctr = 0;
- }
- PIR1bits.TMR2IF = 0;
- }
- }
- void main(void)
- {
- unsigned char ctr;
- unsigned char buffer;
- init_MPU();
- init_EUSART();
- while (1)
- {
- reset:
- CLRWDT();
- ctr = 0;
- buffer = 0;
- if (PIR1bits.RCIF == 1) // If the receive buffer is full
- {
- buffer = RCREG; // Put the contents of receiver
- // register into a buffer register.
- if (buffer == 'R') // If the received character is 'R',
- {
- while (PIR1bits.RCIF == 0) // Wait until a new character.
- {
- CLRWDT(); // Feed the dog in the mean time.
- __delay_ms(1); // If there is a wait more than timeout,
- if (ctr++ > COM_TIME_OUT)
- goto reset; // reset the state machine.
- }
- buffer = RCREG; // Put the new character into buffer.
- if (buffer != 'R' && buffer != 'G' && buffer != 'B')
- {
- red_duty = buffer; // Set the PWM value.
- TXREG = 'K';
- }
- }
- else if (buffer == 'G') // If the received character is 'G',
- {
- while (PIR1bits.RCIF == 0) // Wait until a new character.
- {
- CLRWDT(); // Feed the dog in the mean time.
- __delay_ms(1); // If there is a wait more than timeout,
- if (ctr++ > COM_TIME_OUT)
- goto reset; // reset the state machine.
- }
- buffer = RCREG; // Put the new character into buffer.
- if (buffer != 'R' && buffer != 'G' && buffer != 'B')
- {
- green_duty = buffer; // Set the PWM value.
- TXREG = 'K';
- }
- }
- else if (buffer == 'B') // If the received character is 'B',
- {
- while (PIR1bits.RCIF == 0) // Wait until a new character.
- {
- CLRWDT(); // Feed the dog in the mean time.
- __delay_ms(1); // If there is a wait more than timeout,
- if (ctr++ > COM_TIME_OUT)
- goto reset; // reset the state machine.
- }
- buffer = RCREG; // Put the new character into buffer.
- if (buffer != 'R' && buffer != 'G' && buffer != 'B')
- {
- blue_duty = buffer; // Set the PWM value.
- TXREG = 'K';
- }
- }
- }
- } // End of big old while loop
- } // End of main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement