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

CCS cannot calculate next time

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



Joined: 16 Jun 2010
Posts: 31

View user's profile Send private message

CCS cannot calculate next time
PostPosted: Mon Aug 19, 2013 9:34 am     Reply with quote

I want to calculate next time in 24hr not use am. pm. I keep time 5 value in array and sort time in array. After that, find minimum time which more than current time. If have no value more than current time then next time is minimum time.

This is my code. If I use this code when I use Set_Feed_Time() and set only 1 time in array , Then function next_time() will show wrong value by return 1280 minute or next time = 21.20 which the right value should be the first value of array.

Code:
#include <16F886.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)

#include "flex_LCD.c"
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#include "input.c"

#define ADDR_DS1307  0xD0 //Address DS1307

int ext_int_flag = FALSE;
int set_hr=24;
int set_min=60;

typedef struct{
   BYTE sec;     // seconds
   BYTE min;     // minute
   BYTE hr;      // hour
   BYTE day;
   BYTE date;
   BYTE month;
   BYTE year;
}DS1307_RTC;

DS1307_RTC RTC;

typedef struct{

   int min;     // minute
   int hr;      // hour
   int sec_delay;
}time_feed;

time_feed t_feed;


void DS1307_Write(unsigned char ctl, unsigned char dat);
BYTE DS1307_Read(unsigned char ctl);
void DS1307_WriteDate(void);
void DS1307_WriteTime(void);

void DS1307_ReadDate(void);
void DS1307_ReadTime(void);
   
   
BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;

  temp = binary_value;
  retval = 0;

  while(1)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  }

  return(retval);
}

// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));
}

   /*******************************************************/
void DS1307_Write(unsigned char ctl, unsigned char dat){
   i2c_start();
   i2c_write(ADDR_DS1307);
   i2c_write(ctl);
   i2c_write(dat);
   i2c_stop();
}

/*******************************************************/
BYTE DS1307_Read(unsigned char ctl){
   
   BYTE dat;
   
   i2c_start();
   i2c_write(ADDR_DS1307);
   i2c_write(ctl);
   
   i2c_start();
   i2c_write(ADDR_DS1307+1);
   dat = i2c_read(0);
   i2c_stop();
   return(dat);
}

/*******************************************************/
void DS1307_WriteDate(void){
   DS1307_Write(0x04,RTC.date);
   DS1307_Write(0x05,RTC.month);
   DS1307_Write(0x06,RTC.year);
}

/*******************************************************/
void DS1307_WriteTime(void){
   DS1307_Write(0x00,RTC.sec);
   DS1307_Write(0x01,RTC.min);
   DS1307_Write(0x02,RTC.hr);
}

/*******************************************************/
void DS1307_ReadDate(void){
   RTC.date = DS1307_Read(0x04);
   RTC.month = DS1307_Read(0x05);
   RTC.year = DS1307_Read(0x06);
}

/*******************************************************/
void DS1307_ReadTime(void){
   RTC.sec = DS1307_Read(0x00);
   RTC.min = DS1307_Read(0x01);
   RTC.hr = DS1307_Read(0x02);
}

void DS1307_SetTime(int hr, int min){

   RTC.hr = bin2bcd(hr);
   RTC.min = bin2bcd(min);
   RTC.sec = 0x00;
}

/************* Delay *************/
void delay(){
int sec_delay;
  for(sec_delay=t_feed.sec_delay;sec_delay>0;sec_delay--){
  lcd_gotoxy(1,2);
  delay_ms(1000);
  printf(lcd_putc,"\r Food = %d sec " sec_delay);
  }

}

/************* time **************/
void time(){

int hr=0;
int min=0;

    delay_ms(50);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"\r    -- : --    ");
    delay_ms(50);

while(1){

if(input(PIN_B1)==0){

     delay_ms(100);
     hr++;

if(hr>=24){hr=0;}

    delay_ms(100);
    lcd_gotoxy(1,2);
 
    printf(lcd_putc,"\r    %02d : %02d   ", hr,min);
    delay_ms(50);
   

}else if(input(PIN_B2)==0){

     delay_ms(100);
     min++;

if(min>=60){min=0;}


    delay_ms(100);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"\r    %02d : %02d   ", hr,min);
    delay_ms(50);
 
}

