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] Accessing Individual Bits mikroC pro for PIC Help me

Status
Not open for further replies.

nandana

Newbie level 4
Joined
Dec 21, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,339
I want to turn on LEDs on portb of 16f628A. I wrote following code. I need to make my program as following.
1. Turn off led (00000000)
2. After 1sec turn on PORTB.F1 (00000010)
3. then After 1sec turn on PORTB.F2 (00000110)
4. then After 1sec turn on PORTB.F3 (00001110)
5. then After 1sec turn on PORTB.F4 (00011110)
then loop back to Turn off led

but when simulating using isis,,,
every time only one LED is turning on. it is look like following
1. Turn off led (00000000)
2. After 1sec turn on PORTB.F1 (00000010)
3. then After 1sec turn on PORTB.F2 (00000100)
4. then After 1sec turn on PORTB.F3 (00001000)
5. then After 1sec turn on PORTB.F4 (00010000)
then loop back to Turn off led

could you please some one help me to solve this !! Why I cant Access Individual Bit without changing others.e

Edited
I have attached all files including isis.
I am using Mikroc pro for PIC v5.6.1



Code:
void main(void)
{

  CMCON=0x07;
  TRISB = 0x01;

do
  { PORTB=0;
         Delay_ms(1000);
     PORTB.F1=0;  
         Delay_ms(1000);
     PORTB.F2=1; 
         Delay_ms(1000);
     PORTB.F3=1;
         Delay_ms(1000);
     PORTB.F4=1;
         Delay_ms(1000);
   } while(1);
}
 

Attachments

  • Accessing Individual Bits mikroC pro for PIC Help me.zip
    49.7 KB · Views: 81
Last edited:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main(void)
{
 
  CMCON=0x07;
  TRISB = 0x01;
 
do
  { PORTB= 0b00000000;
         Delay_ms(1000);
     PORTB= 0b00000010;  
         Delay_ms(1000);
     PORTB= 0b00000110; 
         Delay_ms(1000);
     PORTB= 0b00001110;
         Delay_ms(1000);
     PORTB= 0b00011110;
         Delay_ms(1000);
   } while(1);
}



Hi what compiler you are using, try updating the full port...
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
while(1){
    PORTB = PORTB | 0x00;
    shift = 0x01;
    for(i = 0, i < 3; i++){
        PORTB = PORTB | (shift << 1);
        Delay_ms(1000);
    }
    PORTB = PORTB & 0b11100001;
}

 
Last edited:

i think jayanth in your for loop may be 3 to 4 right ya wrong please tell me!!!
 

in your do loop invert your values instead, if its on it turns off else off it turns on.
Code:
do
  { //PORTB=0;
         Delay_ms(1000);
     PORTB.F1= ~PORTB.F1;  
         Delay_ms(1000);
     PORTB.F2= ~PORTB.F2; 
         Delay_ms(1000);
     PORTB.F3= ~PORTB.F3;
         Delay_ms(1000);
     PORTB.F4=  ~PORTB.F4;
   } while(1);
 

Hi the mistake is

Code C - [expand]
1
PORTB = PORTB | (shift << i);


1 instead of i, and 3 instead of 5..

I wrote in binary because can be changed for any order of data.........
 

i think again trouble in for loop because first time i = 0 then PORTB | (shift<<i) result is 0x0000 0001 and this is not getting our original question . so start i = 1 to 5 is the desired result am i right ya wrong ?
 

exactly

Code C - [expand]
1
2
3
4
5
6
7
8
while(1){
    PORTB  = 0x00; // | changed
    Delay_ms(1000); // changed
     for(i = 0, i < 4; i++){ // 4 changed
        PORTB = PORTB | (0x02 << i); // i changed
        Delay_ms(1000);   // first delay for 0 value changed
    }
}

 

Sorry for the mistake. Fixed code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
while(1){
    PORTB = PORTB | 0x00;
    shift = 0x10;
    for(i = 0, i < 3; i++){
        PORTB = PORTB | (shift << 1);
        Delay_ms(1000);
    }
    PORTB = PORTB & 0b11100011;
}
 
 
Initial state
 
PORTB = 0x00
shift = 0x10;
 
first loop of for, i = 0
 
    PORTB = 0x00 | (0b00000100) //PORTB = 0x10
 
