|
|
View previous topic :: View next topic |
Author |
Message |
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
Need Help regarding C program? |
Posted: Mon Jul 05, 2010 6:21 am |
|
|
The program is written for PIC 12F629. Want to change it for 18F252. The mid part is creating some problem and I am confused...
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)
|
This is not the issue. I can change it and the serial port pin.
The problem comes here.
Code: |
#include "RC5.h"
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
|
This above part of the program is confusing me a lot.
I don't know what I do I want to put IR interrupt at RB0 of PIC. What I do not understand this code....
The rest of the program is down.
Code: |
#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();
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Jul 05, 2010 7:55 am |
|
|
Several things.
First, the data sheet. The RC5.h stuff, is setting up registers to talk directly to the chip's hardware. The addresses of this are in the respective data sheets.
On the 'no problem' part, remember the 252, does not have an internal oscillator. You will need an external crystal. You would be much 'better off', choosing a chip like the 2520, which is pin compatible to the 252, and has got an internal oscillator (and is also now usually cheaper....).
Best Wishes |
|
|
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
|
Posted: Tue Jul 06, 2010 12:39 am |
|
|
Thanks for the reply.
About the hardware. I have only available 18F252. I will use external crystal, not a issue.
The issue is the min part of the program that is going above my head.
Code: |
#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
|
I am not expert in programing. Could you please guide me how can I do this.
I will be very thankful. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Jul 06, 2010 2:19 am |
|
|
Look in the data sheet for the 629. Start by looking for 'GPIO'. Find that this is the 'General purpose IO' port at address 5. The only port on this chip (you will have to make a decisaion which of the several ports you have available on the 252, you are going to substitute for it). Possibly suggest port B, since the code is using an interrupt, and PortA, does not offert this. Then look in the data sheet for the 252. PortB is at 0xF81. So substitute this.
Then the same for Timer0, etc. etc..
So the first line would become:
Code: |
#pragma byte GPIO = 0xF81
|
By keeping the name the same, you don't then have to alter the code using this.
However you have a lot of thinking then to do. For instance, 'Timer0', on the 629, is 8bit, while on the 252, it can be 8/16bit, so you will need to program it to 8bit mode, to emulate the timer on the 629.
You need to sit down with both data sheets, and start commenting the code. Work out what bits this stuff is setting/using, and then what the equivalents are on the second chip, and what other changes are needed. It is not hard, but rerquires you to be _meticulous_.
Best Wishes |
|
|
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
|
Posted: Tue Jul 06, 2010 11:42 pm |
|
|
Thanks man..
I checked the data sheet of PIC12F629 & PIC18F252. Got the table of Special function registers and the Hex address of each function is describe in the data sheet.
Printout of both the data sheet is in front of me.
Got address of INTCON,GPIO,PIR1.
Two things creating a problem:
IOC and TMR0.
Did not see the TMRO in PIC18F252 and IOC.
TMR0 as described in PIC12F629. Its a module's register but did not find that type in PIC18F252.
Can I use TMR2 register of PIC18F252 in place of TMR0 register?
And what can I do with this IOC? |
|
|
sarmad_101
Joined: 17 Jun 2010 Posts: 12
|
|
Posted: Wed Jul 07, 2010 12:52 am |
|
|
I have made these changes in the program at the starting....
Code: |
#pragma byte PORTB = 0xF81 //port RBO for IR data
#pragma byte TMR0 = 0xFCC
#pragma byte INTCON = 0xFF2
#pragma byte PIR1 = 0xF9E
#pragma bit TMR1F = PIR1.0
#pragma byte IOC = 0xF81 // interrupt on change to RB0 pin
#pragma bit IR_IOC = IOC.0 // RBO interrupt on change
#pragma bit IR_PIN = PORTB.0 // RBO IR PIN
#pragma bit IR_INT = INTCON.0
#define IR_TIME TMR0
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|