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

there must be a better way
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

there must be a better way
PostPosted: Mon Jan 02, 2006 3:49 am     Reply with quote

OK, we all know i dont know what i am doing Smile. Can sombode tell me a better way of doing this??

Code:


                                   lightbank1_on_h = atoi(gets(rs232data));
               gets(rs232data);
               lightbank1_off_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_m = atoi(rs232data);



i know there is a emu thing in c, but i have no idea how to use it here.

Thanks, Mark
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 02, 2006 5:08 am     Reply with quote

Hi All, just to add, what i am trying to go is seta bunch of vars using rs232. This all works, but my program is now to big and i am getting out out rom errors. Is there a way of maybe getting a list and reading it?

Thanks for all your help

Mark
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Mon Jan 02, 2006 9:23 am     Reply with quote

Markdem wrote:
Is there a way of maybe getting a list and reading it?


Look at the list file (.lst) in the same directory as the HEX file.

As far as trying to do what you are in your code sample it appears as though you are just doing:

Code:

               lightbank1_on_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_m = atoi(rs232data);
               gets(rs232data);


Five times?

Why not:

Code:

           for(i = 0;i < 5;i++) {
               lightbank1_on_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_h = atoi(rs232data);
               gets(rs232data);
               lightbank1_on_m = atoi(rs232data);
               gets(rs232data);
               lightbank1_off_m = atoi(rs232data);
               gets(rs232data);

           }


Or, explain in more detail what you're trying to do. Is this your reception of data to set the 1307?

John
dyeatman



Joined: 06 Sep 2003
Posts: 1923
Location: Norman, OK

View user's profile Send private message

PostPosted: Mon Jan 02, 2006 9:35 am     Reply with quote

Another suggestion, why not put the gets() and Atoi() in a function and call it? Something along the lines of:

int getdata()
{
get your string input here, convert to int using atoi() then return with the int value

Update: As Humberto says below, make sure to validate the string then add a null (0x0) to the end of the number string after reception and BEFORE using atoi(). Otherwise the function may "run off into the wilderness" while doing the conversion and give you strange results.
}

lightbank1_off_h = getdata();

Later on after you get things basically working, rather than using gets(), it would be a good idea to use an #int_rda() interrupt routine to get the chars and store them in a buiffer. This way you have time to do other things in Main() and dont spend all your time sitting and waiting on gets().


Last edited by dyeatman on Mon Jan 02, 2006 11:32 am; edited 3 times in total
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Jan 02, 2006 11:05 am     Reply with quote

atoi(string)
By definition the string parameter in the function atoi()
is a pointer to a null terminated string of characters.

1) If you don't have a characters array that ends with 0x00 (null) as the string terminator, the atoi() function will try to process incoming data (by incrementing a pointer) until it can finds a null byte somewhere.
Before using atoi() you must validate the received data because if the result can't be represented, the behavior of atoi() is undefined.

2) Try to ovoid using atoi() if you have not enough room in your memory. Remember that it use stdlib.h

3) Try to make the ASCII to int convertion in one place, not in every new line of your code.

Humberto
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

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

PostPosted: Mon Jan 02, 2006 1:05 pm     Reply with quote

Humberto wrote:
3) Try to make the ASCII to int convertion in one place, not in every new line of your code.


Mark is fetching a new character on every other line, so he's trying to convert new data with atoi().
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Jan 02, 2006 2:11 pm     Reply with quote

Quote:

Mark is fetching a new character on every other line, so he's trying to convert new data with atoi().


= Double wrong !!!

Do not forget he starts this thread with this phrase:

there must be a better way

Humberto
ckielstra



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

View user's profile Send private message

PostPosted: Mon Jan 02, 2006 6:09 pm     Reply with quote

Quote:
2) Try to ovoid using atoi() if you have not enough room in your memory. Remember that it use stdlib.h
I want to emphasize on this. atoi() is a very versatile function, it accepts both decimal and hexadecimal, upper and lower characters, positive and negative values but this comes at a price: look at the list file and you'll see it takes over 400 bytes on a PIC18 with v3.241

