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.

So...I have my junebug....and Im totally lost

Status
Not open for further replies.

strokedmaro

New Member
I got my junebug today (really fast shipping by the way :) ) and I downloaded MPLAB IDE, PICkit 2, swordfish and a ton of other stuff...When first plugged in it flashed the LED's from left to right forever but I somehow "programmed" it to stop doing that while tripping down the cavernous menus in MPLAB. Ive looked everywhere for tutorials like: Get a junebug, download this and that...now open this and put this here...do this to program the PIC...now you should see this happening.

I thought this type of info would be included with a tutor. I need to get it to do something from A to Z so I at least get the order and process down. Then I can worry about making changes and testing. Ive looked at tutorials and read a ton of info but never having programmed anything Im totally lost. If there are any sites specifically for the junebug (I've searched but nothing comes up). Maybe a good start would be to help me get the original LED flashing code back on the PIC.

I know all you old hats get tired of the new guys but I need some help. THANKS!:D
 

blueroomelectronics

Well-Known Member
Well the LED flashing demo is in the back of the manual. There is a large 88 page MPLAB manual, and for quick Swordfish BASIC SE might be a good place to start (free SE version works great on the Junebug) There are lots of examples here too, just search for Junebug.
Also the PICkit2 demo board manuals are a good start but you'll have to modify the programs for the 18F part.
PS the Inchworm quick project poster might help, it applies to the Junebug too.

I'd be happy to help.
 

strokedmaro

New Member
Maybe I'm a little out of my league....I have the manuals and I know the demo is in the manual but I have no Idea with what program in what order it takes to get it back on the PIC. Before buying the junebug I read the student guide from Parallax called "whats a micro controller" and it was very informative. Thats the first time I started understand what was going on and it was broken down into easy steps with good explanations.

I guess I will keep reading but what would you suggest I start with? Tell me what to read first and I will start there.
 
Programming 001

If you are totally new to programming, you have jumped into the deep end of the ocean, complete with sharks. :eek:

So, if that is so, climb out of the ocean and start in the beginners pool. As Bill says, begin with Swordfish Basic and work on blinking LEDs. Everyone in PICland, etc kinda starts there. Basic kinda takes care of lots of things for you. Pic assembler is kinda strange because of "bank switching, etc".

1) Write / Copy a simple Swordfish Basic program that turns on an LED and waits around for a second or so. Turn it off. Wait for a second or so, go back to the top. The specs for the 18F1320 can be helpful (once you have stared at it for a long time and understand what Bill hooked up to what.

2) The Pickit 2 software (obtained from MicroChip's web site) will allow you to take the .hex file and write it into the 18F1320 onboard the Junebug using the other part of the Junebug (the programmer) :D

3) The check box on the programmer on the right that says Vdd Pickit On will power the pic and start it running.

4) Led should blink, but mileage may vary depending on you program.

5) Begin pulling hair out when it all isn't exactly like it is supposed to be.


Yes, I have been programming a LONG time. But have only been doing PICs about 6 months.

;)
 

blueroomelectronics

Well-Known Member
Then try Swordfish BASIC SE, it has everything you need to get your feet wet. I've got a small LED module written for the Junebugs tutor.
The very simple blink program
Code:
{
*****************************************************************************
*  Name    : BLINK_1.BAS                                                   *
*  Author  : William Richardson                                             *
*  Notice  : Copyright (c) 2008 blueroomelectronics                         *
*          : All Rights Reserved                                            *
*  Date    : 1/7/2008                                                       *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F1320
Clock = 4               // 4MHz clock
Include "junebug.bas"
Dim Count As Byte
While True
    For Count = 0 To 6
        LED(Count)
    DelayMS(300)        // delay in 1000s of a second
    Next
Wend        // repeat forever
End

And the Junebug module
Code:
{
*****************************************************************************
*  Name    : JUNEBUG.BAS                                                    *
*  Author  : William Richardson                                             *
*  Notice  : Copyright (c) 2008 blueroomelectronics                         *
*          : All Rights Reserved                                            *
*  Date    : 2/1/2008                                                       *
*  Version : 0.92beta                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

Module Junebug
Device  = 18F1320
Public Sub LED(x As Byte)
  Select x
    Case 1      // LED 1
    TRISA.7 = 1
    High(PORTA.0)
    Low (PORTA.6) 
    Case 2      // LED 2
    TRISA.7 = 1
    Low (PORTA.0)
    High(PORTA.6)      
    Case 3      // LED 3
    TRISA.0 = 1
    High(PORTA.6)
    Low (PORTA.7)      
    Case 4      // LED 4
    TRISA.0 = 1
    Low (PORTA.6)
    High(PORTA.7)      
    Case 5      // LED 5
    TRISA.6 = 1
    Low (PORTA.0)
    High(PORTA.7)      
    Case 6      // LED 6
    TRISA.6 = 1
    High(PORTA.0)
    Low (PORTA.7)      
  Else          // All LEDs Off 
    TRISA.0 = 1
    TRISA.6 = 1
    TRISA.7 = 1
  End Select
End Sub
Config OSC = INTIO2, WDT = OFF, LVP = OFF
ADCON1  = %11110101

OSCCON = $62            // 4 MHz clock
 
Upon some review

It appears that with a bit of work, you can get Swordfish integrated into MPLAB. This can make the writting of the program into the pic a bit simpler. (One stop shopping).

Futz, everything about PIC assembler is weird. But then all my assembler (and there's tons of it is mostly on BIG iron). RISC stuff is better done in a "higher" language.

:D
 

blueroomelectronics

Well-Known Member
Since Swordfish does not support the debugger and it has an excellent IDE...

Here's what you need.
The PK2CMD.EXE from Microchip
(Swordfish programmer executeable)
then enter
/PPIC$target-device$ /F$long-hex-filename$ /M /R /H2
in the parameter window
 
Last edited:

futz

Active Member
August Treubig said:
Futz, everything about PIC assembler is weird.
I have to somewhat agree. Their asm is a bit funky. But it's not SO terribly different from other assembly languages that I wasn't able to pick it up pretty quickly (coming from mostly Motorola assembly language background).

RISC stuff is better done in a "higher" language. :D
Not for me. I love assembler.
 

3v0

Coop Build Coordinator
Forum Supporter
This a C18 Junebug program written by one of the students I tutor. I am not saying she started from scratch or wrote it without guidance. She does however understand what each line does. Not bad for 12.

Create a procject with the the MCC18 compiler (free from microchip). This code will be your main.c, add the linker and header file then build, program and run. Also works with the debugger :)
Code:
// *** C18 Junebug 18F1320 LED 
// 
// THIS PROGRAM MAKE TWO LEDs WALK ACROSS THE JUNEBUG AND BACK
// NOTE: Project requires a linker scrpt.
// Add either 18f1320i.lkr (for debug) or 18f1320.lkr (no debug) to your project.
//
// BY RHIANNON 

#pragma	config OSC = INTIO2, WDT = OFF, LVP = OFF, DEBUG = ON
#include <p18f1320.h>

#define byte unsigned short
#define LED_ON_FOR 50    


const byte valA[] = {0b00000001,0b01000000,0b01000000,0b10000000,0b10000000,0b00000001}; 
const byte dirA[] = {0b10111110,0b10111110,0b00111111,0b00111111,0b01111110,0b01111110};

void lightTwo(int first)
{
  int onCount,blinky;
  
  for(onCount=0; onCount<LED_ON_FOR; onCount++)
  {
    for(blinky=first; blinky<(first+2); blinky++)
    {
      LATA=valA[blinky];
      TRISA=dirA[blinky];
    }
  } 
}  
               
void main(void)
{
  int idx;

  for(idx=0;idx<5;idx++)   
  {
    lightTwo(idx);  
  }  
  
  for(idx=3;idx>0;idx--)
  {
    lightTwo(idx);
  }  
}

EDIT: To understand the program you need to study the charlie plexed leds on the junebug schematic.
 

mvs sarma

Well-Known Member
strokedmaro said:
Maybe I'm a little out of my league....I have the manuals and I know the demo is in the manual but I have no Idea with what program in what order it takes to get it back on the PIC. Before buying the junebug I read the student guide from Parallax called "whats a micro controller" and it was very informative. Thats the first time I started understand what was going on and it was broken down into easy steps with good explanations.

I guess I will keep reading but what would you suggest I start with? Tell me what to read first and I will start there.

You are perfect in your starting by owning JUNEBUG. As you know, there is no shortcut to success. It is by hard work and practice and continued writing, you learn. hope to see your programs posted at the earliest.All the best and start earnestly and don't stop halfway round.
 

blueroomelectronics

Well-Known Member
3v0 said:
This a C18 Junebug program written by one of the students I tutor. I am not saying she started from scratch or wrote it without guidance. She does however understand what each line does. Not bad for 12.

Tell her well done, I'm several times that age and I'm just starting out with C18. I may take a peek at C30 as I can't figure out MPASM30...
 

3v0

Coop Build Coordinator
Forum Supporter
blueroomelectronics said:
Tell her well done, I'm several times that age and I'm just starting out with C18. I may take a peek at C30 as I can't figure out MPASM30...

WIll do that. She is great to work with. I showed her how to figure out the constants to light the first two LEDs and she figured out the other 4. It is amazing to me how much kids can absorb it you start with concepts they understand.

The teaching thing limits the time I have to mess with other things I want to do. Playing with the 24 and 30 fall into that basket. that and doing SMD boards Maybe this summer.

C30 may be easier to use then MCC18.
 

strokedmaro

New Member
Yay!

blueroomelectronics said:
Then try Swordfish BASIC SE, it has everything you need to get your feet wet. I've got a small LED module written for the Junebugs tutor.
The very simple blink program
Code:
{
*****************************************************************************
*  Name    : BLINK_1.BAS                                                   *
*  Author  : William Richardson                                             *
*  Notice  : Copyright (c) 2008 blueroomelectronics                         *
*          : All Rights Reserved                                            *
*  Date    : 1/7/2008                                                       *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F1320
Clock = 4               // 4MHz clock
Include "junebug.bas"
Dim Count As Byte
While True
    For Count = 0 To 6
        LED(Count)
    DelayMS(300)        // delay in 1000s of a second
    Next
Wend        // repeat forever
End

And the Junebug module
Code:
{
*****************************************************************************
*  Name    : JUNEBUG.BAS                                                    *
*  Author  : William Richardson                                             *
*  Notice  : Copyright (c) 2008 blueroomelectronics                         *
*          : All Rights Reserved                                            *
*  Date    : 2/1/2008                                                       *
*  Version : 0.92beta                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

Module Junebug
Device  = 18F1320
Public Sub LED(x As Byte)
  Select x
    Case 1      // LED 1
    TRISA.7 = 1
    High(PORTA.0)
    Low (PORTA.6) 
    Case 2      // LED 2
    TRISA.7 = 1
    Low (PORTA.0)
    High(PORTA.6)      
    Case 3      // LED 3
    TRISA.0 = 1
    High(PORTA.6)
    Low (PORTA.7)      
    Case 4      // LED 4
    TRISA.0 = 1
    Low (PORTA.6)
    High(PORTA.7)      
    Case 5      // LED 5
    TRISA.6 = 1
    Low (PORTA.0)
    High(PORTA.7)      
    Case 6      // LED 6
    TRISA.6 = 1
    High(PORTA.0)
    Low (PORTA.7)      
  Else          // All LEDs Off 
    TRISA.0 = 1
    TRISA.6 = 1
    TRISA.7 = 1
  End Select
End Sub
Config OSC = INTIO2, WDT = OFF, LVP = OFF
ADCON1  = %11110101

OSCCON = $62            // 4 MHz clock

I don't know how it works yet, but I was able to get it blinking the way it did before by copying and pasting what you've listed here! I guess you could say that this is my first success at programming a PIC! At least I know the order of things now. Thanks for re-sparking my interest!!!

EDIT: a little copying and pasting in swordfish and Ive been able to get the LED's to flash in any order! Now I'm having a little fun! I still don't understand the software behind it but I at least am able to get something working which helps tremendously!

blueroomelectronics: thank you...now to find/figure out how to incorporate the buttons! Than should help me understand a little more!
 
Last edited:
Avoiding the Sharks

Well,

You have avoided the sharks and the deep dark holes on the first go round. Now you have a success and have a project saved that works. So you were able to:

1) get something to compile without errors. (copying it or not, sometimes that is a milestone).

2) Successfully program the PIC (Bill makes that fairly easy with the onboard 18F1320). But sometimes simple things can be challenging..

3) Ran the sample and watched the pretty blinking lights. (Nothing like being a success).

You have the idea behind the process down. Things like LCDs, and others can also make for fun.

Drive on, and ask as many questions as required.

;)
 

blueroomelectronics

Well-Known Member
Switches usually require debouncing, but here's something to tide you over. It uses the Debug mode of MPLAB to work. You need to open a Watch window and select PORTB to see the 1's (switch open) change to 0 (closed/pressed)
Code:
;*** blackbox.asm demonstrates use of the debugger
;*** Junebug DIP switches 1,2,3 on all others off
;*** uses pushbuttons 1,2 and 3 (RB0, RB2 and RB5)
;*** a PORTB Watch window will update in Animate mode
    list p=18F1320
    include <p18F1320.inc>
    CONFIG  OSC=INTIO2,WDT=OFF,LVP=OFF
    org     0x00         ; reset vector
    movlw   0x72        ; 
    movwf   OSCCON        ; 8MHz internal osc
    setf    ADCON1        ; set all I/O to digital
    bcf     INTCON2,RBPU    ; enable PORTB input pullups
    bra $             ; loop forever
    END
 

Pommie

Well-Known Member
Most Helpful Member
Bill,

I took the liberty of adding a key read routine to your Junebug.bas file. It returns 1 to 3 if a key is pressed with debounce. I also changed your Blinky.bas to toggle between the LEDs above the buttons. The key read routine also does auto repeat and 2 key rollover. Try holding a button down and then press another at the same time.

Mike.

Code:
{
*****************************************************************************
*  Name    : BLINK_1.BAS                                                   *
*  Author  : William Richardson                                             *
*  Notice  : Copyright (c) 2008 blueroomelectronics                         *
*          : All Rights Reserved                                            *
*  Date    : 1/7/2008                                                       *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Device = 18F1320
Clock = 4               // 4MHz clock
Include "junebug.bas"
Dim Keys As Byte,Lit As Byte
While True
    Keys=ReadKeys               //get the key value
    If Keys<>0 Then             //any key pressed?
        Lit=Lit And 1 Xor 1     //keep bottom bit and toggle it
        Keys=Keys-1<<1          //keys = 0,2 or 4
        Lit=Lit+Keys            //combine them
    EndIf    
    LED(Lit+1)                  //turn on the LED
    DelayMS(10)                 // delay in 1000s of a second
Wend                            // repeat forever
End

Code:
{
*****************************************************************************
*  Name    : JUNEBUG.BAS                                                    *
*  Author  : William Richardson                                             *
*  Notice  : Copyright (c) 2008 blueroomelectronics                         *
*          : All Rights Reserved                                            *
*  Date    : 2/1/2008                                                       *
*  Version : 0.92beta                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

Module Junebug
Device  = 18F1320
Public Sub LED(x As Byte)
  Select x
    Case 1      // LED 1
    TRISA.7 = 1
    High(PORTA.0)
    Low (PORTA.6) 
    Case 2      // LED 2
    TRISA.7 = 1
    Low (PORTA.0)
    High(PORTA.6)      
    Case 3      // LED 3
    TRISA.0 = 1
    High(PORTA.6)
    Low (PORTA.7)      
    Case 4      // LED 4
    TRISA.0 = 1
    Low (PORTA.6)
    High(PORTA.7)      
    Case 5      // LED 5
    TRISA.6 = 1
    Low (PORTA.0)
    High(PORTA.7)      
    Case 6      // LED 6
    TRISA.6 = 1
    High(PORTA.0)
    Low (PORTA.7)      
  Else          // All LEDs Off 
    TRISA.0 = 1
    TRISA.6 = 1
    TRISA.7 = 1
  End Select
End Sub

Function GetBit(Bin As Byte) As Byte
Dim I As Byte
    GetBit=0
    For I=1 To 8
        If ((Bin And 1) = 1) Then
            GetBit=I 
        EndIf
        Bin=Bin>>1
    Next   
End Function

Dim OldKeys As Byte                     
Dim OldEdges As Byte
Dim KeyCount As Byte
Const KeyDelay=30                       //initial delay before repeating in 10mS
Const KeyRepeat = 12                    //repeat delay in 10mS

Public Function  ReadKeys() As Byte
Dim Keys As Byte
Dim Edges As Byte
    INTCON2.7=0                         //WPUs on port B
    ReadKeys=0                          //return Zeor unless key pressed
    Keys=0
    If(PORTB.0=0) Then                  //move from port B to Keys
        Keys=Keys Or 1 
    EndIf
    If(PORTB.2=0) Then 
        Keys=Keys Or 2 
    EndIf
    If(PORTB.5=0) Then 
        Keys=Keys Or 4 
    EndIf
    Edges=(Keys Xor OldKeys) And Keys   //edges contains new key presses
    If Edges <> 0 Then                  //if new keypress
        OldEdges=Edges                  //keep copy of it for repeating
        KeyCount=KeyDelay               //set initial delay
        ReadKeys=GetBit(Edges)          //return key number
    EndIf
    If Keys<>0 Then                     //repeating?
        KeyCount=KeyCount-1             //Yes, so count down
        If(KeyCount=0) Then             //time to repeat yet?
            ReadKeys=GetBit(OldEdges)   //yes, return key number
            KeyCount=KeyRepeat          //set repeat count
        EndIf
    EndIf
    OldKeys=Keys                        //keep copy for next time
End Function

Config OSC = INTIO2, WDT = OFF, LVP = OFF
ADCON1  = %11110101
OSCCON = $62            // 4 MHz clock
 

blueroomelectronics

Well-Known Member
Nice coding as always Pommie.

I've been writing a reaction timer game for the Junebug, I'll post it here when done.
Press reset, wait random delay, LED lights up, you hit the button and the time is output on the serial port (UART tool handy for this)

May I include your code and credit to you in the JUNEBUG.BAS module.

PS what else should the module contain? The UART perhaps...
 
Last edited:

AtomSoft

Well-Known Member
Im not a advanced well nor intermediate programmer but i used to program in VB6 when it was highly popular. This is like a god send syntax is like almost the same (haven't been through it all). I made that module(junebug.bas) and put it my user library folder for future use. (wife ordered my Junebug off ebay (no offense - blame her) um.. Im not understanding the bas though...

Case 1 // LED 1
TRISA.7 = 1 // Can you comment these lines plz
High(PORTA.0) // Can you comment these lines plz
Low (PORTA.6) // Can you comment these lines plz

Wouldnt this only be usefull if LEDs are full on PORTA? ( i guess just usefull for junebug) What If I wanted to set RA0, RA3 and RA4 as outputs, and RA1 and RA2 as inputs? This would be my intent if programming a PIC and not using it on the junebug hence like a production pic. ready for the world outside the bug.

I would have to rewrite the bas? Basically how would i send a ON/OFF (high/low) to a pin/port without the bas ?

Would this work then ?
Code:
Device = 18F1320
Clock = 4

// alias to port pin...
Dim LEDA As PORTA.0
Dim LEDB As PORTA.1

// main program...
Low(LEDA)
Low(LEDB)

Repeat
   Toggle (LEDA)
   DelayMS (500)
   Low(LEDA)
   
   Toggle (LEDB)
   DelayMS (500)
   Low(LEDB)   
Until false

Was bored how about this then?

Code:
Device = 18F1320
Clock = 4

// alias to port pin...
Dim LEDA As PORTA.0
Dim LEDB As PORTA.1

const intHigh = 2    // interrupt on INTCON bit:0 ? I think ? or any intcon with a HIGH 

interrupt OnTimer(intHigh)    //Button is pressed on INTCON/PORTB:0 i think? or any intcon with a HIGH 
   Low(LEDA)
  
   Toggle (LEDB)
   DelayMS (1000)
   Low(LEDB)
end interrupt



// main program...
Low(LEDA)
Low(LEDB)

Repeat
   Toggle (LEDA)
   DelayMS (500)
   Low(LEDA)
Until false
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
The high and low commands set the pins to output. To set the pins as you described you would do, TRISA=%00011001 - the bits that are 1 become inputs and the rest outputs.

If your wondering how the LEDs work with the above code, you should look at the circuit diagram of the Junebug on Bill's site and google Charlieplexing.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Top