if(input(PIN_B3)==0){

    lcd_gotoxy(1,2);
    printf(lcd_putc,"\r Time : %02d:%02d ", hr,min);
    delay_ms(500);


DS1307_SetTime(hr,min);
DS1307_WriteTime();

   break;
     
}
}  // while hr

}

/************* check same time **************/
int check_time(int hr[], int hr_ck, int min[], int min_ck,int count){
  int x;
  for(x=0;x<count;x++){
 
  if((hr[x]== hr_ck) && (min[x]== min_ck)){
 
    if((hr_ck==24)&& (min_ck==60)){
   
        return 0;
        break;

   }else{
        return 1;
        break;
     
   }
  }
  }
   return 0;
 
}

/************ get now time ***************/
int16 getNow(){

   int16 now_hr,now_min;
   int16 now_all;
   DS1307_ReadTime();


   now_hr = bcd2bin(RTC.hr);
   now_min = bcd2bin(RTC.min);
     
   now_all = (now_hr*60)+now_min;
   
      //printf("\r\n n=%ld",now_all);
      return now_all;
}

/**************************************/
int16 next_time(int16 hr, int16 min, int pos){

int32 minimVal;
int i=0,check;
int16 now_all;
int32 all[5],t;
int16 hrk[5],mnk[5];
int n,j;
   
hrk[pos] = hr;
mnk[pos] = min;
all[pos] =(hrk[pos]*60 + mnk[pos]) ;

     //minimVal2 = (hrk[0]*60) + mnk[0];
     now_all = getNow();
     printf("\r\n pos=%d, n=%ld, ",pos,all[pos]);

     n = sizeof(all)/sizeof(all[0]);

   /* bubble sort */
   for (i = 0; i > 0; i--) {   
       for (j = 0; j < i; j++) {   /* move max (in [0] to [i]) to last ([i]) */
           if (all[j] > all[j+1]) {   /* exchange if bigger than next */
               t = all[j];
               all[j] = all[j+1];
               all[j+1] = t; }}
           
               }
               
               printf("\r\n i %ld ", all[0]); 
               minimVal= all[0];     /********** This line will show right or wrong value it up to code in EXT_ISR(). ***********/
             

  for (i = 0; i < pos+1; i++) {
   
  if(all[i]>now_all){
  if(all[i]!=1500){
      minimVal= all[i];
      break;
  }
 }
       
     // printf("\r\n%ld ", all[i]);
       
   }

//printf("\r\n min %ld ", minimVal);

     return minimVal;
}

/************* feed time **************/
void feed_time(int count){


int hr[5],min[5];
int16 next;

while(1){

   if(input(PIN_B1)==0){

       delay_ms(250);
       set_hr++;

if(set_hr>24){
   set_hr=0;
   set_min= 0;
}

if(set_hr==24){
     set_min=60;
     delay_ms(50);
     lcd_gotoxy(1,2);
     printf(lcd_putc,"\r F%d :  -- : -- ",count+1);
     delay_ms(50);
}else{
     delay_ms(100);
     lcd_gotoxy(1,2);
     printf(lcd_putc,"\r F%d :  %02d : %02d ", count+1, set_hr,set_min);
     delay_ms(50);
}
   
}else if(input(PIN_B2)==0){

     delay_ms(250);
     set_min++;

if(set_min>=60){
     set_min=0;
   
}
    delay_ms(100);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"\r F%d :  %02d : %02d " count+1, set_hr,set_min);
    delay_ms(50);
   
}

if(input(PIN_B3)==0){
      hr[count] = set_hr;
     min[count] = set_min;

  if(check_time(hr,set_hr,min,set_min,count)){
     
      delay_ms(100);
      lcd_gotoxy(1,1);
      //printf(lcd_putc,"\rn=%d ar= %d,va= %d" ,count, set_hr,set_min);
      printf(lcd_putc,"\r This is same. ");
     delay_ms(500);
   
     }

    else{

          next= next_time(hr[count],min[count],count);
          t_feed.hr=next/60;
          t_feed.min=next%60;
         
          /*lcd_gotoxy(1,1);
          printf(lcd_putc,"\r hr =%d , min=%d " ,t_feed.hr,t_feed.min);
          delay_ms(500); */
         
if(set_hr==24){

    delay_ms(50);
    lcd_gotoxy(1,1);
    printf(lcd_putc,"\r F%d :  -- : -- ",count+1);
    delay_ms(50);
   
          lcd_gotoxy(1,2);
          printf(lcd_putc,"\r                  ");
          delay_ms(50);

    if((count+2)!=6){

         lcd_gotoxy(1,2);
         printf(lcd_putc,"\r F%d :  -- : -- ",count+2);
         delay_ms(50);
    }
}else{

    delay_ms(100);
    lcd_gotoxy(1,1);
    printf(lcd_putc,"\r F%d :  %02d : %02d " count+1, set_hr,set_min);
    delay_ms(50);
   
    if((count+2)!=6){
         lcd_gotoxy(1,2);
         printf(lcd_putc,"\r F%d :  %02d : %02d ",count+2,set_hr,set_min);
         delay_ms(50);
    }
   
}
         
     break;
   
     }

//break;
     
}
     
}  // while hr

}