If you know the format of the data you are receiving you can write a less versatile but more compact version by stripping all unneeded functionality from the CCS supplied function in stdlib.h.
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 02, 2006 7:38 pm     Reply with quote

Thank you all for your replys. The blockof code i have pasted is wrong, The lightbank changes 5 times, so it gets on on_h, off_h, on_m and off_m for 5 light banks. Basicly i am making a timer for 5 lights, and the data i am getting here is the hour and min that the timers need to go on and off.

I have put in alot of time tring to think of ways that i can use for loops, and i might have a way of doing it. However, is this really the best way of doing this. All i really need is to get 20 ints form my vb app. Is there maybe a better way of sending that much data??

I am not really loking for code (unless you really want to write it), i just need ideas

Again, thanks so much for your help
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 02, 2006 7:52 pm     Reply with quote

just to make it easyer to see what i am tring to do, here is my code.

Please dont lauth at it, i am tring to learn Smile


Code:

#include <16f877a.h>
#device adc=8
#fuses NOWDT,HS, NOPROTECT, BROWNOUT, NOPUT, NODEBUG, NOLVP
#use delay(clock=20000000)
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3)
#use rs232(baud=57600,parity=N,bits=8,xmit=PIN_C6,rcv=PIN_C7)

#include <STDLIB.H>
#include <ds1307.c>
#include <lcd420.c>
#include <24LC256.c>
#include <ctype.h>

#define FAN           PIN_C0
#define LIGHT1         PIN_D0                                                      //global defines
#define LIGHT2         PIN_D1
#define LIGHT3         PIN_D2
#define LIGHT4         PIN_D3
#define LIGHT5         PIN_D4
#define PHVALVE        PIN_D5
#define HEATER1       PIN_D6   
#define HEATER2        PIN_D7                                    

#define LCDBACKLIGHT   PIN_E0
#define SYSOK          PIN_E1

//enum memory
//   {addmaxtemp=0, addmaxhoodtemp, addmaxph,
//    addlightbank1_off_h, addlightbank1_on_h, addlightbank1_on_m, addlightbank1_off_m,
//    addlightbank2_off_h, addlightbank2_on_h, addlightbank2_on_m, addlightbank2_off_m,
//    addlightbank3_off_h, addlightbank3_on_h, addlightbank3_on_m, addlightbank3_off_m,
//    addlightbank4_off_h, addlightbank4_on_h, addlightbank4_on_m, addlightbank4_off_m,
//    addlightbank5_off_h, addlightbank5_on_h, addlightbank5_on_m, addlightbank5_off_m
//   };

#define   addmaxtemp                   0x00   
#define   addmaxhoodtemp                0x01                                                      //global memory allocations
#define addmaxph                   0x02
#define addlightbank1_on_h             0x03
#define addlightbank1_off_h          0x04
#define addlightbank1_on_m             0x05
#define addlightbank1_off_m          0x06
#define addlightbank2_on_h             0x07
#define addlightbank2_off_h          0x08
#define addlightbank2_on_m             0x09
#define addlightbank2_off_m          0x10
#define addlightbank3_on_h             0x11
#define addlightbank3_off_h          0x12
#define addlightbank3_on_m             0x13
#define addlightbank3_off_m          0x14
#define addlightbank4_on_h             0x15
#define addlightbank4_off_h            0x16
#define addlightbank4_on_m            0x17
#define addlightbank4_off_m          0x18
#define addlightbank5_on_h             0x19
#define addlightbank5_off_h          0x20
#define addlightbank5_on_m             0x21
#define addlightbank5_off_m          0x22                                             

