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

Variable for value in setup_vref not working

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



Joined: 18 Jan 2013
Posts: 7

View user's profile Send private message

Variable for value in setup_vref not working
PostPosted: Fri Jan 18, 2013 5:56 pm     Reply with quote

So I want to vary the value of vref in the project I'm working on, but I'm finding that something like:

Code:
int vtest = 10;
setup_vref(VREF_OUT_CVREF | VREF_C1_HIGH | vtest);

won't work at all, but this:
Code:
setup_vref(VREF_OUT_CVREF | VREF_C1_HIGH | 10);

works just fine.

Any ideas on why or how I can get it to work?

edit:
ICD3
PIC16F785
MPLAB X
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 18, 2013 9:45 pm     Reply with quote

What is your CCS compiler version ? This problem is likely dependent
upon the version.
The version is a 4-digit number given at the top of the .LST file.
The .LST file is generated after a successful compilation and will be
in your project directory. Examples of version numbers:
http://www.ccsinfo.com/devices.php?page=versioninfo
sirmeowsalot



Joined: 18 Jan 2013
Posts: 7

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 12:12 pm     Reply with quote

Thanks for the response, PCM programmer!

Looks like we're running 4.130

Should I see if I can get them to update/upgrade? Can anyone verify that it works with a newer version of the compiler?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 1:22 pm     Reply with quote

If someone else doesn't check if the version makes a difference in the
generated code, I do it in about 6 hours from now. I can't do it at my
present location.
sirmeowsalot



Joined: 18 Jan 2013
Posts: 7

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 6:20 pm     Reply with quote

Awesome! Thanks in advance!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 21, 2013 8:25 pm     Reply with quote

There is a bug in vs. 4.130 for setup_vref() for the 16F785. The code
doesn't load the REFCON register correctly. The bug still exists in vs. 4.140.
Code:

0017:  MOVLW  0A    // 10 -> Wreg
0018:  BCF    03.6
0019:  MOVWF  25    // Wreg -> vtest

.................... int vtest = 10; 
.................... 
.................... setup_vref(VREF_OUT_CVREF | VREF_C1_HIGH | vtest); 

001A:  MOVF   25,W  // main.vtest -> Wreg
001B:  IORLW  80    // OR Wreg with VREF_C1_HIGH (0x80)
001C:  MOVWF  26    // Wreg -> main.@SCRATCH1

001D:  MOVLW  02    // VREF_OUT_CVREF (02) -> Wreg
001E:  MOVWF  27    // Wreg -> main.@SCRATCH2

001F:  MOVF   26,W  // main.@SCRATCH1 -> Wreg
0020:  BSF    03.5
0021:  MOVWF  19    // Wreg -> VRCON

0022:  MOVF   00,W  // INDF -> Wreg  *** Bug - should load from @SCRATCH2
0023:  MOVWF  18    // Wreg -> REFCON
.................... 
.................... setup_vref(VREF_OUT_CVREF | VREF_C1_HIGH | 10); 
0024:  MOVLW  8A
0025:  MOVWF  19   // VRCON = 0x8A
0026:  MOVLW  02
0027:  MOVWF  18   // REFCON = 0x02
....................


Here is a macro that will replace the built-in setup_vref() function.
Place the macro code above main(), as shown below, and then
call setup_vref().
Code:

#include<16F785.h>
#fuses INTRC_IO,NOWDT
#use delay(clock = 4M)

#byte VRCON = getenv("SFR:VRCON")
#byte REFCON = getenv("SFR:REFCON")
int16 temp16;

#define setup_vref(value)  \ 
temp16 = value;       \
VRCON = temp16;       \         
REFCON = temp16 >> 8

//=============================
void main()
{
int vtest = 10;

setup_vref(VREF_OUT_CVREF | VREF_C1_HIGH | vtest);

while(1);
}
sirmeowsalot



Joined: 18 Jan 2013
Posts: 7

View user's profile Send private message

PostPosted: Tue Jan 22, 2013 1:45 pm     Reply with quote

Awesome! You rock, PCM programmer!

Thanks for the macro!

Not very good at assembly so I couldn't figure out what it was doing differently when I took a look earlier, so thanks for the explanation on that as well!
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Jan 22, 2013 5:57 pm     Reply with quote

This is why it's a good thing to learn assembler - for future reference.

Little things like this will become easy to find and easy to fix.

I recently ran into a similar problem with setup_vref on the PIC24 series with PCD.

setup_vref was writing to the wrong register.


-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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