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

my program stops when it gets to delay_ms

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



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

my program stops when it gets to delay_ms
PostPosted: Wed Jun 21, 2006 7:24 pm     Reply with quote

I'm using CCS PCM C Compiler, Version 3.223, 28885.
I have a couple identical boards with pic16f877a's on them. I programed them for the first time in a couple months and they would not run. So I stripped the program down to the bare essentials. Just blinking leds in b6 and b7. without the Delay_ms lines the code runs. I verify it with my O-scope since it is too fast to see. when I put in a delay_ms line then the code stops there and led1 stays on. What could I possibly be doing wrong here? It happens on both boards.
Thanks
Ringo


Here is the code

Code:

#include "16f877A.h"
#fuses hs,NOwdt,noprotect,put,lvp
#device *=16
#use delay(clock=20000000)
//#include "stdlib.h"
#define   LED1_PIN                  PIN_B6
#define   LED2_PIN                  PIN_B7
#define LED1_ON output_high(LED1_PIN)
#define LED2_ON output_high(LED2_PIN)
#define LED1_OFF output_low(LED1_PIN)
#define LED2_OFF output_low(LED2_PIN)
/***************************************************************************
void main() --
****************************************************************************/

void main()
{
int i;
 while(1)
 {
   led1_on;
   led2_off;
   delay_ms(150);
   led1_off;
   led2_on;
   delay_ms(150);
 }

}//end main




and the .lst file
Code:

CCS PCM C Compiler, Version 3.223, 28885               21-Jun-06 21:20

               Filename: 21_motor_test.LST

               ROM used: 62 words (1%)
                         Largest free fragment is 2048
               RAM used: 6 (2%) at main() level
                         7 (2%) worst case
               Stack:    1 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   018
0003:  NOP
....................  #include "16f877A.h"
....................  //////// Standard Header file for the PIC16F877A device //////////////// 
.................... #device PIC16F877A 
.................... #list 
.................... 
.................... #fuses hs,NOwdt,noprotect,put,lvp 
.................... #device *=16 
.................... #use delay(clock=20000000) 
0004:  MOVLW  22
0005:  MOVWF  04
0006:  BCF    03.7
0007:  MOVF   00,W
0008:  BTFSC  03.2
0009:  GOTO   017
000A:  MOVLW  06
000B:  MOVWF  78
000C:  CLRF   77
000D:  DECFSZ 77,F
000E:  GOTO   00D
000F:  DECFSZ 78,F
0010:  GOTO   00C
0011:  MOVLW  7B
0012:  MOVWF  77
0013:  DECFSZ 77,F
0014:  GOTO   013
0015:  DECFSZ 00,F
0016:  GOTO   00A
0017:  RETLW  00
.................... //#include "stdlib.h" 
.................... #define   LED1_PIN                  PIN_B6   
.................... #define   LED2_PIN                  PIN_B7 
.................... #define LED1_ON output_high(LED1_PIN) 
.................... #define LED2_ON output_high(LED2_PIN) 
.................... #define LED1_OFF output_low(LED1_PIN) 
.................... #define LED2_OFF output_low(LED2_PIN) 
.................... /*************************************************************************** 
.................... void main() -- 
.................... ****************************************************************************/ 
....................   
.................... void main() 
.................... { 
0018:  CLRF   04
0019:  BCF    03.7
001A:  MOVLW  1F
001B:  ANDWF  03,F
001C:  BSF    03.5
001D:  BSF    1F.0
001E:  BSF    1F.1
001F:  BSF    1F.2
0020:  BCF    1F.3
0021:  MOVLW  07
0022:  MOVWF  1C
0023:  MOVF   1C,W
0024:  BCF    03.5
0025:  BCF    0D.6
.................... int i; 
....................  while(1) 
....................  { 
....................    led1_on; 
0026:  BSF    03.5
0027:  BCF    06.6
0028:  BCF    03.5
0029:  BSF    06.6
....................    led2_off; 
002A:  BSF    03.5
002B:  BCF    06.7
002C:  BCF    03.5
002D:  BCF    06.7
....................    delay_ms(150); 
002E:  MOVLW  96
002F:  MOVWF  22
0030:  CALL   004
....................    led1_off; 
0031:  BSF    03.5
0032:  BCF    06.6
0033:  BCF    03.5
0034:  BCF    06.6
....................    led2_on; 
0035:  BSF    03.5
0036:  BCF    06.7
0037:  BCF    03.5
0038:  BSF    06.7
....................    delay_ms(150); 
0039:  MOVLW  96
003A:  MOVWF  22
003B:  CALL   004
....................  } 
003C:  GOTO   026
....................   
.................... }//end main 
....................   
....................   
.................... 
003D:  SLEEP