int sec, min, hrs, day, mth, year, dow, newdata;                                          // setup varibales
int heatstat, newtimes;                                          
int currentph, maxph, currenttemp, maxtemp, maxhoodtemp, fanstat;
int currenthood, currentkh, phvalvestat,currentc02;
int lightbank1_on_h, lightbank1_off_h, lightbank2_on_h, lightbank2_off_h;
int lightbank3_on_h, lightbank3_off_h, lightbank4_on_h, lightbank4_off_h;   
int lightbank5_on_h, lightbank5_off_h, lightbank1_stat, lightbank2_stat;
int lightbank3_stat, lightbank4_stat, lightbank5_stat, phvalve_stat, fan_stat;
int lightbank1_force, lightbank2_force, lightbank3_force, lightbank4_force, lightbank5_force;
int phvalve_force, heater_force, fan_force;
int lightbank1_on_m, lightbank1_off_m, lightbank2_on_m, lightbank2_off_m;
int lightbank3_on_m, lightbank3_off_m, lightbank4_on_m, lightbank4_off_m;   
int lightbank5_on_m, lightbank5_off_m;
int serialreq;
char rs232data [5];
 

#int_rda                                                //request recived from vbapp                                             
void serial()
   {
      serialreq = 0;

      serialreq = getc();
      switch (serialreq)
         {
            case 48:                                    //requset to get current live data
               printf("%U\n",currenttemp);   
               printf("%U\n",currenthood);   
               printf("%U\n",currentph);   
               printf("%U\n",currentkh);   
               printf("%U\n",currentc02);   
               printf("%U\n",lightbank1_stat);
               printf("%U\n",lightbank2_stat);
               printf("%U\n",lightbank3_stat);
               printf("%U\n",lightbank4_stat);
               printf("%U\n",lightbank5_stat);
               printf("%U\n",heatstat);
               printf("%U\n",phvalvestat);
               printf("%U\n",fanstat);
               break;

            case 49:                                          //request to get user set data
               printf("%u\n",maxtemp);                                          
               printf("%u\n",maxhoodtemp);                                             
               printf("%u\n",maxph);                                          
               printf("%02u:%02u\n",lightbank1_on_h,lightbank1_on_m);                     
               printf("%02u:%02u\n",lightbank1_off_h,lightbank1_off_m);                  
               printf("%02u:%02u\n",lightbank2_on_h,lightbank2_on_m);                     
               printf("%02u:%02u\n",lightbank2_off_h,lightbank2_off_m);                     
               printf("%02u:%02u\n",lightbank3_on_h,lightbank3_on_m);                     
               printf("%02u:%02u\n",lightbank3_off_h,lightbank3_off_m);                  
               printf("%02u:%02u\n",lightbank4_on_h,lightbank4_on_m);                     
               printf("%02u:%02u\n",lightbank4_off_h,lightbank4_off_m);                     
               printf("%02u:%02u\n",lightbank5_on_h,lightbank5_on_m);                  
               printf("%02u:%02u\n",lightbank5_off_h,lightbank5_off_m);                                    
               break;

            case 50:                                                   //request to force light 1 on or off
               lightbank1_force = getc();
               break;
            case 51:                                                   //request to force light 2 on or off
               lightbank2_force = getc();
               break;
            case 52:                                                   //request to force light 3 on or off
               lightbank3_force = getc();
               break;
            case 53:                                                   //request to force light 4 on or off
               lightbank4_force = getc();
               break;
            case 54:                                                   //request to force light 4 on or off
               lightbank5_force = getc();
               break;
            case 55:                                                   //request to force heater on or off
               heater_force = getc();
               break;
            case 56:                                                   //request to force ph valve on or of
               phvalve_force = getc();
               break;
            case 57:                                                   //request to force fan on or of
               fan_force = getc();
               break;
            case 97:                                                   /set time
               gets(rs232data);
               hrs = atoi(rs232data);
               gets(rs232data);
               min = atoi(rs232data);
               gets(rs232data);
               sec = atoi(rs232data);
               gets(rs232data);
               day = atoi(rs232data);
               gets(rs232data);
               mth = atoi(rs232data);
               gets(rs232data);
               year = atoi(rs232data);
               ds1307_set_date_time(day,mth,year,1,hrs,min,sec);
               break;
            case 98:                                                      //set water vars
               gets(rs232data);
               maxtemp = atoi(rs232data);
               gets(rs232data);
               maxhoodtemp = atoi(rs232data);
               gets(rs232data);
               maxph = atoi(rs232data);
               newdata = 1;
            default:
               printf("fffff");
               break;
         }   
   }      