/************* Sec Period **************/
void sec_feed(){

//unsigned int sec=10;

while(input(PIN_B3)){

if(input(PIN_B1)==0){
      delay_ms(100);
      t_feed.sec_delay++;

   if(t_feed.sec_delay>=250){t_feed.sec_delay=250;}
      delay_ms(100);
      lcd_gotoxy(1,2);
      printf(lcd_putc,"\r Time : %d sec ",t_feed.sec_delay);
      delay_ms(50);
   
}

if(input(PIN_B2)==0){
      delay_ms(100);
      t_feed.sec_delay--;

   if(t_feed.sec_delay<=1){t_feed.sec_delay=1;}
      delay_ms(100);
      lcd_gotoxy(1,2);
      printf(lcd_putc,"\r Time : %d sec ",t_feed.sec_delay);
      delay_ms(50);

}
}  // while sec
t_feed.sec_delay = t_feed.sec_delay;
//delay();

}

/************* Set time **************/
void Set_Time(){

    delay_ms(50);
    lcd_gotoxy(1,1);
    printf(lcd_putc,"\r   Set Clock   ");
    delay_ms(50);
   
    lcd_gotoxy(1,2);
    printf(lcd_putc,"[Next]      [OK]");
    delay_ms(50);

while(1){

if(input(PIN_B3)==0){

      delay_ms(500);
     
      time();
     
      break;

}

   if(!input(PIN_B0)){
   
        break;
    }
}
delay_ms(500);
}

/************* Set Feed Time **************/
void Set_Feed_Time(){
    int i;
    lcd_gotoxy(1,1);
       printf(lcd_putc,"\r Set Feed Time ");
    delay_ms(50);
   
    lcd_gotoxy(1,2);
       printf(lcd_putc,"[Next]      [OK]");
    delay_ms(50);


while(1){

if(input(PIN_B3)==0){

    delay_ms(50);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"\r F1 :  -- : -- ");


      delay_ms(500);

     
    for(i=0;i<5;i++){     // count feed time
        feed_time(i);
        delay_ms(500);
    }
     
     if(t_feed.hr!=25){
          delay_ms(50);
          lcd_gotoxy(1,2);
          printf(lcd_putc,"\r Next : %02d:%02d  " ,t_feed.hr,t_feed.min);
          delay_ms(500);
     }else{
          delay_ms(50);
          lcd_gotoxy(1,2);
          printf(lcd_putc,"\r                  ");
          delay_ms(50);
      }
      break;

}

   if(!input(PIN_B0)){
          delay_ms(50);
          lcd_gotoxy(1,2);
          printf(lcd_putc,"\r                  ");
          delay_ms(50);
      break;
    }
}
delay_ms(500);
}

/***************** Set Period Time *********************/

void Set_Period_Time(){

    lcd_gotoxy(1,1);
    printf(lcd_putc,"\rSet Feed Period");
    delay_ms(50);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"[Next]      [OK]");
    delay_ms(50);
   
while(1){
   
if(input(PIN_B3)==0){

    delay_ms(100);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"\r Time : %d sec ",t_feed.sec_delay);
    //delay_ms(50);
   
    delay_ms(500);
    sec_feed();
    break;
}

    if(!input(PIN_B0)){
   
      break;
    }
}
delay_ms(500);
   }
   
/***************** Rec Voice *********************/

