|
|
View previous topic :: View next topic |
Author |
Message |
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
Dumb question on Unions |
Posted: Sun Feb 06, 2005 7:35 pm |
|
|
This may seem pretty remedial... but I've never used unions before...
Code: | struct portd_pin_map {
int LED; // LED's 1-8
} portd ;
#byte portd = 8 // Port C
struct portd_pin_map const PORTD_SET = {0};
struct porte_pin_map {
int LED:2; // LED's 9-10
boolean BAT_LED; // Center LED11
} porte ;
#byte porte = 9 // Port C
struct porte_pin_map const PORTE_SET = {0,0};
union led_map {
int portd.led,
int porte.led
} led_display;
|
I don't have that right since I get a compile error (expecting a ; or , -- on the first line inside the union)... so I thought I would ask.
Thanks in advance,
-Ben |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Feb 06, 2005 9:16 pm |
|
|
Not sure what you are trying to do
Code: |
struct portd_pin_map {
int LED; // LED's 1-8
} portd ;
#byte portd = 8 // Port C
struct portd_pin_map const PORTD_SET = {0};
struct porte_pin_map {
int LED:2; // LED's 9-10
boolean BAT_LED; // Center LED11
} porte ;
#byte porte = 9 // Port C
struct porte_pin_map const PORTE_SET = {0,0};
union led_map {
int somevar1;
int somevar2;
} led_display;
|
but that's how to declare a union. Do you know what a union is? From your code I think you probably were trying to do this
Code: |
struct portd_pin_map {
int LED; // LED's 1-8
} portd ;
#byte portd = 8 // Port C
struct portd_pin_map const PORTD_SET = {0};
struct porte_pin_map {
int LED:2; // LED's 9-10
boolean BAT_LED; // Center LED11
} porte ;
#byte porte = 9 // Port C
struct porte_pin_map const PORTE_SET = {0,0};
union led_map {
struct portd_pin_map portd;
struct porte_pin_map porte;
} led_display;
|
But that doesn't really make since either since you location where portd and porte are. Basically the above union is an 8 bit variable that can be accessed by different members but won't have anything to do with portd or porte. What exactly are you trying to do? |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Feb 06, 2005 9:25 pm |
|
|
I have 10 LED's (8 on port-D and 2 on port-E) of which I want to assign as a single INT16 so when I set them according to their 10bit value, I can just assign...
The 11th LED is for something else - thus I'd like to make it it's own element.
From what I've read online, I think I'm on the wrong track with Union...
MAKE8(); will be fine too. |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Sun Feb 06, 2005 10:35 pm |
|
|
Actually, you may be on the right track with the unions. Here's some code that I whipped up and compiled with 3.212 for the 18F452 and it looks like it might do what you want. First the header file:
Code: |
#include <18F452.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,WDT128,HS, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV20, PUT, \
NOSTVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, \
NOCPB, NOEBTR, NOEBTRB
#byte port_a = 0x0F80
#byte port_b = 0x0F81
#byte port_c = 0x0F82
#byte port_d = 0x0F83
#byte port_e = 0x0F84
#bit LED8 = port_e.0
#bit LED9 = port_e.1
struct LED_struct
{
int port_d : 8;
int LED8 : 1;
int LED9 : 1;
int filler : 6;
};
union
{
struct LED_struct LEDs;
long int long_led_data;
}LED_union;
#byte LED_union = 0xF83
|
Now the main file:
Code: |
#include "C:\Cfiles\Tests\union1\union1.h"
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
LED_union.long_led_data = 0x0555;
LED_union.LEDs = 0x02AA;
LED_union.LEDs.LED8 = 1;
while(1)
{
;
}
}
|
Now here's the listing of the important bits where we manipulate the union:
Code: |
.................... LED_union.long_led_data = 0x0555;
0046: MOVLW 05
0048: MOVWF F84
004A: MOVLW 55
004C: MOVWF F83
.................... LED_union.LEDs = 0x02AA;
004E: MOVLW 02
0050: MOVWF F84
0052: MOVLW AA
0054: MOVWF F83
.................... LED_union.LEDs.LED8 = 1;
0056: BSF F84.0
|
It looks like it might be doing what you want. Note the setting of a bit by accessing it with the union.
Let us know if this is what you are looking for.
Charlie |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Feb 07, 2005 12:11 am |
|
|
Charlie U wrote: | Actually, you may be on the right track with the unions. Here's some code that I whipped up and compiled with 3.212 for the 18F452 and it looks like it might do what you want. First the header file:
code removed for redundancy
It looks like it might be doing what you want. Note the setting of a bit by accessing it with the union.
Let us know if this is what you are looking for.
Charlie |
Actually, that is along the lines of what I'm doing... Thanks... the usage is a lot clearer to me now.
I'm using a 16F877. Quite frankly between union{} and make8(), I really don't mind which as long as I can do it quickly.
Thanks for the help everyone,
-Ben |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|