Junebug help??

Status
Not open for further replies.
kinda crude but might work?

just playing around thinking out loud
RB0 does show 4.5v if none of the dips are on.
thinking maybe a different approach?? and still utilize RB0 to read the dip switches.
reguardless how I do the dip switches before I still only get 2.5 v on any A port IF any of the dip switches are ON
with this new circuit maybe the PIC will read the dip switches??
 

Attachments

  • Capture5-3-2009-9.45.27 PM.jpg
    166.2 KB · Views: 262
Your using PortB.0 to make PortA0 to 3 high you pull all of them low and set 1 high with the dip switch. You poll for which switch is high. It's your code not the switch layout that's wrong If you hooked the switches up like I showed you they will read right.But you can only set 1 switch high at a time.
 
If you do that like you drawled it It's for sure not going to work I showed you how to hook
it up to read your dip switch But you can't read but one switch at a time you where using portb0 for power. I don't want to sound mean but your changing things before you get it to work is making a mess of it. It's the code that's not working not the switch layout that I showed you.
 
I will take a second look

I know that the circuit you drew out should work but I am not getting the 4.5v on any of the porta inputs regardless of how I set the dips.
doesn't make sense.
I am going to recheck again my wiring and make sure its wired correctly. It doesn't make sense so yes it must be the code.
no don't worry about it, you don't sound mean. I am happy I have people who can help with this code.
 
When you post your circuit try to get all of it. I can't see the PIR I think it's how you have it hooked up is what wrong.
 
I am pretty sure its in the code

I breadboarded the circuit with-out the PIR (ground pin17 RB2) to trigger.
I have pin 8 RB0 connected to pin 1RA0 = works but not right. supposed to be 25ms delay(really fast)
I have resistors connected to RB1, RB2, RB3 connected to ground.
RB# ports are supposed to change DELAYMS() when selected port is high.
Am trying to eliminate as many component variables as possible.
Its got to be in the code as it is stuck on one DELAYMS()
which one??. I think its 100ms but not sure.
thinking about eliminating the other portsRA1,2,3 and see if it will read ONLY the variable of port1 for DELAYMS() I think its 25MS or supposed to be??
 

Attachments

  • Capture5-4-2009-11.46.52 AM.jpg
    147.8 KB · Views: 245
OH forgot

I read 4.5v on pin 8 before the unit is triggered
BUT
when its triggered and pin8 is connected to pin 1 I read 2.1v on pin 1 which is supposed to be high to select the dip switch variable for DELAYMS()
 
I going to try your code with it wire right what you posted I don't see working how the pic to no what your wanting it to do with that .
 
Take a look at this It mach's your code
 

Attachments

  • swmrdeb.PNG
    7.3 KB · Views: 259
Last edited:
now im confussed??

unless I made a mistake drawing my dip switches.
you have a resistor going from RB0 to RB1.
I will look at my schematic??
question-how do I designate an input port as an input and an output port as an output?
 
Clear drawing of test circuit

Where did RB1 come from (I need to reread my code)I found it. RB1 controls the 9v to the LM386 amp.
could have come from all the copy n paste.
Eliminate for now until I get the dips working.??
 

Attachments

  • Capture5-4-2009-4.25.16 PM.jpg
    131.8 KB · Views: 284
  • Capture5-4-2009-4.31.17 PM.jpg
    145.7 KB · Views: 265
From the top Ok you want to make sound right
1. PortB0 was to supply power so you can put the pic to sleep and not use any but what the Pir used and the little the pic will use in sleep.
2 I just used PortB0 for power to test the switch your using for testing it will not be like that when you install your PIR
3. PortA 0 to 2 are to change sounds right.
So let's get this part working and then add the amp and the PIR what do you think?
 
I think I found it!!!

I don't have PORTB.0 set up with TRISB.0
TRIS sets the port as OUTPUT??
hopfully this is the problem
 
code with revisions

will test later tonight
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 PORTB.1 // turns on amp power
Dim AmpTris As TRISB.1//turns on amp pin 9
Dim DipRD As PORTB.0 // sets port as output for dip switches
Dim dipRDTris As TRISB.0//turns on dip read pin 8
Dim Speed As Word
Dim dip1 As PORTA.0 //input for 25ms

Dim dip2 As PORTA.1 // input for 50ms

Dim dip3 As PORTA.2 //input for 100ms

Dim dip4 As PORTA.3 // input for random dips sw


//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
      PORTB.0=1 // goes high to read dips


//main code starts here

       OSCCON = $72            //select 8MHz internal clock
       NOT_RBPU=0              //WPUs on port B
       ADCON1=$7f              //all digital
       TRISB.0=0               //make output
       PORTB.0=1 //makes pin 8 high for dip switches
       T1CON = %10000001       //pre=1
       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
        TRISA =%00001111          //sets your inputs on porta 
While(TRUE) //repeat forever

    If PORTA.0=1 Then //dip1 pin 1
       Speed=25
    EndIf
    
    If PORTA.1=1 Then // dip 2 pin 2
       Speed=50 
    EndIf
    
    If PORTA.2=1 Then //dip3 pin 6
       Speed=100
    EndIf
    
    If PORTA.3=1 Then //dip4 pin 7
       Speed=Rand(10)*15+15    //Speed = 25 to 250 random select speed
       
    EndIf
    //PORTB.0=0// port goes LOW to conserve powerfinished reading dips
    //leave this off for now and see what happens  
   
    
If PORTB.1=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
 
Ok I hook it up Like I showed you and it works the three dips change speed and portb.1
turns it on I was going to remove the junk out of your code that's not being used.
This code works if you hook your switches right
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 PORTB.1 // turns on amp power
Dim AmpTris As TRISB.1//turns on amp pin 9
Dim Speed As Word
Dim dip1 As PORTA.0

Dim dip2 As PORTA.1

Dim dip3 As PORTA.2

Dim dip4 As PORTA.3

Dim dipRD As PORTB.0

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


      


//main code starts here

       OSCCON = $72            //select 8MHz internal clock
       NOT_RBPU=0              //WPUs on port B
       ADCON1=$7f              //all digital
       TRISB.0=0               //make output
       PORTB.0=1 //makes pin 8 high for dip switches
       T1CON = %10000001       //pre=1
       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
        TRISA =%00001111          //sets your inputs on porta 
While(TRUE) //repeat forever

    If dip1=1 Then //dip1 pin 1
       Speed=25
    EndIf
    
    If dip2=1 Then // dip 2 pin 2
       Speed=50 
    EndIf
    
    If dip3=1 Then //dip3 pin 6
       Speed=75
    EndIf
    
    If dip4=1 Then //dip4 pin 
       Speed=Rand(10)*15+15    //Speed = 25 to 250 random select speed
       
    EndIf
   
If PORTB.1=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
I added the last dip4
random select speed
It works to all 4 dips read 4.58 volts
all play a tune that's not the same
 
Last edited:
MrDeb when you was using a transistor to drive your speaker what was it I used a 2n3904 I had it laying on the table it's half as loud as a 2n2222 which I used when we started this thing. Try this
 

Attachments

  • sound.PNG
    10.5 KB · Views: 267
Last edited:
switch from RB1to grd?

looking at schematic I assume this is to act like the PIR? to trigger circuit.
havn't looked code over yet but will try your code out.
did you make any changes??
I wonder if you added a TRIS to port b.0 to make it an output??
its time for bed - 11pm here in Salmon Idaho.
THANKS AGAIN
 
So was I correct about TRIS?

I looked for it but never saw it for port b0.
my last post with code I included the TRISb.0 bu never tried it.
You mean I actually discovered why the code I had wasn't working??
well it's 5am and time to start testing the new code.
 
syntax error

you need a boolean expression
Code:
If dip4 Then //dip4 pin
I added =1 and no syntax
bad be80be no ice cream for you! LOL
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…