//void lightsetup()                                                            
//   {   
//               disable_interrupts(INT_RDA);
//               lightbank1_on_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_h = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_on_m = atoi(rs232data);
//               gets(rs232data);
//               lightbank1_off_m = atoi(rs232data);
//               enable_interrupts(INT_RDA);
//               newdata = 1;   
//   }

void phwork()                                             //READ PH VALUE SUB
   {
      set_adc_channel(1);                                    //port a0
      delay_ms(200);
      currentph = 0;
      currentph = Read_ADC();                                 //read ph value
      delay_ms(200);
      lcd_gotoxy(6,3);
      printf(lcd_putc,"%03U",currentph);

      if(phvalve_force < 50)                                 //works out if valve is forced on or off
         {
            if(phvalve_force == 48)
               {
                  output_low(phvalve);
                  phvalvestat = 0;
                  lcd_gotoxy(17,4);
                   printf(lcd_putc,"OFF");
               }
            else
               {   
                  output_high(phvalve);
                  phvalvestat = 1;
                  lcd_gotoxy(17,4);
                   printf(lcd_putc,"ON ");
               }   
         }
      else
         {
            delay_ms(1);                                 //do ph controll here   
         }
   }   

void tempwork()                                             //READ TEMP VALUE SUB
   {
      set_adc_channel(0);                                    //port a0
      delay_ms(200);
      currenttemp = 0;
      currenttemp = Read_ADC();                              //read temp value   
      delay_ms(200);
      lcd_gotoxy(6,2);
      printf(lcd_putc,"%03U",currenttemp);                           
   
      if(heater_force < 50)
         {
            if(heater_force == 48)
               {
                  output_low(heater1);
                  output_low(heater2);
                  heatstat = 0;
                  lcd_gotoxy(9,4);
                   printf(lcd_putc,"OFF");
               }
            else
               {   
                  output_high(heater1);
                  output_high(heater2);
                  heatstat = 1;
                  lcd_gotoxy(9,4);
                   printf(lcd_putc,"ON ");
               }   
         }
      else
         {
            if(currenttemp >= maxtemp)
            {
               output_low(heater1);
               output_low(heater2);
               heatstat = 0;
               lcd_gotoxy(9,4);
               printf(lcd_putc,"OFF");
            }
            else
            {
               output_high(heater1);
               output_high(heater2);
               heatstat = 1;
               lcd_gotoxy(9,4);
               printf(lcd_putc,"ON ");
            }
      
         }

   }

