i have bought two IR click and tried to communicate each other using two EasyPIC7 but failed. Could it be done using example code below?
Do anyone has simpler code?
Thanks
Code: Select all
/*
* Project name:
IR_click
* Copyright:
(c) Mikroelektronika, 2011.
* Revision History:
20122712:
- initial release (JK);
* Description:
This program demonstrates usage of IR click board.
IR click board will transmit messages to the device.
It is used with code for other device (mikromedia for PIC18FJ)
Development system will receive messages from mikromedia and respond with status message.
Also two development systems can communicate between each other.
* Test configuration:
MCU: PIC18F45K22
http://ww1.microchip.com/downloads/en/DeviceDoc/41412F.pdf
Dev.Board: EasyPIC v7
http://www.mikroe.com/easypic/
Oscillator: HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
Ext. modules: IR click board : ac:IR_click
http://www.mikroe.com/click/ir
Relay click board : ac:Relay_click
http://www.mikroe.com/click/relay
SW: mikroC PRO for PIC
http://www.mikroe.com/mikroc/pic/
* NOTES:
- Place IR click board into mikroBUS socket2.
- Place Relay click board into mikroBUS socket1.
- Put button jumper (J17) into VCC position and pull-down PORTD0,1 (PORTD three state switch).
*/
sbit ir_in at RA3_bit; // Infrared receiver input
sbit ir_in_direction at TRISA3_bit; // Infrared receiver input
/*******************************************************************************
* Function IR_NEC_Send(char IR_NEC_Address, char IR_NEC_COmmand)
*******************************************************************************/
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
// constants
const char MyAddress = 1;
const char DeviceAddress = 2;
// Globals
char keephex[3]; // store hex value as ascii
char count_byte;
char button_state1 = 0, button_state2 = 0;
extern char IR_Received_byte[4];
extern char IR_Received_Flag;
void IR_State_Machine();
void IR_NEC_Send(char IR_NEC_Address, char IR_NEC_COmmand);
/*******************************************************************************
* Timer1 requirements
* ------------------------------------------------------------------------------
* Prescaler 1:1; TMR1 Preload = 64733; Actual Interrupt Time : 100 us
*******************************************************************************/
void InitTimer1(){
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xFC;
TMR1L = 0xDD;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt(){
if (TMR1IF_bit){
TMR1IF_bit = 0;
TMR1H = 0xFC;
TMR1L = 0xDD;
//Enter your code here
IR_State_Machine();
}
}
/*******************************************************************************
* Send Current status of the relays
*******************************************************************************/
void IR_NEC_Send_Status(){
char temp;
temp = (LATE0_bit << 4) + LATC0_bit;
IR_NEC_Send(DeviceAddress, temp);
}
/*******************************************************************************
* Convert to string
*******************************************************************************/
void hexout(unsigned char hexval){
int temp_hex;
char hextxt[7];
temp_hex = hexval; // to integer
IntToHex(temp_hex, hextxt); // integer to ASCII hex
keephex[0] = hextxt[2]; // store ascii hex for later
keephex[1] = hextxt[3];
keephex[2] = 0; // null terminate
}
/*******************************************************************************
* Main function
*******************************************************************************/
void main() {
ANSELA = 0; // Configure PORTA pins as digital
ANSELB = 0; // Configure PORTB pins as digital
ANSELC = 0; // Configure PORTC pins as digital
ANSELD = 0; // Configure PORTD pins as digital
ANSELE = 0; // Configure PORTE pins as digital
ir_in_direction = 1; // IR input
TRISE0_bit = 0; // output for relays
TRISC0_bit = 0;
LATC0_bit = 0;
LATE0_bit = 0;
TRISD0_bit = 1; // input buttons
TRISD1_bit = 1;
PWM2_Init(38000); // Initialize PWM2 (and PWM2) module at 38KHz
PWM2_Set_Duty(80); // approx 1/3 duty cycle is typical for IR
Delay_ms(1000); // Wait for LCD to stabilize
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1, 1, "IR click Example");
Lcd_Out(2, 1, "waiting for IR "); // Print text to Lcd, 2nd row, 1st column
InitTimer1();
while(1){
if (IR_Received_Flag == 1){
TMR1IE_bit = 0;
// process received message
if ((IR_Received_byte[0] == ~IR_Received_byte[1]) && (IR_Received_byte[2] == ~IR_Received_byte[3])){
if (IR_Received_byte[0] == MyAddress){ // correct address
// dosplay message bytes on LCD (for debugging purposes)
Lcd_Out(2,1, "NEC "); // Print text to Lcd, 2nd row, 1st column
for(count_byte = 0 ; count_byte < 4 ; count_byte++){
hexout(IR_Received_byte[count_byte]);
Lcd_Chr(2,(count_byte * 3)+5,keephex[0]);
Lcd_Chr(2,(count_byte * 3)+6,keephex[1]);
}
switch (IR_Received_byte[2]) {
case 0xAA : LATC0_bit = 1; break; // turn on relay1
case 0xBB : LATC0_bit = 0; break; // turn off relay1
case 0xCC : LATE0_bit = 1; break; // turn on relay2
case 0xDD : LATE0_bit = 0; break; // turn off relay2
case 0x55 : IR_NEC_Send_Status(); break;
}
}
}
IR_Received_Flag = 0;
Delay_ms(150);
IR_NEC_Send_Status();
TMR1IE_bit = 1;
}
// RD0 - send command for reley1
if (RD0_bit){
if (button_state1 == 0){
IR_NEC_Send(DeviceAddress, 0xAA);
button_state1 = 1;
}
else {
IR_NEC_Send(DeviceAddress, 0xBB);
button_state1 = 0;
}
}
// RD0 - send command for reley2
if (RD1_bit){
if (button_state2 == 0){
IR_NEC_Send(DeviceAddress, 0xCC);
button_state2 = 1;
}
else {
IR_NEC_Send(DeviceAddress, 0xDD);
button_state2 = 0;
}
}
}
}