CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to read a register value without debugger

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

How to read a register value without debugger
PostPosted: Fri Jan 10, 2014 1:53 am     Reply with quote

Hello,

I have a basic problem. My software crashes when I want to use it with a debugger, so I'd like to read register values with a printf, if it's possible.

So I tried to do that :

1/ Make a structure for my CONFIG3H register :
Code:

struct {
   int1 CANMX;       //0   //ECAN MUX bit
   int1 T0CKMX;      //1   //Timer0 Clock Input MUX bit
   int1 T3CKMX;      //2   //Timer3 Clock Input MUX bit
   int1 MSSPMSK;     //3   //MSSP V3 7-Bit Address Masking Mode Enable bit
   int  void654:3;   //4:6 //Bits non utilisés
   int1 MCLRE;       //7   //MCLR Pin Enable bit
} CONFIG3H;


2/ Then get the adress of my CONFIG3H register :

Code:

#byte CONFIG3H = getenv("SFR:CONFIG3H") //0x300005



3/ and get the TRISC register adress :

Code:

#byte TRISC = getenv("SFR:TRISC")


I'd like to read these two registers, so I did :

4/ Declare another byte variable to get the CONFIG3H adress, because CONFIG3H is already declared as a structure.

Code:

#byte CONFIG3H1 = getenv("SFR:CONFIG3H") //0x300005


5/ And then now, I go to read my registers values as :

Code:

  fprintf(COM1, "TRISC:%x\n\r", TRISC);
  fprintf(COM1, "CONFIG3H:%x\n\r", CONFIG3H1);

 //We use  PIN C6 et C7 of CAN BUS (modif of CONFIG3H register)
 // (CAN_ENABLE_CAN_RC6_RC7 is defined as 0, value to activate the function)

 CONFIG3H.CANMX=CAN_ENABLE_CAN_RC6_RC7;

 set_tris_c((*0xF94 & 0xBF ) | 0x80);

   fprintf(COM1, "TRISC:%x\n\r", TRISC);
   fprintf(COM1, "CONFIG3H:%x\n\r", CONFIG3H1);



But my values are the sames before and after the execution of the instructions.
I'm quite sure my instructions are correct, so I'm wondering if my reading values printed are correct.. Is my reading method which is wrong ?

How can I correctly read a register value with a common or a specific PCWH function ?

Thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 2:09 am     Reply with quote

Post your PIC. Always do this.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 2:48 am     Reply with quote

Start by telling us your processor, & compiler version.

Some general comments.

If a variable is already declared, #byte locates this variable at the address specified (this is what you are doing with the structure).
So your declarations can simplify to:
Code:

struct {
   int1 CANMX;       //0   //ECAN MUX bit
   int1 T0CKMX;      //1   //Timer0 Clock Input MUX bit
   int1 T3CKMX;      //2   //Timer3 Clock Input MUX bit
   int1 MSSPMSK;     //3   //MSSP V3 7-Bit Address Masking Mode Enable bit
   int  void654:3;   //4:6 //Bits non utilisés
   int1 MCLRE;       //7   //MCLR Pin Enable bit
} CONFIG3H;

#byte CONFIG3H1 = getenv("SFR:CONFIG3H") //0x300005
#byte CONFIG3H=CONFIG3H1


Less likely to have problems from typing errors etc...

Then as a second general comment. Copy the registers to local versions, before using printf. Anything that might change while being accessed, should have this done.

Then the question comes what port is your UART on?. If it is on PortC, then the compiler will override the TRIS settings for this, as soon as you print, unless you have 'fixed' the TRIS, by selecting FAST_IO.....

Then depending on your chip, many configuration bits have requirements before they can change. It is common for some things to require the peripheral to be switched 'off', before changes can be made. Also some chips have locking procedures to prevent configuration registers being changed accidentally. This depends on the chip.

You also have to be careful, that some chips don't 'have' all the register bits. For some chip families the data sheet is generic, and has a warning like (copied from one data sheet):

"Some registers and associated bits
described in this section may not be
available on all devices. Refer to
Section 4.0 “Memory Organization” in
this data sheet for device-specific register
and bit information."

So we can't tell without knowing your chip what may or may not be actually changing.

How is "CAN_ENABLE_CAN_RC6_RC7" declared?.

The method is basically OK, but needs a lot more work...
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 3:19 am     Reply with quote

Sorry for forgetting this informations !

My PIC is 18F46K80 and the compiler is PCH Compiler v4.124
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 3:39 am     Reply with quote

Line 2 of the section about configuring this module:

"Ensure that the ECAN module is in Configuration
mode."....

However there is a big further comment. CANMX, and config3, are _fuses_ not writeable at run time.
Look at the definition for the register. Bit is defined as R/P. Read only/programmable.....

CANC fuse.

Best Wishes
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 3:49 am     Reply with quote

I understand the simplification of the declaration, thanks.

To answer to your questions, my UART is on PortD :

Code:

