![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
Been searching awhile now for example on how to use a C18 structure to emulate the basic "bit" access that BoostC includes automatically, that is; Code: myvar.0 = 1; // set bit 0 in variable "myvar" to '1' TIA, Mike, K8LH | |
| |
| | #2 |
|
Look here: Defining single bit variables in Microchip C18 like btbass from there said: "look at the Pic18F header files. They make extensive use of bitfields. You can learn a trick or two by studying the header files." heh i must say C18 is the least documented compiler i ever saw lol
__________________ AtomSofts eBay Store AtomSoftTech: C18 TIPS & TRICKS v9 PDF Nokia 6100 Driver/Software My Name: Jason Lopez http://atomsofttech.info/ | My YouTube Videos! My Favorite Store: dipmicro Electronics Last edited by AtomSoft; 4th July 2009 at 08:52 PM. | |
| |
| | #3 | |
| Quote:
In my opinion the simplest way is to define two macros: Code: #define bitset(var,bitno) (var|=1<<bitno) #define bitclr(var,bitno) (var&=~(1<<bitno)) I use them with 8-bit variables. Last edited by eng1; 4th July 2009 at 09:01 PM. | ||
| |
| | #4 |
|
Nice!!! Do you mind if i though that in my C18 PDF? Whats your name? Its would be like: Code: #define SpecialBit 2 unsigned char MyVar; MyVar = 0; bitset(MyVar,SpecialBit); "MyVar would be 0 to start then after the bitset command, MyVar equals 00000100."
__________________ AtomSofts eBay Store AtomSoftTech: C18 TIPS & TRICKS v9 PDF Nokia 6100 Driver/Software My Name: Jason Lopez http://atomsofttech.info/ | My YouTube Videos! My Favorite Store: dipmicro Electronics Last edited by AtomSoft; 4th July 2009 at 09:26 PM. | |
| |
| | #5 | |
| Quote:
Code: mode.0 ^= 1; ; toggle sideband mode | ||
| |
| | #6 |
|
This works for x.b0 ^= 1; Code: typedef union {
struct {
unsigned b0:1;
unsigned b1:1;
unsigned b2:1;
unsigned b3:1;
unsigned b4:1;
unsigned b5:1;
unsigned b6:1;
unsigned b7:1;
};
struct {
unsigned asByte:8;
};
} bitByte;
....
bitByte x;
x.b3=1;
x.asByte = 0x55;
The upside is that you can use variables on the RHS; Generated asm Code: 131: bitByte ii; 132: 133: ii.b3=1; 0152 0E01 MOVLW 0x1 0154 86DB BSF 0xfdb, 0x3, ACCESS 134: 135: ii.asByte = 0x55; 0156 52DE MOVF 0xfde, F, ACCESS 0158 0E55 MOVLW 0x55 015A 6EDD MOVWF 0xfdd, ACCESS
__________________ Please post questions to the forums. PM's are for personal communication. BCHS/3v0's Tutorials Junebug USB PIC programmer kit., USB Bit Whacker, The 15 Minute Printed Circuit Board! (+drill time) | |
| |
| | #7 |
|
could you not fix the x.asByte=123 by simply defining it after? like Code: #define MyVar x.asByte MyVar=123;
__________________ AtomSofts eBay Store AtomSoftTech: C18 TIPS & TRICKS v9 PDF Nokia 6100 Driver/Software My Name: Jason Lopez http://atomsofttech.info/ | My YouTube Videos! My Favorite Store: dipmicro Electronics | |
| |
| | #8 |
|
It sounds like a good idea but the preprocessor will then change MyVar.b0 to MyVar.asByte.b0
__________________ Please post questions to the forums. PM's are for personal communication. BCHS/3v0's Tutorials Junebug USB PIC programmer kit., USB Bit Whacker, The 15 Minute Printed Circuit Board! (+drill time) | |
| |
| | #9 |
|
heh i know hence why i only stated it would fix x.asByte = 123 ;p; Could you nest the b0 in the asByte?
__________________ AtomSofts eBay Store AtomSoftTech: C18 TIPS & TRICKS v9 PDF Nokia 6100 Driver/Software My Name: Jason Lopez http://atomsofttech.info/ | My YouTube Videos! My Favorite Store: dipmicro Electronics Last edited by AtomSoft; 4th July 2009 at 10:12 PM. | |
| |
| | #10 |
|
I'm just trying to find a way to test and clear individual switch flag bits like I do in BoostC. Suggestions? Thanks. Code: #define swSet swflags.0
#define swUp swflags.1
#define swDn swflags.2
while(1)
{ while(swSet) // while "set" mode
if(swUp) // if "up" switch press
{ swUp = 0; // clear switch flag bit
incvalue(); // bump value
}
if(swDn) // if "dn" switch press
{ swDn = 0; // clear switch flag bit
decvalue(); // bump value
}
}
Last edited by Mike, K8LH; 4th July 2009 at 10:38 PM. | |
| |
| | #11 |
|
How about simple ANDing? Note: i didnt test this out lol but seems fine in my head lol Code: while(1){
while(swflags & (1<<swSet)) // while "set" mode
if(swflags & (1<<swUp)) // if "up" switch press
{
swflags = ~(1<<swUp); // clear switch flag bit
incvalue(); // bump value
}
if(swflags & (1<<swDn)) // if "dn" switch press
{
swflags = ~(1<<swDn); // clear switch flag bit
decvalue(); // bump value
}
}
__________________ AtomSofts eBay Store AtomSoftTech: C18 TIPS & TRICKS v9 PDF Nokia 6100 Driver/Software My Name: Jason Lopez http://atomsofttech.info/ | My YouTube Videos! My Favorite Store: dipmicro Electronics Last edited by AtomSoft; 4th July 2009 at 10:39 PM. | |
| |
| | #12 |
|
Jason, That will work perfectly with minor correction but it's not as clean or as intuitive as the BoostC example... Last edited by Mike, K8LH; 4th July 2009 at 10:46 PM. | |
| |
| | #13 |
|
heh and i made this too: Code: #define bitset(var,bitno) (var|=1<<bitno);
#define bitclr(var,bitno) (var&=~(1<<bitno));
unsigned char bitchk(unsigned char var,char bitno){
unsigned char tmp;
tmp = var & (1<<bitno);
return tmp;
}
while(1){
while(bitchk(swflags,swSet)) // while "set" mode
if(bitchk(swflags,swUp)) // if "up" switch press
{
bitclr(swflags,swUp); // clear switch flag bit
incvalue(); // bump value
}
if(bitchk(swflags,swDn)) // if "dn" switch press
{
bitclr(swflags,swDn); // clear switch flag bit
decvalue(); // bump value
}
}
__________________ AtomSofts eBay Store AtomSoftTech: C18 TIPS & TRICKS v9 PDF Nokia 6100 Driver/Software My Name: Jason Lopez http://atomsofttech.info/ | My YouTube Videos! My Favorite Store: dipmicro Electronics Last edited by AtomSoft; 4th July 2009 at 10:45 PM. | |
| |
| | #14 |
| What was wrong with my suggestion ? I am not saying it is the greatest but is does what you asked for.
__________________ Please post questions to the forums. PM's are for personal communication. BCHS/3v0's Tutorials Junebug USB PIC programmer kit., USB Bit Whacker, The 15 Minute Printed Circuit Board! (+drill time) Last edited by 3v0; 4th July 2009 at 11:26 PM. | |
| |
| | #15 |
|
Hi 3v0, Thank you very much for for showing me how to build the structure. Unfortunately, if I understand you correctly, accessing the variable as a byte becomes extremely clumsy (ie; x.asbyte = 0x55). Right now the portion of my program that manipulates the switch flags variable as a byte works just fine in both BoostC and in C18 (the C18 code below looks almost identical to the BoostC version) so I don't want to muck up that part. I really do appreciate the input guys... Thank you!!! Mike Code: unsigned char swold = 0; // switch state latch
unsigned char flags = 0; // short switch flag bits
unsigned char flong = 0; // long switch flag bits
unsigned char tmr1k = 0; // 1 second timer
unsigned char tmr25 = 25; //
unsigned char beep = 0; //
unsigned char bctr = 32; //
#pragma interrupt isr_hi
/****************************************************************/
/* time from 'newhi' (press) to 'newlo' (release) determines */
/* which flag bit, short or long, is toggled */
/* */
/* swnew.0 ___---___---___----------___----------___ */
/* swold.0 ____---___---___----------___----------__ */
/* delta.0 ___-__-__-__-__-_________-__-_________-__ */
/* newhi.0 ___-_____-_____-____________-____________ */
/* newlo.0 ______-_____-____________-____________-__ */
/* flags.0 _______------____________________________ short */
/* slong.0 __________________________-------------__ long */
/* */
void isr_hi() // 1 msec interrupts
{ unsigned char swnew; //
unsigned char delta; //
unsigned char newhi; //
unsigned char newlo; //
PIR1bits.TMR2IF = 0; // clr timer2 interrupt flag
if(beep) // if beep task running
{ if(!(beep&1)) // if beep is even (b0 = 0)
PORTB ^= 1<<spkr; // toggle speaker pin
if(!--bctr) // if end of 32 msec count
{ bctr = 32; // reset 32 msec timer and
beep--; // decrement beep counter
} //
} //
if(!--tmr25) // if 25 msec interval
{ tmr25 = 25; // reset for 25 msecs and...
swnew = ~PORTC; // sample active lo switches
swnew &= 0b00000111; // on RC2, RC1, and RC0 pins
delta = swnew ^ swold; // state changes (hi or lo)
newhi = delta & swnew; // new hi level changes
newlo = delta & swold; // new lo level changes
swold = swnew; // update switch state latch
if(tmr1k) // if 1 second timer running
if(!--tmr1k) // dec it and if timed out
beep = 4; // task double beep feedback
if(newhi) // if new press
{ tmr1k = 1000/25; // start 1 second timer and
beep = 2; // task single beep feedback
} //
if(newlo) // if new release
{ if(!tmr1k) // if 1 second timeout
flong ^= newlo; // toggle 'long' switch flag
else // otherwise
flags ^= newlo; // toggle 'short' switch flag
tmr1k = 0; // clear 1 second timer
} //
} //
}
Last edited by Mike, K8LH; 5th July 2009 at 12:46 PM. | |
| |
|
| Tags |
| bits, c18, structure |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| C18 Byte Structure | AtomSoft | Micro Controllers | 15 | 8th December 2008 02:07 PM |
| Structure of resistor question | jasonbe | Electronic Theory | 25 | 3rd December 2008 05:40 PM |
| Programming structure in C | demestav | Micro Controllers | 8 | 12th May 2007 04:37 AM |
| How to initialize CCS static structure | janetsmith2000@yahoo.com | Micro Controllers | 1 | 6th March 2004 04:09 PM |
| Basic structure | Win32_under_the_API | Micro Controllers | 1 | 2nd September 2003 05:13 PM |