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.

Junebug help??

Status
Not open for further replies.
It seemed to work before

need to check voltages etc.
Havn't got time right now as getting ready for a big scout dinner. Just got done cooking 45 pounds of potatoes au grautin. LOTS of cheese
will check back in more.
maybe blueroom will respond as to dip switch settings.
 
All dip switches off Pommie has a junebug He told you that All dip switches OFF
SW6-1* Tutor (PK2 controls 18F1320 MCLR), free running mode & SW5 RESET
SW6-2 * Tutor (PK2 controls 18F1320 PGD)
SW6-3 * Tutor (PK2 controls 18F1320 PGC)
SW6-4 TX Uses PGD for PICkit 2 UART test mode (PICkit 2 software)
SW6-5 RX Uses PGC for PICkit 2 UART test mode (PICkit 2 software)
SW6-6 IR IN (38KHz IR) receiver on, Pushbutton 1 is always enabled
SW6-7 VR2 (0-5V pot) or USER port RA3 I/O
SW6-8 VR1 (0-5V pot) or USER port RA1 I/O
Reset switch SW4 will function when SW6-1 is off. Remember you can control reset via MPLAB.
* Turn SW6-1,2,3 OFF to when using the ICD connector (CON2)
Looks like you would want them all off to me
 
Last edited:
Once the PIR output stabilizes and stays active for at least 250 milliseconds, the PIR output is
considered valid and the program will drop through to the main control section. You need a delay for the PIR
Code:
If PIR = 1 then        //see if it is high
    delayMs(250)         // so you don't get bad triggers 
endif
I tested your code the speaker didn't come on with out the PIR
 
Last edited:
Thanks be80be

I inserted the code you posted.
long night last night so will get back at it later today hopfully.
I wonder why the chip got programed with the dip switches set like I posted??
will have to look at my set up again.
I wonder if it has something to do with the fact that I only had the 5 connections going to my perf board not the complete circuit??
I inserted your code into the interupt section??
Code:
INTERRUPT MyInt()
T1CON.0=0 //stop timer
TMR1=-Tones(Tone) //reset period
T1CON.0=1 //restart timer
    IF Tone=5 THEN //if silence
    Speaker=0 //speaker off
    Amp=0 // amp off
    If PIR = 1 then        //see if it is high
    delayMs(250)         // so you don't get bad triggers 
endif
    ELSE //otherwise
    TOGGLE(Speaker) //make sound
    ENDIF
TMR1IF=0 //clear interrupt flag
END INTERRUPT

I am contemplating ordering the parts for 5 units but thinking I should wait but then I would not get a true test if I only have one unit. contemplating what to do..
you say you got my code to work??
which code-extra dip switches or ?. how about the PIR= w/ transistor for a low on the port or high ??
 
I tried the last code you posted a page or two back It see if portB.2 was high and it's not. So it can't be triggering you speaker.
 
The last code I think turned on the speaker

if it didn't have the dip switch routine then it worked.
If it had the dip switch routine then it stayed on.
of coarse I wonder if I programmed the PIC correctly as I had on the Junebug = switch 2-5 on all others off
now I understand I need to shut all the switches off.
will get back to it later. still exhausted from last night and my brain has cobwebs this morn.
then my E drive decided not to work??
got it working after I reset computer.
egads I hope this is not how my day is going to go.
 
WELL BACK AT IT got it kinda working

need to add the snippet of code Pommie posted and
heres the hard part - adding some code for the dip switch settings.
Reason for dip switches?
so the speed of the delay and number of tones can be hanged in the field.
have about 20-4pos dips that were given to me and I figure might as well use them.
I dim two of them bu maybe get all four into the act.
just looking for suggestions on where to include the code and what.
I have speed as the variaible determined on the dip switch setting.
Code:
DEVICE = 18F1320
CLOCK = 8 // 8MHz clock
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

DIM NOT_RBPU AS INTCON2.7
DIM TMR1IE AS PIE1.0
DIM TMR1IF AS PIR1.0
DIM TMR1 AS TMR1L.AsWord
DIM Speaker AS PORTB.3
DIM SpeakerTris AS TRISB.3
DIM Amp AS PORTA.1 // turns on amp power
DIM AmpTris AS TRISA.1
DIM dip3TRIS AS TRISB.1 //changes delay  dip switch setting
DIM dip4 AS PORTB.0 //changes # of tones dip switch setting
DIM dip4TRIS AS TRISB.0
DIM PIR AS PORTB.2 // turns on PIR  
DIM PIRTris AS TRISB.2
DIM speed AS BYTE
DIM TUNE AS BYTE

//global variables


DIM Seed AS LONGWORD, Tone AS BYTE
DIM i AS BYTE

//half period delays = clock speed divided by 2*frequency
CONST Tones(18) AS WORD = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)

//interrupt routine
INTERRUPT MyInt()
T1CON.0=0 //stop timer
TMR1=-Tones(Tone) //reset period
T1CON.0=1 //restart timer
    IF Tone=5 THEN //if silence
    Speaker=0 //speaker off
    Amp=0 // amp off
    PIR=1 //PIR off
    ELSE //otherwise
    TOGGLE(Speaker) //make sound
    ENDIF
TMR1IF=0 //clear interrupt flag
END INTERRUPT
FUNCTION Rand(Range AS BYTE) AS BYTE
DIM i AS BYTE, feed AS BIT, temp AS WORD
    FOR i = 0 TO 7 //generate 8 bits
    Feed = Seed.30 XOR Seed.27 //make new bit
    Seed=Seed*2+Feed //shift seed left and add new bit
    NEXT
Temp=(Seed AND 255) * Range //change Rand from 0 to 255
Rand = Temp/256 //to 0 to (Range-1)
    END FUNCTION

//main code starts here

OSCCON = $72 //select 8MHz internal clock
NOT_RBPU=0 //WPUs on port B
ADCON1=$71 //all digital
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
ENABLE(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    PIRTris=1
    Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
    IF PORTB.2=0 THEN //if PIR activated portRB2 is high
    PORTB.1=1 speed=200 //dip switch
   
    PORTB.0=1 TUNE=50 // dip switch
    

    FOR i = 1 TO 200 //play 20 tones
    Tone=Rand(5) //each tone is random frequency
DELAYMS(speed) //and for 1.00 seconds
NEXT //end for loop
    ELSE //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
    ENDIF //end if condition
WEND //end of while loop
havn't got something right??
can I add 2 more variables for speed depending on dip switch settings?
 
revised schematic but portb.1??

had to change connections on the amp transistor. this works
now trying to input the variable=speed to change the delayMS
tried going to ground with pin 9 (portrb1) but it doesn't change.
I inserted 25 in the delayms(25) (had delayms(speed)but it didn't work) and it runs much faster with 25 as delayms but I want to control speed using the dip switches.have 4 so dip1=100, dip 2 = 150, dip 3 = 25, dip 4 = 250 or what ever values I want.
Code:
DEVICE = 18F1320
CLOCK = 8 // 8MHz clock
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

DIM NOT_RBPU AS INTCON2.7
DIM TMR1IE AS PIE1.0
DIM TMR1IF AS PIR1.0
DIM TMR1 AS TMR1L.AsWord
DIM Speaker AS PORTB.3
DIM SpeakerTris AS TRISB.3
DIM Amp AS PORTA.1 // turns on amp power
DIM AmpTris AS TRISA.1
DIM dip3 AS PORTB.1
DIM dip3TRIS AS TRISB.1 //changes delay
DIM dip4 AS PORTB.0 //changes # of tones
DIM dip4TRIS AS TRISB.0
DIM PIR AS PORTB.2 // turns on PIR
DIM PIRTris AS TRISB.2
DIM speed AS BYTE
DIM TUNE AS BYTE


//global variables


DIM Seed AS LONGWORD, Tone AS BYTE
DIM i AS BYTE
    
//half period delays = clock speed divided by 2*frequency
CONST Tones(18) AS WORD = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)

//interrupt routine

INTERRUPT MyInt()
    T1CON.0=0 //stop timer
    TMR1=-Tones(Tone) //reset period
    T1CON.0=1 //restart timer
    IF Tone=5 THEN //if silence
    Speaker=0 //speaker off
    Amp=0 // amp off
    PIR=1 //PIR off
    ELSE //otherwise
    TOGGLE(Speaker) //make sound
    ENDIF
    TMR1IF=0 //clear interrupt flag
END INTERRUPT
FUNCTION Rand(Range AS BYTE) AS BYTE
DIM i AS BYTE, feed AS BIT, temp AS WORD
    FOR i = 0 TO 7 //generate 8 bits
    Feed = Seed.30 XOR Seed.27 //make new bit
    Seed=Seed*2+Feed //shift seed left and add new bit
    NEXT
Temp=(Seed AND 255) * Range //change Rand from 0 to 255
Rand = Temp/256 //to 0 to (Range-1)
    END FUNCTION

//main code starts here

OSCCON = $72 //select 8MHz internal clock
NOT_RBPU=0 //WPUs on port B
ADCON1=$71 //all digital
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
ENABLE(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    PIRTris=1
    Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
    IF PORTB.2=0 THEN //if PIR activated portRB2 is low
    PORTB.1=0 speed=25
    PORTB.1=1 speed=200

    FOR i = 1 TO 200 //play 20 tones
    Tone=Rand(5) //each tone is random frequency
DELAYMS(25) //and for 25-200 seconds [COLOR="Red"]here is where I want to change[/COLOR]
NEXT //end for loop
    ELSE //otherwise
Tone=5 //silence
i=Rand(255) //make rand more random
    ENDIF //end if condition
WEND //end of while loop

any suggestions?
dip switch settings on Junebug using the USB to power project??
 

Attachments

  • Capture4-23-2009-8.46.52 PM.jpg
    Capture4-23-2009-8.46.52 PM.jpg
    142.9 KB · Views: 269
I guess what you are trying to do is,
Code:
WHILE(TRUE) 			//repeat forever
    if PORTB.1=0 then
        speed=25
    else
        speed=200
    endif
    IF PORTB.2=0 THEN 		//if PIR activated portRB2 is low
        FOR i = 1 TO 200 	//play 20 tones
            Tone=Rand(5) 	//each tone is random frequency
            DELAYMS(speed) 	//and for 25-200 seconds here is where I want to change
        NEXT 			//end for loop
    ELSE 			//otherwise
        Tone=5 			//silence
        i=Rand(255) 		//make rand more random
    ENDIF 			//end if condition
WEND 				//end of while loop

Edit, you don't want the transistor on the positive side of the amp or the amp will only get 4.7V. It needs to be in the ground connection.

Mike.
 
Last edited:
Thanks Pommie will give it a try

I tried the transistor on the ground side and got what sounded like static as well as the desired tones.
I am going to reconnect the amp pin 4 to collector only (I had pin 2 and the speaker ground tied in as well but going straight to ground with pins 2 and speaker ground only.
hopfully I can add some more variables to the speed
 
YES but trying to add more to it

I added your p[osted code and it worked.
I moved the transistor to the ground side of amp and powered the base using porta.1 (inserted a toggle(amp) and it works. need to recaculate the base resistor as the port only shows 2.5v
now trying to add more dip switch combos but keep getting WHILE expected
Code:
ENABLE(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    PIRTris=1
    Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
    WHILE(TRUE) 			//repeat forever
    if PORTB.1=0 then
        speed=25
    else
        speed=200
    endif
WHILE(TRUE) //repeat forever
    
    if PORTa.0=0 then
        speed=100
        else
        speed=25
    endif
    IF PORTB.2=0 THEN 		//if PIR activated portRB2 is low
        FOR i = 1 TO 200 	//play 20 tones
            Tone=Rand(5) 	//each tone is random frequency
            DELAYMS(speed) 	//and for 25-200 seconds here is where I want to change
        NEXT 			//end for loop
    ELSE 			//otherwise
        Tone=5 			//silence
        i=Rand(255) 		//make rand more random
    ENDIF 			//end if condition

   
   Tone=5 //silence
   i=Rand(255) //make rand more random
   

WEND //end of while loop

adding, cutting pasting etc but??
 
wonder if something like this?

if PORTa.0=0 then
speed=50
port a1=0 speed +50 // a dip switch setting
porta0=0 speed+75
port a2 = 0 speed +100
speed=25
endif
IF PORTB.2=0 THEN //if PIR activated portRB2 is low
FOR i = 1 TO 200 //play 20 tones
Tone=Rand(5) //each tone is random frequency
DELAYMS(speed) //and for 25-200 seconds

not real sure of syntax but ?? add 50 or so to speed per switch setting
 
What about ADC for the variable? of dip sw.

I have been kinda reading and ran across ADC inputs for a PIC.
Wonder if putting 4 resistors in series then have the 4 pos dip switch select the different dip switches thus producing 16 different voltages?? instead of using 4 different ports??
 
keep getting errors??

thought I would try using variable arrays. keeps asking for next or end expected??
then subroutine out of bounds?
Code:
DEVICE = 18F1320
CLOCK = 8 // 8MHz clock
CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

DIM NOT_RBPU AS INTCON2.7
DIM TMR1IE AS PIE1.0
DIM TMR1IF AS PIR1.0
DIM TMR1 AS TMR1L.AsWord
DIM Speaker AS PORTB.3
DIM SpeakerTris AS TRISB.3
DIM Amp AS PORTA.1 // turns on amp power
DIM AmpTris AS TRISA.1
DIM dip1 AS PORTa.1
DIM dip1TRIS AS TRISa.1 //changes speed
DIM dip2 AS PORTa.2
DIM dip2TRIS AS TRISa.2 //changes speed
DIM dip3 AS PORTa.3
DIM dip3TRIS AS TRISa.3 //changes speed
DIM dip4 AS PORTa.4 //changes # of tones
DIM dip4TRIS AS TRISa.4
DIM PIR AS PORTB.2 // turns on PIR
DIM PIRTris AS TRISB.2
DIM speed AS BYTE



//global variables


DIM Seed AS LONGWORD, Tone AS BYTE
DIM i AS BYTE
    
//half period delays = clock speed divided by 2*frequency
CONST Tones(18) AS WORD = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,
2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)
[COLOR="Red"]Const [/COLOR]
     [COLOR="Red"] constArray(4) as byte=(25,40,55,70)
      [COLOR="Red"]for array = 0 to bound(4)[/COLOR]
     [COLOR="Red"] dip1=25  //dip switch values[/COLOR]
      [COLOR="Red"]dip2=40[/COLOR]
      [COLOR="Red"]dip3=55[/COLOR]
      [COLOR="Red"]dip4=70[/COLOR]
      speed()=dip1+dip2+dip3+dip4
      next[/COLOR]
     
      
end
      
      
//interrupt routine

INTERRUPT MyInt()
    T1CON.0=0 //stop timer
    TMR1=-Tones(Tone) //reset period
    T1CON.0=1 //restart timer
    IF Tone=5 THEN //if silence
    Speaker=0 //speaker off
    Amp=0 // amp off
    PIR=1 //PIR off
    ELSE //otherwise
    TOGGLE(Speaker) //make sound
    TOGGLE(Amp)//turn amp on
    ENDIF
    TMR1IF=0 //clear interrupt flag
END INTERRUPT
FUNCTION Rand(Range AS BYTE) AS BYTE
DIM i AS BYTE, feed AS BIT, temp AS WORD
    FOR i = 0 TO 7 //generate 8 bits
    Feed = Seed.30 XOR Seed.27 //make new bit
    Seed=Seed*2+Feed //shift seed left and add new bit
    NEXT
Temp=(Seed AND 255) * Range //change Rand from 0 to 255
Rand = Temp/256 //to 0 to (Range-1)
    END FUNCTION

//main code starts here

OSCCON = $72 //select 8MHz internal clock
NOT_RBPU=0 //WPUs on port B
ADCON1=$71 //all digital
T1CON = %10000001 //pre=1
Tone=5 //no sound please
TMR1IE=1 //enable timer 1 interrupt
ENABLE(MyInt) //set interrupt going
    SpeakerTris=0 //Setup Port
    PIRTris=1
    Seed=$12345678 //seed random number
WHILE(TRUE) //repeat forever
    WHILE(TRUE) 			//repeat forever
    speed=dip1+dip2+dip3+dip4
        
    ELSE
        speed=200
    ENDIF
WHILE(TRUE) //repeat forever
    
    IF PORTB.0=0 THEN
        speed=25
    PORTB.1=0 THEN speed+50
        ELSE
        speed=25
    ENDIF
    IF PORTB.2=0 THEN 		//if PIR activated portRB2 is low
        FOR i = 1 TO 200 	//play 20 tones
            Tone=Rand(5) 	//each tone is random frequency
            DELAYMS(speed) 	//and for 25-200 seconds here is where I want to change
        NEXT 			//end for loop
    ELSE 			//otherwise
        Tone=5 			//silence
        i=Rand(255) 		//make rand more random
    ENDIF 			//end if condition

   
   Tone=5 //silence
   i=Rand(255) //make rand more random
   

WEND //end of while loop

any suggestions???
 
totally lost again

trying to make each dip switch equal a set value using an array.
well can't get it to work??
any help
Code:
Const 
dim speed(4) as byte=(25,40,55,70)
      for speed = 0 to bound(4)
      if dip1=1 then speed(1)  //dip switch values
      if dip2=1 then speed(2)
      if dip3=1 then speed(3)
      if dip4=1 then speed(4)
      speed()=speed(1+2+3+4)
      
      
end
 
You need to find a good tutorial on Basic. The code you keep posting is, how can I put this politely, rubbish.

Try to understand how each command works. While always has to have a Wend, If anyways endif, for always next etc.

Try to get an understanding of program flow and this stuff will be really easy.

Mike.
 
You need to find a good tutorial on Basic
That would be nice if the there
was such a thing. Oh this is how to get started with swordfish basic Looks more like how to use a module then how to use basic to me
BeginningSFBasic
Module Options

Many Swordfish modules are configured at compile time using options. For example,

#option LCD_RS = PORTE.0

Make sure that the #option is declared before the module is included. If you declare the option after the include file, it will be ignored.
Strings

When declaring strings, don't forget to allow enough space for the null terminator. For example, if you want to declare a 3 character string, you need to use

dim MyStr as string(4)

If you don't explicitly give a string dimension, Swordfish defaults to 24. That is, 23 characters plus a null terminator.
Variables

Public or Private?

With in SwordFish a variable can be thought of in 2 states Static (Public) eg will not be touched by the compiler, or recycled (Private) where once the Function/Sub has finished it will be recycled for use by other routines.

Not quite correct...

It's better to think in terms of scope. A module level variable (that is, a variable not declared in a sub or function) is static, but it can be public or private. Every sub or function within a module can see a module level variable - even if it is private (and assuming it is declared before it is referenced). However, if you import a module into another module or program, only public variables can be seen. In programming terms, public subroutines, functions and variables are the modules 'interface'

Variables declared withing a subroutine or function are of course private to the routine they are declared in - referred to as 'local'. Parameters are local in terms of scope. For example, you cannot reference a parameter by name outside the procedure it is declared in. However, parameters do provide the 'interface' to a sub or function.

All 'local' variables (parameters and variables) are recycled by the compiler. Module level variables (private or public) are not recycled and are static.

Hope the above is useful...

David Barker

To understand how you the variable you declared is seen by the compiler you have to think of the different context the compiler sees the variable in.

1 Main, this is with in the main program i.e not with in a Module or a Function/Sub Variables declared here................

To be continued once I get my facts right

Please correct add to this section by editing it, just click the Edit button below
That's from swordfishbasic real help full more like what Mike said
how can I put this politely, rubbish.
MrDeb It takes time to do this when you have to learn how to write the code and the more you add to it the harder it gets. Mike posted you a good code to start with. But you need to learn that part and how it works before adding to it. Or you will never get done with it.
That's what so hard about using a pic we want the end first.
You have to set up your code so you can get to the end. It's all about flow have to think like a pick.
 
Last edited:
I have to agree w/Pommie=rubish but

I am chipping away at it.
I keep getting then statement errors.
am making progress. a little but some
will ask for suggestions and then I hopfully can change to fit my preferences,.
I presently have Pommies code intermixed with be80be's
have one version that works but it shows a hint that I declared something but didn't use it.
had several on one version and discovered I had an end statement that wasn't supposed to be there.
YES I would love to find a good book that describes all the syntax.
 
Unused declarations aren't bad, just means once you get things to the point it works go over it with a fine tooth comb from top to bottom and correct things neaten things up and comment as much as possible. If you don't comment your code judiciously in a few months you'll go back to that same code and have no idea what it does =)

MrDeb, what language is this in again?
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top