void Rec_Voice(){

    lcd_gotoxy(1,1);
    printf(lcd_putc,"\r  Record Voice  ");
    delay_ms(50);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"[Next]      [OK]");
    delay_ms(50);
   
while(1){
   
     if(input(PIN_B3)==0){
         delay_ms(500);
         //sec_feed();
         break;
     }

    if(!input(PIN_B0)){
   
      break;
    }
}
delay_ms(500);
   }
   
/***************** Play Voice *********************/

void Play_Voice(){

    lcd_gotoxy(1,1);
    printf(lcd_putc,"\r  Play Voice  ");
    delay_ms(50);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"[Next]      [OK]");
    delay_ms(50);
   
while(1){
   
if(input(PIN_B3)==0){
    delay_ms(500);
    //sec_feed();
    break;
}

    if(!input(PIN_B0)){
   
      break;
    }
}
delay_ms(500);
   }

#INT_EXT                         
void EXT_ISR(void)               
{
ext_int_flag = TRUE;
}

/*******************************************************/

void main(){

t_feed.sec_delay = 10;
int16 next;

lcd_init();

enable_interrupts(GLOBAL);

    enable_interrupts(INT_EXT);  // Set external interrupt                   (4)
    ext_int_edge(H_TO_L);        // External interrupt high to low edge      (5)



while(TRUE){

if(ext_int_flag == TRUE)
{
    delay_ms(500);
    Set_Time();
    Play_Voice();
    Rec_Voice();
    Set_Period_Time();
    Set_Feed_Time();
     
    ext_int_flag = FALSE;

}
   
   lcd_gotoxy(1,1);
   DS1307_ReadTime();
   
   if((t_feed.hr==bcd2bin(RTC.hr)) && (t_feed.min==bcd2bin(RTC.min))){
        delay();
        next = next_time(24,60,5);
        t_feed.hr=next/60;
        t_feed.min=next%60;
                       
          lcd_gotoxy(1,2);
          printf(lcd_putc,"\r Next : %02d:%02d  " ,t_feed.hr,t_feed.min);
          delay_ms(500);
   }
   
   printf(lcd_putc,"\rTime : %2X:%2X:%2X\n", RTC.hr, RTC.min, RTC.sec);
   delay_ms(500);

}
}



This code if I use function in EXT_ISR like this it will show right next time value. But It can't use Play_Voice,Rec_Voice. because it will show error Out of ROM.

Code:
#INT_EXT                         
void EXT_ISR(void)               
{
    delay_ms(500);
    Set_Time();
    //Play_Voice();
    //Rec_Voice();
    Set_Period_Time();
    Set_Feed_Time();
}


Last edited by mmc01 on Wed Aug 21, 2013 3:05 am; edited 1 time in total
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Aug 19, 2013 3:15 pm     Reply with quote

can you condense this to just enough code to show
where it runs off the rails ?

the post is awfully big to get a handle on.
mmc01



Joined: 16 Jun 2010
Posts: 31

View user's profile Send private message

PostPosted: Mon Aug 19, 2013 8:09 pm     Reply with quote

I have problem with this function.

Code:
/**************************************/
int16 next_time(int16 hr, int16 min, int pos){

int32 minimVal;
int i=0,check;
int16 now_all;
int32 all[5],t;
int16 hrk[5],mnk[5];
int n,j;
   
hrk[pos] = hr;
mnk[pos] = min;
all[pos] =(hrk[pos]*60 + mnk[pos]) ;

//minimVal2 = (hrk[0]*60) + mnk[0];
now_all = getNow();
printf("\r\n pos=%d, n=%ld, ",pos,all[pos]);

n = sizeof(all)/sizeof(all[0]);

   /* bubble sort */
   for (i = 0; i > 0; i--) {   
       for (j = 0; j < i; j++) {   /* move max (in [0] to [i]) to last ([i]) */
           if (all[j] > all[j+1]) {   /* exchange if bigger than next */
               t = all[j];
               all[j] = all[j+1];
               all[j+1] = t; }}
           
               }
               
               printf("\r\n i %ld ", all[0]);
               minimVal= all[0];     /********** This line will show right or wrong value it up to code in EXT_ISR(). ***********/
             

  for (i = 0; i < pos+1; i++) {
   
  if(all[i]>now_all){
  if(all[i]!=1500){
      minimVal= all[i];
      break;
  }
 }
       
     // printf("\r\n%ld ", all[i]);
       
   }

//printf("\r\n min %ld ", minimVal);

return minimVal;
}


