View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
What happen with this code? |
Posted: Fri Jan 22, 2010 4:05 pm |
|
|
Hi, there is something wrong with this code, the same code works OK in a pic18f4550 but it did not work correctly on a pic18f452 or a pic16f88, are not error of micro, because I have tried other programs and everything works ok, someone I could tell which is the reason for this?
Code: | #include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20MHZ)
#use fast_io(b)
#use fast_io(d)
#define PULSADOR PIN_A0
#define LEDR PIN_B0
int Entrada;
int Modo;
#define ON output_high
#define OFF output_low
void main(){
set_tris_A(0b00011111);
set_tris_B(0b00000000);
setup_adc_ports(NO_ANALOGS);
Modo = 0;
Entrada = 0;
ON(LEDR);
Delay_ms(1000);
OFF(LEDR);
while(TRUE){
if(!input(PULSADOR) && (Entrada == 0)){
Modo = 1;
Entrada ++;
ON(LEDR);
delay_ms(500);
OFF(LEDR);
}
if(!input(PULSADOR) && (Entrada == 1)){
Modo = 2;
Entrada ++;
ON(LEDR);
delay_ms(500);
OFF(LEDR);
delay_ms(500);
ON(LEDR);
delay_ms(500);
OFF(LEDR);
}
if(!input(PULSADOR) && (Entrada == 2)){
Modo = 3;
Entrada = 0;
ON(LEDR);
delay_ms(500);
OFF(LEDR);
delay_ms(500);
ON(LEDR);
delay_ms(500);
OFF(LEDR);
delay_ms(500);
ON(LEDR);
delay_ms(500);
OFF(LEDR);
}
}
}
|
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Fri Jan 22, 2010 4:41 pm |
|
|
OK, so what does "not working correctly" mean?
Also, you set B and D as fast_io() but then set the TRIS for A and B.
I would eliminate both of these sections and let the compiler take care of the ports for you. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Fri Jan 22, 2010 4:53 pm |
|
|
Quote: | OK, so what does "not working correctly" mean? |
I have already removed set B and D as fast_io() , but the problem remained the same |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Jan 22, 2010 5:08 pm |
|
|
You didn't answer the question. What do you observe? |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Sun Jan 24, 2010 6:56 pm |
|
|
The problem is that nothing happens, although I press PULSADOR, pic does not detect this entry !!! |
|
|
icesynth
Joined: 03 Sep 2007 Posts: 32 Location: Edmonton, Alberta
|
|
Posted: Sun Jan 24, 2010 7:23 pm |
|
|
With respect to:
Code: |
ON(LEDR);
Delay_ms(1000);
OFF(LEDR);
|
Does the LED change state at powerup?
If you meter the pins to the PIC, can you see the signal changing?
If you sub in PIN_B1 (and change the tris for it) for PULSADOR, can you see if there is a change on the codes operation? This will help rule out a problem with the setup_adc_ports
Is the correct chip selected in the programming software?
If you only put this in your main while(true) loop, what does it do? What does it do when using PIN_B0 for the input?
Code: |
output_bit (PIN_B0 ,input(PIN_A0))
|
The code should make the LED reflect the state of the input pin. _________________ Programming for the the real world.
--Chris Burchett
Sylver Technologies Inc. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Jan 26, 2010 7:49 am |
|
|
I see that the pic is continually resetting |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Tue Jan 26, 2010 8:05 am |
|
|
Quote: | I see that the pic is continually resetting |
Why do you say that?
Why are you NOT answering questions you are being asked?
Quote: |
Does the LED change state at powerup?
If you meter the pins to the PIC, can you see the signal changing?
If you sub in PIN_B1 (and change the tris for it) for PULSADOR, can you
see if there is a change on the codes operation? This will help rule out a
problem with the setup_adc_ports
Is the correct chip selected in the programming software?
If you only put this in your main while(true) loop, what does it do?
What does it do when using PIN_B0 for the input?
|
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
icesynth
Joined: 03 Sep 2007 Posts: 32 Location: Edmonton, Alberta
|
|
Posted: Tue Jan 26, 2010 9:28 am |
|
|
Quote: | I see that the pic is continually resetting |
And a reason for you saying WHY you think it is continually resetting would be helpful as well...
ie: "I see that the pic is continually resetting ..... Because I see this and this and......" _________________ Programming for the the real world.
--Chris Burchett
Sylver Technologies Inc. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Jan 26, 2010 9:45 am |
|
|
I am sorry..
At the beginning of my code, after of configure the ADC, the pic must to turning on the LEDR and after 500ms must turn off it, but I notice that the LED continuously flashes on and off so I say that the pic is continually resetting
Code: | void main(){
set_tris_A(0b00011111);
set_tris_B(0b00000000);
setup_adc_ports(NO_ANALOGS);
Modo = 0;
Entrada = 0;
ON(LEDR);
Delay_ms(1000);
OFF(LEDR); |
|
|
|
Guest
|
|
Posted: Tue Jan 26, 2010 10:07 am |
|
|
What do you have connected to the MCLR pin? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Tue Jan 26, 2010 10:29 am |
|
|
Code: |
ON(LEDR);
Delay_ms(1000);
OFF(LEDR);
while(TRUE){
if(!input(PULSADOR) && (Entrada == 0)){
Modo = 1;
Entrada ++;
ON(LEDR);
delay_ms(500);
OFF(LEDR);
}
|
Notice in the above code the LED will initially come on for 1 second and you expect it to then go off. However, if PULSADOR is low for the first input test you immediately drop into the routine and turn LEDR back on for another 500ms.
After the 500ms delay it exits the first routine and goes right into the second routine then into the third routine so the LED should be basically
be flashing at a 500ms rate with irregular pauses.
Try changing:
To
This will cause the PIC to flash the LED for one second and then loop
forever. If the LED comes on for just 1 second then stays off the PIC is
not resetting and the logic in your While loop is the problem. If the LED
continuously flashes it is resetting. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Jan 26, 2010 10:55 am |
|
|
Quote: | Pruebe a cambiar:
Código:
while (TRUE) (
Para
Código:
while (TRUE);
| Now the pic is not reset, it appears that the problem is in logic of my While loop |
|
|
|