PDA

View Full Version : Ports don't stay on when sequencing them.



cnc002
- 28th July 2017, 22:52
Below is the simple program I am working with and it should work. However, the outputs on port B will not stay on. Portb.0 will come on then go off after the 2 second pause, them portb.1 will come on for 2 seconds and go off then portb.2 comes on and stays on. I am using a PIC16F882, simply because I have several laying around.

It has been several years since I worked with PICs and with PBP so I am certain there is something stupidly simple to fix this.

Thank you to anyone who will assist me on this,

Here is the code:

adcon1 = 111
define osc = 8
porta = %00000000
portb = %00000000
portc = %00000000
trisa = %11111111
trisb = %00000000
trisc = %00001111

'Alias define
power1 var portb.0
power2 var portb.1
power3 var portb.2

main:
if portc.0 = 1 then
gosub Startup
endif

if portc.1 = 1 then
gosub shutdown
endif

goto main

startup:
pause 500
high power1
pause 2000
high power2
pause 2000
high power3
'pause 2000
'portb.3 = 1
'pause 2000
'portb.4 = 1
'pause 2000
'portb.5 = 1
'pause 500
return

shutdown:
pause 500
'portb.5 = 0
'pause 2000
'portb.4 = 0
'pause 2000
'portb.3 =0
'pause 2000
low power3
pause 2000
low power2
pause 2000
low power1
pause 500
return

end

richard
- 29th July 2017, 00:06
classic RMW read modify write
watch this
http://www.microchip.com/webinars.mi...cName=en556253
click on
View streaming media version:

cnc002
- 29th July 2017, 15:20
classic RMW read modify write
watch this
http://www.microchip.com/webinars.mi...cName=en556253
click on
View streaming media version:

Can you give me the complete url path? The one above appears to be missing part of the address and of course I get a 404 error when I click on it.

Thanks

richard
- 29th July 2017, 15:37
sorry the link did not paste in properly
http://www.microchip.com/webinars.microchip.com/WebinarDetails.aspx?dDocName=en556253

cnc002
- 29th July 2017, 15:43
sorry the link did not paste in properly
http://www.microchip.com/webinars.microchip.com/WebinarDetails.aspx?dDocName=en556253

No problem, your reminding me about the RMW made me remember the "glitch" in the 16F series PIC. I simply changed my code to writing to the complete port at once rather than individual bits. I.E. portb = %00000001 to turn on portb.0 and portb = %00000011 to keep portb.0 on while turning on portb.1. Works like a charm, many thanks for the assistance.

Randy Abernathy

mpgmike
- 5th August 2017, 16:08
Bit masking is another method, using the AND and OR (& = AND, | = OR). AND is a way to turn off selected pins without affecting others: LATB = PORTB & 111110 would turn off PORTB.0. LATB = PORTB | 000001 would turn on PORTB.0. LATB = PORTB & 110000 would turn off the low nibble while LATB = PORTB | 001111 would turn on the lower nibble. Doing it this way leaves the pins you don't want to mess with alone. They remain totally unaffected. This would apply to pins tied to PWM, INTERRUPTS, or anything else.