Continue to Site

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.

  • 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.

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

Status
Not open for further replies.

sahu

Member
i am using mcroc .
when i use
PORTB =0b00111111;
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.
 
Try

Code:
PORTB.RB0=1;
PORTB.RB1=0;
PORTB.RB2=1;
PORTB.RB3=1;
PORTB.RB4=0;
 
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.
 
like this...
void main()
{
TRISA = 0x01; // setting RA0 analog input & RA3 -RA5 digital i\p
TRISB = 0x00; // setting port B as output.
// 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 ;
 
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?
 
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?
pic 16f72 & 16F676
both mcu i can use. i can change port b & c
 
Last edited:
Try

Code:
PORTB.RB0=1;
PORTB.RB1=0;
PORTB.RB2=1;
PORTB.RB3=1;
PORTB.RB4=0;

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 ?
 
On the 16f676 you need to set three registers
7.1 A/D Configuration and Operation
There are three registers available to control the
functionality of the A/D module:
1. ADCON0 (Register 7-1)
2. ADCON1 (Register 7-2)
3. ANSEL (Register 7-3)
7.1.1 ANALOG PORT PINS
The ANS7:ANS0 bits (ANSEL<7:0>) and the TRISA
bits control the operation of the A/D port pins. Set the
corresponding TRISA bits to set the pin output driver to
its high impedance state. Likewise, set the corresponding
ANS bit to disable the digital input buffer.

And the Comparator turn it off witch I think you did.

The 16f72 should work portb fine it's all I/O
 
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

I am use like this... for 16f676
Code:
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:
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.
 
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:
pic 16f676 code as below...
Code:
//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
PORTC =0b1111; etc
specific bits for o\p operating.
 
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
 
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

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.
 
Is this your configuration on your 676?

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)
                    ---------
*/
 
Is this your configuration on your 676?

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)
                    ---------
*/

yes !!!
 
In the MikroC software debugger, the following worked:

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)?
 

Attachments

  • test.c
    8.8 KB · Views: 211
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 .
 
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:

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.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top