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

Great Problem with Division..

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



Joined: 22 Feb 2006
Posts: 14

View user's profile Send private message

Great Problem with Division..
PostPosted: Tue Mar 14, 2006 8:56 pm     Reply with quote

Hi all,

I have great problem with integer division. If I divide two integer variables the program jumps to initial point of device .h file. for example 16f84a.h. Then my ISR routine become ineffective...

I will present my C and ASM/LST codes as follows.

#include "C:\Documents and Settings\Parthipan\Desktop\PICC\division.h"

void main()
{
int a;
int b;
int c;
int d;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);

a=10;
b=5;
c=10/5;
d=a/b;
}


*******************************************

CCS PCM C Compiler, Version 3.242, 16465 13-Nov-01 07:49

Filename: C:\Documents and Settings\Parthipan\Desktop\PICC\division.lst

ROM used: 48 words (5%)
Largest free fragment is 976
RAM used: 6 (9%) at main() level
9 (13%) worst case
Stack: 1 locations

*
0000: MOVLW 00
0001: MOVWF 0A
0002: GOTO 019
0003: NOP
.................... #include "C:\Documents and Settings\Parthipan\Desktop\PICC\division.h"
.................... #include <16F84.h>
.................... //////// Standard Header file for the PIC16F84 device ////////////////
.................... #device PIC16F84
.................... #list
....................
....................
.................... #FUSES NOWDT //No Watch Dog Timer
.................... #FUSES HS //High speed Osc (> 4mhz)
.................... #FUSES NOPUT //No Power Up Timer
.................... #FUSES NOPROTECT //Code not protected from reading
....................
.................... #use delay(clock=20000000)
....................
....................
....................
.................... void main()
.................... {
0019: CLRF 04
001A: MOVLW 1F
001B: ANDWF 03,F
.................... int a;
.................... int b;
.................... int c;
.................... int d;
.................... setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
001C: BSF 03.5
001D: MOVF 01,W
001E: ANDLW C7
001F: IORLW 08
0020: MOVWF 01
....................
.................... a=10;
0021: MOVLW 0A
0022: BCF 03.5
0023: MOVWF 0F
.................... b=5;
0024: MOVLW 05
0025: MOVWF 10
.................... c=10/5;
0026: MOVLW 02
0027: MOVWF 11
.................... d=a/b;
0028: MOVF 0F,W
0029: MOVWF 13
002A: MOVF 10,W
002B: MOVWF 14
002C: GOTO 004 <-------******My Problem is here!
002D: MOVF 0D,W
002E: MOVWF 12
.................... }
002F: SLEEP

Configuration Fuses:
Word 1: 3FFA HS NOWDT NOPUT NOPROTECT

********************************************************

Why this behavior? when I divide two numbers, code works well. When I divide two int variables like a/b doesn't work!!

Please help me.. Rolling Eyes

Thank you,

Best Regards,

Thushi
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 14, 2006 11:41 pm     Reply with quote

It works perfectly well, but you have understand how the compiler works
and also how to use the MPLAB simulator.

1. In the Configure menu of MPLAB, go to the Select Device menu
and make sure it has 16F84 selected.

2. Modify your program so it doesn't fall off the end of main() by
adding a while(1) statement at the end.

3. Set the 'c' and 'd' variables to zero at the start of the program,
so you can see them change to the correct value each time you
run the program in the MPLAB simulator.

Add the lines shown in bold below:
Quote:

#include <16F84.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading

#use delay(clock=20000000)

void main()
{
int a;
int b;
int c;
int d;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);

c = 0;
d = 0;

a=10;
b=5;
c=10/5;
d=a/b;

while(1);
}


Now you're ready to test it:
1. In the debugger menu in MPLAB, select "MPLAB SIM" as the tool.
2. Compile the program.
3. Bring up a Watch window in MPLAB.
4. Add variables a,b,c, and d to the Watch window.
5. Double-click in the left margin of the source code window, on the
"while(1);" line. This will put a Breakpoint on that line. You will
see a red dot with a "B" in it on that line.
6. In the Debugger menu, select "Run".
7. You should see the Watch window get set to this:
Code:

A     0x0A
B     0x05
C     0x02
D     0x02

That proves the code is working. I tested this with MPLAB vs. 7.20
and with your version of the compiler, vs. 3.242.

If you single step through the program above by pressing F7 each time,
you will see the program pointer jump to the start when the "d=a/b;"
line is executed. As I explained in my post in your previous thread
(which you ignored), this happens because the compiler library code
for integer division is placed at address 0x0004. But if you press F7
one more time, the program pointer will jump back down to the main()
program again. It will end up at the "while(1);" statement. The
watch window will have the correct values, as shown above.

Your program can work, but you have to modify it as shown in
the list of items 1, 2, and 3 that I posted above.
Holy Ghost
Guest







PostPosted: Wed Mar 15, 2006 7:28 pm     Reply with quote

Dear PCM, are you a teacher in any way ??
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 15, 2006 7:30 pm     Reply with quote

Nope.
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