# simpel programing help required for pic16f72

Status
Not open for further replies.

#### Mithun_K_Das

How can I use RB5,RB6 & RB7 to give some value output without disturbing other pins. I just want to generate a value 0 to 7 and need to show this value only those 3 pins. Note: Other pins are being used for other purpose. so I can't disturb them.

as I did:
unsigned char *I;
void main()
{
char I;
TRISB = 0x00;
PORTB = 0;

//I = RB6_bit;
//I = RB7_bit;

//TRISC = 0x00;
while(1)
{
RB5_bit = I;
RB6_bit = I;
RB7_bit = I;
I = 1;
Delay_ms(100);
I = 1;
Delay_ms(100);
I = 1;
Delay_ms(100);

}

}

but its not working

PORTB &= 0b00011111 or 0x1F; // resets RB5,6 and 7
PORTB |= (number)<<5; // number = 0 to 7

Check

View topic - [TUT] [C] Bit manipulation (AKA "Programming 101") :: AVR Freaks

Any bit ANDed with 1 keeps the value it had and any bit ANDed with 0 is cleared
also any bit ORed with 1 results to 1 and any bit ORed with 0 keeps the value it had

For single bit operations you can use the macros explained https://www.edaboard.com/threads/206148/#post869568

If you want to set of clear multiple bits then use & and |

PORTA = PORTA & 0b10101010; clear bits 0,2,4,6 , the rest keep the value they had
PORTA = PORTA | 0b10101010; set bits 1,3,5,7 , the rest keep the value they had

Alex

I understand this. But I need to show the numbers only in the MSB side. that means RB5,RB6,RB7. I did the program but its showing at the lower 3bits only.

---------- Post added at 08:34 ---------- Previous post was at 08:29 ----------

Oh! I did it. But I need to make this more simple and logical.

void main()
{
int number;

TRISB = 0x00;
PORTB = PORTB | 0b00011111;// clear bits 0,2,4,6 , the rest keep the value they had

while(1)
{
number = 1;
PORTB = (number)<<5;
Delay_ms(1000);
number = 2;
PORTB = (number)<<5;
Delay_ms(1000);
number = 3;
PORTB = (number)<<5;
Delay_ms(1000);
number = 4;
PORTB = (number)<<5;
Delay_ms(1000);
number = 5;
PORTB = (number)<<5;
Delay_ms(1000);
number = 6;
PORTB = (number)<<5;
Delay_ms(1000);
number = 7;
PORTB = (number)<<5;
Delay_ms(1000);
}

}

PORTB = PORTB | 0b00011111;// clear bits 0,2,4,6 , the rest keep the value they had
This is not correct, the code sets to 1 bits 0,1,2,3,4

the rest of your code works fine, how can you make it more logical.
You can only use a macro to make it more readable maybe

#define setMSB(x) PORTB = (x)<<5;

then you can use setMSB(number);

Alex

I'm using mikroCpro for pic. #define setMSB(x) PORTB = (x)<<5; dosen't support here.

void main()
{
int number;

TRISB = 0x00;
// clear bits 0,2,4,6 , the rest keep the value they had

while(1)
{
RB0_bit = 0;
RB1_bit = 1;
RB2_bit = 0;
RB3_bit = 1;

//PORTB = |PORTB & 0b00011111;

number = 1;
PORTB = (number)<<5|PORTB & 0b00011111;
Delay_ms(1000);
number = 2;
PORTB = (number)<<5|PORTB & 0b00011111;
Delay_ms(1000);
number = 3;
PORTB = (number)<<5|PORTB & 0b00011111;
Delay_ms(1000);
number = 4;
PORTB = (number)<<5|PORTB & 0b00011111;
Delay_ms(1000);
number = 5;
PORTB = (number)<<5|PORTB & 0b00011111;
Delay_ms(1000);
number = 6;
PORTB = (number)<<5|PORTB & 0b00011111;
Delay_ms(1000);
number = 7;
PORTB = (number)<<5|PORTB & 0b00011111;
Delay_ms(1000);
}

}

---------- Post added at 08:59 ---------- Previous post was at 08:58 ----------

Tell me how can I simplify this program?

So you want to write the number in bits 7,6,5 and keep bits 4,3,2,1,0 values?

In that case I would go with
PORTB = (PORTB & 0b00011111) | ((number)<<5);

the first part clears bits 7,6,5 and the second part sets them to the new value

Alex

---------- Post added at 11:09 ---------- Previous post was at 11:04 ----------

You can use a loop to simplify your code

Code:
for (i=1;i<8;i++)
{
PORTB = (PORTB & 0b00011111) | (i<<5);
Delay_ms(1000);
}

is this what you mean?

Alex

Ya right, I was looking for this one. but not the for loop. I need to change the value of i with change of current to make a feedback of a current control system. Thanks a lot.

I think your problem is solved but I like to add some more inputs

create a funtion
Code:
outport(unsigned char num)
{
PORTB = ((PORTB & 0x1F)|((num)<<5));
Delay_ms(1000);
}
Call in main function as
Code:
outport(current value);

I'm using mikroCpro for pic. #define setMSB(x) PORTB = (x)<<5; dosen't support here.

in page 232 it says that you can use macro with parameters

#define _MAX(A, B) ((A) > (B)) ? (A) : (B)

Read from page 231 downwards to see how to use exactly

Alex

Status
Not open for further replies.