If I call function like this in main() and set only 1 value in array (another value set to 24hr , 60min ) it will return minimVal = 1280 in array 2 actually it have only 1 array.

Code:
while(TRUE){

if(ext_int_flag == TRUE)
{
    delay_ms(500);
    Set_Time();
    Play_Voice();
    Rec_Voice();
    Set_Period_Time();
    Set_Feed_Time();
     
ext_int_flag = FALSE;

}


But if I call function like this . It can calculate right value but can't use Play_Voice() , Rec_Voice() because it will show error Out of ROM .

Code:
#INT_EXT                         
void EXT_ISR(void)               
{
    delay_ms(500);
    Set_Time();
    //Play_Voice();
    //Rec_Voice();
    Set_Period_Time();
    Set_Feed_Time();
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19396

View user's profile Send private message

PostPosted: Tue Aug 20, 2013 12:30 am     Reply with quote

The 'in interrupt' way, is awful.
All interrupt routines have to be put into the bottom page of ROM. Hence the error. However it also brings with it the problem that by having a delay inside the interrupt, interrupts will be disabled for all the code in the main routines, killing performance.

You need to radically re-think your main loop. There are sections of this, where the processor sits looping for ages. Instead you should be looping as fast as possible, and doing the delays by having a system 'tick'. First problem then is that because of the long delays in the main loop, the 'external' version may hardly ever get called.
Then the version out in the main, when dealing with 'times' should always update a 'shadow' copy of the time, rather than the real time. Only once the value is set how you want, disable interrupts, and write this value.
The code I'm afraid is a mess. The indentation makes no sense at all, and the 'logic' is basically non existent. It's about as unreadable as some 'Chinglish' instruction manuals that sometimes come with equipment, where they have done a 'literal' translation of the Chinese words, without understanding the target language.... It is almost unintelligible, and what you are actually trying to do, is not at all obvious.
mmc01



Joined: 16 Jun 2010
Posts: 31

View user's profile Send private message

PostPosted: Tue Aug 20, 2013 2:18 am     Reply with quote

I don't know how to re-think main loop. it have to use external interrupt . but I will remove delay and use debouce switch instead.
ckielstra



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

View user's profile Send private message

PostPosted: Tue Aug 20, 2013 7:01 am     Reply with quote

I only looked at your code for about 5 seconds and then got a massive headache.

Here are some very basic PIC programming tips:
- Fix your indentation. The program is almost impossible to read because the start of each line is not aligned to the lines below and above. Perhaps your editor is set to use tabs instead of spaces?
- ALWAYS add the 'errors' keyword to the #rs232 line. Without this keyword the UART will stop working when the receive buffer overflows, that is, after 3 characters that you don't read fast enough.
- ALWAYS add the NOLVP fuse. In the last 5 years I have seen no one on this forum to use the old Low Voltage Programmers. Without this setting the processor will reset when a low voltage comes on the B3 pin.
- Give variables and functions a name that describes what they are doing. A function like delay() and time() is useless.
- Add a short description to each function about what it is doing and what the variables are doing.

Code:
/*******************************************************/
void DS1307_WriteTime(void){
   DS1307_Write(0x00,RTC.sec);
   DS1307_Write(0x01,RTC.min);
   DS1307_Write(0x02,RTC.hr);
}

void DS1307_SetTime(int hr, int min){

   RTC.hr = bin2bcd(hr);
   RTC.min = bin2bcd(min);
   RTC.sec = 0x00;
}

...
DS1307_SetTime(hr,min);
DS1307_WriteTime();
...
Very creative coding.... Rolling Eyes
Much easier when you combine this into 1 function. Same applies to all the other DS1307 read/write functions. That way you can get rid of the global rtc structure (did I already mention how tricky it is to share the same global variable in different functions?)

Code:
printf(lcd_putc,"\r Food = %d sec " sec_delay);
I'm surprised this is compiling as a ',' is missing before sec_delay.

Compiling your code I get a warning:
Quote:
>>> Warning 206 "main.c" Line 441(1,1): Variable of this data type is never greater than this constant
This is a serious bug that you need to fix. Hint: in CCS an integer is 8 bits size by default.

Code:
if(input(PIN_B1)==0){
Code like this will become easier to read when you add a define at the top for your code with a more logical name for the input pin. For example:
Code:
#define ENTER_SWITCH   PIN_B1

if(input(ENTER_SWITCH)==0){


Code:
int check_time(int hr[], int hr_ck, int min[], int min_ck,int count){
  int x;
  for(x=0;x<count;x++){
 
  if((hr[x]== hr_ck) && (min[x]== min_ck)){
 
    if((hr_ck==24)&& (min_ck==60)){
   
      return 0;
      break;

   }else{
      return 1;
      break;
     
   }
  }
  }
   return 0;
 
}
The break commands are doing nothing as the return instruction will already have you moved out from the function. Get rid of these.


This was only after a short look at your code. Because of all these I didn't have time and energy left to look into your real problem.
Ttelmah



Joined: 11 Mar 2010
Posts: 19396

View user's profile Send private message

PostPosted: Tue Aug 20, 2013 8:47 am     Reply with quote

Do a search here on state machines.

Realistically the functions in your main loop, are a candidate for a single function (the stuff from the interrupt), outside the state machine, and the rest as a state machine. With perhaps a 'tick' interrupt at 50Hz, doing the keyboard scans, and providing all the delays. Much more reliable debounce, and everything that should be called will be called as needed.

There have been lots of threads in the past detailing this style of code.

Then use a bit of logic in your actual functions.

I too suffered from a headache when trying to even begin to solve what you actually want the code to do.
mmc01



Joined: 16 Jun 2010
Posts: 31

View user's profile Send private message

PostPosted: Wed Aug 21, 2013 3:11 am     Reply with quote

I have problem with Set_Feed_Time() function. when I use this code in main.

Code:
while(TRUE){

if(ext_int_flag == TRUE)
{
    Set_Feed_Time();
    ext_int_flag = FALSE;
}


And I edit EXT_ISR(void) like this .

Code:
#INT_EXT                         
void EXT_ISR(void)               
{
    delay_ms(500);
    Set_Time();
    delay_ms(500);
    Set_Period_Time();
    delay_ms(500);
}


It show error out of rom a segment or the program is too large Set_Feed_Time . I have problem with Set_Feed_Time() .
Ttelmah



Joined: 11 Mar 2010
Posts: 19396

View user's profile Send private message

PostPosted: Wed Aug 21, 2013 3:55 am     Reply with quote

You cannot do these functions in the ISR.

Multiple reasons, first it results in the rest of your code being crippled by delays in the ISR.
Second as I've already said the ISR code has to be in the bottom page of ROM, so you cannot have anything this large in the ISR.

You need to radically re-think. The code is so bad, that it really beggar's belief. Sorry, but it is like the guy complaining that screws don't work properly, and it then turns out he is driving them with a hammer.
ckielstra



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

View user's profile Send private message

PostPosted: Wed Aug 21, 2013 3:59 am     Reply with quote

Golden programming rule: Keep interrupt code as fast as possible!
You have 1500ms of delay in the interrupt! Rolling Eyes

There are so many basic problems with the design of your code that the 'Out of ROM' message is the least to worry about.


Ttelmah did give you some really good advice, he spent more time looking into your code then I'm willing to do. Read his advice again. State machines are a very powerful concept.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat Aug 24, 2013 2:33 am     Reply with quote

Your opening title was "CCS cannot calculate next time".
You then swamp us with pages of indecipherable code.
I simply glanced at it, and gave up!

My advice is simplify, simplify, simplify.

Back to the titled problem.

You say CCS can't do the calculation.
No. You have to tell the CCS compiler what you want and how to do it.

I suggest you play at being computer.
Go through all possible situations.
Work out where you want to be for each case.
Then start to think about coding.

If you still have an issue, post no more than 10 lines of complete compilable code, which show the problem.

Mike
mmc01



Joined: 16 Jun 2010
Posts: 31

View user's profile Send private message

PostPosted: Mon Aug 26, 2013 9:28 am     Reply with quote

Thank you. Now I move Set_Feed_Time() to main() and change getNow() from
Quote:

int16 now_hr,now_min;
int16 now_all;


to int32 now_hr,now_min,now_all; and it work ! . It can calculate correct value.

If I use int16 it will calculate wrong value. but int16 can calculate correct value in external interrupt function.
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