Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 30th May 2009, 01:43 AM   #1
Default Sony Infared Code in C18

Here ya go:

Code:
/* **************************************************************************
;																			*
;	Filename: SIRC															*
;	Date: May 29, 2009														*
;	File Version: 001														*
;																			*
;	Author:   Jason Lopez													*
;	Company:  AtomSoft														*
;																			*
;****************************************************************************
; Notes:
; I am delaying in this code a nice even number every 200uS. Mainly because if
; i count each pulse it would be accurate but too many counts for a char type
; and i dont want to use a int. So i can get a 600 count by skipping every 300uS
; and the count will be a nice low number...3
; 
; For a 1.2mS aka HIGH i should get a count of 6 and for the start 2.4mS i should
; get a count of 12. Since this isnt too accurate i range it about 1-2 numbers 
; above or below the standard.
; 
;****************************************************************************
*/
#include <p18f2525.h>
#include <delays.h>
#include <string.h>

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67, XINST = OFF

unsigned char lTime;

unsigned char MyAdd;
unsigned char MyCmd;

#define irTris TRISCbits.TRISC4
#define irPin PORTCbits.RC4

void main(void);
void GetSIRC(unsigned char *address, unsigned char *command);

void main(void){

    char tmp;
    OSCCON = 0x72;                  //8MHz clock
    while(!OSCCONbits.IOFS);        //wait for osc stable

    TRISC = 0;
    irTris = 1;

    while(1){
        GetSIRC(&MyAdd,&MyCmd);
    }
}

void GetSIRC(unsigned char *address, unsigned char *command){
    unsigned char ir_add;
    unsigned char ir_cmd;
    char x;

StartLook:
    ir_add = ir_cmd = 0;

    while(irPin);               //wait for it to be low
    lTime = 0;                  //reset the counter

    while(irPin == 0){          //while the pin is low which is our pulse count
        lTime++;                //increment every 200uS until pin is high
        Delay100TCYx(4);        //200uS delay
    }

    if(lTime <= 10)             //Start too short
        goto StartLook;         //Restart
    if(lTime >= 14)             //Start too long
        goto StartLook;         //Restart
			
    lTime = 0;
    for(x=0;x<7;x++){           //repeat 7 times for command
        ir_cmd >>= 1;           //if it was skipped or is done ORing then shift over the 1

        while(irPin);           //wait for it to be low
        lTime = 0;              //reset the counter

        while(irPin == 0){      //while the pin is low which is our pulse count
            lTime++;            //increment every 200uS until pin is high
            Delay100TCYx(4);    //200uS delay
        }

        if(lTime >= 6)          //If its high then OR a 1 in else skip
            ir_cmd |= 0x40;     //if its less than 6 its a 0 so dont OR it		
								
    }
    for(x=0;x<5;x++){           //repeat 5 times for address/device
        ir_add >>= 1;           //if it was skipped or is done ORing then shift over the 1

        while(irPin);           //wait for it to be low
        lTime = 0;              //reset the counter

        while(irPin == 0){      //while the pin is low which is our pulse count
            lTime++;            //increment every 200uS until pin is high
            Delay100TCYx(4);    //200uS delay
        }

        if(lTime >= 6)          //If its high then OR a 1 in else skip
            ir_add |= 0x10;     //if its less than 6 its a 0 so dont OR it			
    }

    *address = ir_add;
    *command = ir_cmd;
}
I even made a little RBG led test using digits 1-7. Ill make a small vid and show in a bit.

Last edited by AtomSoft; 30th May 2009 at 03:39 AM.
AtomSoft is online now  
Old 30th May 2009, 01:54 AM   #2
Default

Hi Jason,

You have been busy lately. I wrote the same code a while back but mine is slightly different. I notice that you are oring the received bit into bit 7 and so your data is left justified. By oring into the lower bits you get the values published by Sony.

Here's mine for comparison,
Code:
#define IR_in   PORTAbits.RA6
#define IR_tris TRISAbits.TRISA6

unsigned char GetPulse(void){
unsigned char count;
    count=0;
    IR_tris=1;
    while(IR_in==1); 
    while(IR_in==0){
        count++;
        Delay100TCYx(1);   //50uS assumes 8MHz
    }
    return count;
}

void GetIR(char *cmd,char *dev){
char i;
    *cmd=0;
    *dev=0;
    while(GetPulse()<40);    //wait for start pulse
    for(i=0;i<7;i++){
        *cmd=*cmd>>1;
        if(GetPulse()>18)
            *cmd|=0x40;
    }
    for(i=0;i<5;i++){
        *dev=*dev>>1;
        if(GetPulse()>18)
            *dev|=0x10;
    }
}
Mike.
Pommie is online now  
Old 30th May 2009, 02:37 AM   #3
Default

Darn i knew i miscounted bits since its 7 bits you or 0x40 for the 7th and since its 5 bits for address you or with 0x10