Configuration Fuses:
   Word  1: 3FF2   HS NOWDT PUT NODEBUG NOPROTECT BROWNOUT LVP NOCPD NOWRT

_________________
Ringo Davis
cmdrdan



Joined: 08 Apr 2005
Posts: 25
Location: Washington

View user's profile Send private message

PostPosted: Wed Jun 21, 2006 7:32 pm     Reply with quote

In the #FUSES statement, change LVP to NOLVP and see if that makes a difference. There are several threads on this forum relating to the hazards of using the LVP fuse....

Dan
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Wed Jun 21, 2006 7:46 pm     Reply with quote

I use a LVP programmer, without that fuse I can't program. Been using it for years without a problem so far. The LVP pin is pulled low to keep it in that mode so it is not floating or anything.
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Wed Jun 21, 2006 8:14 pm     Reply with quote

If I comment out the delay_ms line then the .lst file changes much more dramatically then I expected. Whey would that change things way above those lines, and before main?
Ringo
without the delay
Code:

CCS PCM C Compiler, Version 3.223, 28885               21-Jun-06 22:03

               Filename: 21_motor_test.LST

               ROM used: 36 words (0%)
                         Largest free fragment is 2048
               RAM used: 6 (2%) at main() level
                         6 (2%) worst case
               Stack:    0 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   004
0003:  NOP
....................  #include "16f877A.h"
....................  //////// Standard Header file for the PIC16F877A device //////////////// 
.................... #device PIC16F877A 
.................... #list 
.................... 
.................... #fuses hs,NOwdt,noprotect,put,lvp 
.................... #device *=16 
.................... #use delay(clock=20000000) 
.................... //#include "stdlib.h" 
.................... #define   LED1_PIN                  PIN_B6   
.................... #define   LED2_PIN                  PIN_B7 
.................... #define LED1_ON output_high(LED1_PIN) 
.................... #define LED2_ON output_high(LED2_PIN) 
.................... #define LED1_OFF output_low(LED1_PIN) 
.................... #define LED2_OFF output_low(LED2_PIN) 
.................... /*************************************************************************** 
.................... void main() -- 
.................... ****************************************************************************/ 
....................   
.................... void main() 
.................... { 
0004:  CLRF   04
0005:  BCF    03.7
0006:  MOVLW  1F
0007:  ANDWF  03,F
0008:  BSF    03.5
0009:  BSF    1F.0
000A:  BSF    1F.1
000B:  BSF    1F.2
000C:  BCF    1F.3
000D:  MOVLW  07
000E:  MOVWF  1C
000F:  MOVF   1C,W
0010:  BCF    03.5
0011:  BCF    0D.6
.................... int i; 
....................  while(1) 
....................  { 
....................    led1_on; 
0012:  BSF    03.5
0013:  BCF    06.6
0014:  BCF    03.5
0015:  BSF    06.6
....................    led2_off; 
0016:  BSF    03.5
0017:  BCF    06.7
0018:  BCF    03.5
0019:  BCF    06.7
.................... //   delay_ms(1); 
....................    led1_off; 
001A:  BSF    03.5
001B:  BCF    06.6
001C:  BCF    03.5
001D:  BCF    06.6
....................    led2_on; 
001E:  BSF    03.5
001F:  BCF    06.7
0020:  BCF    03.5
0021:  BSF    06.7
.................... //   delay_ms(1); 
....................  } 
0022:  GOTO   012
....................   
.................... }//end main 
....................   
....................   
.................... 
0023:  SLEEP

