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

write in eeprom question

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



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

write in eeprom question
PostPosted: Fri Feb 27, 2009 4:58 pm     Reply with quote

Hi

Someone can help to correct this program?
I use PIC18F452 at 20Mhz

It don't have any errors but my problem is... it stay write correct in eeprom?

I need save STATE variable if power turnoff or if I make reset CPU.
Code:

int8  STATE;

void turnoff() //corte definitivo de igniçao
{
int8 hb,oldip;
int16 time;

oldip=0;
hb=0;
STATE=1;
   
   output_low(GREEN);
   output_high(RED);
   
   write_eeprom(0, STATE);     
   
   
   output_high(R2);           
   delay_ms(1000);
   output_low(R2);
   delay_ms(1000);
   
   while(TRUE)                 
   {
   do {
       if ((input(PBREAK)==1) && (input(SENSOR)==1))
       {                                         
         if (input(HBREAK)==1)                         
            {
            oldip=1;
            hb++;
            }
       }
         else oldip=0;
              delay_ms(10);
              time++;
        }while (time<=700);    //esquivalente +- a 9sec
   }
       
   if (hb==3)
   {
      STATE=0;
      time=0;
      hb=0;
      write_eeprom(0, STATE);       
      reset_cpu();
   }else
      {
      STATE=1;
      time=0;
      hb=0;
      write_eeprom(0, STATE);     
      reset_cpu();
      }
}

(...)


void main()
{
int8 start,door;

STATE=0;
door=0;
write_eeprom(0, STATE);
correct=0;

output_low(RED);
output_low(GREEN);     
While(TRUE)
{
   
   //STATE==1 circuito electrico curtado
   //STATE==0 circuito electrico ligado
   
   STATE = read_eeprom(0);       
   if (STATE==1) turnoff();       
   if (STATE==0) correct==0;
   
   if(correct==0)
   {
   do {
   start=input(IGN);
   }while(start!=1);
       
   readcode();
   }
   
   if (correct==1)           
   {                         
   output_high(GREEN);
   
      do {
      door=input(DOORS);     
      start=input(IGN);
      }while((door!=1 || start!=1));

   }
   if (correct==0)
   {
   output_high(PIN_C5);
     fstop();
   }
 
}
}

Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Fri Feb 27, 2009 5:36 pm     Reply with quote

Your code enters an infinite loop here:

Code:
   while(TRUE)                 
   {
   do {
       if ((input(PBREAK)==1) && (input(SENSOR)==1))
       {                                         
         if (input(HBREAK)==1)                         
            {
            oldip=1;
            hb++;
            }
       }
         else oldip=0;
              delay_ms(10);
              time++;
        }while (time<=700);    //esquivalente +- a 9sec
   }


You need to remove the while(TRUE) ala:

Code:
   do {
       if ((input(PBREAK)==1) && (input(SENSOR)==1))
       {                                         
         if (input(HBREAK)==1)                         
            {
            oldip=1;
            hb++;
            }
       }
         else oldip=0;
              delay_ms(10);
              time++;
        }while (time<=700);    //esquivalente +- a 9sec
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Sat Feb 28, 2009 11:46 am     Reply with quote

thanks I don´t see that :D

now I stay test my program with prototipe but I have some problems..

in this part:

Quote:


int8 STATE; //variavel que vai dizer o estado do carro
int8 correct; //codigo correcto


void readcode()
{
int16 time;
int count,oldip,d;

count=0;
time=0;
oldip=input(SENSOR);



do {
d=input(DOORS);
}while(d!=0);
delay_ms(2000);

do {
d=input(DOORS);
}while(d!=0);

output_high(PBREAK);
do {
if (input(SENSOR) == 1)
{
if (oldip==0)
{
oldip=1;
count++;
}
}
else
oldip=0;
delay_ms(10);
time++;
}while (time<=1500);


if (count==3)
{
time=0;
count=0;
correct=1;
}

if (count!=3)
{
time=0;
count=0;
correct=0;
}
}