void lightwork()
   {
      ds1307_get_time(hrs,min,sec);
      if(lightbank1_force < 50)
         {
            if(lightbank1_force == 48)
               {
                  output_low(light1);
                  lightbank1_stat = 0;
               }
            else
               {   
                  output_high(light1);
                  lightbank1_stat = 1;
               }   
         }
      else
         {
            if (hrs >= lightbank1_on_h && hrs <= lightbank1_off_h && min >= lightbank1_on_m && min < lightbank1_off_m)
               {
                  output_high(light1);
                  lightbank1_stat = 1;
               }
            else
               {
                  output_low(light1);
                  lightbank1_stat = 0;
               }
         }

      if(lightbank2_force < 50)
         {
            if(lightbank2_force == 48)
               {
                  output_low(light2);
                  lightbank2_stat = 0;
               }
            else
               {   
                  output_high(light2);
                  lightbank2_stat = 1;
               }   
         }
      else
         {
            if (hrs >= lightbank2_on_h && hrs <= lightbank2_off_h && min >= lightbank2_on_m && min < lightbank2_off_m)
               {
                  output_high(light2);
                  lightbank2_stat = 1;
               }
            else
               {
                  output_low(light2);
                  lightbank2_stat = 0;
               }                           
         }

      if(lightbank3_force < 50)
         {
            if(lightbank3_force == 48)
               {
                  output_low(light3);
                  lightbank3_stat = 0;
               }
            else
               {   
                  output_high(light3);
                  lightbank3_stat = 1;
               }   
         }
      else
         {
            if (hrs >= lightbank3_on_h && hrs <= lightbank3_off_h && min >= lightbank3_on_m && min < lightbank3_off_m)
               {
                  output_high(light3);
                  lightbank3_stat = 1;
               }
            else
               {
                  output_low(light3);
                  lightbank3_stat = 0;
               }                           
         }

      if(lightbank4_force < 50)
         {
            if(lightbank4_force == 48)
               {
                  output_low(light4);
                  lightbank4_stat = 0;
               }
            else
               {   
                  output_high(light4);
                  lightbank4_stat = 1;
               }   
         }
      else
         {
            if (hrs >= lightbank4_on_h && hrs <= lightbank4_off_h && min >= lightbank4_on_m && min < lightbank4_off_m)
               {
                  output_high(light4);
                  lightbank4_stat = 1;
               }
            else
               {
                  output_low(light4);
                  lightbank4_stat = 0;
               }                              
         }
      
      if(lightbank5_force < 50)
         {
            if(lightbank5_force == 48)
               {
                  output_low(light5);
                  lightbank5_stat = 0;
               }
            else
               {   
                  output_high(light5);
                  lightbank5_stat = 1;
               }   
         }
      else
         {
            if (hrs >= lightbank5_on_h && hrs <= lightbank5_off_h && min >= lightbank5_on_m && min < lightbank5_off_m)
               {
                  output_high(light5);
                  lightbank5_stat = 1;
               }
            else
               {
                  output_low(light5);
                  lightbank5_stat = 0;
               }                              
         }
   }   

void hoodfan()
   {
      if(fan_force < 50)
         {
            if(fan_force == 48)
               {
                  output_low(fan);
                  fanstat = 0;
               }
            else
               {   
                  output_high(fan);
                  fanstat = 1;
               }   
         }
      else
         {
            output_low(fan);                              //this is where we check if the hood is getting too hot                              
         }
   }

void readuservars()                                          //reads user set vars from eeprom
   {
      maxtemp = read_eeprom(addmaxtemp);   
      maxhoodtemp = read_eeprom(addmaxhoodtemp);
      maxph = read_eeprom(addmaxph);
      lightbank1_on_h = read_eeprom(addlightbank1_on_h);
      lightbank1_off_h = read_eeprom(addlightbank1_off_h);
      lightbank1_on_m = read_eeprom(addlightbank1_on_m);
      lightbank1_off_m = read_eeprom(addlightbank1_off_m);
      lightbank2_on_h = read_eeprom(addlightbank2_on_h);
      lightbank2_off_h = read_eeprom(addlightbank2_off_h);
      lightbank2_on_m = read_eeprom(addlightbank2_on_m);
      lightbank2_off_m = read_eeprom(addlightbank2_off_m);
      lightbank3_on_h = read_eeprom(addlightbank3_on_h);
      lightbank3_off_h = read_eeprom(addlightbank3_off_h);
      lightbank3_on_m = read_eeprom(addlightbank3_on_m);
      lightbank3_off_m = read_eeprom(addlightbank3_off_m);
      lightbank4_on_h = read_eeprom(addlightbank4_on_h);
      lightbank4_off_h = read_eeprom(addlightbank4_off_h);
      lightbank4_on_m   = read_eeprom(addlightbank4_on_m);      
      lightbank4_off_m = read_eeprom(addlightbank4_off_m);
      lightbank5_on_h = read_eeprom(addlightbank5_on_h);
      lightbank5_off_h = read_eeprom(addlightbank5_off_h);
      lightbank5_on_m = read_eeprom(addlightbank5_on_m);
      lightbank5_off_m = read_eeprom(addlightbank5_off_m);
   }   

