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.

Referring to bits of a register for variables in XC8? (microchip c compiler)

Status
Not open for further replies.
T

treez

Guest
Newbie level 1
Hello,
I am writing XC8 (free version) C for PIC18F65K22 in MPLAB.X environment.

I wish to define an 8 bit register, which I am calling “dip8”.
I also wish to refer to each bit of this register.
How do I do this in C?
Do I have to use inline assembler.

Here is how I declare the variable…
Unsigned int dip8;


Here is my code so far
* File: 3CH TEST.c
*
* Created on 24 October 2014, 21:33
*/
/*DEFINE OUTPUTS*/
#define ch1bit LATE,LATE1
#define ch2bit LATE,LATE0
#define ch3bit LATG,LATG0
#define ind_ledbit LATC,LATC2
#define fanconbit LATE,LATE2
#define tripbit LATA,LATA4
#define mcp4013_csbit LATC,LATC3
#define mcp4013_udbit LATC,LATC4

/* TURN LEDS ON AND OFF*/
#define ON1 LATE,LATE1 = 1; /*Turn on chan1*/
#define OFF1 LATE,LATE1 = 0; /*Turn off chan1*/
#define ON2 LATE,LATE0 = 1; /*Turn on chan2*/
#define OFF2 LATE,LATE0 = 0; /*Turn off chan2*/
#define ON3 LATG,LATG0 = 1; /*Turn on chan2*/
#define OFF3 LATG,LATG0 = 0; /*Turn off chan2*/
#define FANON LATE,LATE2 = 1; /*Turn fan on*/
#define FANOFF LATE,LATE2 = 0; /*Turn fan off*/
/*DEFINE INPUTS FROM DIPSWITCH*/
/*Note that the new board has pullups on the dipswitch,*/
/*whereas 550587 PCB had pull downs, thus logic is reversed.*/
#define modebit dip8,0 /*low = external connector sets power via 0-10V*/
#define LSBbit dip8,1 /*00 -> 3.52A, 01 -> 2.64A, 10 -> 1.76A, 11 -> OFF*/
#define MSBbit dip8,2
#define polaritybit dip8,3 /*high = 10V gives max current*/
#define t80t90bit dip8,4 /*high = 80degC, low=90degC*/
#define fanbit dip8,8 /*high = fan fitted, low = no fan fitted*/
/*DEFINE INPUTS FROM EXTERNAL CONNECTOR*/
#define re_setbit ext_in,0
#define ch1_extbit ext_in,1
#define ch2_extbit ext_in,2
#define ch3_extbit ext_in,3
#define clearbit ext_in,4


unsigned int dip8;
unsigned int ext_in;

#include <xc.h>
/*
*
*/
void main(void) {

return ();
}
 

It's usually done with unions. I'm not using XC8 but I would expect predefined unions for bit access in GenericTypeDefs.h or a similar include file.

Like this one supplied with C30

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef union
{
    UINT8 Val;
    struct
    {
        __EXTENSION UINT8 b0:1;
        __EXTENSION UINT8 b1:1;
        __EXTENSION UINT8 b2:1;
        __EXTENSION UINT8 b3:1;
        __EXTENSION UINT8 b4:1;
        __EXTENSION UINT8 b5:1;
        __EXTENSION UINT8 b6:1;
        __EXTENSION UINT8 b7:1;
    } bits;
} UINT8_VAL, UINT8_BITS;

 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top