Previous in Forum: Eng.Mujeeb.Dubai   Next in Forum: icu and ics
Close
Close
Close
7 comments
Rate Comments: Nested
Anonymous Poster

Help with Program Code?

04/29/2010 2:51 AM

Hello;

This is my program; and it is not work.

the main Idea fo my program that; when any input of A0,A1,A2 change.

the PIC 16F877A, must make a pulses at the Outputs B0,B1,B2 if the input B3=0.

the Pulses is like this on (300ms) off (300ms) as appear in the program

#include "D:\cc\d\main.h"
int check_change();
void menu();
void end();
void cursor();
int which_MSG();
int8 sen1,sen2,sen3,i;


void main()
{

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);setup_wdt(WDT_18MS);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

output_low(PIN_B0);
output_low(PIN_B1);
output_low(PIN_B2);

sen1=input(PIN_A0);
sen2=input(PIN_A1);
sen3=input(PIN_A2);
start:
if (check_change()==1) //&& input(PIN_B3)==0 )
goto messaging;
else
goto start;
messaging:
sen1=input(PIN_A0);
sen2=input(PIN_A1);
sen3=input(PIN_A2);

end();
menu();
menu();
menu();
cursor();
cursor();
cursor();
cursor();
menu();
for(i=0; i<which_MSG(); i++)
cursor();
menu();
menu();
cursor();
menu();
menu();
cursor();
menu();
menu();
end();
goto start;
}

int check_change()
{
if( (sen1==input(PIN_A0)) && (sen2==input(PIN_A1)) && (sen3==input(PIN_A2)) )
return 0;

else
return 1;
}

void menu()
{
output_high(PIN_B0);
delay_ms(900);
output_low(PIN_B0);
delay_ms(900);
}
void end()
{
output_high(PIN_B1);
delay_ms(900);
output_low(PIN_B1);
delay_ms(900);
}
void cursor()
{
output_high(PIN_B2);
delay_ms(900);
output_low(PIN_B2);
delay_ms(900);
}

int which_MSG()
{
int8 s1,s2,s3;
s1=input(PIN_A0);
s2=input(PIN_A1);
s3=input(PIN_A2);
if(s1==0 && s2==0 && s3==0)
return 0;
else if(s1==0 && s2==0 && s3==1)
return 1;
else if(s1==0 && s2==1 && s3==0)
return 2;
else if(s1==0 && s2==1 && s3==1)
return 3;
else if(s1==1 && s2==0 && s3==0)
return 4;
else if(s1==1 && s2==0 && s3==1)
return 5;
else if(s1==1 && s2==1 && s3==0)
return 6;
else if(s1==1 && s2==1 && s3==1)
return 7;

}

what is the wrong???

Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.
Guru
Hobbies - Musician - New Member Australia - Member - Torn and breading Engineering Fields - Nanoengineering - New Member APIX Pilot Plant Design Project - Member - New Member

Join Date: Jun 2006
Location: Magnetic Island, Queensland, Australia
Posts: 3721
Good Answers: 74
#1

Re: Help with Program Code?

04/29/2010 11:56 PM

Wow

what is the wrong???

Its your program, at least that is what you claim. If any one here could find what is wrong with it, it would be theirs now, wouldn't it?

You hang in there Mate, Rome was not built in a day, Ky.

__________________
The Twain Has Met
Reply
Guru

Join Date: May 2007
Location: Geelong, Australia
Posts: 1084
Good Answers: 54
#2

Re: Help with Program Code?

04/30/2010 1:46 AM

Sorry, I can't/won't help. We all have these problems, it's the act of solving them that makes us better and faster programmers for next time.

Break it down into smaller steps and see if each bit does what you think it does.

By the way,

if(s1==0 && s2==0 && s3==0)
return 0; etc

can be simplified a tad, try reading the inputs as a word and masking unused bits

__________________
If there's something you don't understand...Then a wizard did it. As heard on "The Simpsons".
Reply
Guru

Join Date: Oct 2007
Location: Zagreb, Republic of Croatia (native name Hrvatska) ,EU, Europe
Posts: 545
Good Answers: 8
#3

Re: Help with Program Code?

04/30/2010 3:22 AM

I would say for one thing that You do not check for signal change but only for signal state, as You have to save previous state of input lines and compare new state with this to see if there is change.

Then You have to add condition test of B0==0 even if You detect change on input lines, which is not there even if comment say it is........

Last but not least, I would do it using bits switching in control byte and test whole byte for change in comparison with saved state of control byte.

If there is change and B0==0, then all You need to do is to have test to check which bit is on and send corresponding pulse on output line.

That way You get all combinations covered very simply without need for testing which it actually is, else for all Your numbered combinations You have to have same combination of outputs.

Now only question is whether You have to have signals in parallel when outputting them or You can do each one after another (I know about leech circuits that keep holding signal on line after getting them switched on, but I do not know what You are using as I am not Engineer of Electronic, just an old pro in programming that happened to have programmed controller for Magnetic Tape device that my friend has made for me).

I do not know if You can use threads in case signals have to go in parallel, but I guess it is not applicable here.

Main rule in programming is to make it as simple as possible, and in my time also with as few instructions as possible.

Sorry that I have no time to go over this program in details, and I was not programming in C for at least 20 years, so I am little rusty in that department :-))

__________________
Per Aspera ad Astra
Reply
Guru

Join Date: Oct 2007
Location: Zagreb, Republic of Croatia (native name Hrvatska) ,EU, Europe
Posts: 545
Good Answers: 8
#4
In reply to #3

Re: Help with Program Code?

04/30/2010 4:06 AM

Sorry, I wrote >>test of B0==0 << when it should have been >>test of B3==0<<.....

Another advice: we were testing program on the paper in old times, where You write table with names and state they have, and then walk trough program one instruction at time and see if it do what it is supposed to do. Make sure You set and test all combinations of input and check whether You get appropriate combination on output....

Then also You can add simple routine that would generate all combination of inputs and let program run on those inputs, with printout of inputs and outputs which You can check for correctnes.......

So when You are sure it is all working correctly, you remove input generating routine and output printing, and You have program You need to do whatever it is.........

__________________
Per Aspera ad Astra
Reply
Guru

Join Date: Aug 2005
Location: Hemel Hempstead, UK
Posts: 5826
Good Answers: 322
#5

Re: Help with Program Code?

04/30/2010 4:20 AM

I don't have any knowledge experience or real understanding: but,

What delimits what happens in the for loop?

Should this:

for(i=0; i<which_MSG(); i++)
cursor();
menu();
menu();
cursor();
menu();
menu();
cursor();
menu();
menu();
end();
goto start;
}

Look like something like this

for(i=0; i<which_MSG(); i++)
{
cursor();
menu();
menu();
cursor();
menu();
menu();
cursor();
menu();
menu();
end();
}
goto start;
}

__________________
If you spend all your time looking for people and things to complain about: trust me, you will find plenty to complain about.
Reply
Guru

Join Date: Oct 2007
Location: Zagreb, Republic of Croatia (native name Hrvatska) ,EU, Europe
Posts: 545
Good Answers: 8
#7
In reply to #5

Re: Help with Program Code?

04/30/2010 3:10 PM

Yes, that goto in a loop is mistake definitely!

But this is also syntax error to forget to write parentheses

(IF it is "C" language) so this program is not compiled also.........

Test on B3==0 should be uncommented.

Subroutine labeling/names are badly chosen

(end(), cursor(), menu()) as names do not reflect

function performed and therefore create confusion.

Then instead

goto messaging

whole program part under messaging:

should be put there instead.

This is because messaging is not referenced anywhere else,

so it is not necessary.

It is not explained why pulses on some lines are repeated

different number of times, and program has no way to

finish working as it always cycle back to "start:"

>> for(i=0; i<which_MSG(); i++)

this is bad practice to set condition for end of loop on function

result that could change, as compiler should execute function

which_MSG()

each loop pass before comparing it to i, so if returned value

is changed, for can loop forever or one or more states would

be skipped as only highest value would be used to finish the loop.

But, this depend on frequency of state changes.

So better practice would be to assign value of message to variable

j==which_MSG() and then use this variable to control the loop.........

for(i=0; i<j; i++)

Still it is not clear why same sequence of pulses should be repeated

according to message value.........

Obviously this is a homework only without practical value, and we should have not interfered with our Guest learning process :-((

__________________
Per Aspera ad Astra
Reply
Guru

Join Date: Apr 2010
Location: About 4000 miles from the center of the earth (+/-100 mi)
Posts: 9910
Good Answers: 1141
#6

Re: Help with Program Code?

04/30/2010 6:56 AM

One obvious problem is the Goto jump in the for loop. Try to write the program without goto to get away from the spaghetti logic.

Reply
Reply to Forum Thread 7 comments
Copy to Clipboard

Users who posted comments:

ffej (1); Henrik14 (3); ky (1); Randall (1); Rixter (1)

Previous in Forum: Eng.Mujeeb.Dubai   Next in Forum: icu and ics
You might be interested in: Eddy Current Probes, Machine Vision Systems, Chuck Jaws

Advertisement