Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Bizarre problem with Junebug LEDs

Status
Not open for further replies.

Gfaja

New Member
Here's the code I'm running
Code:
list p=18F1320
include <p18F1320.inc>
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

org 0x000			; RESET vector
; INIT
INIT 	movlw b'01111110'
		movwf TRISA
		movlw b'00000001'
		movwf PORTA
		movlw b'11000000'
		movwf TRISB
		movlw b'11111111'
		movwf PORTB			; Set pullups

; MAIN
MAIN	
		btfsc PORTB,RB2 ; Test button 2
		goto MAIN
		movlw b'10000000' ; Once it's been pressed switch the led and leave it
		movwf PORTA
		goto MAIN

END

it was working fine, but I changed it to use the other button (RB5). Then instead of lighting LED6 it would light LED 1 and 3, LED 6 would flicker a bit, then after about 20 seconds it would switch, 1 and 3 would go out then 6 would go on. It still responded to button presses as expected. Switching back to RB2 the problem remained.

Does anyone have any idea what's going on? Looking at the diagram I understand why 1 and 6 are on.. if RA6 is disconnected, 1 is on and 7 is off, I would expect 1, 3 AND 6 to go off, and clearly it's something to do with those three. What am I missing?

Thanks
 
Try this:
Code:
list p=18F1320
include <p18F1320.inc>
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

org 0x000			; RESET vector

INIT 	
	movlw b'01111110'
	movwf TRISA

	movlw b'00000001'
	movwf PORTA

	movlw b'00100101'    ;Proper port tris setup now (1 = input)
	movwf TRISB
	
	movlw    0x80        
	movwf    INTCON2     ; Set pullups
	movlw    0x7F
	movwf    ADCON1      ; Set to all digital 

MAIN	
    btfsc PORTB,RB2 ; Test button 2
    goto MAIN
    movlw b'10000000' ; Once it's been pressed switch the led and leave it
    movwf PORTA
    goto MAIN

END
Info in datasheet:

pullup-jpg.30924


Using the above note from datasheet you can see to enable pullups:
Code:
	movlw    0x80        
	movwf    INTCON2     ; Set pullups
OR
Code:
	bsf    INTCON2,RBPU     ; Set pullups
And since RB4:RB0 are auto set to ANALOG on startup it will always read as a 0 until you set it as digital:
In datasheet and junebug manual:
RB0 = SW1
RB2 = SW2
RB5 = SW3
Now to figure how to set ADCON1 you have to find this:

I edited the image so you can see how the registers line up so you can see which PortB pins have what name in ADCON1 register. So you can then:
Code:
	movlw    0x7F
	movwf    ADCON1      ; Set to all digital
or just set the ones you need RB0, RB2, RB5:
Code:
	movlw    b'00100101'
	movwf    ADCON1      ; Set RB0, RB2, RB5 digital for button input.
 

Attachments

  • pullup.jpg
    pullup.jpg
    27.7 KB · Views: 526
Last edited:
Ahh ****.. I even read up on adcon, but I guess I was too used to my arduino.
Why is it adcon1, and not just adcon?

And thirdly.. New code makes perfect sense, except why aren't we setting bit 7 in adcon1?

And finally.. I'm still not exactly sure why the junebug was acting like it was.. it still doesn't really make sense to me and the inconsistency is the weirdest. What does writing to an input pin do?

Thanks
 
Ahh ****.. I even read up on adcon, but I guess I was too used to my arduino.
Why is it adcon1, and not just adcon?

And thirdly.. New code makes perfect sense, except why aren't we setting bit 7 in adcon1?

And finally.. I'm still not exactly sure why the junebug was acting like it was.. it still doesn't really make sense to me and the inconsistency is the weirdest. What does writing to an input pin do?

Thanks

Why is it adcon1, and not just adcon?
From the datasheet section 17:
17.0 10-BIT ANALOG-TO-DIGITAL CONVERTER (A/D) MODULE

The ADCON0 register, shown in Register 17-1,
controls the operation of the A/D module. The
ADCON1 register, shown in Register 17-2, configures
the functions of the port pins. The ADCON2 register,
shown in Register 17-3, configures the A/D clock
source, programmed acquisition time and justification.
So basically it is the registers name.