void writeuservars()                                    //wites user set vars to eeprom
   {
      disable_interrupts(INT_RDA);
      write_eeprom(addmaxtemp, maxtemp);
      write_eeprom(addmaxhoodtemp, maxhoodtemp);
      write_eeprom(addmaxph, maxph);
      write_eeprom(addlightbank1_on_h,lightbank1_on_h);
      write_eeprom(addlightbank1_on_h,lightbank1_off_h);
      write_eeprom(addlightbank1_on_h,lightbank1_on_m);
      write_eeprom(addlightbank1_on_h,lightbank1_off_m);
      write_eeprom(addlightbank2_on_h,lightbank2_on_h);
      write_eeprom(addlightbank2_on_h,lightbank2_off_h);
      write_eeprom(addlightbank2_on_h,lightbank2_on_m);
      write_eeprom(addlightbank2_on_h,lightbank2_off_m);
      write_eeprom(addlightbank3_on_h,lightbank3_on_h);
      write_eeprom(addlightbank3_on_h,lightbank3_off_h);
      write_eeprom(addlightbank3_on_h,lightbank3_on_m);
      write_eeprom(addlightbank3_on_h,lightbank3_off_m);
      write_eeprom(addlightbank4_on_h,lightbank4_on_h);
      write_eeprom(addlightbank4_on_h,lightbank4_off_h);
      write_eeprom(addlightbank4_on_h,lightbank4_on_m);
      write_eeprom(addlightbank4_on_h,lightbank4_off_m);
      write_eeprom(addlightbank5_on_h,lightbank5_on_h);
      write_eeprom(addlightbank5_on_h,lightbank5_off_h);
      write_eeprom(addlightbank5_on_h,lightbank5_on_m);
      write_eeprom(addlightbank5_on_h,lightbank5_off_m);
      enable_interrupts(INT_RDA);
   }

void printtime()
   {
      ds1307_get_time(hrs,min,sec);
      lcd_gotoxy(1,1);
      printf(lcd_putc,"\%02d:\%02d:\%02d", hrs,min,sec);
   }

void printdate()
   {
      ds1307_get_date(day,mth,year,dow);
      lcd_gotoxy(11,1);
      printf(lcd_putc,"%02d/\%02d/20\%02d",day,mth,year);
   }


