an help to write ir tx and rx code in mikrobasic

Status
Not open for further replies.

leonardigno

Newbie level 3
Joined
Feb 23, 2016
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
56
Hi guys
I need to traslate your c ir tx code in mikrobasic code for 12F675 , could you help
me? because there's some c istruction not simply to traslate for me.
In addition if you can could you help me to write a mikrobasic tx code using 16F88 device?
.
Ps: before post this message i tried to use the soft_uart
library but don't work fine. I suppose is need this pwm trasmission protocol at 38khz.
Below my hardware system to tests this code

Thank you in advance.



Code:
===================================
======= The code wrote by pkar ========
unsigned counter = 0;
unsigned input_data, bit_count;

enum {
    Idle,
    Start_bit,
    Capture_bit
};

char Current_state = Idle;
char got_data = 0;
char Command_code, Device_code;

void interrupt(){
  if(INTCON.INTF){
    T1CON.TMR1ON =1; //timer1 On
    switch (Current_state){
     case Idle:
           OPTION_REG.INTEDG = 1; //interrupt on rising edge.
           counter = 0;
           Current_state = Start_bit;
           break;
     //found the rising edge, check lenght for 2.4ms
     case Start_bit:
           //correct signal, move on to next state
           if(counter==4) {
            counter = 0;
            bit_count = 0;
            input_data = 0;
            Current_state = Capture_bit;
            } else {
              //fault signal, reset to Idle
              Current_state = Idle;
            }
           break;
     case Capture_bit:
           //check plus length for 0 or 1
           if(counter==2){
            input_data >>= 1; // add 0 to received data
            bit_count++;
           }
           else if(counter==3){
              input_data >>= 1;
              input_data |= 0x8000; //add 1 to received data
              bit_count++;
            }
           //complete 12 bit
           if(bit_count >= 12){
            got_data = 1;
            input_data >>= 4;
            OPTION_REG.INTEDG = 0; //interrupt on falling edge.
            Current_state = Idle;
           }
           counter = 0;
           break;
      default: Current_state = Idle;
    }
  INTCON.INTF = 0; //clear interrupt flag.
  }
  if(PIR1.TMR1IF){
    counter++;
    TMR1H = 0xFF; //16bit timer1 register value High order(8bit)
    TMR1L = 0x6A; //16bit timer1 register value low order(8bit)
    if(counter > 5) {
      Current_state = Idle;
      counter = 0;
      OPTION_REG.INTEDG = 0; //interrupt on falling edge
    }
    PIR1.TMR1IF = 0; //clear interrupt flag
   }
}
//******************************************************************************
// MAIN MAIN MAIN MAIN
//******************************************************************************
void main() {
  TRISIO = 0b00001100; //Configure I/O Pin
  GPIO = 0x00; //Set all output to 0
  ANSEL = 0; //All digital I/O
  CMCON = 7; //Disable Comparator
//******************************************************************************
// GP2/INTE interrupt set up
//******************************************************************************
  INTCON.INTE = 1; //enable GP2/INT interrupt.
  OPTION_REG.INTEDG = 0; //interrupt on falling edge.
//******************************************************************************
// Timer1 interrupt setting, interrupt every 600us timer register value TMR1H = 0xFF; TMR1L = 0x6A;
// Clock source Internal Oscillator 4Mhz
//******************************************************************************
  T1CON.T1CKPS1 = 1; //prescaler 1:4
  T1CON.T1CKPS0 =0;
  PIE1.TMR1IE = 1; //enable timer1 interrupt
//******************************************************************************
// Global interrupt enable
//******************************************************************************
  INTCON.PEIE = 1; //enable interrupt
  INTCON.GIE = 1; //enable global interrupt

while(1){
       if(got_data){
       Command_code = input_data & 0x7F;
       Device_code = input_data >> 7;
       got_data = 0;
        if(Device_code == 1){
         switch (Command_code){
           case 1: GPIO.GP0 = ~GPIO.GP0; break;
           case 2: GPIO.GP1 = ~GPIO.GP1; break;
           case 3: GPIO.GP4 = ~GPIO.GP4; break;
           case 4: GPIO.GP5 = ~GPIO.GP5; break;
         }
       }
      }
     asm clrWDT;
     }

}
 
Last edited by a moderator:

I'm sorry to the stupid stars but i made a copy and cat from another link using my phone !!!! :wink:
This is the code:

======= The code wrote by pkar ========


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
unsigned counter = 0;
unsigned input_data, bit_count;
 
enum {
    Idle,
    Start_bit,
    Capture_bit
};
 
char Current_state = Idle;
char got_data = 0;
char Command_code, Device_code;
 