Thanks i fixed my code now your a great fixer dude!
AtomSoft is online now  
Old 30th May 2009, 02:39 AM   #4
Default

Mike the thing is i do you tabs. The code is formatted well in MPLAB i dont use spaces i use tabs. But when i post here its hideous.
AtomSoft is online now  
Old 30th May 2009, 02:43 AM   #5
Default

Code in first post is now updated. May also look cleaner.
AtomSoft is online now  
Old 30th May 2009, 03:14 AM   #6
Default

Jason,

If you set MPLAB to convert tabs to spaces then it comes out fine.

Mike.
Pommie is online now  
Old 30th May 2009, 03:20 AM   #7
Default

OOPS i knew i forgot to do something when i redid my PC lol thanks i forgot about that option completely lol. heh look at this:


I had to attach the code. I set the tab to space thing and closed and reopened but still ugly looking lol.
Attached Files
File Type: c sirc.c (3.7 KB, 16 views)

Last edited by AtomSoft; 30th May 2009 at 03:25 AM.
AtomSoft is online now  
Old 30th May 2009, 03:29 AM   #8
Default

Looks good. It's always pleasing when something like this finally works.

Mike.
Pommie is online now  
Old 30th May 2009, 03:35 AM   #9
Default

i fixed the coding issue! I had to manually replace all the spacing with new tabs lol
Code:
/* **************************************************************************
;																			*
;	Filename: SIRC															*
;	Date: May 29, 2009														*
;	File Version: 001														*
;																			*
;	Author:   Jason Lopez													*
;	Company:  AtomSoft														*
;																			*
;****************************************************************************
; Notes:
; I am delaying in this code a nice even number every 200uS. Mainly because if
; i count each pulse it would be accurate but too many counts for a char type
; and i dont want to use a int. So i can get a 600 count by skipping every 300uS
; and the count will be a nice low number...3
; 
; For a 1.2mS aka HIGH i should get a count of 6 and for the start 2.4mS i should
; get a count of 12. Since this isnt too accurate i range it about 1-2 numbers 
; above or below the standard.
; 
;****************************************************************************
*/
#include <p18f2525.h>
#include <delays.h>
#include <string.h>

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67, XINST = OFF

unsigned char lTime;

unsigned char MyAdd;
unsigned char MyCmd;

#define irTris TRISCbits.TRISC4
#define irPin PORTCbits.RC4

#define Led_Red LATCbits.LATC1
#define Led_Blue LATCbits.LATC2
#define Led_Green LATCbits.LATC3

void main(void);
void GetSIRC(unsigned char *address, unsigned char *command);

void main(void){

char tmp;
OSCCON = 0x72;      			//8MHz clock
while(!OSCCONbits.IOFS);		//wait for osc stable

TRISC = 0;
irTris = 1;

while(1){
GetSIRC(&MyAdd,&MyCmd);

switch(MyCmd){
    case 0:
        Led_Red = 0;
        Led_Blue = 1;
        Led_Green = 1;
        break;
    case 1:
        Led_Red = 1;
        Led_Blue = 0;
        Led_Green = 1;
        break;
    case 2:
        Led_Red = 1;
        Led_Blue = 1;
        Led_Green = 0;
        break;
    case 3:
        Led_Red = 0;
        Led_Blue = 0;
        Led_Green = 1;
        break;
    case 4:
        Led_Red = 1;
        Led_Blue = 0;
        Led_Green = 0;
        break;
    case 5:
        Led_Red = 0;
        Led_Blue = 0;
        Led_Green = 0;
        break;
    case 6:
        Led_Red = 1;
        Led_Blue = 1;
        Led_Green = 1;
        break;
}
}
}

void GetSIRC(unsigned char *address, unsigned char *command){
    unsigned char ir_add;
    unsigned char ir_cmd;
    char x;

StartLook:
    ir_add = ir_cmd = 0;

    while(irPin);               //wait for it to be low
    lTime = 0;                  //reset the counter

    while(irPin == 0){          //while the pin is low which is our pulse count
        lTime++;                //increment every 200uS until pin is high
        Delay100TCYx(4);        //200uS delay
    }

    if(lTime <= 10)             //Start too short
        goto StartLook;         //Restart
    if(lTime >= 14)             //Start too long
        goto StartLook;         //Restart
			
    lTime = 0;
    for(x=0;x<7;x++){           //repeat 7 times for command
        ir_cmd >>= 1;           //if it was skipped or is done ORing then shift over the 1

        while(irPin);           //wait for it to be low
        lTime = 0;              //reset the counter

        while(irPin == 0){      //while the pin is low which is our pulse count
            lTime++;            //increment every 200uS until pin is high
            Delay100TCYx(4);    //200uS delay
        }

        if(lTime >= 6)          //If its high then OR a 1 in else skip
            ir_cmd |= 0x40;     //if its less than 6 its a 0 so dont OR it		
								
    }
    for(x=0;x<5;x++){           //repeat 5 times for address/device
        ir_add >>= 1;           //if it was skipped or is done ORing then shift over the 1

        while(irPin);           //wait for it to be low
        lTime = 0;              //reset the counter

        while(irPin == 0){      //while the pin is low which is our pulse count
            lTime++;            //increment every 200uS until pin is high
            Delay100TCYx(4);    //200uS delay
        }

        if(lTime >= 6)          //If its high then OR a 1 in else skip
            ir_add |= 0x10;     //if its less than 6 its a 0 so dont OR it			
    }

    *address = ir_add;
    *command = ir_cmd;
}
Thanks Pommie (MIKE) and MIKE lol. I fixed the first post code with the nice looking code so everyone can understand it better lol

