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.

40khz signal from 12f675

Status
Not open for further replies.

cybersky

Member
Hi
I am trying to build a very simple ultrasonic range finder. I have 12f675 on hand and wanted to use them but they lack CCP so i cannot use pwm. However as i understand the pwm would also only be able to manage 20Khz and not the 40Khz I require. I also wanted to use the FreqOut function but that also only does 10Khz Max. Please gimme some direction as i would love to remove the 555 I am using at the moment. I want something will not require "calibration" and should have super low (like 1) part count.
 
you can use inline assembler to create the 40kHz signal in software. There are plenty of headroom to get 40kHz when the pic12f675 is running at 4MHz.
 
hi cyber.

How do plan to operate the ultrasound range finder transducer, being used with 40KHz PWM drive signal.?

Eric
 
Hi
I am trying to build a very simple ultrasonic range finder. I have 12f675 on hand and wanted to use them but they lack CCP so i cannot use pwm. However as i understand the pwm would also only be able to manage 20Khz and not the 40Khz I require. I also wanted to use the FreqOut function but that also only does 10Khz Max. Please gimme some direction as i would love to remove the 555 I am using at the moment. I want something will not require "calibration" and should have super low (like 1) part count.

Maybe you need to choose a microcontroller that can do what you need. I am not familiar with the 12f675, but if everything you said is correct, then you have a wrong part for the job. But, it sounds strange that you can not figure out how to produce 40Khz signal.. 40Khz is not really very high frequency.
 
I too am puzzled as to why you want to use PWM :confused:
 
I too am puzzled as to why you want to use PWM :confused:

Well.. with microcontrollers, when you need to produce a signal at certain frequency, or pwm.. it is the same module (peripheral). "PWM" is used in this context to refer to that peripheral. Of course the OP only needs 40Khz 50% duty square wave.. no modulation needed.
 
Well.. with microcontrollers, when you need to produce a signal at certain frequency, or pwm.. it is the same module (peripheral). "PWM" is used in this context to refer to that peripheral. Of course the OP only needs 40Khz 50% duty square wave.. no modulation needed.

hi T,

The modulation I would define as the Gating of the 40KHz 'burst' of say 10 cycles ever few millisecs.

This could easily be achieved by pulsing any output pin of the MCU at 40KHz at the the required burst interval.
The pin signal will need to be amplified and made as push/pull in order to drive the transducer 'hard' enough.

E.
 
hi T,

The modulation I would define as the Gating of the 40KHz 'burst' of say 10 cycles ever few millisecs.

This could easily be achieved by pulsing any output pin of the MCU at 40KHz at the the required burst interval.
The pin signal will need to be amplified and made as push/pull in order to drive the transducer 'hard' enough.

E.

This is going into strange discussion about things we all know.. I am still wondering what the OPs real problem is. Producing those 40Khz bursts should not be a problem. Maybe he is a beginner with PICs..
 
This is going into strange discussion about things we all know.. I am still wondering what the OPs real problem is. Producing those 40Khz bursts should not be a problem. Maybe he is a beginner with PICs..

Lets hope he comes back with more info' , as you say we could speculate all day...:rolleyes:

E
 
Lets hope he comes back with more info' , as you say we could speculate all day...:rolleyes:

E

Yes.. simple software loop with correct timing should not be hard to do. Many times the problem is how to get the timing right.. and how to verify that the output is correct, because many hobbyists do not have proper tools to measure and test the system. Oscilloscope and a logic analyzer are very important tools when building embedded systems.
 
Hi,

The 12F675 doesnt have a PWM peripheral does it? So it has to be done in software ie bit banged.
 
Hi MrT
1) I am fairly new to PIC. I am do software dev.
2) I guess i got caught up in the "lazyness" of using a ready made function - I should have seen that a simple pin toggle could do the job. But I wnted to be sure that whatever i used would be accurate / correct because i have no way of verifying.
3) I did not want to use PWM it is just the example everyone uses. - I dont do assembler so that is not an option.

But now as you pointed out - how do you get the timing right if you cant use a scope to check. sometimes simple said is not simply done.
And why be critical of why someone is struggling? I only assked for some input, not for anyone to DO my work. These forums used to be about sharing ideas and experience, and yet nowadays it seems more about who wants the most recognition.
I am sorry for the rant. Please do accept my thanks I am grateful that out of all of this atleast I was given a different "angle" to persue.
 
Hi MrT
1) I am fairly new to PIC. I am do software dev.
2) I guess i got caught up in the "lazyness" of using a ready made function - I should have seen that a simple pin toggle could do the job. But I wnted to be sure that whatever i used would be accurate / correct because i have no way of verifying.
3) I did not want to use PWM it is just the example everyone uses. - I dont do assembler so that is not an option.

You need to learn assembler then, it's quite easy to do - and it's FAR easier(and more accurate) to generate your 40KHz signal by bit-banging than by using PWM.

There are assembler examples for IR remote control in my tutorials, and I also have a 'robot radar' on another website with a 12C509, which I built for Cybot - although the code isn't mine.
 
Hi MrT
But now as you pointed out - how do you get the timing right if you cant use a scope to check.

Confidence, experience, read the datasheet, cross your fingers and pray to the god you believe in.
 
Hi MrT
1) I am fairly new to PIC. I am do software dev.
2) I guess i got caught up in the "lazyness" of using a ready made function - I should have seen that a simple pin toggle could do the job. But I wnted to be sure that whatever i used would be accurate / correct because i have no way of verifying.
3) I did not want to use PWM it is just the example everyone uses. - I dont do assembler so that is not an option.

But now as you pointed out - how do you get the timing right if you cant use a scope to check. sometimes simple said is not simply done.

