View previous topic :: View next topic |
Author |
Message |
sraiderk
Joined: 09 Feb 2005 Posts: 48
|
Using Array of bits with struct |
Posted: Wed May 09, 2018 6:12 am |
|
|
In CCS C web page, there is a explanation like that: Quote: | Bit Arrays
You can create an array of bits (or booleans). You cannot create a pointer to an array of bits or to a bit. |
However, I am using compiler version 5.076. My code is simplified at below.
Program output is always NOT. Why? Is there any restriction using of bits array in struct?
Code: |
struct x
{
short arraybits [200];
} K;
While(True) {
K.arraybits[3]=True;
if (K.arraybits[3]==True) {
printf(lcd_putc,"OK");
else
printf(lcd_putc,"NOT");
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 09, 2018 8:13 am |
|
|
I ran the following program in vs. 5.078 in MPLAB vs. 8.92 simulator
and it works. It displayed this in the Output window:
Test program:
Code: | #include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//======================================
void main()
{
struct x
{
short arraybits [200];
}K;
K.arraybits[3]=True;
if(K.arraybits[3]==True)
printf("OK");
else
printf("NOT");
while(TRUE);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19548
|
|
Posted: Wed May 09, 2018 9:05 am |
|
|
Two things:
First as written the code could work if the RAM location just happened to be 'TRUE' at boot. Therefore I've amended it with:
Code: |
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//======================================
void main()
{
struct x
{
short arraybits [200];
}K;
K.arraybits[3]=True;
if(K.arraybits[3]==True)
{
printf("OK");
K.Arraybits[3]=FALSE;
if(K.arraybits[3]==FALSE)
{
printf("OK");
}
}
else
printf("NOT");
while(TRUE);
}
|
This correctly displays 'OKOK' with the current compiler. Verifying that the code both sets and clears the bit.
However I then 'went back in time', and realised a critical difference. Going back to 5.049, I had to add the 'NOXINST' fuse. The older compiler was not setting this by default, and it makes the code go really screwy!... |
|
|
sraiderk
Joined: 09 Feb 2005 Posts: 48
|
|
Posted: Thu May 10, 2018 1:31 am |
|
|
Thank you for all reply.
I tried the Ttelmah's code with pic18f27k40 and ccs c version 5.076. There is no problem. I mean that I saw "OKOK" output on LCD. So, I will check my orginal code to find reason why problem occuring. Because, it's really big code. Thanks... |
|
|
|