ADCON has to do with the Analog to Digital Conversion(ADC). And selecting which pin to read.

ADCON1 has to do with setting the PINs to Digital or ANALOG.

ADCON2 has to do with the timing of the ADC.

Why not set bit 7 of ADCON1:
because this is its purpose: "Unimplemented: Read as ‘0’"

It has no use. Its not used.


Writing to a INPUT pin: i have no clue what would happen... i assume nothing :)

arduino is more of a shortcut thing i think. You miss out on a lot i suppose. Like the basic stamp. You never get full control. Like of every register. Of course its harder this way but way more satisfying and precise.
 
Last edited:
Yeah I'm using the arduino for simple robotics and stuff but using pics for smaller more integrated things as well as to make something w/out feeling like I cheated. Arduinos are TOO easy.
 
Hm now it started randomy switching to LED 5 immediately after starting up now, when it should be LED 6 until I hit the button

Code:
; *** Junebug 18F1320 LED sequencer demo ***
; Flashes LEDs1 thru 6 from left to right forever
; DIP Switch (SW6) must have TUTOR on (SW6-1,2,3) all other switches off
list p=18F1320
include <p18F1320.inc>
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

org 0x000			; RESET vector
; INIT
INIT	movlw b'01111110'
		movwf TRISA
		
		movlw b'00000001'
		movwf PORTA		

		movlw b'00100101'
		movwf TRISB

		bsf   INTCON2,RBPU     ; Set pullups

		movlw 0x7F
		movwf ADCON1      ; Set to all digital 


; MAIN
MAIN	
		btfss PORTB,RB2
		goto MAIN
		movlw b'10000000'
		movwf PORTA
		goto MAIN

END
 
Have to bump this.

Haven't touched my junebug in a while, so it's running the code in the above post.
It won't connect anymore, resetting gets the right LED lit but plugging it in make it quickly switch to what should be lit only after pressing the button. Program also completely ignores button presses. Maybe I screwed up soldering in.

Video is posted and pictures of my soldering job. Any ideas what I could have done wrong?
**broken link removed**
1
2
3
**broken link removed**
 
Last edited:
Nope. Yellow is dimly lit. Now I consistently get the USB not recognized message when plugging it in.
 
Hmm remove the 18F2550. Is there a color dot painted on the bottom?

A programmed Junebug only needs power & a working crystal (plus the pushbutton) to get the LED flashing.
 
sigh.... the chip was barely in the socket..
one of the indents is white?

put it back in and it flashed red and is updating its software right now. yellow is off.
Now have it running, but still have it so LED 5 is light a split second after LED 6 as if I had hit the button. That or it switches randomly/not when I press the switch. So basically the videos are still accurate other than the programming problem. Any ideas?
 
Yep the dot means I programmed it. You have to push down pretty firmly on that machine socket.

Since the Junebug sounds like it's ok, IMO it would be an excellent time to give the debug mode a spin.

Add the two following lines to the beginning of your code (else the debug mode is really slow)

movlw 0x72
movwf OSCCON

This will speed up the PIC to 8MHz

Then simply pick the debug mode instead of the program mode. You can single step , run , animate, set breakpoints and watch variables.

TIP open a "watch" window and watch PORTB & TRISB
You can toggle bits directly in this mode and see the results directly on the LEDs.
 
Ah ok. I will do that. I had a problem with only being allowed one breakpoint, and it was super slow, I assumed it ran by default on full speed.
 
The default internal osc speed is 31.25 kHz, good for flashing LEDs but really slow for the debugger.

I started out with a Parallax Clearview 5x ICE for the 16C54 way back before debuggers were available.
 
Hm, I get popups for stupid things,
Stepping Target
PICkit 2 Ready

Stepping Target
PICkit 2 Ready

Stepping Target
PK2Error0029: Failed PICkit 2 operation: Step

Running Target
PK2Error0029: Failed PICkit 2 operation: Halt
PICkit 2 Ready

Target Halted

It fails the step when it's on the movwf TRISB and the bsf is the next command (consistently)
 
Hmm make sure DEBUG is enabled (from the MPLAB pulldown) and change TRISB bits 6&7 to 1.
Code:
movlw b'11100101'    ;Proper port tris setup now (1 = input)
movwf TRISB
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top