Professional Documents
Culture Documents
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. //-------------------------------------------------------------------