Configuration Fuses:
   Word  1: 3FF2   HS NOWDT PUT NODEBUG NOPROTECT BROWNOUT LVP NOCPD NOWRT


with the delay added
Code:

CCS PCM C Compiler, Version 3.223, 28885               21-Jun-06 22:07

               Filename: 21_motor_test.LST

               ROM used: 62 words (1%)
                         Largest free fragment is 2048
               RAM used: 6 (2%) at main() level
                         7 (2%) worst case
               Stack:    1 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   018
0003:  NOP
....................  #include "16f877A.h"
....................  //////// Standard Header file for the PIC16F877A device //////////////// 
.................... #device PIC16F877A 
.................... #list 
.................... 
.................... #fuses hs,NOwdt,noprotect,put,lvp 
.................... #device *=16 
.................... #use delay(clock=20000000) 
0004:  MOVLW  22
0005:  MOVWF  04
0006:  BCF    03.7
0007:  MOVF   00,W
0008:  BTFSC  03.2
0009:  GOTO   017
000A:  MOVLW  06
000B:  MOVWF  78
000C:  CLRF   77
000D:  DECFSZ 77,F
000E:  GOTO   00D
000F:  DECFSZ 78,F
0010:  GOTO   00C
0011:  MOVLW  7B
0012:  MOVWF  77
0013:  DECFSZ 77,F
0014:  GOTO   013
0015:  DECFSZ 00,F
0016:  GOTO   00A
0017:  RETLW  00
.................... //#include "stdlib.h" 
.................... #define   LED1_PIN                  PIN_B6   
.................... #define   LED2_PIN                  PIN_B7 
.................... #define LED1_ON output_high(LED1_PIN) 
.................... #define LED2_ON output_high(LED2_PIN) 
.................... #define LED1_OFF output_low(LED1_PIN) 
.................... #define LED2_OFF output_low(LED2_PIN) 
.................... /*************************************************************************** 
.................... void main() -- 
.................... ****************************************************************************/ 
....................   
.................... void main() 
.................... { 
0018:  CLRF   04
0019:  BCF    03.7
001A:  MOVLW  1F
001B:  ANDWF  03,F
001C:  BSF    03.5
001D:  BSF    1F.0
001E:  BSF    1F.1
001F:  BSF    1F.2
0020:  BCF    1F.3
0021:  MOVLW  07
0022:  MOVWF  1C
0023:  MOVF   1C,W
0024:  BCF    03.5
0025:  BCF    0D.6
.................... int i; 
....................  while(1) 
....................  { 
....................    led1_on; 
0026:  BSF    03.5
0027:  BCF    06.6
0028:  BCF    03.5
0029:  BSF    06.6
....................    led2_off; 
002A:  BSF    03.5
002B:  BCF    06.7
002C:  BCF    03.5
002D:  BCF    06.7
....................    delay_ms(1); 
002E:  MOVLW  01
002F:  MOVWF  22
0030:  CALL   004
....................    led1_off; 
0031:  BSF    03.5
0032:  BCF    06.6
0033:  BCF    03.5
0034:  BCF    06.6
....................    led2_on; 
0035:  BSF    03.5
0036:  BCF    06.7
0037:  BCF    03.5
0038:  BSF    06.7
....................    delay_ms(1); 
0039:  MOVLW  01
003A:  MOVWF  22
003B:  CALL   004
....................  } 
003C:  GOTO   026
....................   
.................... }//end main 
....................   
....................   
.................... 
003D:  SLEEP

Configuration Fuses:
   Word  1: 3FF2   HS NOWDT PUT NODEBUG NOPROTECT BROWNOUT LVP NOCPD NOWRT

_________________
Ringo Davis
Ttelmah
Guest







PostPosted: Thu Jun 22, 2006 8:13 am     Reply with quote

The amount of change, is both expected/normal.
Basically, the optimiser is smart enough, that if you never use a 'delay', it does not generate the code for it, removing it from the memory. When you code 'delay_ms(1)', the code for the delay is called, and the code (which is actually generated at the delay statement line), is then present.
So the code that actually 'implements' the delay, is the twenty lines starting at address 4, and the 'main', gains the call to this.
The code looks 'spot on'.
It loops 6 times, round a counter, which counts 256 times, taking 3 instruction times per inner loop, and an extra four in the outer loops. It then loops 123 times, again taking 3 instruction times per loop, and repeats this whole operation, for the number of times passed to the 'delay' function. The time is then:
1/(20000000/4) = instruction time.