void main()                                              //START OF MAIN PROGRAM
   {
      lcd_init();                                          //int lcd display
      printf(lcd_putc,"\f");
      lcd_gotoxy(1,1);
      printf(lcd_putc,"AquaMaster");   
      lcd_gotoxy(1,2);
      printf(lcd_putc,"Mark Demczuk");
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Booting - 10%%");
      delay_ms(200);
        
      output_d(0x00);                                       //make sure outputs are low
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Booting - 40%%");
      delay_ms(200);

      readuservars();                                       //get eeprom vars
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Booting - 50%%");
      delay_ms(200);

      lightbank1_force = 50;                                 //set vars                                       
      lightbank2_force = 50;   
      lightbank3_force = 50;   
      lightbank4_force = 50;   
      lightbank5_force = 50;
      heater_force = 50;
      phvalve_force = 50;   
      lightbank1_stat = 0;
      lightbank2_stat = 0;
      lightbank3_stat = 0;
      lightbank4_stat = 0;
      lightbank5_stat = 0;
      newdata = 0;
      newtimes = 1;

      lcd_gotoxy(1,4);
      printf(lcd_putc,"Booting - 60%%");
      delay_ms(200);

      setup_port_a(ALL_ANALOG);                              //setup adc
         setup_adc(ADC_CLOCK_INTERNAL);
         set_adc_channel(0);               
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Booting - 70%%");
      delay_ms(200);
      
      ds1307_init();
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Booting - 80%%");
      delay_ms(200);

      init_ext_eeprom();                                    //setup eeprom      
      lcd_gotoxy(1,4);
      printf(lcd_putc,"Booting - 90%%");
      delay_ms(200);

      printf(lcd_putc,"\f");                                 //setup of screen
      lcd_gotoxy(1,2);
      printf(lcd_putc,"TEMP:");
      lcd_gotoxy(13,2);
      printf(lcd_putc,"GH:");
      lcd_gotoxy(1,3);
      printf(lcd_putc,"pH:");
      lcd_gotoxy(13,3);
      printf(lcd_putc,"CO2:");
      lcd_gotoxy(1,4);
      printf(lcd_putc,"HEATERS:OFF");
      lcd_gotoxy(13,4);
      printf(lcd_putc,"CO2:OFF");

      enable_interrupts(INT_RDA);
      enable_interrupts(GLOBAL);
   
      while(1)                                        //start of main loop
            {   
               output_high(sysok); 
               phwork();                                 //readph
               printtime();
               printdate();
               output_low(sysok); 
               tempwork();                                 //readtemp
               printtime();
               output_high(sysok);
               if(newdata == 1)
                  {
                     newdata = 0;
                     writeuservars();
                  }                             
               lightwork();                              //check lights
               printtime();
               output_low(sysok); 
               hoodfan();                                 //check for overheating
               printtime();
            
                                       
            }

   }
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Jan 02, 2006 8:30 pm     Reply with quote

This function is too long.

Quote:

#int_rda //request recived from vbapp
void serial()
{
serialreq = 0;

serialreq = getc();
switch (serialreq)
{
case 48: //requset to get current live data
printf("%U\n",currenttemp);
printf("%U\n",currenthood);
printf("%U\n",currentph);
printf("%U\n",currentkh);
printf("%U\n",currentc02);
printf("%U\n",lightbank1_stat);
printf("%U\n",lightbank2_stat);
printf("%U\n",lightbank3_stat);
printf("%U\n",lightbank4_stat);
printf("%U\n",lightbank5_stat);
printf("%U\n",heatstat);
printf("%U\n",phvalvestat);
printf("%U\n",fanstat);
break;

case 49: //request to get user set data
printf("%u\n",maxtemp);
printf("%u\n",maxhoodtemp);
printf("%u\n",maxph);
printf("%02u:%02u\n",lightbank1_on_h,lightbank1_on_m);
printf("%02u:%02u\n",lightbank1_off_h,lightbank1_off_m);
printf("%02u:%02u\n",lightbank2_on_h,lightbank2_on_m);
printf("%02u:%02u\n",lightbank2_off_h,lightbank2_off_m);
printf("%02u:%02u\n",lightbank3_on_h,lightbank3_on_m);
printf("%02u:%02u\n",lightbank3_off_h,lightbank3_off_m);
printf("%02u:%02u\n",lightbank4_on_h,lightbank4_on_m);
printf("%02u:%02u\n",lightbank4_off_h,lightbank4_off_m);
printf("%02u:%02u\n",lightbank5_on_h,lightbank5_on_m);
printf("%02u:%02u\n",lightbank5_off_h,lightbank5_off_m);
break;

case 50: //request to force light 1 on or off
lightbank1_force = getc();
break;
case 51: //request to force light 2 on or off
lightbank2_force = getc();
break;
case 52: //request to force light 3 on or off
lightbank3_force = getc();
break;
case 53: //request to force light 4 on or off
lightbank4_force = getc();
break;
case 54: //request to force light 4 on or off
lightbank5_force = getc();
break;
case 55: //request to force heater on or off
heater_force = getc();
break;
case 56: //request to force ph valve on or of
phvalve_force = getc();
break;
case 57: //request to force fan on or of
fan_force = getc();
break;
case 97: /set time
gets(rs232data);
hrs = atoi(rs232data);
gets(rs232data);
min = atoi(rs232data);
gets(rs232data);
sec = atoi(rs232data);
gets(rs232data);
day = atoi(rs232data);
gets(rs232data);
mth = atoi(rs232data);
gets(rs232data);
year = atoi(rs232data);
ds1307_set_date_time(day,mth,year,1,hrs,min,sec);
break;
case 98: //set water vars
gets(rs232data);
maxtemp = atoi(rs232data);
gets(rs232data);
maxhoodtemp = atoi(rs232data);
gets(rs232data);
maxph = atoi(rs232data);
newdata = 1;
default:
printf("fffff");
break;
}
}