To get the timing right you need to look the assembler listing of the compiled Basic-code and count the instruction cycles or use the Oshonsoft simulator, which gives used time in microseconds, for example between breakpoints.
One instruction cycle/us = 4/clockfrequency(Mhz )
For 40Khz output frequency, cycle time is 25us.
For-Next loop takes 9 instruction cycles.
In Oshonsoft-Basic the delay Waitus is not accurate.

This should work for 4Mhz clockfrequency:
Code:
' PIC12F675
' Output pin GPIO.0
' Needs the I/O and Config initialisation
' 10 pulses at 40Khz
'
Dim I as byte
For i= 1 to 10
GPIO.0=1
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop
ASM:        nop

GPIO.0 = 0
ASM:        nop
ASM:        nop
ASM:        nop
Next i

Btw, you said you are now using 555 for generating 40Khz.
How do you know without oscilloscope, that the timing is right ?
 
Some of these discussions almost sound like that song "if i had a hammer...".

Why would you not use the timers provided in the PIC? That is what they're there for. I was pretty sure I had a blog entry somewhere on this, but I'm obviously struggling to find it in the new forum. In any case, below is the code for talking with the PING (from Parallax). It is using a 12F683 which is a newer version of the 12F675.
Code:
'Author: languer (©2013)
'Pin Allocation:
    'PIN# Main_Fn        Secondary_Fn
    'GP0 -> OUT_RS232    ICSP-DAT
    'GP1 -> N/A          ICSP-CLK
    'GP2 -> N/A
    'GP3 -> N/A          MCLR
    'GP4 -> IO_SONAR
    'GP5 -> N/A
'Usage Information:
    'RS232 Baud Rate: 9600bps
    'Comments: PING only works with +5V.
    'To use lower voltage, recommend using step-up regulator (e.g NCP1402)
    'The SONAR principle determines the distance to an object based on the
    'speed of sound(1_inch = 73.746_usec).If a pulse is sent, its distance (in inches)
    'to target will be; d_in = (t_usec / 73.746)
    'SONAR measures the return pulse, time pulse travels to target and back to the SONAR
    'Or d_in = (t_usec / 147.492)
'General Configuration
    Define CONF_WORD = 0x33c4
'Oscillator/Clock Configuration for internal 8MHz osc
    Define CLOCK_FREQUENCY = 8
    OSCCON = 0x71
'Variable Declarations
'>>I/O
    Symbol io_rs232_tx = GP0  'rs-232 output
    Symbol io_sonar = GP4  'ping/hc-sr04 sensor
'>>Constants
    Const _trisio = %11111110
'>>Variables
    Dim flag_valid_dist As Bit
    Dim _true As Bit
    Dim _false As Bit
    _true = True
    _false = False
'Main Program
main:
    Dim distance_in As Word  'distance in inches
    Call init()
    WaitMs 1000
    Serout io_rs232_tx, 9600, CrLf, "Main..."
    While _true
        flag_valid_dist = False
        distance_in = get_distance()
        If flag_valid_dist = _true Then
            Serout io_rs232_tx, 9600, CrLf, "Distance=", #distance_in, "_in"
        Else
            Serout io_rs232_tx, 9600, CrLf, "Invalid Measurement"
        Endif
        flag_valid_dist = False
        WaitMs 1000
    Wend
End                                             
'init
Proc init()
    AllDigital
    TRISIO = _trisio
    'set TMR1 prescaler to 1:2 -> with 8MHz clock
    'this results in tmr1 resolution of 1uS, with maximum of 65.53ms
    'or 0.007in resolution, with maximum of 442inches
    T1CON.T1CKPS1 = 0
    T1CON.T1CKPS0 = 1
    'clear global variables
    flag_valid_dist = False
    Low io_rs232_tx
    Low io_sonar
    Serout io_rs232_tx, 9600, CrLf, "Power-up..."
End Proc                                         
'get distance procedure
Function get_distance() As Word
    Dim echo_time As Word
    'transmit pulse
        Config io_sonar = Output
        High io_sonar
        WaitUs 10
        Low io_sonar
    'receive echo time
        'zero-out timer1
        TMR1L = 0
        TMR1H = 0
        PIR1.T1IF = 0
        'allow for some holdoff time
        WaitUs 200  '8-cycles of 40kHz is 200us, PING recommends 750us
        'wait for rising edge of echo pulse
        Config io_sonar = Input
        While io_sonar = 0
        Wend
        'turn-on timer1
        T1CON.TMR1ON = True
        'wait for falling edge of echo pulse or timeout
        While io_sonar = 1 And PIR1.T1IF = 0
        Wend
        T1CON.TMR1ON = False
        If PIR1.T1IF Then
            echo_time = 0
        Else
            echo_time.HB = TMR1H
            echo_time.LB = TMR1L
            flag_valid_dist = True  'flag a valid distance was measured
        Endif
        'zero-out timer1
        TMR1L = 0
        TMR1H = 0
        PIR1.T1IF = 0
        'return value
        get_distance = echo_time / 148
End Function
 
<code>
Function get_distance() As Word
Dim echo_time As Word
'transmit pulse
Config io_sonar = Output
High io_sonar
WaitUs 10
Low io_sonar
'receive echo time
</code>

Hi Languer
Thanks for the example code - but I only see you toggle the pin once for a duration of 10us then off. I cant seem to understand how this is a 40khz pulse?
sorry its been a while since I checked in, I was just swamped with other projects.
 
I do not use Oshonsoft but what I would do is setup the output using a timer and then verify it with the
Oshonsoft simulator which has a scope and I think a freq counter. If you don't know how to do this start by studying timers in the data sheet for your processor. Also the doc for the compiler/simulator.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top