what am I doin' wrong?

gerty

Member
I've pulled out what's left of my hair, the goal is to generate a tone for a pa system . What I have is a 24-7 time clock, the contacts close for 1 minute minimum, and I need to cycle a tone for about 2 seconds. A simple task for a pic you say!! Well I've got the tone, but cannot figure how to make it cycle. what I'm using is a MikroE Easy PIC4 developement board and I used a sample program form them (basic). The sample had several tones,I only needed one, so I removed the rest. What it does:upon programming the tone sounds for 2 seconds and stops (good so far) when I press the button for input at porta.0 ,nothing happens. If I press the reset (mclr) button, it sounds for 2 seconds.
This is my first foray into programming, except for the old allen bradley PLC-2.
Here's the code,can someone help?
 

Attachments

Code:
program Chimes

sub procedure Tone1
  Sound_Play(130, 6200)
end sub
sub procedure Melody
  Tone1
end sub

main:

  TRISA = 1
  Sound_Init(PORTB,2)   ' Initialize sound engine

  IF PORTA.0 = 1 THEN        '   if input at porta.0 goes high
  PORTB.2 = true              ' output at portb.2 turns on
     END IF
  DELAY_MS(2000)           'play for 2 seconds
   Melody                ' Play the chimes

end.

How about instead of PORTB.2 = true
TRISB.2 = 0
PORTB.2 = 1
 
Last edited:
also when you press the button what do you want to happen? All its going to do is set the PORTB.2 to output.

Also dont you need a endless loop?
example:
Code:
program Chimes

sub procedure Tone1
  Sound_Play(130, 6200)
end sub
sub procedure Melody
  Tone1
end sub


main:
  TRISA = 1
  TRISB.2 = 0	

  Sound_Init(PORTB,2)   ' Initialize sound engine

  IF PORTA.0 = 1 THEN        '   if input at porta.0 goes high

  PORTB.2 = 1              ' output at portb.2 turns on
     END IF
  DELAY_MS(2000)           'play for 2 seconds
   Melody                ' Play the chimes
   goto main
end.

or

Code:
program Chimes

sub procedure Tone1
  Sound_Play(130, 6200)
end sub
sub procedure Melody
  Tone1
end sub


main:
  TRISA = 1
  TRISB.2 = 0	

  Sound_Init(PORTB,2)   ' Initialize sound engine

e_loop:
  IF PORTA.0 = 1 THEN        '   if input at porta.0 goes high

  PORTB.2 = 1              ' output at portb.2 turns on
     END IF
  DELAY_MS(2000)           'play for 2 seconds
   Melody                ' Play the chimes
   goto e_loop
end.
 
Last edited:
I tried both of them..no effect.
How do you post the code like that?

Yes I need it to loop for 2 seconds.I would like it to put out a tone (2 sec) when button pressed.
 
Last edited:
the # button or use the [ code ] [/ code] syntax

Refresh the page and check new code. I need to see the rest of the code to help ya out. How are you sending a tone to begin with?

Try this:
Code:
program Chimes

sub procedure Tone1
  Sound_Play(130, 6200)
end sub
sub procedure Melody
  Tone1
end sub


main:
  TRISA = 1
  TRISB.2 = 0	

  Sound_Init(PORTB,2)   ' Initialize sound engine

e_loop:
  IF PORTA.0 = 1 THEN        '   if input at porta.0 goes high
    PORTB.2 = 1              ' output at portb.2 turns on
    Melody                ' Play the chimes
    DELAY_MS(2000)           'play for 2 seconds
    PORTB.2 = 0              ' output at portb.2 turns on
  END IF

   goto e_loop
end.
 
That is all the code. clock speed is 8 mhz. Like I said I used an example program from mikroe and removed 4 extra tones I didn't want. I'm just now trying to get this programmed, so there isn't a schematic. If you're not famalliar the developement board has a pushbutton for every input and a led for every output (when programmed as such) . there is a header to connect to the 'real world' and I've got a amp/speaker connected see attached:
I don't know what the code is supposed to look like (herein lies the problem!).

almost forgot! tried changes and tone cycles on/off every 2 seconds,all by itself
 

Attachments

  • easypic4.GIF
    111.8 KB · Views: 218
Last edited:
Put JP17 to GND and try the code again if doesnt work try JP17 to VCC and use
Code:
program Chimes

sub procedure Tone1
  Sound_Play(130, 6200)
end sub
sub procedure Melody
  Tone1
end sub


main:
  TRISA = 1
  TRISB.2 = 0	

  Sound_Init(PORTB,2)   ' Initialize sound engine

e_loop:
  IF PORTA.0 = 0 THEN        '   if input at porta.0 goes high
    PORTB.2 = 1              ' output at portb.2 turns on
    Melody                ' Play the chimes
    DELAY_MS(2000)           'play for 2 seconds
    PORTB.2 = 0              ' output at portb.2 turns on
  END IF

   goto e_loop
end.
 
Last edited:
in there code they check a button like:

Code:
    if Button(PORTB,2,1,1)=true then          ' RB2 plays Melody
      Melody()
    end if

so try :
Code:
program Chimes

sub procedure Tone1
  Sound_Play(130, 6200)
end sub
sub procedure Melody
  Tone1
end sub


main:
  TRISA = 1
  TRISB.2 = 0	

  Sound_Init(PORTB,2)   ' Initialize sound engine

e_loop:
    if Button(PORTA,0,1,1)=true then       '   if input at porta.0 goes high
    PORTB.2 = 1              ' output at portb.2 turns on
    Melody                ' Play the chimes
    DELAY_MS(2000)           'play for 2 seconds
    PORTB.2 = 0              ' output at portb.2 turns on
  END IF

   goto e_loop
end.
 
Last edited:
Philba: watchdog is off
Atomsoft: tried new code and now it doesn't do a thing. Here is the original code from Mikroe that I modified. Did I do/not do something really stupid?


Code:
 '******************************************************************************
' Project: Sound
'
' This project is a simple demonstration of how to
' use sound library for playing tones on a piezo speaker.
' The code can be used with any MCU that has PORTB.
'******************************************************************************
program Chimes

sub procedure Tone1
  Sound_Play(70, 150)
end sub

sub procedure Tone2
  Sound_Play(60, 150)
end sub

sub procedure Tone3
  Sound_Play(50, 200)
end sub

sub procedure Melody
  Tone1
  Tone2
  Tone3
end sub

main:
  Sound_Init(PORTB,2)   ' Initialize sound engine
  Melody                ' Play the chimes
end.
 
Make sure its pull up meaning when nothing is pressed its at a logical 1 and when press it jumps to 0v.

Code:
program Chimes

sub procedure Tone1
  Sound_Play(130, 6200)     ' The 6200 controls the tone length
end sub

sub procedure Melody
  Tone1
end sub


main:
  TRISA = 0xFF

  Sound_Init(PORTB,2)   ' Initialize sound engine

e_loop:
  IF PORTA.0 = 0 THEN        '   if input at porta.0 goes low
    Melody                   ' Play the chimes
    DELAY_MS(2000)           'This just delays 2 seconds not play
  END IF

   goto e_loop
end.
 
Pullup is working (10K) and when button pressed it goes low. Still no tone, I can see I have a lot to learn. The terminology is killing me more than anything. I'll try more in the morning.
Thanks a lot for all your help, really appreciated.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…