PDA

View Full Version : Help with code



financecatalyst
- 11th April 2010, 08:46
Hello, the code is :


'// PIC 16F877A
ADCON1=7
CMCON=7
OPTION_REG=%00000111
TRISA=%111111 : TRISE=%111
TRISB=0 : PORTB=0
TRISC=%10001111 : PORTC=0
TRISD=%00000011 : PORTD=0
CCP1CON=0
@ ERRORLEVEL -306
B VAR BYTE
C VARY BYTE
D VAR BYTE
B=PortB : C[0]=PortC.5
for q=2 to 7
D[q]=PortD[q]
next q
IF B=0 and C=0 and D=0 then
toggle BUZ: pause 20
endif
As you can see I am trying to load ports into the variables. All ports above are outputs.
The problem:
When any of portB is made high in the main program then buzzer is off, as it should be, same is true for portc.5, but PortD is not being read properly. Even if lets say I make PortD.2 to D.7 high, one by one in the code for lets say 2 sec each (2 sec high/ 2 sec low) the buzzer is toggling non-stop. Though it should only toggle during 2 sec Low period.
Can someone help please? Thanks

Acetronics2
- 11th April 2010, 09:51
Hi Finance

1) C VARY Byte

2) Your program loops ... only because there's no END in the END ... and program counter continues to count, overflows, then program restarts @ address 0000 ...

That's why Buzzer toggles ...


Alain

financecatalyst
- 11th April 2010, 10:06
Thanks for the reply. Sorry it was a typing mistake @ vary
This is not my full code. My actual code runs fine with reading port c.5 & Port B.

My problem is correctly reading Port D.2 to D.7 into variable D. and if any of the pin is high, the buzzer should NOT toggle.
But for some reason Variable D=0 is true even if Lets say PortD.2 or D.3 to D.7 is high i.e.=1

I picked important lines from my code to explain the Problem.
Is there any other way which can load PortD.2 to D.7 into variable D?

Acetronics2
- 11th April 2010, 10:24
Ok

so ...

reading PORTD ...




B=PortB : C=PortC.5

D = PORTD & %11111100 ' read PORTD and mask port D.0 and D.1

IF B=0 and C=0 and D=0 then
toggle BUZ: pause 20
endif



should fit ... no indexing necessary !

( BTW ... not sure it was working so fine ... lol )

Alain

financecatalyst
- 11th April 2010, 10:31
Thanks a lot. Problem solved.

financecatalyst
- 11th April 2010, 13:23
This time the problem is with 16F676. While compiling I get the following errors

ERROR: Unable to fit variable RM2_Save
ERROR: Unable to fit variable RR1_Save
ERROR: Unable to fit variable RR2_Save
ERROR: Unable to fit variable RS1_Save
ERROR: Unable to fit variable RS2_Save

I am using DT-Interrupts in this code. Can some1 help me to get rid of these problems and what can I comment out in the ReEnterPBP or the other DT_INTS-14. Thanks

financecatalyst
- 11th April 2010, 13:46
The only thing I am doing in the ISR is the following:

ToggleLEDF:
If F1=1 then Toggle PortA.0
If F2=1 then Toggle PortA.1
If F3=1 then Toggle PortA.2
@ INT_RETURN

'---[TMR2 - interrupt handler]--------------------------------------------------
ToggleLEDS:
If S1=1 then Toggle PortA.0
If S2=1 then Toggle PortA.1
If S3=1 then Toggle PortA.2
pause 100
@ INT_RETURN

Acetronics2
- 11th April 2010, 13:58
Sure 64 Bytes of SRAM is a bit weak to handle DT-Interrupts ....

Try to spare (re-use ) 5 of your byte variables ... may be you can re-use B,C,D and two other temporary variables elsewhere in your Program ...

@ first C can be declared as a BIT ... probably others too ...


Or change for a Wider RAM Chip

Or Ask Darrel for a "Fast Interrupt" Option saving only the " live " registers and letting you choose which variables really need to be saved ... such C " #pragma disablecontexsaving " directive ( I'd love such an option !!! )

Alain