|
|
View previous topic :: View next topic |
Author |
Message |
Richter
Joined: 18 Mar 2005 Posts: 8
|
the good old #byte and struct problem! |
Posted: Fri Jun 03, 2005 6:14 am |
|
|
Hi guys I'm sorry to bring this subject again up:
I m using PIC18F6720 and using the latest version!
plz take a look at the code below, I m trying hopeless just to write and read to/from the ports or other Registers.
I have looked at some examples wich were discussed before but somehow I don t get my error...
Code: |
// these are my definitions (port.c):
int port_a;
#byte port_a = 0xF80
struct port_a_bits_struct
{
int1 port_a_bit0 :0;
int1 port_a_bit1 : 1;
int1 port_a_bit2 : 2;
int1 port_a_bit3 : 3;
int1 port_a_bit4 : 4;
int1 port_a_bit5 : 5;
int1 port_a_bit6 : 6;
int1 unused : 7;
};
#bit port_a_bit0 = port_a.0
#bit port_a_bit1 = port_a.1
#bit port_a_bit2 = port_a.2
#bit port_a_bit3 = port_a.3
#bit port_a_bit4 = port_a.4
#bit port_a_bit5 = port_a.5
#bit port_a_bit6 = port_a.6
struct port_a_bits_struct port_a_Bits;
#byte port_a_Bits = 0xF80
//-------------------------------------------------------------------
// and here are my tryings to write and read from the registers (main.c):
port_a_Bits.port_a_bit0 = 1;
port_a_Bits.port_a_bit1 = 1;
port_a_Bits.port_a_bit2 = 1;
port_a_Bits.port_a_bit3 = 1;
port_a_Bits.port_a_bit4 = 1;
port_a_Bits.port_a_bit5 = 1;
port_a_Bits.port_a_bit6 = 1;
fprintf(COM2," PINs von port_a_Bits: %d%d%d%d%d%d%d",port_a_Bits.port_a_bit0, port_a_Bits.port_a_bit1, port_a_Bits.port_a_bit2, port_a_Bits.port_a_bit3, port_a_Bits.port_a_bit4, port_a_Bits.port_a_bit5, port_a_Bits.port_a_bit6 );
fprintf(COM2," PINs von port_a: %d%d%d%d%d%d%d", port_a.0, port_a.1, port_a.2, port_a.3, port_a.4, port_a.5,port_a.6 );
fprintf(COM2," PINs von PIN_AX: %d%d%d%d%d%d%d",input(PIN_A0),input(PIN_A1),input(PIN_A2),input(PIN_A3),input(PIN_A4),input(PIN_A5),input(PIN_A6) );
|
Can you explain me exactly where is the difference between the readings??
THNX _________________ Why I get all these strange problems ? |
|
|
JPA Guest
|
|
Posted: Fri Jun 03, 2005 7:51 am |
|
|
Quote: | struct port_a_bits_struct
{
int1 port_a_bit0 :0; --> indicate bit field of lenth 0 !
int1 port_a_bit1 : 1;--> indicate bitfield of lenth 1 !
int1 port_a_bit2 : 2;
int1 port_a_bit3 : 3;
int1 port_a_bit4 : 4;
int1 port_a_bit5 : 5;
int1 port_a_bit6 : 6; --> bit field of length 6 !
int1 unused : 7;
};
|
The declaration for the structure defines a structure with members whose length grow from 0 to 7... obviously not what you intend to do. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Fri Jun 03, 2005 9:40 am |
|
|
Look up things you don't understand in the help file. tris fast_io
Change the #include
change the #bytes for your chip. and the clock and debug and your good to go.
Code: | #INCLUDE <18F452.H>
#CASE
#USE DELAY(CLOCK=16000000)
#FUSES HS,NOWDT,NOPROTECT,NOLVP
#DEFINE VER_MAJOR 1
#DEFINE VER_MINOR 00
#USE RS232(BAUD=19200,XMIT=PIN_E0,INVERT,STREAM=DEBUG)
#ZERO_RAM
#use fast_io (a)
struct port_a_bits //total of 6 bits on port A
{
int1 bit0;//define a int 1
int1 bit1;
int1 bit2;
int1 bit3;
int1 bit4;
int1 bit5;
};
struct port_a_bits port_a ;//instatiate the struct
struct port_a_bits a_tris ;//instatiate the struct
//overlay the structs on the physical pins,.. so to speak
#byte port_a = 0xF80 //note: no semicolin 0xF80=portA0 on 18F452
#byte a_tris = 0xF92 //note: no semicolin 0xF92=tris portA0 on 18F452
//======================= MAIN ============================//
void main(void)
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
fprintf(DEBUG,"\n\r");
fprintf(DEBUG,"STARTING 18F452 Test.\n\r");
fprintf(DEBUG,"FIRMWARE VERSION %u.%02u\n\r",VER_MAJOR,VER_MINOR);
a_tris=0; //set all to outputs
do //bit 2 and 5 == 1
{
port_a.bit0 = 0;
port_a.bit1 = 0;
port_a.bit2 = 1;
port_a.bit3 = 0;
port_a.bit4 = 0;
port_a.bit5 = 1;
} while (TRUE);
}
|
BTW this is an example of a small COMPLETE program.
Last edited by treitmey on Fri Jun 03, 2005 9:55 am; edited 4 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Fri Jun 03, 2005 2:49 pm |
|
|
what's the diff. from:
Code: | #byte port_a = 0xF80
#bit port_a_bit0 = port_a.0
#bit port_a_bit1 = port_a.1
#bit port_a_bit2 = port_a.2
#bit port_a_bit3 = port_a.3
#bit port_a_bit4 = port_a.4
#bit port_a_bit5 = port_a.5
#bit port_a_bit6 = port_a.6 |
and
Code: | struct port_a_bits_struct
{
int1 port_a_bit0 :0;
int1 port_a_bit1 : 1;
int1 port_a_bit2 : 2;
int1 port_a_bit3 : 3;
int1 port_a_bit4 : 4;
int1 port_a_bit5 : 5;
int1 port_a_bit6 : 6;
int1 unused : 7;
};
struct port_a_bits_struct port_a_Bits;
#byte port_a_Bits = 0xF80 |
besides that I only have to type:
Code: | port_a_bit0 = 1;
port_a_bit1 = 1;
port_a_bit2 = 1;
port_a_bit3 = 1;
port_a_bit4 = 1;
port_a_bit5 = 1;
port_a_bit6 = 1; |
instead of:
Code: | port_a_Bits.port_a_bit0 = 1;
port_a_Bits.port_a_bit1 = 1;
port_a_Bits.port_a_bit2 = 1;
port_a_Bits.port_a_bit3 = 1;
port_a_Bits.port_a_bit4 = 1;
port_a_Bits.port_a_bit5 = 1;
port_a_Bits.port_a_bit6 = 1; |
_________________ -Matt |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Jun 03, 2005 9:51 pm |
|
|
You should be using the #locate instead of #byte as well |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 03, 2005 11:11 pm |
|
|
I don't think you have to use #locate for SFR's because the compiler
already knows it can't use ram in those addresses because they're
defined as non-usable in the Devices.dat file.
Here is a screen-shot of the PCW Device Editor. Notice that down at the
bottom of the image it says "1 indicates memory may be used by C".
Everything in the range from 0x00 to 0x1F is an SFR, and all those
locations have "00" in them, meaning they're not usable for variables.
http://forum.microchip.com/tm.asp?m=55879&mpage=1&key=&anchor#55879
So for this reason, I don't think you have to use #locate when defining
SFR addresses. You can just use #byte. |
|
|
Richter
Joined: 18 Mar 2005 Posts: 8
|
|
Posted: Mon Jun 06, 2005 12:44 am |
|
|
Hi there:
@all: YES I want to program a SFR file! Which one of the offered should I take for a PIC 18F6720 ? Or doesn t it matter because I only have to change the adresses?
THNX again for a fast reply.
I will tell when I got it worked or not.
@ JPA: Yeah u are right! I never seen the :1 :2 :3 before and wasn t so clear about the meaning. This could be my error. I thpught thes where places of bits. _________________ Why I get all these strange problems ? |
|
|
Richter
Joined: 18 Mar 2005 Posts: 8
|
|
Posted: Mon Jun 06, 2005 1:32 am |
|
|
OK I used one of the SRF files u considered me.
But I try to write in the PORTA byte and just want to read it, but the PIC doesn t seem to write the bits into the register:
I think the adresses are all right. BUT I DON T KNOW WHERE THE PROBLEM is.
here is my test program with defines:
Code: |
/******************************************************************************
* TRISA Memory Mapping, Data Structures and Definitions *
******************************************************************************/
#define TRISA_BASE 0xF92
#define TRISA ((unsigned int *) TRISA_BASE)
typedef struct TrisAStruct
{
unsigned int TrisA0 :1;
unsigned int TrisA1 :1;
unsigned int TrisA2 :1;
unsigned int TrisA3 :1;
unsigned int TrisA4 :1;
unsigned int TrisA5 :1;
unsigned int TrisA6 :1;
unsigned int Dummy7 :1;
} TRISA_STRUCT;
TRISA_STRUCT TrisAbits;
#locate TrisAbits = TRISA_BASE
/******************************************************************************
* PORTA Memory Mapping, Data Structures and Definitions *
******************************************************************************/
#define PORTA_BASE 0xF80
#define PORTA ((unsigned int *) PORTA_BASE)
typedef struct PortAStruct
{
unsigned int A0 :1;
unsigned int A1 :1;
unsigned int A2 :1;
unsigned int A3 :1;
unsigned int A4 :1;
unsigned int A5 :1;
unsigned int A6 :1;
unsigned int unused :1;
} PORTA_STRUCT;
PORTA_STRUCT PortAbits;
#locate PortAbits = PORTA_BASE
|
Code: |
TrisAbits.TrisA0 = 0;
TrisAbits.TrisA1 = 0;
TrisAbits.TrisA2 = 0;
TrisAbits.TrisA3 = 0;
TrisAbits.TrisA4 = 0;
TrisAbits.TrisA5 = 0;
TrisAbits.TrisA6 = 0;
PortAbits.A0 = 1;
PortAbits.A1 = 0;
PortAbits.A2 = 1;
PortAbits.A3 = 0;
PortAbits.A4 = 1;
PortAbits.A5 = 1;
PortAbits.A6 = 0;
TrisAbits.TrisA0 = 1;
TrisAbits.TrisA1 = 1;
TrisAbits.TrisA2 = 1;
TrisAbits.TrisA3 = 1;
TrisAbits.TrisA4 = 1;
TrisAbits.TrisA5 = 1;
TrisAbits.TrisA6 = 1;
fprintf( COM2,"\r\nPINS of PortAbits: %d%d%d%d%d%d%d", PortAbits.A0, PortAbits.A1, PortAbits.A2,PortAbits.A3, PortAbits.A4, PortAbits.A5, PortAbits.A6);
|
EVEN WHEN I TRY THIS THE OUTPUT is different:
Code: |
output_high(PIN_A0);
output_high(PIN_A1);
output_high(PIN_A2);
output_high(PIN_A3);
output_high(PIN_A4);
output_high(PIN_A5);
output_high(PIN_A6);
fprintf( COM2,"\r\nPINS of PortAbits: %d%d%d%d%d%d%d", PortAbits.A0, PortAbits.A1, PortAbits.A2,PortAbits.A3, PortAbits.A4, PortAbits.A5, PortAbits.A6);
fprintf(COM2," PINs von input(PIN_AX): %d%d%d%d%d%d%d",input(PIN_A0),input(PIN_A1),input(PIN_A2),input(PIN_A3),input(PIN_A4),input(PIN_A5),input(PIN_A6) );
|
OUTPUT:
PINS of PortAbits: 1111010 PINs von input(PIN_AX): 0000000
the output of PortAbits is the input i did the last time, but there he didn t displayed it! ..... _________________ Why I get all these strange problems ? |
|
|
Ttelmah Guest
|
|
Posted: Mon Jun 06, 2005 2:42 am |
|
|
You may be running into the classic 'read modify write' problem.
It is important to understand, that when you set a single pin, what happens inside the processor, is that the port is read, the bit corresponding to the pin is set, and this value is written back out to the port. The 'write' takes place at the end of the instruction (in the last clock cycle), while the read occurs at the beginning of the instruction.
Now when a signal is driven to a level (high or low), the corresponding output transistor turns on (which itself takes time), and starts to try to drive the signal to the new requested level. How long the signal actually 'takes' to get to the required level, will depend on a huge number of factors (the capacitance of the signal line, and the loads present). If for example. you set a bit high, on a output that is driving directly into an NPN transistor base wth the transistor wired with it's emitter to the 0v rail, wait for ages, then try to set another bit on the same port high, the first bit will get turned off. The reason here, is that the base-emitter diode in the transistor will prevent the line reaching the voltage level that is seen as 'high', so when the second read modify write is done, the bit is still read as '0'.
Similarly, if you drive an LED with an output, and do not have a current limiting resistor, the Vf of the LED, may well be too low for the output to be seen as high, especially on 'schmitt trigger' inputs, which have a higher voltage requirements for a logic '1', and again the first pin will be read as '0', and turned off.
Again, if there is significant capacitance present, though the voltage will eventually reach the required level, it will take time. If you perform a sequence of bit set operations, the setting, and reading, are only two clock cycles apart, and the same problem may appear. In this case, adding a delay between operations can solve the problem.
Finally there is the issue that not all bits are themselves capable of pulling 'high'. On many chips, pin A4, is an 'open collector' output, and requires a pull-up resistor, or it's signal line will never go high.
So you may well have a hardware problem that is preventing the code functioning as you expect.
Best Wishes |
|
|
Guest
|
|
Posted: Mon Jun 06, 2005 5:18 am |
|
|
hmkay, it s working! I made a meassure and output_low() worked correct. The #byte definition didnot work before!
Now it s working, but only when I m executing the output_low() function before!!!! So I m thinking there must be some initialisations in the output_low() function that i diddn t.
Anybody got an idea of the initialisations (output_low function())?
or is there a way to look into the libs of CCS?
Is there a COMPLETE srf file?
Thnx for all ! |
|
|
Richter
Joined: 18 Mar 2005 Posts: 8
|
|
Posted: Mon Jun 06, 2005 5:20 am |
|
|
Sorry didn t see that I wasn t logged!
The message above was from me! _________________ Why I get all these strange problems ? |
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Mon Jun 06, 2005 10:44 am |
|
|
by using the default set directive "#use standard_io" input() and output() will set the port directions automatically when used. If you use "#use fast_io" or if you do not use the input() ouput() functions, you need to manually setup the port directions by using the set_tris_x() function. See CCS reference manual. _________________ -Matt |
|
|
|
|
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
|