Help converting small PicBasic program to MP LAB


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: Help converting small PicBasic program to MP LAB

    I didn't think a 4MHz PIC would be fast enough to get a decent tone by toggling a pin in a loop, but I will definitely give it a try. The frequency is not critical, but I was looking for a nice smooth adjustable low to high tone. I have two of these PICs running and I need to be able to tell which one (Or if both) are alerting or not. The two sounds need to be heard clearly. Using the sound command is so choppy that it is hard to hear both sounds at the same time.

    Jim


    Quote Originally Posted by HenrikOlsson View Post
    What range of frequencies are you aiming to generate. I see from your SOUND statements that it would equal notes 50 to 113 but I don't know what that translates to in actual frequency (and are those numbers critical?

    Like Art says, all the SOUND command is doing is toggling a pin. You can do that too and you make the ADC do the conversion while waiting to flip the pin. Something like this perhaps (not tested):

    Code:
    SomeValue CON 250
    SomeConstant CON 4
    
    Main:
    IF Trigger THEN
      SPK = 1   ' Set pin high
    
      GO_DONE = 1
      WHILE GO_DONE : WEND   ' AD conversion will always take the same amount of time
      PAUSEUS SomeValue + (ADRES * SomeConstant)
    
      SPK = 0
    
      GO_DONE = 1
      WHILE GO_DONE : WEND   ' AD conversion will always take the same amount of time
      PAUSEUS SomeValue + (ADRES * SomeConstant)
    ENDIF
    Goto Main
    /Henrik.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Help converting small PicBasic program to MP LAB

    I didn't think a 4MHz PIC would be fast enough to get a decent tone by toggling a pin in a loop, but I will definitely give it a try.
    First, the SOUND command is toggling a pin in a loop, isn't it? Second, aren't you running the PIC at 8MHz? :-)

    Let's do some quick back of an envelope calculations. On the 10F222 doing an AD conversion takes 13 CPU cycles (this does NOT include the time needed to charge the S/H capacitor but you're not allowing for any sample time at the moment so lets ignore that). In my code example we're doing two conversions per loop so 26 cycles. The other bit checking/flipping operations is, perhaps, 20 cycles. Lets call 50 instructioncycles so without any PAUSEUS at all in there it should do around 40kHz.

    If you want the most out ouf it make sure you disable the WDT and tell the compiler about it, that will save a couple of cycles per loop.

    The PAUSEUS is obviously there to slow it down and you'll need to play with the values to get the frequency to where you want it. We're doing two conversions per loop to keep the symetry so it should produce fairly Close to 50% dutycycle.

    Give it a try and tell us how it works.

    /Henrik.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Help converting small PicBasic program to MP LAB

    I gave this a try, 10F222 at 8MHz. Generating roughly 300-3000Hz with the following code:
    Code:
    #CONFIG
       __config _IOFSCS_8MHZ & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    DEFINE NO_CLRWDT 1             ' Forces manual use of CLRWDT
    
    GPIO = %0000
    TRISIO = %00111101
    ADCON0   = %01000001	       ' GPIO 0 = Analog, ADC Enabled
    
    SPK VAR GPIO.1
    Trigger VAR GPIO.2
    
    GO_DONE  VAR  ADCON0.1
    
    SomeValue CON 75
    SomeConstant CON 12
    
    Main:
    IF Trigger THEN
      GO_DONE = 1
      SPK = 1                ' Set pin high
      WHILE GO_DONE : WEND   ' AD conversion will always take the same amount of time
      PAUSEUS SomeValue + (ADRES * SomeConstant)
    
      GO_DONE = 1
      SPK = 0                ' Set pin low
      WHILE GO_DONE : WEND   ' AD conversion will always take the same amount of time
      PAUSEUS 16 + SomeValue + (ADRES * SomeConstant)
      ' The 16 above is to make the dutycycle closer to 50% than what it was
      ' without it. I'm quite surprised such a high number was needed and I'm
      ' not sure why that is.
    ENDIF
    Goto Main
    Removing the PAUSEUS statements altoghether brings the frequency to 47kHz so there's plenty of margin.

    /Henrik.

  4. #4
    Join Date
    Feb 2012
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: Help converting small PicBasic program to MP LAB

    That worked Awesome! Thank you so much! I don’t know why I was thinking I was running at 4GHz, but even at 8GHz I didn’t think it would sound that good. Perfect!

    Jim




    Quote Originally Posted by HenrikOlsson View Post
    I gave this a try, 10F222 at 8MHz. Generating roughly 300-3000Hz with the following code:
    Code:
    #CONFIG
       __config _IOFSCS_8MHZ & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    DEFINE NO_CLRWDT 1             ' Forces manual use of CLRWDT
    
    GPIO = %0000
    TRISIO = %00111101
    ADCON0   = %01000001	       ' GPIO 0 = Analog, ADC Enabled
    
    SPK VAR GPIO.1
    Trigger VAR GPIO.2
    
    GO_DONE  VAR  ADCON0.1
    
    SomeValue CON 75
    SomeConstant CON 12
    
    Main:
    IF Trigger THEN
      GO_DONE = 1
      SPK = 1                ' Set pin high
      WHILE GO_DONE : WEND   ' AD conversion will always take the same amount of time
      PAUSEUS SomeValue + (ADRES * SomeConstant)
    
      GO_DONE = 1
      SPK = 0                ' Set pin low
      WHILE GO_DONE : WEND   ' AD conversion will always take the same amount of time
      PAUSEUS 16 + SomeValue + (ADRES * SomeConstant)
      ' The 16 above is to make the dutycycle closer to 50% than what it was
      ' without it. I'm quite surprised such a high number was needed and I'm
      ' not sure why that is.
    ENDIF
    Goto Main
    Removing the PAUSEUS statements altoghether brings the frequency to 47kHz so there's plenty of margin.

    /Henrik.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Help converting small PicBasic program to MP LAB

    You're welcome, glad I could help!
    I would love to play around with a PIC running at 4GHz, let alone 8 :-)

    /Henrik.

  6. #6
    Join Date
    Feb 2012
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: Help converting small PicBasic program to MP LAB

    Hello Henrik,

    I am trying to send you a private message, but I keep getting logged off each time I attempt to. Could you send me a private message so that I can see if reply works? Thanks,

    Jim

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Help converting small PicBasic program to MP LAB

    Oh, I'm getting them Jim, all of them (I think) :-)

    /Henrik.

Similar Threads

  1. Multi servo interrupt program in Mikro Basic converting to PBP
    By queenidog in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd May 2015, 17:17
  2. Converting CLEAR in Proton basic to picbasic pro
    By Nobby123 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th October 2013, 09:19
  3. help converting assembely program
    By ahmed_salah in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 7th August 2008, 15:28
  4. Making the Lab X1 communicate with the Lab X2
    By MatthewM in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 19th June 2008, 22:26
  5. 1st 16F877A program in PICBasic
    By Borisw37 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th January 2005, 04:46

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts