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.
supposed to be pirate basic or ??

using swordfish basic
I got my 3 dip switches set up = no errors when I hit F12
now does it work?? need to program the little picky.
IF it works then dip switch 4 is to make speed random. so each time the PIR triggers the speed is different
any short arrays or ? for this.
planning on using the TONE & seed setup that Pommie posted and I am using for the different tones (when this baby gets going it drives my wife nuts (very annoying sound).
the code is what I changed / added to get the dip switches set up
we is maken progress I think.
there has to be a shorter way to read all the A ports instead of the way I did it??
Code:
WHILE(TRUE) //repeat forever
    speed = 25
    if PORTA.1=0 THEN //dip1
       speed=speed + 10
    endif
    
    if PORTA.2=0 then//dip2 
    speed= speed+50
    endif
    
    if PORTA.3=0 then //dip3 
    speed= speed+70
    endif
 
Mr deb you write basic Like I did when I started baisc it was like this
Code:
1 do this
2 do this
3 do that 
4 goto 30 and do that
30 come back to 1 and start over


while (true) 
    if a= b then 
       c = 1                   // it will keep doing as long as it is true
endif
wend      
for   next                  // run threw all statements one time
sure miss the 1,2,3,4
 
it was working but??

I first turned on the power and the SPEED was 25 (running like a scared rabbit=fast)
then it wasn't going as fast??
I measures voltage on the RA pins (dip switch inputs) only 2.5 v
should put pull up resistors to go to 5v (high) rule number 1 in digital-duh!
the transistor on the ground of the lm386 (Vcc) pin 4/transistor/ground dosn't work as well as 9v/transistor/pin 6 (Vdd)
volume about 1/3 less when transistor switching ground vers Vdd.
could be the loss through the transistor not taking pin 4 to absolute ground??
now to figure out why porta.0 only outputs 2,54v (I think I had 5 at one point in testing??(port A.1 instead of port A.0???
need to take second look at code.
add 10k pull up resistors (they will draw current all the time?? or does the pic need to be woke up via the PIR on port RB2
as for coding- If it works why fix it or will I learn to code like master coder be80be??
 
I just had a look for a tutorial on structured basic and was surprised at the lack of any reasonable ones. Maybe a trip to the library for a structured basic book would be useful. In the mean time I'll help where I can.

I see you have got the hang of the IF statements now.

If you want a shorter way to read port A then you can do,
Code:
    Speed = PORTA       //fetch port A
    Speed = Speed / 2   //shift it right 1 bit
    Speed = Speed And 7 //only keep bottom 3 bits
    Speed = Speed * 25  //speed = 0 to 175
    Speed = Speed + 25  //speed = 25 to 200

Or, you can do all of that on one line,
Code:
    Speed = ((PORTA/2) And 7)*25+25

For both of the above speed should be at least a byte variable and probable best if made a word with,
Code:
Dim Speed as Word
in the variable area. As a word, you can change the above 25s to 50s (or more) to make the times 50 to 400.

You will need pullups on PORTA but you can connect them to another port pin and only make it output when you're reading the DIP switches. This will reduce battery drain.

Mike.
 
Last edited:
will give your code a run

for the money
my code I thought was working but I find that speed was constant 100 instead of returning to 25. even after the power was turned off.
I have reconnected the Junebug to a transistor/speaker thus eliminating having to program in circuit and eliminating any variables concerning the breadboard.
 
added your code and made slight changes

the dang thing was resetting to last trigger incident reguardless of switch settings.
dded several IF THEN & ELSE statements
all seems to be working??be nice to add a random number for dip switch 4 where each trigger the speed changes, reguardles of the switch settings of other 3 switches. Oh yea I got the transistor from LM386 grd pin 4 working.
need to clean up code a little, order parts, etch some pc boards.Will have IC programming capabilities using a strip header.
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 Speed as word

//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
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

Speed = ((PORTb/2) And 7)*25+25

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
Seed=$12345678 //seed random number
While(TRUE) //repeat forever

    If PORTB.2=0 Then //dip1 change to b.0
       speed=100
    EndIf
    
    if Portb.2=1 then // dip 2
       Speed=25
    endif
    
    If PORTB.5=0 Then //dip3
       speed=150
    endif
    
   
    
If PORTB.0=0 Then //if button 1 pressed add PIR=1 here
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
 
need clarification on pullup resistor connections

You will need pullups on PORTA but you can connect them to another port pin and only make it output when you're reading the DIP switches. This will reduce battery drain.

if I understand this put pull up resistors on all my 4 dip switch inputs and then read those 4ports separately??
OR
have each dip switch w/ its own pullup resistor then connect the common on the dip switches to a port, then make that port high?
 

Attachments

  • Capture4-26-2009-4.32.15 AM.jpg
    Capture4-26-2009-4.32.15 AM.jpg
    109.9 KB · Views: 255
another idea to read dip switches

my first schematic was not right.
this one might just work. In this case we ake RA3 HIGH. run the dip switch read routine, then reset RA3 to LOW
this would save battery drain??
my other idea was to get dip switch 4 to INCREMENT the variable SPEED each time the unit triggers or do a random number for SPEED.??
maybe a private routine that is accessed only if dip switch 4 is LOW and canceling all other dip switches.
I must apologize if some of my OFF THE WALL ideas sound crazy.
 

Attachments

  • Capture4-26-2009-8.47.10 AM.jpg
    Capture4-26-2009-8.47.10 AM.jpg
    142.5 KB · Views: 248
You got the idea with RA3 but you don't need the transistor, just connect RA3 to the resistor array. When you want to read the DIP switches you set RA3 to output and high, the rest of the time you set RA3 to input to save power.

If you want DIP 4 to be random then add,
Code:
    If PORTB.2=0 Then //dip1 change to b.0
       speed=100
    EndIf
    
    if Portb.2=1 then // dip 2
       Speed=25
    endif
    
    If PORTB.5=0 Then //dip3
       speed=150
    endif
    
[COLOR="Red"]    If PORTB.x=0 Then //dip4
       speed=Rand(10)*25+25    //Speed = 25 to 250
    endif[/COLOR]

Obviously, change x to the pin number.

Mike.
 
in other words the transistor

is overkill??
I was thinking that shorting RA3 to ground is a no no ( dead short?)
change to an output?
how do I determine an output or input
 
is overkill??
I was thinking that shorting RA3 to ground is a no no ( dead short?)
change to an output?
how do I determine an output or input

You wouldn't be shorting A3 to ground as it would be through the resistor array which should be around 20k each.

To switch a pin to input or output you use the TRIS register. Setting it to 1 makes it input and zero output. So TRISA.3=1 will set PORTA.3 to input.

Mike.
 
THANKS a bunch

next question how or what math (I am not real sharp w/ math) did
If PORTB.x=0 Then //dip4
speed=Rand(10)*25+25 //Speed = 25 to 250<<MATH computations??
endif

I changed the 25's to 15's not so long run time
then I need to determine current draw of circuit
I measured 90ma w/ LM7805 and the LM386 on a breadboard.
need to operate without the regulator and have one bat pak (3-AAA connected to PIC)
and second bat pak in series w/ bat pak 1 but powers the LM386 w/ 9v
 
figuring 90 ma

and a AAA battery has 1150mah then this CRITTER RIDDER should run continuous for 12hrs??
of coarse the regulator won't be in the circuit so that should save something??
 
The maths is pretty simple,

Rand(x) returns a value between 0 and x-1 and so, Rand(10) will return a value between 0 and 9. This gets multiplied by 25 to become 0 to 225. It then gets 25 added to it to make it 25 to 250. It would, of course, be 10 discrete values, 25, 50, 75 ......250.

Mike.
 
I now need to add some TRIS statements

for the dip switch inputs?? but they work I think??
maybe thats why when I had the circuit on a breadboard I was getting only 2.5v out on some of the pins that I thought I set up as outputs and inputs.
 
I recall several post back about

the portRB0-RB? have a resistor network built in and using would conserve current??
Are there some ports for certain jobs than others.
like the INT# ports are for implementing interrupt routines??
 
an abreviated code

here is what I think you were talking about
I coped n pasted into notebook just the snippets that I added.
Copping n pasting entire code is tooo much???

did same for ports A0-A4
Dim dip1 As PORTA.0
Dim dip1 TRIS As TRISA.0 //changes speed pin 1
Dim dip2 As PORTA.1
Dim dip2TRIS As TRISA.1 //changes speed pin 2
Dim dip3 As PORTA.2
Dim dip3TRIS As TRISA.2 //changes speed pin 6
Dim dip4 As PORTA.3
Dim dip4 TRIS As TRISA.3 //changes speed pin 7
Dim dipRD As PORTb.0
Dim dipRD TRIS As TRISB.0 //reads dip switches pin 8

Dim dipRD As PORTb.0
Dim dipRD TRIS As TRISB.0 //reads dip switches pin 8

main code starts here

portB.0=1 // goes high

read dip switch ports


turn off LOW portB.0 finished reading dips

portb.0=0// port goes LOW to conserve power finished reading dips
 
The DIP switches worked because when the Pic powers up all pins are set to input and so you only need to set the pins that need to be output.

PortB does have the internal pullups and so is more suited to reading switches.

The Dim statement is used to tell the compiler the names of variables,
Code:
Dim Speed as Word            //Speed is 16 bits = 2 bytes
Dim dipRDTRIS As TRISB.0     //dipRDTRIS is a bit on port B
in the case of Speed the compiler reserves 2 memory locations and uses them whenever we access Speed. In the case of dipRDTRIS we're telling the compiler that whenever we write dipRDTRIS, we want it replaced with TRISB.0. The Dim statement does not produce any code - this is important. Without further code PORTB.0 will still be an input. To set it to output we have to add,
Code:
dipRDTRIS = 0
We place this instruction near to where the code starts executing.
Code:
//main code starts here

OSCCON = $72            //select 8MHz internal clock
NOT_RBPU=0              //WPUs on port B
ADCON1=$70              //all digital
[COLOR="Red"]dipRDTRIS = 0           //add it here[/COLOR]
T1CON = %10000001       //pre=1

HTH,

Mike.
 
in your code you have $70

to set for all digital but in an earlier post you stated that ALL DIGITAL was supposed to be $71
am sure this matters??
would it be better to utilize the internal resistors on the RB# ports to read the dips or will it really make any difference??
Laying out the pc board it looks easier to have the dip switches near the RA# ports. less trace traffic-LOL
you have been a great help Pommie.
I just need to learn the syntax etc,. Doesn't appear to difficult but a long learning curve to get it right.
swordfish basic = similar to picbasic or ??
any suggestions as for book.
 
I just copied that code from a project to demonstrate where the Tris should be set. For all digital (on a 18F1320) ADCON1 should contain $7f.

Using the internal pullups on portb cuts down on external components and so is normally the best way. You can use porta but you will need the external pullups.

As for Books, I can't help - not read a programming book for many years.:D Maybe a trip to the local library and find one that you find easy to understand.

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top