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 support@ccsinfo.com

Strange ">=" problem

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







Strange ">=" problem
PostPosted: Sat Jan 10, 2009 11:56 am     Reply with quote

Hello!

I'm working on a simple fading example, to test PWM... but I noticed a strange problem in the fade_down() sub.

The for loop never ends:

Code:
for(step = max_duty; step >= 0; step--) {


The only way I can stop it at the correct value is change to:

Code:
for(step = max_duty; step != -1L; step--) {


Maybe it's a stupid mistake but I can't find it...
Thanks!


Code:

#include "main.h"
#include <string.h>

#define PR2 20
#define BUFFER_SIZE 10
#define CR 0x0d
#define LF 0x0a
#define STEP_DELAY 30

char help_cmd[5] = "?";
char fadeup_cmd[10] = "FADEUP";
char fadedown_cmd[10] = "FADEDOWN";
char fadex_cmd[10] = "FADEX";

long max_duty;
char input_buffer[BUFFER_SIZE];
int input_index;
int1 input_ok;
long step;

#INT_RDA
void rs232_isr(void) {

   char temp;
   temp = getc();

   if (temp == CR) {
      input_buffer[input_index] = '\0';     
      input_ok = true;
      printf("\r\n");
      return;
   }

   if (temp == LF) return;

   putc(temp);
   input_buffer[input_index] = toupper(temp);
   
   if (input_index >= (BUFFER_SIZE - 1)) input_index = 0;
   else input_index++;
}


void fade_up() {
 
   setup_timer_2(T2_DIV_BY_1, PR2, 1);
   set_pwm1_duty(0);
   printf("  Start fading up...\r\n");
   
   setup_ccp1(CCP_PWM);
   for(step = 0; step <= max_duty; step++) {
      printf("%Ld\t", step);
      set_pwm1_duty(step);
      delay_ms(STEP_DELAY);
   }
   
   printf("  ...done!\r\n");
}


void fade_down() {

   setup_timer_2(T2_DIV_BY_1, PR2, 1);
   set_pwm1_duty(max_duty);
   printf("  Start fading down...\r\n");
   
   setup_ccp1(CCP_PWM);
   for(step = max_duty; step >= 0; step--) {
      printf("%Ld\t", step);
      set_pwm1_duty(step);
      delay_ms(STEP_DELAY);
   }
   
   printf("  ...done!\r\n");
}


void usage() {

   printf("  Available commands:\r\n");
   printf("  FADEUP - start fading up\r\n");
   printf("  FADEDOWN - start fading down\r\n");
   printf("  FADEX - start cross fading\r\n");
}


void decode_command() {

   if (strcmp(input_buffer, fadeup_cmd)==0)
      fade_up();
   else if (strcmp(input_buffer, fadedown_cmd)==0)
      fade_down();
   else if (strcmp(input_buffer, fadex_cmd)==0) {
      fade_up();
      delay_ms(STEP_DELAY);
      fade_down();
   }
   else if (strcmp(input_buffer, help_cmd)==0)
      usage();
   else printf("  Unknown command: %s\r\n", input_buffer);
}


void main() {
   
   setup_ccp1(CCP_OFF);
   max_duty = 4 * (PR2 + 1);

   input_ok = False;
   input_index = 0;
   
   disable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   printf("> ");

   while(1) {

      if(input_ok) {
     
         disable_interrupts(GLOBAL);
         input_ok = False;
         decode_command();
         
         printf("> ");
         input_index = 0;
         enable_interrupts(GLOBAL);
      }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 10, 2009 12:55 pm     Reply with quote

In CCS, all integer data types are unsigned by default. An unsigned
integer can never be less than 0. To make 'step' into a signed value,
you need to declare like this:
Quote:
signed long step;
LukeD
Guest







PostPosted: Sun Jan 11, 2009 2:03 pm     Reply with quote

Embarassed thanks and sorry for the dumb question
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