I had not read all your code but in the interrupt routine I see the most evident conceptual mistake.
You should know that #int_rda is an interrupt handler, this implies that it should be as short and fast as possible because the microcontroller had been interrumped its normal task to take care of this event. While the microcontroller is running inside the interrupt routine, all other task are pending and waiting.
All that is needed to do inside the interrupt routine is to store all the necesary incoming chars and set a flag when such condition was satisfied and quit (return) to main.

Then in main() normally you have enough time to parse the received packet. Something like this:

Code:

void main()
{
 while(1)
      {
       ..............
       your stuff
       ..............
     
       if(full_string_rcved)
         {             
          switch (serialreq = serial_buffer[n])
                 {
                  case 48:
                  ............
                  ............
                  case 98: 
                 }
          full_string_rcved = FALSE;
         }     
    }
}


If you search in this forum you will find a lot of examples regarding how to store and retrieve chars from a buffer.


Humberto
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

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

Don't do serial communication in the ISR
PostPosted: Mon Jan 02, 2006 10:50 pm     Reply with quote

Here's an example of bad code:

Code:

#int_something_or_other
void isr_something_or_other()
{
 printf("hellp world!/n/r");
}


Here's an example of better code:

Code:

int8 g_iFlag;

#int_something_or_other
void isr_something_or_other()
{
 g_iFlag = TRUE; // set the flag
}

void main()
{
 g_iFlag = FALSE; // initialize the flag
 
 while (1)
 {
  if (g_iFlag == 1) // check the flag
  {
   printf("hello word!\n\r")
  }
 }
}



Moral: Never ever do serial communication in the ISR. Especially RS232.
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 02, 2006 10:59 pm     Reply with quote

Hi All. OK, so i know know i should nt do anything in the isr that will take too much time. Is this correct? the only thing is, i need to act on the request as soon as i recive it. eg, when i get a request from the vb app to turn on a output, i want to do it right then, and not when the pic checks the flag that was set in the isr. How does one do such a thing??
newguy



Joined: 24 Jun 2004
Posts: 1903

View user's profile Send private message

PostPosted: Mon Jan 02, 2006 11:25 pm     Reply with quote

What is your idea of "right away"? There's nothing wrong with doing it the way that has been suggested. The PIC's interrupt routine will set the flag, exit the interrupt, and then the main routine resumes. It will detect the flag and set the lights FAST. Really, REALLY fast. Maybe 30 - 40 instruction cycles at the most, which would be 30 - 40 microseconds if your clock is 4 MHz. 3 or 4 microseconds if you're running the PIC at 40 MHz.

I've developed a few commercial products, and I always handle interrupts in this manner. No data gets lost (missing incoming characters from the UART, button presses, external interrupts, etc.), and the PIC responds instantaneously (to the naked eye, anyway.) Give it a try.
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 02, 2006 11:36 pm     Reply with quote

OK, thanks for that, i will change the isr to only set flags, and then act on the flag.

This, however, does not fix the out of rom problem. Does anyone have any ideas on how i can send some kind fo configuration file to configure the on and off timers??

Thanks you very much for your help
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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