Swordfish L O n G delay

Status
Not open for further replies.
Thinking ahead for future debugging of code
Inserting a USART to output the value of const Tones(6) as the tones are selected?
the method of inserting a USART write routine into code for debugging was suggested over at **broken link removed**
sounds like a viable idea
 
If you need more tones then it is quite simple to change the code to do it.

Mike.
 
Yes more tones would be perhaps better.
Any thought on using the USART to SEE what the random number gererator is outputting?
I am hopeing to try out by inserting a Usart.WRITE insert?
 
ONE PROBLEM
I think the code is CRASHING!
I changed the time on from 5*60 to 1*60 as well as 2*60 from 10*60 (for testing purposes)
Well I finally got around to trying out the code. Works great but when I unchecked the Vdd box in Pickit2 the Junebug kept running the code.
Only way to shut it off was to hit MCLR
This is a minor problem as once it is set up and turned-on I can forget about it.
But Code Crashing might be an issue?
 
If the Junebug kept running the code then it is not crashing, it is doing what it is supposed to do.

What exactly is it doing? And what do you think it should be doing?

Mike.
 
I assume that when I check the Vdd box the unit starts making tones and then outputs the tones
Un-checking the Vdd box I THINK shuts off the Junebug?
I guess it dosn't.
Now adding more tones
In the post # 112 (see link above) it plays lots of tones.
I am stuidying the code trying to decipher what is happening then see how to ADD more tones.
My target is 2-8 khz and 15-27 khz. Playing with the idea of 6 tones/off for 5 seconds then 5 tones but of coarse random.
Looking at inserting (for code flow) a WRITE.uSART routine to SEE what tones are being displayed. As a learning exercise.
 
What about adding more notes ?
I assume that by changing Tones() from 5 to say 12 will change the CONST value thus change the number of tones.
Had an iodea of maybe keeping the Tones at 5 but adding more CONST numbers then insert a DELAYMS (2) then reload a new set of Tones()
 
adding Tones?

If changing this line
//half period delays = clock speed divided by 2*frequency
const Tones(6) as word = (2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000,1000)

TO

//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)-add -more frequencies
then down towards the bottom it says get more rand i = Rand(255)
will/does this reload different frequencies?
this is why I want to include a "testing Usart" to see what frequencies are being loaded
 
You can't just add frequencies to the table and expect them to be used.

I changed it so you can add more frequencies.
Code:
device = 18F1320
clock = 8 // 8MHz clock
config OSC = INTIO2, WDT = OFF, LVP = OFF

include "USART.bas"
include "CONVERT.bas"

dim NOT_RBPU as INTCON2.7
dim TMR1IE as PIE1.0
dim TMR1IF as PIR1.0
dim TMR2IE as PIE1.1
dim TMR2IF as PIR1.1
dim TMR1 as TMR1L.AsWord
dim Speaker as PORTB.3// speaker output port pin 18
dim SpeakerTris as TRISB.3

//global variables
dim Seed as longword, Tone as byte
dim i as word, count as byte
dim speed as byte
dim IntFlag as bit
    
//half period delays = clock speed divided by 2*frequency
//note first value (1000) is not a frequency and shouldn't be changed.
const NumTones = 5
const Tones(NumTones+1) as word = (1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000)

//interrupt routine
interrupt MyInt()
    if TMR1IF=1 then
        T1CON.0=0           //stop timer
        TMR1=-Tones(Tone)   //reset period
        T1CON.0=1           //restart timer
        if Tone=0 then      //if silence
            Speaker=0       //speaker off   
        else                //otherwise
            toggle(Speaker) //make sound
        endif
        TMR1IF=0            //clear interrupt flag
    endif
    if TMR2IF = 1 then
        TMR2IF=0
        Count=Count+1
        If Count=50 Then
            Count=0
            IntFlag=1
        endif
    EndIf
end interrupt

Sub Delay(Seconds as Word)
Dim x as Word
    for x=0 to Seconds
        While(IntFlag=0)
        Wend
        IntFlag=0
    next
end sub

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=$7f              //all digita
TRISB.0=0               //make output
PORTB.0=1               //and high
T1CON = %10000001       //pre=1
Tone=0                  //no sound please
TMR1IE=1                //enable timer 1 interrupt
T2CON = %01001110       //pre=16 post=10
PR2 = 249               //=8000000/4/16/10/250 = 50Hz
TMR2IE=1                //enable timer 2 interrupts
enable(MyInt)           //set interrupt going
SpeakerTris=0           //Setup Port    
Seed=$12345678          //seed random number
speed = 200             //changes via dip switches
USART.SetBaudrate(br9600)
    while TRUE
        for i = 1 to 5*60 	//play for 5 minutes
            while IntFlag=0
                Tone=Rand(NumTones)+1	//each tone is random frequency
                USART.Write(DecToStr(Tone),",")
                delayms(speed) 	        //and for 25-200 seconds here is where I want to change
            wend
            IntFlag=0
        next 			    //end for loop
        USART.Write(13,10,"Silence,")
        Tone=0 			    //silence
        Delay(10*60)        //delay 10 minutes
    WEND
end
I also added the UART code.

To add more tones change this number
Code:
const NumTones = 5
and add more frequencies to this line,
Code:
const Tones(NumTones+1) as word = (1000,2000000/12000,2000000/10000,2000000/8000,2000000/6000,2000000/4000)
Note, the first value is not a frequency and should not be changed.

One thing to keep in mind is that there is a maximum frequency that can be generated. I would guess that it is around 20kHz. To get higher frequencies would require a higher clock speed. With a 20MHz crystal it would manage around 50kHz.

Mike.
 
Last edited:
sounds like a good little device, shame it would be totaly illegal in the uk. over here Bats have some pretty heavy protection and messing with them in any way shape or form leads to some hefty fines! we have a couple of holiday log cabins on the farm that we rent out, one of them has some bats in the roof space and we had to stick notices all over the place in the cabin warning of the bats and penalties for messing with them. but then again i like Bats so i dont mind having them around
 
Thanks Pommie
I played with the Usart code just using an example from the HELP file using CONST.
finally got it working but inserting into the present code I got a 449 error. F12 said the syntax was good. I assume it was an overload?
I need to add a external clock I think?
QUESTION? why won't an 8mhz clock produce 20+ khz? The clock speed slowed down (BUSY) from processing other instructions?
 
thanks pommie
I started playing around with the bare essentials using the HELP file and CONST. Added the USART an LED .
got it working but then inserted into the actual app. F12 said syntax is good
compied and get an 449 error.
assume its an overload?
 
I am going to use that code.
I started playing around with the code I had by disecting etc to see what each part does etc.
This is before you posted the recent code.
I might have to change the clock to external 20mhz to get the 20kz + frequencies.
contemplating using the Pickit2 logic tool to determine if I am actually outputting the 20khz+ frequencies.
First I need to learn how to use it.
Thanks again. Your a great help as well as being very knowledgeable.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…