void main()
{
int8 start,door;

STATE=0;
door=0;
write_eeprom(0, STATE);
correct=0;

output_low(RED);
output_high(GREEN);

While(TRUE)
{

//STATE==1 circuito electrico curtado
//STATE==0 circuito electrico ligado

STATE = read_eeprom(0); STATE
if (STATE==1) turnoff();
if (STATE==0) correct==0;

if(correct==0)
{
do {
start=input(IGN);
}while(start!=1);

readcode();
}


if (correct==1)
{

output_high(GREEN);

do {
door=input(DOORS);
start=input(IGN);
}while((door!=1 || start!=0));

}
if (correct==0)
{
output_high(RED);
fstop();
}

}
}


after program call function readcode(); and I click on sensor 3 times on SENSOR (I confirm thar with output_high/low and an led in "if (count==3)"), I think how program continue in function main I go to "if (correct==1)" and stay on "do" cicle...

but I test this with leds and it after readcode(); it go direct for if(correct==0).

some one can explain me why?

kind regards
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Sat Feb 28, 2009 12:20 pm     Reply with quote

I noticed another error too:

Code:
if (STATE==0) correct==0;


Should be:

Code:
if (STATE==0) correct=0;


No sure whether its causing your problem or not, but it maybe.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Sat Feb 28, 2009 7:21 pm     Reply with quote

Hi

thanks for this correction, I don´t see that...

but my stupid error stay there

Quote:

if (count==3)
{
time=0;
count=0;
correct=1;
}

if (count!=3)
{
time=0;
count=0;
correct=0;
}


I put count=0 in 1º "if" and it go into 2ª "if" because in this time count dosen't 3 but 0 :D

I continue my debug and if I have more problems I write there.

regards
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 3:04 am     Reply with quote

Oops yes Smile 2nd statement should be else-if. All you really need to do is:

Code:
if(count == 3)
   correct = 1;
else
   correct = 0;
time = 0;
count = 0;
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 7:03 am     Reply with quote

Hi

I find oder bug

Quote:

do {
door=input(DOORS);
start=input(IGN);
}while((door!=1) || (start!=0));


this cycle stay work as "AND" but I think how "||" is an "OR" correct?

for I can out of this cycle I need put "door" in 5V and "start" in 0V at same time...

I try test that:

Quote:

do {
door=input(DOORS);
start=input(IGN);

if(door==1) aux2=1;
if(start==0) aux2=1;
}while(aux2!=0);

aux2=0;


but this way program only make this cicle one time.

I test connection and I have on DOORS->0V and IGN->5V

some ideias?
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 2:55 pm     Reply with quote

Code:

aux2=0;
do {
   door=input(DOORS);
   start=input(IGN);

   if(door==1) aux2=1;
   if(start==0) aux2=1;
}while(aux2==0);


The first code doesn't work because you need to use &&, then as soon as door==1 OR start==0 the loop exits.

Code:
}while(door!=1 && start!=0);


It seems backward because you know when you want the loop to exit, but are writing a statement to keep it running.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 7:03 am     Reply with quote

Hi

thanks Sydney oder eyes see better Laughing

other problem:
Code:

void readcode()
{
int16 time;
int count,oldip,d;

count=0;
time=0;
oldip=input(SENSOR);

   do {             
      d=input(DOORS);
      }while(d!=0);
      delay_ms(1000);
   
   output_high(PIN_B1);
   do {
       if (input(SENSOR) == 1)
         {
            if (oldip==0)
               {
               oldip=1;
               count++;
               }
         }
            else
               oldip=0;
               delay_ms(10);
               time++;
      }while (time<=1500);    //esquivalente +- a 20sec
     
   
      if (count==3)         
      {
      count=0;
      time=0;
      correct=1;//#### CODIGO CORRECTO  #############
      }
      else
      {
      time=0;
      count=0;
      correct=0;
      }
}

