1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PORTB =0b00111111; vs port bit (RB1_bit=0;)

Discussion in 'Microcontrollers' started by sahu, Aug 24, 2011.

  1. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    i am using mcroc .
    when i use
    and etc it work properly
    but when i use like this
    RB0_bit=1;
    RB1_bit=0;
    RB2_bit=1;
    RB3_bit=1;
    RB4_bit=0;

    only RB0_bit=1; show.rest
    RB1_bit=0;
    RB2_bit=1;
    RB3_bit=1;
    RB4_bit=0;
    show off
    where i wrong pl sausages me.
     
  2. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    Try

    Code (text):

    PORTB.RB0=1;
    PORTB.RB1=0;
    PORTB.RB2=1;
    PORTB.RB3=1;
    PORTB.RB4=0;
     
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    Make sure the pins aren't analogue, Analogue pins work fine as digital outputs but always read back as zero so the last bit set resets all the others.

    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3

    like this...
     
  6. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    If you were having a problem with PORTA, I would say it could be the analog settings. As far as I know, PORTB is always digital. Which PIC are you using?
     
  7. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    pic 16f72 & 16F676
    both mcu i can use. i can change port b & c
     
    Last edited: Aug 25, 2011
  8. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    i try it in 676 ver in my change portc in place of portb. portc 0 ,1 ,3 .work.but when portc 4 on rest portc 0 ,1 ,3 off.
    where i wrong ?
     
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,807
    Likes:
    135
    Location:
    morristown,tn
    On the 16f676 you need to set three registers
    And the Comparator turn it off witch I think you did.

    The 16f72 should work portb fine it's all I/O
     
  10. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    I am use like this... for 16f676
    Code (text):
    void main()
    {
        TRISA = 0x01; // setting RA0 analog input & RA3 -RA5 digital i\p
        //TRISC = 0x00; // setting port C as output.
                          TRISC.RC0=0;
                          TRISC.RC1=0;
                          TRISC.RC2=0;
                          TRISC.RC3=0;
        // Configure ADCON0 for channel AN0
        ADCON1=0x00;
         while(1)
     {
     ADCON0=0b00000001; //Left justified, Channel AN0 selected, ADON On
     Delay_ms(1) ; //Allow 1ms for holding cap to charge
     GO_DONE_bit=1; //Start A/D process
     while (GO_DONE_bit==1) //Wait for A/D to finish
     {
     }
      //  ANSEL = 0x01;  // RA0 analog input
        CMCON = 0x07 ;
    when i use The 16f72 should work portb fine : PORTB =0b00111111
    but when i use
    RB0_bit=1;
    RB1_bit=0;
    RB2_bit=1;
    RB3_bit=1;
    RB4_bit=0; etc
    only RB0_bit on rest off.
    means specific bits for o\p not working
    hope understand , what i am saying to all
     
    Last edited: Aug 25, 2011
  11. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    If you can, please show your full code for either the 16F72 or the 16F676. Because at the moment, I don't see anything wrong with the 16F72 portion of your code. In the 16F676 portion of code, I'll have to read the datasheet to see if what you are doing is correct. At the moment I don't see you doing anything other than doing an analog to digital conversion and then doing nothing with it.
     
  12. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    To turn off the comparator, wouldn't you set CMCON to 0x00? Otherwise by setting 0x07. you are setting the comparator up for multiplexed input with internal reference plus output. Although looking at the datasheet with 0x07, it looks like RA1 and RA0 are not connected.
     
    Last edited: Aug 26, 2011
  13. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    pic 16f676 code as below...
    Code (text):
    //float ch;
    unsigned int  ADC_Value;
    long ch , tlong;
    /*................................................. ...............................
     Define relay selection port pins
    .................................................. ...............................*/
    sbit RL1 at RC0_bit;
    sbit RL2 at RC1_bit;
    sbit RL3 at RC2_bit;
    sbit RL4 at RC3_bit;
    sbit RL5 at RC4_bit;
    //defined RL1 at PORTA,1;//PIN 12
    //sbit RL2 at RC0_bit;//PIN 10
    //sbit RL3 at RC1_bit;//PIN 09
    //sbit RL4 at RC2_bit;//PIN 08
    //sbit RL5 at PORTA,2;//PIN 11
    /*................................................. ...............................
     Define LED/ bzr selection port pins
    .................................................. ...............................*/
    //sbit L1  at  Rc5_bit;//PIN 05 hi\lo cut
    //sbit L2  at  Rc4_bit;//PIN 06 mains normal
    //sbit BZR at  Rc3_bit;//PIN 07 SOUND

    /*................................................. ...............................
     Define LED/ bzr selection port pins
    .................................................. ...............................*/

    sbit SW1 at  RA5_bit;//PIN 03 hi\lo cut enabol
    sbit SW2 at  RA4_bit;//PIN 02 QEK SATART


    void main()
    {
        TRISA = 0x01; // setting RA0 analog input & RA3 -RA5 digital i\p
        //TRISC = 0x00; // setting port C as output.
                          TRISC.RC0=0;
                          TRISC.RC1=0;
                          TRISC.RC2=0;
                          TRISC.RC3=0;
        // Configure ADCON0 for channel AN0
        ADCON1=0x00;
        //  ANSEL = 0x01;  // RA0 analog input
        CMCON = 0x07 ;
         while(1)
     {
     ADCON0=0b00000001; //Left justified, Channel AN0 selected, ADON On
     Delay_ms(100) ; //Allow 1ms for holding cap to charge
     GO_DONE_bit=1; //Start A/D process
     while (GO_DONE_bit==1) //Wait for A/D to finish
        {
        }

    //loop_01:
    //    L2 = 1;// mains normal on
      //  L1 = 0;// hi\lo cut off
        //BZR = 0;// SOUND off
        //RL4 = 0;// Output relay off R4
        /*................................................. ...............................
        Delay mode selection for user friendly...
        .................................................. ...............................*/

    //    if(SW2)
      //  {
        //    Delay_ms(5);
        //}
        //else
        //{
          //  Delay_ms(1);
        //}
        /* .................................................. .............................
        Main working starts here
        .................................................. ................................*/
       // L1 = 0; // delay indicator off.

        while(1)
        {
           ADC_Value = ADC_Read(0); // A/D conversion. Pin RA0 is an input.
            tlong =(long)ADC_Value*5000; // Convert the result in millivolts
            tlong = tlong/1023; // 0..1023 -> 0-5000mV
            ch = tlong; // Extract volts (thousands of millivolts)

            /*................................................. ...............................
            Main stabilizing is done here
            .................................................. ...............................*/

            if((ch<=4000)&&(ch>=0675))
            {
              if(ch>1035)       //115v
                {
                if(ch>1260)      //140v
                 {
                 if(ch>1503)       //167 v
                  {
                  if(ch>1800)      //200v
                    {
                   if(ch>2160)    //240v
                     {
                    if(ch>2592)    //= 288vac
                      {
                      if(ch>=3115)      //= 346vac
                        {
                          PORTC =0b1111;
                          }
                            else
                             PORTC =0b0111;
                             }
                             else
                            PORTC =0b1011;
                            }
                            else
                            PORTC =0b0011;
                        }
                        else
                        PORTC =0b1001;
                    }
                    else
                    PORTC =0b0001;
                }
                else
                PORTC =0b1000;
                }
                else
                PORTC =0b0000;
            //RL4 = 1;// output relay on.
           // L2 = 1; // mains indicator on
           // L1 = 0; // high //low voltage indicator off
            //BZR = 0; // high //low voltage indicator off
        }
        /*................................................. ...............................
        High voltage protection for load is done here
        .................................................. ...............................*/

        //if(ch > 2100)
       // {
       //     L1 = 1; // high // low voltage indicator on
            //BZR = 1; // cret sound
       //     RL4 = 0; // relay off
       //     L2 = 0; // Mains normal indicator off
       //    goto loop_01;
       // }
        /*................................................. ...............................
        Low voltage protection is done here
        .................................................. ...............................*/
        //if(ch <= 1300)
        //{
        //    L1 = 1; // high // low voltage indicator on
           // BZR = 1; // cret sound
          //  RL4 = 0; // relay off
          //  L2 = 0; // Mains normal indicator off
          //  goto loop_01;
         }
         }
    }  // End main()
    actually i want in place of
    specific bits for o\p operating.
     
  14. Mr RB

    Mr RB Well-Known Member

    Joined:
    Jul 22, 2008
    Messages:
    4,716
    Likes:
    194
    Location:
    Out there
    If you are using MikroC that feature is already included!

    You just use;
    PORTB.F3 = 1; // RB3 is HI
    PORTB.F3 = 0; // RB3 is now LOW

    Or you can use sbit;
    sbit RL1 at PORTC.F0; // defines a single bit variable, in this case it is a port bit
    RL1 = 1; // pin is HI
    RL1 = 0; // pin is LOW
     
  15. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    what's fundamental function of sbit RL1 at PORTC.F0 ?
    or why it not support sbit RL1 at RC0_bit;
    which red COLOR show ,it is my question.
     
  16. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    Is this your configuration on your 676?

    Code (c):


    /*        16F676 Configuration
              O = Output, I = Input
                        _________
                  Vdd   | 1  14 |  Vss
      (I) SW1 --> RA5   | 2  13 |  AN0 <-- ADC_Value (I)
      (I) SW2 --> RA4   | 3  12 |  RA1
             RA3/MCLR   | 4  11 |  RA2
                  RC5   | 5  10 |  RC0 --> RL1 (O)
      (O) RL5 <-- RC4   | 6   9 |  RC1 --> RL2 (O)
      (O) RL4 <-- RC3   | 7   8 |  RC2 --> RL3 (O)
                        ---------
    */

     
     
  17. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    yes !!!
     
  18. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    In the MikroC software debugger, the following worked:

    Code (c):

     if(ch>=3115)      //= 346vac
     {
          //PORTC =0b1111;
          RL1 = 1;
          RL2 = 1;
          RL3 = 1;
          RL4 = 1;
     }
     
    I set ch to 3200, and it executed the code. PORTC was set to 0x0F. Could it be an issue with your circuit(s)?
     

    Attached Files:

    • test.c
      File size:
      8.8 KB
      Views:
      127
  19. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    thank u my dear friend .ofter test real hordwaer i will inform hear.
     
  20. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    i test it with OSHON SOFT .found when i\p volt suddenly up or down , mcu goes like hange .its o\p (rl1 to rl4) show as lost i\p .
     
  21. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    Not sure where to go from here. I tested your code in Oshon Soft to try to understand what you are saying.

    I moved the following code to inside your loop so that the A/D conversion would take place more than one time:

    Code (c):

    GO_DONE_bit=1; //Start A/D process

    while (GO_DONE_bit==1) //Wait for A/D to finish
    {
    }
     
    The code with the AN0 set to 700, so ch=3421. The code set PORTC to 0x0F. One thing that I did notice is that the A/D conversion didn't take place past the first time. The simulator is setup for 100 clock cycles, and it went past that on the second conversion. I had to manually clear the GO_DONE bit to get the code to simulate a A/D conversion. I used the values 700 and 350 to test.

    Just as a clarification i/p = input pin, and o/p = output pin correct? I'm not sure what you mean by 'Its output pin (RL1 to RL4) show as lost input pin' Should lost be last? as in last input pin? Even if lost is last, I don't understand what you are trying to say there.
     

Share This Page