Last edited by AtomSoft; 30th May 2009 at 03:40 AM.
AtomSoft is online now  
Old 30th May 2009, 04:28 AM   #10
Default

Atom that's great work i'm just starting some c18. looked at some lcd code you did it was very good to. Maybe i can to lol.
be80be is online now  
Old 30th May 2009, 11:51 AM   #11
Default

Nice work. Looks much nicer with tabs 4 instead of tabs 8.
Mike, K8LH is offline  
Old 30th May 2009, 12:20 PM   #12
Default

heh thanks guys. i hope people find this useful.
AtomSoft is online now  
Old 30th May 2009, 09:13 PM   #13
Default

I just made a nice remote using SONY (SIRC) Protocol.

Code:
/* **************************************************************************
;																			*
;	Filename: SIRC															*
;	Date: May 29, 2009														*
;	File Version: 001														*
;																			*
;	Author:   Jason Lopez													*
;	Company:  AtomSoft														*
;																			*
;****************************************************************************
; Notes:
;
*/
#include <p18f1320.h>
#include <delays.h>
#include <string.h>

#pragma config WDT = OFF, LVP = OFF, OSC = INTIO2, MCLRE = OFF

#define irTris TRISAbits.TRISA0
#define irPin  LATAbits.LATA0

#define UpTris  TRISBbits.TRISB5
#define DwnTris TRISBbits.TRISB4

#define Up_Btn  PORTBbits.RB5
#define Dwn_Btn PORTBbits.RB4


void main(void);
void SendSIRC(unsigned char Dev, unsigned char Cmd);
void PulseIt(unsigned char time);

void main(void){

    char tmp;
    OSCCON = 0x72;                  //8MHz clock
    while(!OSCCONbits.IOFS);        //wait for osc stable
    ADCON1 = 0xFF;

    UpTris = 1;
    DwnTris = 1;
    irTris = 0;

    SendSIRC(1,1);
    while(1){
        if(Up_Btn == 0){
            SendSIRC(1,0);
            Delay10KTCYx(1);
        }
        if(Dwn_Btn == 0){
            SendSIRC(1,1);
            Delay10KTCYx(1);
        }
    }
}

void SendSIRC(unsigned char Dev, unsigned char Cmd){
    char x;
    PulseIt(4);             //Logic Start

    for(x=0;x<7;x++){
        if(Cmd & 0x01)
            PulseIt(2);     //Logic 1
        else
            PulseIt(1);     //Logic 0
        Cmd <<= 1;
    }
    for(x=0;x<5;x++){
        if(Dev & 0x01)
            PulseIt(2);     //Logic 1
        else
            PulseIt(1);     //Logic 0
        Cmd <<= 1;
    }
    Delay10KTCYx(90);       //45mS Delay
}

void PulseIt(unsigned char time){
    unsigned char x,y,z;

for(y=0;y<time;y++){
    for(x=0;x<24;x++){          //600uS worth
        irPin = 1;
        Delay10TCY();
        Nop();
        Nop();
        Nop();                  //About 7uS
        irPin = 0;
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Delay10TCY();
        Delay10TCY();           //About 18uS
    }
}

    for(x=0;x<24;x++){          //600uS worth
        irPin = 0;
        Delay10TCY();
        Nop();
        Nop();
        Nop();                  //About 7uS
        irPin = 0;
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Nop();
        Delay10TCY();
        Delay10TCY();           //About 18uS
    }
}
AtomSoft is online now  
Old 30th May 2009, 09:27 PM   #14
Default

Everytime I check the new posts on this forum, you've created something new. Good work.
colin mac is offline  
Old 30th May 2009, 09:35 PM   #15
Default

We keep trying to tell him "quality not quantity" but he won't listen (lol)...
Mike, K8LH is offline  
Reply

Tags
c18, code, infared, sony

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
NEED help in connecting infared sensor to MCU(18f4520) meijunfun Micro Controllers 4 17th April 2009 12:15 AM
infared circuit lovelyman72 General Electronics Chat 1 16th May 2005 11:33 AM
Laser or Infared Lap Counter WoomaHodal Electronic Projects Design/Ideas/Reviews 1 29th April 2005 07:14 PM
infared remote blackbag General Electronics Chat 0 22nd July 2003 12:27 AM



All times are GMT. The time now is 12:46 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker