bkar
Junior Member level 3
Dear All,
I am new in using micro controller.
I wanted to build a transmitter & receiver by using 12F675 PIC IC and mikroC compiler for programming.
Here is my code for TRANSMITTER.
//***********************************************************************//
//********************************************************************//
Here is my code for RECEIVER.
//********************************************************************//
//***********************************************************************
Please help me here!
I am new in using micro controller.
I wanted to build a transmitter & receiver by using 12F675 PIC IC and mikroC compiler for programming.
Here is my code for TRANSMITTER.
//***********************************************************************//
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 void InitMain() { TRISIO = 0x1F; GPIO.GP5 = 0; //Define default outputs ANSEL = 0x00; //All I/O digital CMCON = 0x07; //Disable comparator OPTION_REG = 0x00; //Enable pull-ups } void pulseIR(unsigned int pulse) { //38khz IR modulation GPIO.GP5=0; while (pulse > 0) { GPIO.GP5=1; Delay_us(5); GPIO.GP5=0; GPIO.GP5=0; GPIO.GP5=0; Delay_us(3); pulse--; } } void startbit() { pulseIR(96); } void devicebit() { Delay_us(600); pulseIR(48); //1 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 } void key0() { Delay_us(600); pulseIR(48); //1 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 Delay_us(600); pulseIR(24); //0 } void main() { unsigned char j=3; delay_ms(1000); InitMain(); while(1) { if(GPIO.GP0 == 0) { startbit(); j=3; while (j>0) { j--; //it will send the command for 3 times. You can ignor this! key0(); devicebit(); Delay_ms(45); } } //end if } //end while } //end main
//********************************************************************//
Here is my code for RECEIVER.
//********************************************************************//
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 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; 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=24) { 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=12){ input_data >>= 1; // add 0 to received data bit_count++; } else if(counter=18){ input_data >>= 1; input_data |= 0x8000; //add 1 to received data bit_count++; } //compleat 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++; if(counter > 30) { 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; GPIO = 0x00; ANSEL = 0; //All digital I/O CMCON = 7; //Disable Comparator //************************************************************************* // GP2 interrupt set up //************************************************************************* INTCON.INTE = 1; //enable GP2 interrupt. OPTION_REG.INTEDG = 0; //interrupt on falling edge. //************************************************************************* // Timer1 interrupt set up, interrupt every 100us //************************************************************************* T1CON.T1CKPS1 = 1; //prescaler 1:4 T1CON.T1CKPS0 =0; TMR1H = 0xFF; //preload timer1 comparator value. TMR1L = 0xF9; //reset value timer1 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; } } } } }
//***********************************************************************
Please help me here!
Attachments
Last edited by a moderator: