Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Some basic C coding questions.

Status
Not open for further replies.

maniac84

Full Member level 6
Joined
Mar 4, 2012
Messages
337
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
3,661
Hi guys,
I have some basic c language questions.
I'm using Microchip IC and I've seen some routine declare like this: far void Function(void)
Why is it there's a 'far' word in front?
 

The "far" keyword typically indicates the use of the large memory model versus the "near" keyword typically indicates the use of the small memory model.

Typically the keyword "far" and "near" indicate restrictions on the location within either Data (RAM) or Program (ROM) storage depending on the object the keyword is used in conjunction.

To which Compiler are you referring?

Once the exact compiler to which you are referring is known, I should be able to give you specifics.

BigDog
 

From what I can see from the coding, all of the routines in the coding all use the 'far' keyword. What kind of restriction are you talking about? What is the difference if there's not 'far' keyword?
 

It is difficult to be specific without knowing the exact compiler to which you are referring.


However, many microcontroller have restrictions on how various regions of storage, both RAM and ROM, are accessed.

The Microchip C18 Compiler for example, in the case of functions/routines, requires their location, the function pointer, be located in within a 16-bit address range for small memory model "near" keyword and a 24-bit address range for large memory model "far" keyword. The default, neither keyword is specified, is the small memory model.

Reference: Microchip C18 C Compiler User's Guide, Section: 3.1 Memory Models, Page: 37


BigDog
 

It is difficult to be specific without knowing the exact compiler to which you are referring.


However, many microcontroller have restrictions on how various regions of storage, both RAM and ROM, are accessed.

The Microchip C18 Compiler for example, in the case of functions/routines, requires their location, the function pointer, be located in within a 16-bit address range for small memory model "near" keyword and a 24-bit address range for large memory model "far" keyword. The default, neither keyword is specified, is the small memory model.

Reference: Microchip C18 C Compiler User's Guide, Section: 3.1 Memory Models, Page: 37


BigDog
Noted with thanks.
I got another question. The coding also always uses the below:
memset(chTmp, 0, sizeof(chTmp));
I know that this routine means chTmp is equals to 0.
Then why don't they just write "chTmp = 0;" ?
 

Answering the last question, 'chTmp' doesn't have to be a single byte if you use memset. Rather than make just one address equal to zero, memset will fill all the bytes that make up the size of the variable, whether it be a single byte, a word, a float or even an array.

Brian.
 

Noted with thanks.

Below is some code I encounter:

Code:
#pragma code My_HiPrio_Int=0x0008	//high priority interrupt
#pragma code
#pragma interrupt chk_isr

void My_HiPrio_Int(void)
{
	_asm
	GOTO chk_isr
	_endasm
}

void chk_isr(void)		/*Serial Interrupt*/
{	
	INTCONbits.GIE = 0;
	if(PIR3bits.RC2IF==1)		// Receive
		RC_ISR();//see eusart.c
	if (PIR1bits.RCIF==1)
		RA_ISR();
	INTCONbits.GIE = 1;
}

It's for serial interrupt. I need to run 'My_HiPrio_Int' forever to check for receiving data right?
Is it that just by putting '#pragma', it is forever running?
 

Correct! The ISR is called by a hardware signal from one of the EUSARTs every time a byte has been received. It alerts your program that it's time to read the serial data before it gets overwritten by something else arriving.

The code you show is for a PIC with two EUSARTS, when a byte has arrived, it checks which EUSART received it then calls RC_ISR() if it was EUSART 2 or RA_ISR if it was EUSART 1. It probably isn't necessary to to disable GIE and re-enable it afterwards, the ISR hardware will probably do that for you and while disabled, other interrupts might be ignored.

Brian.
 

Correct! The ISR is called by a hardware signal from one of the EUSARTs every time a byte has been received. It alerts your program that it's time to read the serial data before it gets overwritten by something else arriving.

The code you show is for a PIC with two EUSARTS, when a byte has arrived, it checks which EUSART received it then calls RC_ISR() if it was EUSART 2 or RA_ISR if it was EUSART 1. It probably isn't necessary to to disable GIE and re-enable it afterwards, the ISR hardware will probably do that for you and while disabled, other interrupts might be ignored.

Brian.

How do we indicate that it is a ISR?
Is it by declaring "#pragma interrupt" on the routine?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top