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.

how can I access to a single bit by a variable (Mikroc)

Status
Not open for further replies.

kappa_am

Full Member level 6
Full Member level 6
Joined
Jul 16, 2012
Messages
331
Helped
19
Reputation
38
Reaction score
19
Trophy points
1,298
Location
Vancouver
Activity points
3,859
Hi,
I like to access to a single bit of a variable using another variable. I have write following code (red lines in following text), but it has errors. I would be grateful if you give me your recommendations.
Code:
....
 while(1)
  {
  CHSL=0;
   do{
        for (ph=0; ph<3; ph++)
   {
    for (cap=0; cap<3; cap++)
    {
      ADRES=ADREAD(ph*3+cap);
      ADRES=ADRES>>2;
[COLOR="#FF0000"]      if(ADRES<REFC[ph][cap]) OUTM[ph].cap=0;
      else if(ADRES>REFC[ph][cap]) OUTM[ph].cap=0 ;
      else OUTM[Ph+2].cap=1;[/COLOR]
  CHSL=CHSL+1;
     }
   }
  } while(CHSL<10);
  }
  }
 

set bit
BYTE |= (1<<BIT)
clear bit
BYTE & =~ (1<<BIT)
invert bit
BYTE ^= (1<<BIT)
 
I have write following code (red lines in following text), but it has errors.
Come back with complete code (including all variable and related type definitions).
 

thank you. good idea.
I have been studying bit fields. there is a example, but it beat me. could you please explain the code to me what do red lines to me?
Code:
// This example writes low byte of bit field of myunsigned type to PORTA:
myunsigned Value_For_PORTA;

// Declare a pointer to mybitfield type:
mybitfield *TimerControl;
void main() {
[COLOR="#FF0000"]  TimerControl = (mybitfield *) (void *) &T2CON ;[/COLOR]  // explicit casting of pointer to T2CON, so it can be assigned

  ...
  Value_For_PORTA.lo_nibble  = 7;
  Value_For_PORTA.hi_nibble  = 0x0C;
[COLOR="#FF0000"]  PORTA = *(char *) (void *)&Value_For_PORTA;[/COLOR]
      // typecasting :
        // 1. address of structure to pointer to void
        // 2. pointer to void to pointer to char
        // 3. dereferencing to obtain the value

- - - Updated - - -

Dear Fvm,
the code is reading ten analog input and turning some bits "zero" or "one".
the code seems correct because by changing the mentioned lines it works well. For more clarification,I have attached code below.
Code:
unsigned int ADREAD(unsigned char channel){
   unsigned char ADL_byte, ADH_byte;
   unsigned int ADR;
   if(channel.B3 != 0){
    channel.B3=0;
    ADCON0=0x83 | (channel<<3);
     }
   else
   { ADCON0=0x81 | (channel<<3);
    }
   // delay_us(20)
   GO_DONE_BIT=1;
   while(GO_DONE_BIT);
   ADL_byte=ADRESL;
   ADH_byte=ADRESH;
   ADR=(ADH_byte<<8)| ADL_byte;
   return ADR;
   }
void main() {
   unsigned int ADRES;
   unsigned char CHSL;
   unsigned short OUTM[3];
   unsigned short ph, cap;
   unsigned short REFC[3][3];
  CMCON=7;
  ADCON1=0xB0;
  ADCON2=0x30;
  OPTION_REG=0xDE;
  TRISD=0x00;
  PORTD=0x00;
  while(1)
  {
  CHSL=0;
   do{
        for (ph=0; ph<3; ph++)
   {
    for (cap=0; cap<3; cap++)
    {
      ADRES=ADREAD(ph*3+cap);
      ADRES=ADRES>>2;
      if(ADRES<REFC[ph][cap]) OUTM[ph].cap=0;
      else if(ADRES>REFC[ph][cap]) OUTM[ph].cap=0 ;
      else OUTM[ph].(cap+2)=1;
  CHSL=CHSL+1;
     }
   }
  } while(CHSL<10);
  }
  }

- - - Updated - - -

I am still working on it; second while loop could be omitted.
 

Somewhere structrure mybitfield should be declared. Then autor declare pointer to this structure and assign it to address of T2CON register. After that you can work with bit structer as the single bit in byte variable.
 

The original and modfied code doesn't specify bit operations as far as I understand. I don't see it related to the other code example.

There are generic C and compiler specific methods for bit accesses. The compiler specific methods are usually related to hardware features of a specific processor, e.g. PIC bit operations. The respective mikroC syntax is described under "Accessing Individual Bits" in the user manual. But PIC bit operations don't provide bit pointers or variable bit addressing. If you want something of this kind, you'll refer to the generic C methods.
 

In mikroC if you want to access bit fields of a variable you can use var.Fx or var.Bx where x is between 0 and 7. var is of type char or unsigned char that is 8 bits wide (byte). If you want to access say bit 31 of a long type variable then you have to create a bit field structure. For creating small flags I usually use like this.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
unsigned char myFlags = 0;
 
#define SYS_STATUS myFlags.F0
#define RUN myFlags.F7
 
void main() {
 
      while(1) {
 
               SYS_STATUS = 1;
 
               if(RUN) {
 
 
               }
      }
}




I use this in all my mikroC projects.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct bitFields {
 
    unsigned long B0:1; unsigned long B1:1; unsigned long B2:1; unsigned long B3:1; unsigned long B4:1; 
    unsigned long B5:1; unsigned long B6:1; unsigned long B7:1; unsigned long B8:1; unsigned long B9:1; 
    unsigned long B10:1; unsigned long B11:1; unsigned long B12:1; unsigned long B13:1; unsigned long B14:1; 
    unsigned long B15:1; unsigned long B16:1; unsigned long B17:1; unsigned long B18:1; unsigned long B19:1; 
    unsigned long B20:1; unsigned long B21:1; unsigned long B22:1; unsigned long B23:1; unsigned long B24:1; 
    unsigned long B25:1; unsigned long B26:1; unsigned long B27:1; unsigned long B28:1; unsigned long B29:1;
    unsigned long B30:1; unsigned long B31:1;
 
} myFlags;
 
 
#define ON_OFF   myFlags.B0
#define STAT     myFlags.B1
#define RUN_STOP myFlags.B31

 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top