sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
Need Help about C program of RC5. |
Posted: Thu Jun 17, 2010 12:23 am |
|
|
This Program is written for the PIC12F629 in CCS v4.012 and the I/O Pin used in this program is GPIO0=TX, GPIO1=RX, GPIO2=IR. If i want to run it on 18F252. What changes that i have to make to run on it. I/O pins and any other thing.
Want to change Tx to Pin # 17 and Rx to Pin # 18 of PIC18F232 and want to put IR on Pin # 15.
Please help me out. I found this program on the EDA board Forum and the post is really old and author is not replying on the post.
Code: |
#include <12F629.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOCPD //No EE protection
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8)
typedef unsigned int8 U8;
typedef unsigned int16 U16;
typedef int1 BIT;
#define VOID void
#pragma use fast_io (A)
#pragma byte GPIO = 0x05
#pragma byte TMR0 = 0x01
#pragma byte INTCON = 0x0B
#pragma byte PIR1 = 0x0C
#pragma bit TMR1F = PIR1.0
#pragma byte IOC = 0x96
#pragma bit IR_IOC = IOC.2
#pragma bit IR_PIN = GPIO.2
#pragma bit IR_INT = INTCON.0
#define IR_TIME TMR0
#define enable_INT_IR() IR_IOC = 1
#define clr_INT_IR() IR_PIN = 1; IR_INT = 0
#define is_INT_IR() (IR_INT != 0)
#define is_SIGNAL_IR() (IR_PIN==0)
#define is_IDLE_IR() (IR_PIN==1)
#define set_TIME_IR(tm) IR_TIME=tm
#define get_TIME_IR() IR_TIME
#define nIRtimeBase 32 // 32uS
#define nIRtolerance 30 // +/- 30%
#define defIRtime 889 // 889uS RC-5/RC-5X 1 physical (half logic) bit time
#define minIRtime ((((((100-nIRtolerance)*defIRtime)*10)/(nIRtimeBase*100))+5)/10)
#define maxIRtime ((((((100+nIRtolerance)*defIRtime)*10)/(nIRtimeBase*100))+5)/10)
U8 gIRstat; // IR state number
enum { stIR_NONE=0, stIR_IDLE, stIR_SIGNAL };
U16 gIRdata; // IR data (14 logic bits)
#define add_Data_0_IR() gIRdata <<= 1
#define add_Data_1_IR() gIRdata <<= 1; gIRdata |= 1
U8 gIRcount; // IR bit counter
#define nIRcount (14*2) // 14 logic bits = 28 physical bits
//-U8 gIRruntime; // IR running time (counting only if gIRstat != stIR_NONE)
#define nIRruntime 30000 // 25mS: 14*(889+889)=24.892mS
//#define timeout_RUNTIME_IR() (get_timer1() > nIRruntime)
#define timeout_RUNTIME_IR() (TMR1F != 0)
#define set_RUNTIME_IR(tm) set_timer1(65536UL-(tm)); TMR1F = 0
BIT bIRphysicalBitCount; // 0:1st physical bit, 1:2nd physical bit
BIT bIRphysicalBitData; // last physical bit data
BIT bIRnewHit; // 1:new IR code is received
VOID InitIR( VOID ) // initial IR engine
{
gIRstat = stIR_NONE;
bIRnewHit = FALSE;
// hardware dependent: set correct snapped interrupt edge setting
enable_INT_IR();
clr_INT_IR();
}
VOID TimeoutIR( VOID ) // check IR running timeout
{
if( gIRstat != stIR_NONE) {
if( timeout_RUNTIME_IR() ) { // timeout
InitIR();
}
}
}
VOID PollIR( VOID ) // put it in ISR (trigger when IR_PIN edge is changed)
{
U8 mIRtime;
mIRtime = get_TIME_IR(); set_TIME_IR(0);
switch( gIRstat ) {
case stIR_NONE:
if( is_SIGNAL_IR() ) { // 1st logic bit (start bit) is 1 (01)
// IR engine is starting
gIRstat++; // gIRstat = stIR_IDLE;
//----------- gIRruntime = 0; // IR running time is counting
set_RUNTIME_IR(nIRruntime); // IR running time is counting
gIRdata = 0; // it is not necessary if mask unused bits before using it
gIRcount = 1; // as hit 1st physical bit
bIRphysicalBitCount = 1; // ready to process 2nd physical bit
return;
}
return;
case stIR_IDLE: // now is IR.IDLE signal (process last SIGNAL)
if( mIRtime > 2*maxIRtime ) { // time > 2t
goto _error_IDLE_PollIR; // last signal too long
}
if( mIRtime < 1*minIRtime ) { // time < 1t
goto _error_IDLE_PollIR; // last signal too short
}
bIRphysicalBitData = 0; // now is IDLE
if( bIRphysicalBitCount == 0 ) { // now is 1st physical bit (1x)
if( mIRtime > 2*minIRtime ) { // time > 2t
goto _error_IDLE_PollIR; // bad code, bi-phase no this (11) combination
}
bIRphysicalBitCount = 1; // ready to process 2nd physical bit
} else { // now is 2nd physical bit (01)
add_Data_1_IR(); // add logic bit data 1 (01)
if( mIRtime < 2*minIRtime ) { // time < 2t
bIRphysicalBitCount = 0; // ready to process 1st physical bit
} else { // time >= 2t, it is 011 (extra physical bit)
gIRcount++; // add extra physical bit count
bIRphysicalBitCount = 1; // ready to process 2nd physical bit
}
}
break;
case stIR_SIGNAL: // now is IR.SIGNAL signal (process last IDLE)
if( mIRtime > 2*maxIRtime ) { // time > 2t
goto _error_SIGNAL_PollIR; // last signal too long
}
if( mIRtime < 1*minIRtime ) { // time < 1t
goto _error_SIGNAL_PollIR; // last signal too short
}
bIRphysicalBitData = 1; // now is SIGNAL
if( bIRphysicalBitCount == 0 ) { // now is 1st physical bit (0x)
if( mIRtime > 2*minIRtime ) { // time > 2t
goto _error_SIGNAL_PollIR; // bad code, bi-phase no this (00) combination
}
bIRphysicalBitCount = 1; // ready to process 2nd physical bit
} else { // now is 2nd physical bit (10)
add_Data_0_IR(); // add logic bit data 0 (10)
if( mIRtime < 2*minIRtime ) { // time < 2t
bIRphysicalBitCount = 0; // ready to process 1st physical bit
} else { // time >= 2t, it is 100 (extra physical bit)
gIRcount++; // add extra physical bit count
bIRphysicalBitCount = 1; // ready to process 2nd physical bit
}
}
break;
}
// ready to process next physical bit
gIRcount++;
if( gIRcount >= nIRcount ) { // the last physical bit
goto _complete_check_PollIR;
}
if( gIRcount == nIRcount-1 ) { // the 27th physical bit
if( bIRphysicalBitData == 0 ) { // now is 0 as complete, due to no more new edge will be changed
bIRphysicalBitCount = 0; // as ready to process 1st physical bit
add_Data_0_IR(); // add logic bit data 0 (10)
goto _complete_check_PollIR;
}
}
// the 1~27th physical bit
gIRstat = bIRphysicalBitData == 0 ? stIR_SIGNAL : stIR_IDLE; // swap state
// hardware dependent: toggle snapped interrupt edge setting
//-clr_INT_IR();
return;
_complete_check_PollIR:
if( bIRphysicalBitCount == 0 ) { // correct, no any pending physical bit
InitIR();
// ex: check Start Bit, process Toggle(Repeat) Bit, translate Address/Command Bits....
bIRnewHit = TRUE;
return;
}
_error_IDLE_PollIR:
_error_SIGNAL_PollIR:
InitIR();
return;
}
void main()
{
port_a_pullups(0xFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
// TODO: USER CODE!!
printf( "IR\n" );
InitIR();
while(TRUE) {
if( is_INT_IR() ) {
clr_INT_IR();
PollIR();
if( bIRnewHit == TRUE ) { // new IR code is coming
bIRnewHit = FALSE;
// process IR event here
printf( "%4lX\n", gIRdata );
}
}
TimeoutIR();
}
}
|
|
|