second loop, i = 1
 
    PORTB = 0b00000100 | (0b00001000) //PORTB = 0b00001100
 
third loop, i = 2
 
    PORTB = 0b00001100 | (0b00010000)  // = 0b00011100 RB2-4 ON, RB0-1 OFF

 

Hi you put shift = 0x10 instead of 0b10 and 1 instead i and no delay for all 0 s and first time clearing port with | operator......
 

exactly

Code C - [expand]
1
2
3
4
5
6
7
8
while(1){
    PORTB  = 0x00; // | changed
    Delay_ms(1000); // changed
     for(i = 0, i < 4; i++){ // 4 changed
        PORTB = PORTB | (0x02 << i); // i changed
        Delay_ms(1000);   // first delay for 0 value changed
    }
}


now that is ok and clear bro!!!!
 

Hi,
I am using Mikroc pro for PIC v5.6.1
I have edited post and attached all files including isis.
I need to control Individual Bits on portb without changing other bits of portb.
 

hey nandana have u tried post #2 or post #8 as venkadesh_M suggested or not ?
 

This method is also correct. But I need to Access Individual Bits.
Because I am going to make a latch type output controller. I have 4 outputs from another circuit and I want to give those to a 16f628a and then make latch type outputs. so I want to Access Individual Bits.
any help accept kindly. thank you


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main(void)
{
 
  CMCON=0x07;
  TRISB = 0x01;
 
do
  { PORTB= 0b00000000;
         Delay_ms(1000);
     PORTB= 0b00000010;  
         Delay_ms(1000);
     PORTB= 0b00000110; 
         Delay_ms(1000);
     PORTB= 0b00001110;
         Delay_ms(1000);
     PORTB= 0b00011110;
         Delay_ms(1000);
   } while(1);
}



Hi what compiler you are using, try updating the full port...

- - - Updated - - -

hi nick703
i tried #2.
that method is correct for control a byte. But I need to Access Individual Bits.

Because I am going to make a latch type output controller. I have 4 outputs from another circuit and I want to give those to a 16f628a and then make latch type outputs. so I want to Access Individual Bits.
any help accept kindly. thank you

hey nandana have u tried post #2 or post #8 as venkadesh_M suggested or not ?


I haven't complete input part yet. Firstly I tried to make these out puts. Then I face this problem
 

u tried that type of code
Code:
void main(void)
{

  CMCON=0x07;
  TRISB = 0x01;
  PORTB=0;

do
  { 
         Delay_ms(1000);
     PORTB.F1=1;  
         Delay_ms(1000);
     PORTB.F1=1;
     PORTB.F2=1; 
         Delay_ms(1000);
     PORTB.F1=1;
     PORTB.F2=1;
     PORTB.F3=1;
         Delay_ms(1000);
     PORTB.F1=1;
     PORTB.F2=1;
     PORTB.F3=1;
     PORTB.F4=1;
         Delay_ms(1000);
   } while(1);
}
 

I tried this also. But every time after Delay only last command makes out put.
The problem is when I Access Individual Bits other all bits going to zero.
help me. If you can download my attached file, then you can see the result.
Help me.!!
And thank you very much for waiting your valuable time for me.!!

u tried that type of code
Code:
void main(void)
{

  CMCON=0x07;
  TRISB = 0x01;
  PORTB=0;

do
  { 
         Delay_ms(1000);
     PORTB.F1=1;  
         Delay_ms(1000);
     PORTB.F1=1;
     PORTB.F2=1; 
         Delay_ms(1000);
     PORTB.F1=1;
     PORTB.F2=1;
     PORTB.F3=1;
         Delay_ms(1000);
     PORTB.F1=1;
     PORTB.F2=1;
     PORTB.F3=1;
     PORTB.F4=1;
         Delay_ms(1000);
   } while(1);
}
 

You have two PICs? You want to read individual bits of a port? Then you need to set then pins as inputs and not outputs. Maybe you are doing reverse.
 

and also make port b as a output so TRISB is set to 0x00;
 

Hi the problem is read modify write operation put a resistor between output and LED then the first program itself will work fine............
 

Attachments

  • New folder (3).7z
    41.2 KB · Views: 43
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top