void interrupt(){
  if(INTCON.INTF){
    T1CON.TMR1ON =1; //timer1 On
    switch (Current_state){
     case Idle:
           OPTION_REG.INTEDG = 1; //interrupt on rising edge.
           counter = 0;
           Current_state = Start_bit;
           break;
     //found the rising edge, check lenght for 2.4ms
     case Start_bit:
           //correct signal, move on to next state
           if(counter==4) {
            counter = 0;
            bit_count = 0;
            input_data = 0;
            Current_state = Capture_bit;
            } else {
              //fault signal, reset to Idle
              Current_state = Idle;
            }
           break;
     case Capture_bit:
           //check plus length for 0 or 1
           if(counter==2){
            input_data >>= 1; // add 0 to received data
            bit_count++;
           }
           else if(counter==3){
              input_data >>= 1;
              input_data |= 0x8000; //add 1 to received data
              bit_count++;
            }
           //complete 12 bit
           if(bit_count >= 12){
            got_data = 1;
            input_data >>= 4;
            OPTION_REG.INTEDG = 0; //interrupt on falling edge.
            Current_state = Idle;
           }
           counter = 0;
           break;
      default: Current_state = Idle;
    }
  INTCON.INTF = 0; //clear interrupt flag.
  }
  if(PIR1.TMR1IF){
    counter++;
    TMR1H = 0xFF; //16bit timer1 register value High order(8bit)
    TMR1L = 0x6A; //16bit timer1 register value low order(8bit)
    if(counter > 5) {
      Current_state = Idle;
      counter = 0;
      OPTION_REG.INTEDG = 0; //interrupt on falling edge
    }
    PIR1.TMR1IF = 0; //clear interrupt flag
   }
}
 
// MAIN MAIN MAIN MAIN
 
void main() {
  TRISIO = 0b00001100; //Configure I/O Pin
  GPIO = 0x00; //Set all output to 0
  ANSEL = 0; //All digital I/O
  CMCON = 7; //Disable Comparator
 
// GP2/INTE interrupt set up
 
  INTCON.INTE = 1; //enable GP2/INT interrupt.
  OPTION_REG.INTEDG = 0; //interrupt on falling edge.
 
// Timer1 interrupt setting, interrupt every 600us timer register value TMR1H = 0xFF; TMR1L = 0x6A;
// Clock source Internal Oscillator 4Mhz
 
  T1CON.T1CKPS1 = 1; //prescaler 1:4
  T1CON.T1CKPS0 =0;
  PIE1.TMR1IE = 1; //enable timer1 interrupt
 
// Global interrupt enable
 
  INTCON.PEIE = 1; //enable interrupt
  INTCON.GIE = 1; //enable global interrupt
 
while(1){
       if(got_data){
       Command_code = input_data & 0x7F;
       Device_code = input_data >> 7;
       got_data = 0;
        if(Device_code == 1){
         switch (Command_code){
           case 1: GPIO.GP0 = ~GPIO.GP0; break;
           case 2: GPIO.GP1 = ~GPIO.GP1; break;
           case 3: GPIO.GP4 = ~GPIO.GP4; break;
           case 4: GPIO.GP5 = ~GPIO.GP5; break;
         }
       }
      }
     asm clrWDT;
     }
 
}

 
Last edited by a moderator:

Hi,

do you like to use just IR diodes / photodiodes for sending/receiving UART data?
Or do you want to have IrDA conform communication?

Klaus
 

Hi Klaus
I'd like a Tx and Rx mikroBasic code (one direction communication) only, Nek or Sony Syrc or Philips protocoll communication .
The 12F675 MCU as Tx and 16F88 as Rx asTv, AVR IR remote control(i'll use only four switchs because the 12F675 is a little MCU).

I don't need a full-duplex and double direction UART communication (don't see my pictures i use it to try several projects).
I'll use one IR diodes to trasmit and one photodiodes to receive.
I suppose, in this case, is mandatory to use the 38Khz IR Protocol (sirk,nek,,philips) communication.

In the past:
I Firts time i tryed to use UART (one way) communication (using Soft_UART library) but don't work fine.
In second time i tryed to use the Time0 Interrupt (setting Option_reg)) to generate 38khz but at moment i have some difficult to write code because i need to mix the 38khz-signal and the data.

If possible i'm searching a good and simply/Clear Mikrobasic code (unless jumping, unless use assembly code, unless strange codes) because i want understand how the program work.

Unfortunatelly the 12Fxx and 16Fxx MCU don't have the PWM Library or IR Library. (in mikrobasic Compiler), it's need to write in the TX/RX code !!!!

Leonardo
 

Hi,

UART: high = high, low = low, CMOS/TTL levels, idle = high

IrDA: high = OFF, low = 3/16 pulse, idle = OFF

IR remote control: high = long packet of 38kHz pulses, low = short packet if 38kHz pulses, idle = off.
( 38kHz means: 36kHz up to 38kHz)
There are some different codings.

*******
For IR communication one uses pulses or modulated signals to avoid ambient light influence.

***
If you want to use 36/38 kHz modulation, then I recommend to use dedicated receiver devices. They include optical filters, photodiode, amplifer with AGC, demodulators, comoarators.

If you want to transmit IrDA style, then there are dedicated IrDA transceiver circuits and coding ICs.

I don't recommend to simply switch ON/OFF a IR LED with the (unmodulated) signals of an UART.

Klaus
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…