count = (((256*3)+3)*6 + 123*3) + 3 = 4998

total time (you need to add the call/return to this) = instruction time*4998

= 0.9996mSec

With the time for the call/return, this is just about as near as is possible.

So, there is nothing wrong with the code.
The possibility then is that something else is preventing the chip working. A 'classic' that produces this sort of failure, is not having a current limiting resistor to the LEDs, which then work if only turned on for a few uSec, but if switched on for longer, pull the supply rail down, stopping the chip...

Best Wishes
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 8:26 am     Reply with quote

I can't imagine it is something wrong with the boards. I built a dozen of these and the other 10 have been running in a commercial robot for about a year. These are spares I use for testing code and have worked fine for the past year until I put them up for about a month and just got them down to test some code. and both are acting the same way. So if it is Hardware, then it happened to both boards at the same time.

It always stops running code at the delay_ms line. I even tried taking out the led stuff and setting pwm to 10%, then delaying and setting it to 90%. It does the same thing where it sets the first one, then just stops running and never sets the second one. The crystal is still running the whole time, what else could make the chip stop running code? I'm confused by this one. Eventually I'll change the chip out if it comes to that, but I'm hoping to find something before going to that extreme. It is a TQFP on a crowed board.
Thanks
Ringo
_________________
Ringo Davis
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 8:44 am     Reply with quote

I've had this exact thing happen to me and after several days of frustration and several dents in my computer case(I only used a small hammer Wink ) I found the problem was that I had an interrupt enabled but no ISR declared. Once I disabled the interrupt or put in a blank ISR the delay() function started to work properly. Check to make sure you have ISR functions for ALL enabled interrupts. Hope that helps.

Ronald
Ttelmah
Guest







PostPosted: Thu Jun 22, 2006 8:52 am     Reply with quote

OK. One thought. Maybe the compiler does not like having 'mixed case', in the fuse statement. If the watchdog was being left enabled, then with the delay present, it'd quickly reset. Are you sure the clock is 20MHz?. If the clock is not correctly declared, then the code will be incorrect in it's delays, and could wtchdog, before getting out of the first part of the routine...

Best Wishes
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 8:56 am     Reply with quote

In this simple code I don't have any interrupts enabled (I'll remember that for later though). I'll try putting in a global disable tonight though.
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 8:58 am     Reply with quote

I'll fix the watchdog code, maybe it is making it hang, but it is not resetting. In my PWM test once the pwm starts it does not stop, if the pic was resetting the pwm would stop right?
_________________
Ringo Davis
Ttelmah
Guest







PostPosted: Thu Jun 22, 2006 10:10 am     Reply with quote

It would _hiccup_, but the break would be tiny without the delay, and might not even be visible, unless you have a digital scope, with the ability to sync, on a longer gap between edges. With the delays present, it might sit permanently in the first part of the loop.

Best Wishes
kam



Joined: 15 Jul 2005
Posts: 59

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 10:53 am     Reply with quote

About 1 year ago, I was starting to program pic10f206's using Microchips ICD2 and mplab v7.30, and guess what?! The program would stop/crash whenever the delay_ms was used. Long story short, mplab was not programming the 206's WatchDogTimer fuse correctly, it was always ON, hense in the delay_ms, the WDT kicked in...

~Kam (^8*
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 11:18 am     Reply with quote

I installed PCM vs. 3.223, compiled your program without changes
and programmed it into a PicDem2-Plus board. It ran OK.
I had to jumper pin B3 to ground, through a pull-down resistor, because
of the LVP fuse. I also had to remove the ICD2 connector after
programming, because the ICD2 uses pins B6 and B7.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Jun 22, 2006 11:22 am     Reply with quote

If it works on your board that sure makes it sound like HW, but on two identical boards? I'll replace the pic and see what happens.
Thanks
Ringo
_________________
Ringo Davis
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