View previous topic :: View next topic |
Author |
Message |
rami_shama
Joined: 29 Apr 2010 Posts: 11
|
My program didn't work |
Posted: Thu Apr 29, 2010 12:58 am |
|
|
Hello,
This is my program and it is not working. The main idea for my program is that:
When any input of A0,A1,A2 changes, the PIC 16F877A must make 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
Code: |
#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??? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Apr 29, 2010 2:05 am |
|
|
OK, several issues:-
1. You don't show main.h which I presume has all your fuse settings in it.
2. You don't say what it does do.
3. Try a simple led on off routine to see if it works.
4. You use to program in assembler didn't you. There is no need for those goto's in your code.
Code: |
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);
|
I prefer
Code: |
while (true)
{
while (!check_change())
{
}
sen1=input(PIN_A0);
sen2=input(PIN_A1);
sen3=input(PIN_A2);
...
}
|
5. Because you are using A0, A1 and A2 I would code it differently:0
Code: |
sen = input_a() & 0x07; // Gets A0, A1 and A2
// in check_change
return(sen != (input_a() & 0x7F)); // returns true if changed or false
|
6.Do you realy need to re-check the input in your for loop or should you be working on the last read value ?
7. Again because you are using A0, A1 and A2
Code: |
int which_MSG()
{
return(input_a & 0x07);// will return 0, 1, 2, 3, 4, 5, 6 or 7
}
|
That is about it for now, I expect your pic is not actually running because I would expect the code to actually work, unless it is a hardware issue, No pullups on the pins A0, A1 or A2 ? |
|
|
rami_shama
Joined: 29 Apr 2010 Posts: 11
|
|
Posted: Thu Apr 29, 2010 3:24 pm |
|
|
Hello,
I will try to use your suggestions, and I will tell you about the results.
about main.h
Code: | #include <16F877A.h>
#device adc=8
#FUSES WDT //Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
|
|
|
|
rami_shama
Joined: 29 Apr 2010 Posts: 11
|
|
Posted: Sat May 01, 2010 3:37 am |
|
|
Hello,
I'm edit my program and now it is :
Code: | #include "main.h"
int sen,i;
void menu();
void cursor();
void end();
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
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);
sen= input_a() & 0x07;
while (true)
{
while ((sen != (input_a() & 0x7F)) && input(PIN_B3)==0 )
{
sen= input_a() & 0x07;
end();
menu();
menu();
menu();
cursor();
cursor();
cursor();
cursor();
menu();
for(i=0; i<(input_a() & 0x07); i++)
cursor();
menu();
menu();
cursor();
menu();
menu();
cursor();
menu();
menu();
end();
}
}
}
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);
}
|
and here the main.h
Code: | #include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
|
Note: The Compiler is : PIC C Compiler V4.102
Last edited by rami_shama on Sat May 01, 2010 2:45 pm; edited 2 times in total |
|
|
rami_shama
Joined: 29 Apr 2010 Posts: 11
|
|
Posted: Sat May 01, 2010 3:49 am |
|
|
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Sat May 01, 2010 6:17 am |
|
|
Compare:
Quote: | for(i=0; i<(input_a & 0x07) |
What's missing? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
rami_shama
Joined: 29 Apr 2010 Posts: 11
|
|
Posted: Sat May 01, 2010 2:44 pm |
|
|
dyeatman wrote: | Compare:
Quote: | for(i=0; i<(input_a & 0x07) |
What's missing? |
ohhh!!!
It is bad!!
Thank you very much
I will try to run it and I will tell you if it is ok at PIC. |
|
|
rami_shama
Joined: 29 Apr 2010 Posts: 11
|
|
Posted: Sun May 02, 2010 2:01 pm |
|
|
it is still didn't work! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun May 02, 2010 4:26 pm |
|
|
Compare:
Code: | sen= input_a() & 0x07; |
and Code: | while ((sen != (input_a() & 0x7F)) | Why the difference?
And this most likely is the cause of the test never being TRUE.
A (small) error in the CCS wizard generates an invalid configuration: Code: | setup_spi(SPI_SS_DISABLED); | should be: |
|
|
rami_shama
Joined: 29 Apr 2010 Posts: 11
|
|
Posted: Tue May 04, 2010 7:45 am |
|
|
Look at this Code, it is work
Code: | #include "F:\cc\main.h"
int check_change();
void menu();
void end();
void cursor();
int which_MSG();
int sen1,sen2,sen3,i;
void standby();
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_tris_a(0x11);
set_tris_b(0xE8);
output_high(PIN_B5);
if(input(PIN_A0))
sen1=1;
else sen1=0;
if(input(PIN_A1))
sen2=1;
else sen2=0;
if(input(PIN_A2))
sen3=1;
else sen3=0;
while (true)
{
standby();
if (check_change()==1)
{
end();
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();
delay_ms(1500);
if(input(PIN_A0)) sen1=1;
else sen1=0;
if(input(PIN_A1)) sen2=1;
else sen2=0;
if(input(PIN_A2)) sen3=1;
else sen3=0;
end();
}
}
}
int check_change()
{
if(sen1!=input(PIN_A0) || sen2!=input(PIN_A1) || sen3!=input(PIN_A2))
return 1;
else
return 0;
}
void menu()
{
output_high(PIN_B0);
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
void end()
{
output_high(PIN_B1);
delay_ms(500);
output_low(PIN_B1);
delay_ms(500);
}
void cursor()
{
output_high(PIN_B2);
delay_ms(500);
output_low(PIN_B2);
delay_ms(500);
}
int which_MSG()
{
if(sen1==0 && sen2==0 && sen3==0)
return 0;
else if(sen1==0 && sen2==0 && sen3==1)
return 1;
else if(sen1==0 && sen2==1 && sen3==0)
return 2;
else if(sen1==0 && sen2==1 && sen3==1)
return 3;
else if(sen1==1 && sen2==0 && sen3==0)
return 4;
else if(sen1==1 && sen2==0 && sen3==1)
return 5;
else if(sen1==1 && sen2==1 && sen3==0)
return 6;
else if(sen1==1 && sen2==1 && sen3==1)
return 7;
}
void standby()
{
output_low(PIN_B5);
delay_ms(100);
output_high(PIN_B5);
delay_ms(100);
end();
}
|
|
|
|
|