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

PIC12F509 int16 and int8 counter code

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



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PIC12F509 int16 and int8 counter code
PostPosted: Mon May 17, 2010 9:33 am     Reply with quote

Hi,
with tick defined as a int16 the compiler produces the following code:

Code:
....................          if (get_timer0()==0) tick++;
0095:  MOVF   01,W
0096:  BTFSS  03.2
0097:  GOTO   09B
0098:  INCF   0F,F
0099:  BTFSC  03.2
009A:  INCF   10,F
....................          if (tick > 270) {
009B:  MOVLW  00
009C:  MOVWF  07
009D:  MOVF   10,W
009E:  SUBWF  07,W
009F:  BTFSC  03.0
00A0:  GOTO   0B1
00A1:  XORLW  FF
00A2:  BTFSS  03.2
00A3:  GOTO   0AA
00A4:  MOVLW  0E
00A5:  MOVWF  07
00A6:  MOVF   0F,W
00A7:  SUBWF  07,W
00A8:  BTFSC  03.0
00A9:  GOTO   0B1
....................             output_toggle(LED);
00AA:  BCF    0C.0
00AB:  MOVF   0C,W
00AC:  TRIS   6
00AD:  MOVLW  01
00AE:  XORWF  06,F


Looks OK. But doesn't work. With tick an int8 and the check as tick > 245 code works fine:

Code:
....................          if (get_timer0()==0) tick++;
0093:  MOVF   01,W
0094:  BTFSC  03.2
0095:  INCF   0D,F
....................          if (tick > 245) {
0096:  MOVLW  F5
0097:  MOVWF  07
0098:  MOVF   0D,W
0099:  SUBWF  07,W
009A:  BTFSC  03.0
009B:  GOTO   0A2
....................             output_toggle(LED);
009C:  BCF    0A.0
009D:  MOVF   0A,W
009E:  TRIS   6
009F:  MOVLW  01
00A0:  XORWF  06,F


What have a overlooked? (please!). WDT is disabled so processor is not resetting.
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon May 17, 2010 9:37 am     Reply with quote

I've just narrowed it down. With tick set to an int16 code works as expected with if (tick > 254) but change it to if (tick > 255) which should work with an int16, code breaks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 17, 2010 12:13 pm     Reply with quote

What's your compiler version ? I only have vs. 4.073, which is the
version of the free PCB compiler that's bundled with MPLAB v8.46.

I made the following test program and stepped through the code in MPLAB
simulator and it does work. 'result' goes to 0x01 in the Watch Window.
Code:

#include <12F509.H>
#fuses INTRC,NOWDT
#use delay(clock=4000000)

//=========================
void main()
{
int8 result;
int16 tick;


result = FALSE;
tick = 300;

if(tick > 255)
   result = TRUE;
else
   result = FALSE;

while(1);
}


Here's the .LST file for it. With the comparison to > 255, the compiler
can optimize the test so that it only checks to see if the MSB of 'tick' is
non-zero. If that's the case, then the test is true. You can see them do
that in the code below.
Code:

.................... void main()
.................... {
0002:  CLRF   04
.................... int8 result;
.................... int16 tick;
.................... 
.................... 
.................... result = FALSE;
0003:  CLRF   0D
.................... tick = 300;
0004:  MOVLW  01
0005:  MOVWF  0F
0006:  MOVLW  2C
0007:  MOVWF  0E
.................... 
.................... if(tick > 255)
0008:  MOVLW  00   
0009:  MOVWF  07    // Set scratch register to 0x00
000A:  MOVF   0F,W  // Get MSB of 'tick' into W
000B:  SUBWF  07,W  // Compare MSB of 'tick' to 0x00
000C:  BTFSC  03.0  // Jump if a borrow occurred (ie., if MSB != 0)
000D:  GOTO   011
....................    result = TRUE;
000E:  MOVLW  01   // If MSB is != 0, then set 'result' = 01 (True)
000F:  MOVWF  0D
.................... else
0010:  GOTO   012
....................    result = FALSE;
0011:  CLRF   0D
.................... 
.................... while(1);
0012:  GOTO   012
.................... }
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Mon May 17, 2010 12:52 pm     Reply with quote

Mine is version 4.107. Thanks. I've got your code to work (I am using real hardware and an LED as I don't use Mplab, your result variable I changed to a PIN!). I've gone back to the original code and mysteriously the code now works! I don't know why but I am happy! Finger trouble I used to say! I did think I was going insane!
Thanks for you help.
I hadn't realised MPLAB has a simulator....this has turned out to be a good question and answer as I haven't used MPLAB for maybe 10 years! Time to have another look I think as I usually ICD most code development but on these little parts you can't.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon May 17, 2010 3:51 pm     Reply with quote

The Simulator in MPLAB is great! Too bad you didn't know about this feature as it could have saved you a lot of time.
I often used the included stopwatch for timing measurements, much easier than pulsing an output pin connected to a scope.
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