void main()
{
int8 start,door;

STATE=0;
write_eeprom(0, STATE);
correct=0;

output_low(RED);
output_high(GREEN);

While(TRUE)
{
   
   //STATE==1 circuito electrico curtado
   //STATE==0 circuito electrico ligado
   
   STATE = read_eeprom(0);       
   if (STATE==1) turnoff();     
   if (STATE==0) correct=0;
   
   if(correct==0)
   {
   do {
   start=input(IGN);
   }while(start!=1);
       
   readcode();
   }
   

   if (correct==1)           
   {                         
      output_high(GREEN);
     
      do {                 
      door=input(DOORS);     
      start=input(IGN);
      output_low(PIN_B1);
      }while(door!=1 && start!=0);
   
      output_high(PIN_B1);
      delay_ms(1000);
      output_low(PIN_B1);
      delay_ms(1000);
     
      [b]readcode();[/b]
   }
   if (correct==0)
   {
      output_high(RED);
      fstop();
   }
 
}
}

problem now is ... why when readcode() function is called for the 2ª time and I press SENSOR 3 times don't work? it return correct=0 and got to "if (correct==0)"


Oder problem stay on EEPROM Write...

when on turnoff() function I have "STATE=0;" and "write_eeprom(0, STATE);" when I make "reset_cpu();" I program start on main() funtion I have

Quote:

STATE = read_eeprom(0); STATE
if (STATE==1) turnoff();
if (STATE==0) correct=0;


after that my program don't go to "turnoff()" function Crying or Very sad it continue normaly in order...

someone know where stay the problem?

regards
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 10:31 am     Reply with quote

Its hard to see how you want it to work?

It looks like you need to press SENSOR 3 times, every time readcode() is called for the red light not to come on.

Your program seems overcomplicated, for what it needs to do.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 10:51 am     Reply with quote

hi

look only main and readcode function:

Quote:

void readcode()
{
int16 time;
int count,oldip,d;

count=0;
time=0;
oldip=input(SENSOR);



do {
d=input(DOORS);
}while(d!=0);
delay_ms(1000);

output_high(YELOW);
do {
if (input(SENSOR) == 1)
{
if (oldip==0)
{
oldip=1;
count++;
}
}
else
oldip=0;
delay_ms(10);
time++;
}while (time<=1500); //esquivalente +- a 20sec


if (count==3) //exclusivamente 3 vezes
{
count=0;
time=0;
correct=1;
}
else
{
time=0;
count=0;
correct=0;
}
}

void main()
{
int8 start,door;

STATE=0;
write_eeprom(0, STATE);

output_low(RED);
output_high(GREEN);

While(TRUE)
{

//STATE==1 circuito electrico curtado
//STATE==0 circuito electrico ligado

STATE = read_eeprom(0);
if (STATE==1) turnoff();
if (STATE==0) correct=0;

if(correct==0)
{
do {
start=input(IGN);
}while(start!=1);

readcode();
}


if (correct==1)
{

do {
door=input(DOORS);
start=input(IGN);
output_low(YELOW);
}while(door!=1 && start!=0);

readcode();
}
if (correct==0)
{
output_high(RED);
output_low(YELOW);
fstop();
}

}
}


I start the program and it go to readcode() after I need put my code (press 3 times SENSOR) after that it wait for door=0 or start=1, when one of this occur it go to readcode() e the problem stay there...

I press 3 times SENSOR and when return of readcode() red light turn on.

my ideia is while I put correct code it never go to "if (correct==0)"...

PS: Sydney look at your pm box
Sydney



Joined: 13 Feb 2009
Posts: 71

View user's profile Send private message

PostPosted: Mon Mar 02, 2009 11:30 am     Reply with quote

I dont see what the problem is, but if readcode() is returning correct==1 on the 1st call, then it should on the 2nd also
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