PIC programming

Discussion in 'Embedded Systems and Microcontrollers' started by ericyeoh, Jun 19, 2010.

  1. ericyeoh

    Thread Starter Active Member

    Aug 2, 2009
    58
    0
    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?
     
  2. forum.user

    New Member

    Jun 19, 2010
    2
    1
    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
     
    ericyeoh likes this.
  3. ericyeoh

    Thread Starter Active Member

    Aug 2, 2009
    58
    0
    Thx man ! Now it's clear for me.
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
    Code ( (Unknown Language)):
    1.  
    2. if(T01F) T01F=0;
    3.  
    4.  
    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: Jun 20, 2010
  5. Dad_addy

    New Member

    Apr 12, 2010
    4
    0
    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.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  7. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    The code:
    Code ( (Unknown Language)):
    1. 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.
    Code ( (Unknown Language)):
    1. #define T01F 2
    so the statement
    Code ( (Unknown Language)):
    1.  
    2. if(T01F)
    3. {
    4.   T01F=0;
    5. }
    would be nonsense and should not compile since you are effectively saying
    Code ( (Unknown Language)):
    1. if(2)
    2. {
    3.   2=0
    4. }
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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: Jun 20, 2010
  9. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    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:
    Code ( (Unknown Language)):
    1. if(INTCON.2)
    2. {
    3.   INTCON.2=0;
    4. }
    Hope this helps.
    Tahmid.
     
  10. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    True but in the context of the original question, for the code
    Code ( (Unknown Language)):
    1. void interrupt(void)
    2. {
    3.          if(intcon & (1<<T01F))
    4.        {
    5.                 Clear_bit(intcon, T01f);
    6.         }
    7. }
    8.  
    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.
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
Loading...