You are on page 1of 7

1.

// IR Remote Control for Philips TV using RC5


protocol
2. //------------------------------------------------------------------3. //AKENAFAB
4. //http://www.todopic.com.ar/foros/
5. //------------------------------------------------------------------6. // 14bits wide ->|START|START|
TOGGLE|5BIT ADDRESS|6BIT COMMAND|<7. // 38KHz Carrier Frequency
8. //------------------------------------------------------------------9. #include <12F1822.h>
10. //------------------------------------------------------------------11. #FUSES INTRC_
IO,NOCLKOUT,NOWDT,PUT,NOMCLR,
BROWNOUT,BORV25,PROTECT,CPD,NOIESO,
NOFCMEN,NOLVP
12. #use delay(clock=32M)
13. #use fast_io(A)
14. #zero_ram
15. //------------------------------------------------------------------16. #byte IOCAP = 0x391 //
ON rising-edge interrupt
17. #byte IOCAN = 0x392
//ON falling-edge interrupt
18. #byte IOCAF = 0x393 //
IOC FLAG register
19. #byte INTCON = 0x0B //
Interrupt Control Register
20. //------------------------------------------------------------------21. //---------------------------------------------------------22. #define IR_LED PIN_A2
23. #define ON 1
24. #define OFF 0
25. //------------------------------------------------------------------26. long pwm_duty_cycle=416; //PWM
MAX = 832 , PWM DUTY SET to 50%
27. char button_pressed;
28. char new_button_pressed;
29. char RA_FLAG;
30. //-------------------------------------------------------------------

31. long commands[5]={


0b11000000001100,0b
11000000010000,0b11000000010001,
32.
0b11000000100000,0b
11000000100001};
33. char command_index;
34. /*
35. long TV_STAND_BY
=0b11000000001100; // ON-OFF
36. long TV_VOL_UP
=0b11000000010000; //
Volumen +
37. long TV_VOL_DOWN
=0b11000000010001; //
Volumen 38. long TV_CH_UP
=0b11000000100000; // Channel
+
39. long TV_CH_DOWN
=0b11000000100001; // Channel
40. */
41. //------------------------------------------------------------------42. void ir_transmit(char command); //
Transmits the COMMAND
43. void PWM_pulse(char state);
// Controls the PWM
output
44.
45. void main(){
46. //-----------------------------------------------------------------47. setup_comparator(NC_NC);
// COMPARATOR OFF
48. SETUP_ADC(ADC_OFF);
// ADC OFF
49. SETUP_ADC_PORTS(NO_ANALOGS)
; // NO ANALOG PINS
50.
51. output_A(0x00);
// OutputLatch
52. set_tris_A(0B111011); //
53. //
RA0=INPUT
54. //
RA1=INTPUT
55. //

RA2=IR_LED OUTPUT
56. //
RA3=INPUT
57. //
RA4=INPUT
58. //
RA5=INPUT
59.
60. PORT_A_PULLUPS(0b111011);
// Internal pull-ups
resistor enabled
61.
62. //------------------------------------------------------------------63. enable_interrupts(INT_RA); //
Interrupt On-Change on PORT
64. IOCAN=0b111011;
//Interrupt On-Change RA High to
Low
65. IOCAP=0b000000;
66. bit_clear(INTCON,0);
//Clearing IOCF flag bit
67. IOCAF=0; //
Clearing IOC flags
68.
69. enable_interrupts(GLOBAL);
//Global interrupts enabled
70. //------------------------------------------------------------------71. //--------- MAIN PROGRAM -----------------------------\
\\\
72. while(1){
73.
74. sleep();
75.
76. if(RA_FLAG==TRUE){
//If any button was
pressed
77. RA_FLAG=FALSE;
//Resefting
the RA FLAG for the new incoming data
detection
78.
79. button_
pressed=~button_pressed; //
Masking buttons
80. button_
pressed&=0b111011; //
getting the pressed button

81.
82. switch(button_pressed)
{
83.
84. case 0x01:
command_index=0; //Button 0
85.
break;
86. case 0x02:
command_index=1; //Button 1
87.
break;
88. case 0x08:
command_index=2; //Button 2
89.
break;
90. case 0x10:
command_index=3; //Button 3
91.
break;
92. case 0x20:
command_index=4; //Button 4
93.
break;
94.
95. }//switch
button-command
96.
97. ir_transmit(command_
index); //Transmiting
selected code
98.
99. IOCAF=0;
//Clearing IOC flags
100. bit_clear(INTCON,0);
//Clearing IOCF Global
flag big
101. bit_set(INTCON,3);
//Enabling
IOC interrupt
102. }//IF-RA FLAG
103.
104. }////end while
105. }// main
106.
107. //-------- INTERRUPTS
------------------------------------108. #INT_RA
109. void port_change(){

110. IOCAF=0;
//Clearing IOC flags
111. bit_clear(INTCON,0);
//Clearing IOCF Global
flag bit
112. bit_clear(INTCON,3);
//Disabling IOC interrupt
to avoid reentrance
113.
114. button_pressed=input_A();
//Reading PORT
115.
116. RA_FLAG=TRUE;
//Botton
pressed Flag
117. }//Interrupt On Change
118. //------------------------------------------------------------------119. //-------- FUNCTIONS
-------------------------------------120. void ir_transmit(char index){
//Transmits the IR command
121. char x;
122. char rc5_bit;
123.
124. do{
//At Least once transmitted
125. rc5_bit=13;
//MSB firts
126. for(x=0;x<14;x++){
//Transmiting 14 bits
127.
128. if(bit_test(commands
[index],rc5_bit)==1){ //
Transmiting _01_
129. PWM_pulse
(OFF);
130. delay_us
(880);
131. PWM_pulse
(ON);
132. delay_us
(880);
133. }
134.
135. else {
136. PWM_pulse
(ON);
//Transmiting _10_

137. delay_us
(880);
138. PWM_pulse
(OFF);
139. delay_us
(880);
140.
141. }
142.
143. rc5_bit--;
144. }//for x address
145. PWM_pulse(OFF);
146. delay_ms(114);
//
Delay between same command
147.
148. new_button_pressed=input_A();
149. new_button_pressed=~new_
button_pressed;
150. new_button_
pressed&=0b111011;
151.
152. }while(new_button_pressed==button_
pressed); //While the
same button stays pressed
153.
154. //Updating Toggle-bit
155. if(bit_test(commands[index],11)==0)bit_set
(commands[index],11);
156. else bit_clear(commands[index],11);
157.
158. }//ir-transmit function
159.
160. //------------------------------------------------------------------161. void PWM_pulse(char state){
//Controls the PWM
output
162.
163. if(state==ON){
164. SETUP_CCP1(CCP_PWM)
; // PWM
165. SETUP_TIMER_2(T2_
DIV_BY_1, 207,1); // Frequency =
38KHz 166. SET_PWM1_DUTY(pwm_
duty_cycle); // Duty cycle = 50% 167. }
168. else{

169. SETUP_CCP1(CCP_OFF)
; // PWM OFF
170. SETUP_TIMER_2(T2_
DISABLED, 207,1); // Timer2 disabled
171. SET_PWM1_DUTY(pwm_
duty_cycle); // Duty cycle = 50% - JUST
FOR MATCHING TIME
172. output_low(pin_A2);
// Ensures
the output is on LOW state
173. }//else
174. }//function PWM PULSE
175.
176. //-------------------------------------------------------------------

You might also like