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.

C18 variable bit addressing? (i.e. varable.4)

Status
Not open for further replies.

Fred.Amoson

New Member
I'm still learning C18 and I was wondering if there was an easy to access a single bit of a variable. For instance if I have a "char variable" and I want to access bit 4, in SourceBoost I could just use "variable.4" and not have to use any masks. Is there an easy way to do that in C18?
 
You can do,
if(Var&1<<4==0)
and
Var|=1<<4;
Note that if(Var&1<<4==1) will never be true. It should be if(Var&1<<4==1<<4) or if(Var&1<<4!=0).

Alternatively setup a typedef,
Code:
typedef struct BitVar{
  unsigned Bit0:1;
  unsigned Bit1:1;
}BitVar;

When you want to use it,
Code:
void main(void){
BitVar Test;
    Test.Bit0=1;

Or,
Code:
void main(void){
struct Flags{
    unsigned isSpace:1;
    unsigned isCR:1;
    unsigned isLf:1;
}Flags;
    Flags.isSpace=1;

There are probably easier ways and I'm sure someone will suggest them soon.

Mike.
 
Thanks for the info Pommie. It is unfortunate (and surprising) that Microchip didn't build in a better way to do this.

felis, I assume that you are referring to the typedef?
 
Fred.Amoson said:
It is unfortunate (and surprising) that Microchip didn't build in a better way to do this.

It seems to be the fashion to make languages more and more convoluted. It is as though the people that know it and write the specifications are protecting their jobs. However, I am a cynical guy and I'm sure that is the real problem.:rolleyes:

Mike.
 
Fred.Amoson said:
felis, I assume that you are referring to the typedef?


No, I'm referring to bit definitions for various PIC registers. For example, PORT A is defined as:

Code:
extern volatile near unsigned char       PORTA;
extern volatile near union {
  struct {
    unsigned RA0:1;
    unsigned RA1:1;
    unsigned RA2:1;
    unsigned RA3:1;
    unsigned RA4:1;
    unsigned RA5:1;
    unsigned RA6:1;
    unsigned RA7:1;
  };
  struct {
    unsigned AN0:1;
    unsigned AN1:1;
    unsigned AN2:1;
    unsigned AN3:1;
    unsigned T0CKI:1;
    unsigned AN4:1;
    unsigned OSC2:1;
    unsigned OSC1:1;
  };
  struct {
    unsigned :2;
    unsigned VREFN:1;
    unsigned VREFP:1;
    unsigned :1;
    unsigned SS:1;
    unsigned CLKO:1;
    unsigned CLKI:1;
  };
  struct {
    unsigned :2;
    unsigned CVREF:1;
    unsigned :2;
    unsigned NOT_SS:1;
  };
  struct {
    unsigned :5;
    unsigned LVDIN:1;
  };
} PORTAbits;

If you want to modify the whole byte you write 'PORTA = ...', if you need to change a single bit you write 'PORTAbits.RA0 = ...'
 
felis said:
Give me an example of a better way.

Well, if you wanted to change bit 3 of variable Flags then you should be able to do,
Flags.3=1;

Mike.
 
Hmm then I wonder what's wrong with this program (doesn't seem to detect the button on RB0)
Code:
#include <p18f1320.h>

#pragma config WDT=OFF,OSC=INTIO2,LVP=OFF

void main(void){
OSCCON = 0x62;            // 4MHz
ADCON1 = 0;             // make all I/O digital
TRISA = 0b00111111;     // LED 3
TRISB = 0b11110111;     // CCP1 (RA3) for LED
INTCON2bits.RBPU = 0;    // enable weak pullups on port B
PR2 = 0b00011001;        // Duty
T2CON = 0b00000100;
CCPR1L = 0b00001001;
CCP1CON = 0b00001100;
LATA = 0;
    while(1){            // IR to LED3
        if(PORTBbits.RB0==0) 
        LATAbits.LATA6 = 1;
        else
        LATAbits.LATA6 = 0;
        }    
}
 
That would be because RB0 is an analogue input and so always reads zero. Try adding ADCON1=0x7f;

Mike.
 
Thanks Mike, how did I miss that one. Works like a charm.

PS it's an IR LED tester for the Junebug, you put an IR LED on CON5 (RB3,GND) and it simply sends out a 40KHz signal (I'll lower it to 38)
**broken link removed**
PICkit2 2.5 Logic Anaylzer Tool
**broken link removed**
The IR LED connected via cable to CON5
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top