format to change bit by byte operation in HITEC compiler.?

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

format to change bit by byte operation in HITEC compiler.?

Postby aswad » Fri Aug 23, 2013 9:26 am

hello, can i know how the format that we can change bit by bit from byte.

let say,,
a=0b00000000

in decision making
if ( button1=1)
{bit 0 of a=1;}
if ( button2=1)
{bit 1 of a=1;}
if ( button3=1)
{bit 3 of a=1;}

anyone can help?
aswad
Novice
 
Posts: 23
Joined: Sun Apr 08, 2012 10:30 am

Re: format to change bit by byte operation in HITEC compiler

Postby A380 » Fri Aug 23, 2013 4:47 pm

One of the methods is you have to declare 'a' as a struct as following.

struct
{
unsigned char Button1 :1;
unsigned char Button2 :1;
unsigned char Button3 :1;
unsigned char B4 :1;
unsigned char B5 :1;
unsigned char B6 :1;
unsigned char B7 :1;
unsigned char B8 :1;
}a;

if(button1 == 1){ a.Button1 = 1; }
if(button2 == 1){ a.Button2 = 1; }
if(button3 == 1){ a.Button3 = 1; }
User avatar
A380
Discoverer
 
Posts: 120
Joined: Tue May 19, 2009 2:44 pm
Location: Malaysia

Re: format to change bit by byte operation in HITEC compiler

Postby shahrul » Fri Aug 23, 2013 8:57 pm

Can use operation AND and OR.
If want set bit 0, a=a|0x01
any bit OR with 1 will result 1
If want clear bit 0, a=a&0xFE
any bit AND with 0 will result 0
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: format to change bit by byte operation in HITEC compiler

Postby aswad » Sat Aug 24, 2013 12:22 am

yes, i done with bitwise operation.
aswad
Novice
 
Posts: 23
Joined: Sun Apr 08, 2012 10:30 am


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 38 guests