PIC programming

Thread Starter

ericyeoh

Joined Aug 2, 2009
58
1.Could anyone tell me whats really happen to this:

void interrupt(void)
{
if(intcon & (1<<T01F))
{
Clear_bit(intcon, T01f);
}
}

The PIC ucontroller 16F84A.
2.How is the function of "1<<T01F" provide true?
 

forum.user

Joined Jun 19, 2010
2
Hi,

In INTCON register T01F is 2 bit so, 1 is shifted by 2 places i,e 4.

1<<T01F is nothing but 0x01 << 2 i,e 4
and intcon & (1<<T01F) means (0x04 & 0x04) = 1 = TRUE
 

t06afre

Joined May 11, 2009
5,934
A clumsy way of doing it. And it will only obscure the code. It seams like T01F is already defined as a bit. So then it is correct to write in C
Rich (BB code):
if(T01F) T01F=0;
Which C compiler do you use Ericyeoh? The 16f84 is old, and the C compilers has evolved a lot since the the 16f84 chip was relased. So if your code is old, it may be somwhat out of date.
 
Last edited:

Dad_addy

Joined Apr 12, 2010
4
Can anyone there help me with C programming of 16F88 A/D converter that converts sensor signal connected to PORTA bit 0 (RA0) to digital signal. The chip operates between 2 volts to 4 volts. Based on the output voltage obtained from the sensor, a 7-Segment is illuminateed if a voltage obtained is from 2.48 to 3.30 volts to get a letter U and letter E if the voltage is from 2.6 volts to 3.65 volts. The most significant bit of the 7-Segment display is a.
 

t06afre

Joined May 11, 2009
5,934
And what is your problem;) Can you tell us what you have done so far and why you struggle. And please name the C compiler you are using
 

AlexR

Joined Jan 16, 2008
732
A clumsy way of doing it. And it will only obscure the code. It seams like T01F is already defined as a bit. So then it is correct to write in C
Rich (BB code):
if(T01F) T01F=0;
Which C compiler do you use Ericyeoh? The 16f84 is old, and the C compilers has evolved a lot since the the 16f84 chip was relased. So if your code is old, it may be somwhat out of date.
The code:
Rich (BB code):
if(intcon & (1<<T01F))
is the correct way to do bit testing in C. Compilers that allow direct manipulation of register bits use propriety non-standard extensions to the C language. They may make it easier to write code but makes porting code between compiler it a real pain in the posterior.
If T01F is bit 2 of the intcon register it would have been defined as equal to 2.
I.E.
Rich (BB code):
#define T01F 2
so the statement
Rich (BB code):
if(T01F)
{
  T01F=0;
}
would be nonsense and should not compile since you are effectively saying
Rich (BB code):
if(2)
{
  2=0
}
 

t06afre

Joined May 11, 2009
5,934
Then it comes to microcontrollers and C. I am much more concern about writing code that the optimizer can understand and optimize, versus portability. Also bit fields is a part of ANSI C.
But anyway The OP uses BoostC so if( intcon & (1<<T0IF) ) is the same as if( test_bit( intcon, T0IF) )
I am used to HI-Tech C and here
if(T01F)
{
T01F=0;
}
will this make sense. The optimizer will understand it and compile it to optimized code using bit testing in the machine code
 
Last edited:

Tahmid

Joined Jul 2, 2008
343
If T01F is bit 2 of the intcon register it would have been defined as equal to 2.
I.E.
Rich (BB code):
#define T01F 2
so the statement
Rich (BB code):
if(T01F)
{
  T01F=0;
}
would be nonsense and should not compile since you are effectively saying
Rich (BB code):
if(2)
{
  2=0
}
Hi,
In some compilers, eg. mikroC, T01F is not predeclared as 2, rather as INTCON.2, so in such circumstances, the code is correct.

So the code would stand as:
Rich (BB code):
if(INTCON.2)
{
  INTCON.2=0;
}
Hope this helps.
Tahmid.
 

AlexR

Joined Jan 16, 2008
732
Hi,
In some compilers, eg. mikroC, T01F is not predeclared as 2, rather as INTCON.2, so in such circumstances, the code is correct.

So the code would stand as:
Rich (BB code):
if(INTCON.2)
{
  INTCON.2=0;
}
Hope this helps.
Tahmid.
True but in the context of the original question, for the code
Rich (BB code):
void interrupt(void)
{
         if(intcon & (1<<T01F))
       {
                Clear_bit(intcon, T01f);
        }
}
to be valid T01F must be defined as 2 (or some number between 0 and 7) and in that context the statement T01F = 0 is rubbish and will throw up a compile error.
 

t06afre

Joined May 11, 2009
5,934
I guess everybody is correct, as this depends very much on the compiler and how it solves bit operations. Since the OP use BoostC my answer will not apply.
 
Top