#use rs232(baud=9600,parity=N,xmit=PIN_D6,rcv=PIN_D7,bits=8,stream=COM1,restart_wdt)


so technically, it should no cause problem on my PortC.

I read the requirements to change my register. I do it in the config mode, as the library do it to configure TRISB (B2 and B3 are default pin for the CAN BUS), in "void can_init(void)" function, which set the configuration mode before changing all the parameters :

Code:

void can_init(void) {
   can_set_mode(CAN_OP_CONFIG);   //must be in config mode before params can be set
   can_set_baud();

   RXB0CON=0;
   RXB0CON.rxm=CAN_RX_VALID;
   RXB0CON.rxb0dben=CAN_USE_RX_DOUBLE_BUFFER;
   RXB1CON=RXB0CON;

[...]

    // THIS IS THE DEFAULT INSTRUCTION TO CONFIGURE CAN BUS (so put in commentary)
   //set_tris_b((*0xF93 & 0xFB ) | 0x08);   //b3 is in, b2 is out
   
   CONFIG3H.CANMX=CAN_ENABLE_CAN_RC6_RC7;
   
    set_tris_c((*0xF94 & 0xBF ) | 0x80);

   // or :
   //TRISC &= 0xBF;
   //TRISC |= 0x80;
   
   can_set_mode(CAN_OP_NORMAL);
}


I'm sure that my chip has these registers, I've check the adresses and the bits functions.


And finally, I declared "CAN_ENABLE_CAN_RC6_RC7" as :

Code:

#ifndef CAN_ENABLE_CAN_RC6_RC7           // add by me on 20/12/13 for PIC18F46K80
   #define CAN_ENABLE_CAN_RC6_RC7 0      // 0 we use  pins RC6 and RC7 for CANTX et CANRX
#endif

#ifndef CAN_DISABLE_CAN_RC6_RC7          // 1 Defaults pins used for CANTX et CANRX are RB2 et RB3 (1 is the default value in the register)
   #define CAN_DISABLE_CAN_RC6_RC7 1     
#endif


and also my CANMX bit :

Code:

//#bit CANMX = getenv("BIT:CANMX")   // 0x300005.0


Is there someting wrong in my method or in my code ?

Thank you for aswering so quick !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 4:05 am     Reply with quote

Yes.
It is not a register....

It is not in the RAM memory space at all, but a fuse.
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 4:59 am     Reply with quote

I saw there is a fuse for it. But the fuse is specific to PCHW compiler.

And despite of the fuse, the library still have in his code :

Code:

//set_tris_b((*0xF93 & 0xFB ) | 0x08);   //b3 is in, b2 is out


which isn't put into commentary. That's why I don't want to use the fuse.

Moreover, configuring the alternates PortC pins CAN BUS (C6 abd C7) is not so complicated....

I just have to change the CANMX bit of the CONFIG3H register to 0 value,

Code:

CONFIG3H.CANMX=CAN_ENABLE_CAN_RC6_RC7;

and configure the PortC pins with the correct I/O direction :

Code:

set_tris_c((*0xF94 & 0xBF ) | 0x80);


I want to use alternate BUS CAN pins (RC6 and RC7), but I want to do that "manually", and so I want to verify the correct values of my registers....
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 5:22 am     Reply with quote

You are missing the point.

CONFIG3H, is _not_ a RAM register. It is a block of fuses.

As I said, look at the definitions for this 'register' bits. _Read only/programmable_. They are not in the RAM space chip.

Quote from the data sheet (again - try reading it....).

"The Configuration bits can be programmed (read as
‘0’) or left unprogrammed (read as ‘1’) to select various
device configurations. These bits are mapped starting
at program memory location, 300000h."

Note 'program memory space', _not_ RAM.

It is controlled by the CANC/CANB fuse.
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 6:34 am     Reply with quote

I read it. CONFIG3H is tagged as a "register"...:

Quote:

REGISTER 28-5: CONFIG3H: CONFIGURATION REGISTER 3 HIGH (BYTE ADDRESS 300005h)

on page 463.

So I only can modify this register with the fuse ?? Not "manually" ??

I don't really understand why because of being in program memory rather than in RAM causes so much problems...

I'd just like to read and modify his values as I want...

Thank u for helping me
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jan 10, 2014 7:05 am     Reply with quote

You're missing the point about 'configuration registers' AKA 'fuses'.
These are a small group of memory that are programmed ONCE when the PIC is 'burned' and never,ever get changed unless the PIC gets reused for another purposes.
One way to alter them would be to use MPLAB and manually configure them, then burn(download) those registers.
You CANNOT alter them from within the PIC program.

hth
jay
baklou



Joined: 10 Jan 2014
Posts: 22

View user's profile Send private message

PostPosted: Mon Jan 13, 2014 1:39 am     Reply with quote

Ok... I understood the difference between fuses and classics registers.
Sorry to have focus on my "registers" and want to change them manually.

I still have a problem anyway, because using the fuse and configuring the I/O , I don't have any signal on CANTX pin...

Thank you for your advices Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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