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.

[SOLVED] Please explain this C code (LPC2148)

Status
Not open for further replies.

ps_arunkumar

Member level 1
Joined
Nov 24, 2011
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,545
I got a program with my LPC2148 development kit. The program is for enabling interrupt on the controller. I couldn't understand the following piece of code. Also, the following line, confuses me what is it doing. I have attached the whole program. Please help me what is the line following line do and brief explanation is really appreciated.

VICIntSelect &= ~ VIC_BIT(VIC_EINT2);

Code:
void init_ext_interrupt()
{
  EXTMODE = EXTMODE_EXTMODE2_MASK;
  
  EXTPOLAR &= ~EXTPOLAR_EXTPOLAR2_MASK;

  PINSEL0 = (PINSEL0 & ~(3 << 30)) | (1 << 31);
  
  /* initialize the interrupt vector */
  VICIntSelect &= ~ VIC_BIT(VIC_EINT2);           // EINT0 selected as IRQ
  VICVectAddr5 = (unsigned int)Ext_ISR;        // address of the ISR
  VICVectCntl5 = VIC_ENABLE | VIC_EINT2;
  VICIntEnable = VIC_BIT(VIC_EINT2);              // EINT0 interrupt enabled

  EXTINT &= ~EXTINT_EINT2_MASK;	
}
 

Re: Please explain this C code?

You should download and read the guide for LPC from https://www.hitex.com/index.php?id=download-insiders-guides

If you want to set an interrupt request as fast interrupt then you need to set the bit of that interrupt to 1 in the VICIntSelect register, your line clears the bit that represents EIN0 to make sure it is 0 and assigned to IRQ.
0 is the default value so you will not see that line used sometimes.

You can also check my application https://www.edaboard.com/threads/196143/#post951883
 

Re: Please explain this C code?

What is this line mean. Could explain the operation of AND and NOT doing there. Please explain me. I am seeing more lines with AND, OR notations. I couldn't figure out that. Please don't ask me to study C book or work with Turbo C. Please explain what this line will do.

VICIntSelect &= ~ VIC_BIT(VIC_EINT2);
 

Re: Please explain this C code?

Basically it clears the bit as I have explained using the fact that any value ANDed with 0 results to 0.

Suppose that you have a register with a value (I'll give you 8bit examples) like 0b10101010 and you want to set bit0 to a value of 1 , this would be simple using
0b10101010 | 0b00000001 which results to 0b10101011

Now suppose the opposite , you want to clear bit1 (not bit0), what you need is to AND it with 0 so 0b10101010 & 0b11111101 results to 0b10101000

When you want to clear or set a specific bit then instead of calculating the value you should use you can use the shift operator, for example

0b10101010 | (1<<0) which results to 0b10101011

to do the same thing with the AND operator you need a value that has 1s in all bits except from the one you want to clear so

(1<<1) equals 0b00000010 but what we want is to flip all bits in order to get 0b11111101 , this can easily be written as ~0b00000010 so in the above example
0b10101010 & ~(1<<1) results to 0b10101000

In your code there is a macro somewhere that translates VIC_BIT(VIC_EINT2) to VIC_BIT(16) and that to 0x00010000 , this is fine if you want to use it with OR to set the bit but in order to clear it you need to AND the compliment of this which is ~0x00010000 and equals 0xFFFEFFFF
 

Re: Please explain this C code?

What is the initial value of VICIntSelect. As per my understanding with your good example, I should NOT VIC_BIT(VIC_EINT2) and then should perform AND operation with VICIntSelect. So what is the value of VICIntSelect.

I used you ARMwizard tool and it was an excellent tool. Thanks for the tool.
 

Re: Please explain this C code?

The initial value of all bits in the register is 0 that is why I wrote in the previous post that many times you will not see this line used to clear the value.
That beiing said, clearing a bit although it should already be 0 in not a bad thing.

The reset values of all registers are described in the user manual of the mcu

I used you ARMwizard tool and it was an excellent tool. Thanks for the tool.

Thanks

---------- Post added at 17:25 ---------- Previous post was at 15:36 ----------

What is the initial value of VICIntSelect. As per my understanding with your good example, I should NOT VIC_BIT(VIC_EINT2) and then should perform AND operation with VICIntSelect..

The NOT operator (!) is not the same operator as 2s compliment (~) .

~(0b10101010) = 0b01010101
~(0b11110000) = 0b00001111
~(0b10000000) = 0b01111111

!(0b10101010) = 0b00000000
!(0b01010101)=0b00000000
!(0b00000000)= 0b00000001

!(any non 0 value) = 0b0 (decimal 0)
!(0) = 0b1 (decimal 1)
 

Re: Please explain this C code?

I posted previously for AND and OR operation. One senior explained me with this. Here after reset the default was set to "xxxx". After AND operation with "1111", the pin still stays as "xxxx". Could you please explain why.

Code:
If we take the constants first:
 (3 << 30) in binary: 1100.0000.0000.0000.0000.0000.0000.0000
~(3 << 30) in binary: 0011.1111.1111.1111.1111.1111.1111.1111
 (1 << 31) in binary: 1000.0000.0000.0000.0000.0000.0000.0000

PINSEL0 at start           xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx
after PINSEL0 & ~(3 << 30) 00xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx
after PINSEL | (1 << 31)   10xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx
 

Any value ANDed with 1 keeps the previous value
Any value ANDed with 0 results to 0

it's the opposite with OR

Any value ORed with 1 results to 1
Any value ORed with 0 keeps the previous value
 

But my question is with that xxxx. Does that mean they are "1111" after reset. I will check in datasheet regarding this.
But if you explain me i will be able to understand more.
 

There is no x value , all the bits are 0 after reset.

The x represent any value, 0 or 1, what he was trying to tell you is that all the bits except from the ones which are ANDed with 0 will keep the value they had
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top