12F683 - how to set it to use external 4 MHz clock?


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62


    Did you find this post helpful? Yes | No

    Default

    Hi again,

    Ok, not the most elegant piece of code, but it works. Basically this allows you to instruct the 12F683 to output a regular pulse at a desired frequency or duty cycle on pin 5. You can communicate with the chip via a serial com port [I'm using a USB to RS232 converter].

    Personally, I establish a connection with hyperterminal that comes with XP.
    Press 'f' to set frequency [press enter after you type a value in Hz]
    Press 'f' to set the dutycycle (0 - 100%) [+enter] - the program will auto scale it 0 - 255.

    Or alternatively, type 'f?' or 'd?' to determine the current set dutycycle of frequency.

    Note: There is little in the way of error checking on the input. I just wanted to get this back to you guys to show what I'd done and that I was grateful for your help last Friday. Now that I have reliable 2-way com's on the serial bus, I'll set it up with a nice little application in MatLab.

    Best regards, J.

    P.S. One thing I couldn't sort out was clearing the screen at the start in hyperterminal from the serial port. People say to use Esc/[2J or something but I can't get it to work!!!


    Include "modedefs.bas"
    ANSEL=0
    CMCON0 = %00000111
    define osc 4

    Serial_out var gpio.0
    Serial_in var gpio.1
    command var byte
    input_char var byte
    pulse_width var word
    pulse_width_percent var byte
    pulse_width_percent_new var byte
    pulse_frequency var word
    pulse_frequency_new var word
    variable_output var word

    pulse_width = 128
    pulse_width_percent = 50
    pulse_frequency = 20000

    pause 100
    serout Serial_out,T9600,[13]
    pause 100
    serout Serial_out,T9600,[13," "]
    pause 100
    SEROUT Serial_out,T9600,[13,"This is a test",13,10]
    SEROUT Serial_out,T9600,[13,"f for frequency",13,10]
    SEROUT Serial_out,T9600,[13,"d for dutecycle (0-100)",13,10]
    SEROUT Serial_out,T9600,[13,"Press ? after f/d to find current value",13,10]

    mainloop:

    Serin Serial_in,T9600,command

    If (command = 100) then
    SEROUT Serial_out,T9600,["Enter duty cycle : "]
    input_char = 0
    pulse_width_percent_new = 0
    while input_char<10
    pulse_width_percent_new = (pulse_width_percent_new * 10) + input_char
    gosub getchar
    wend
    if input_char <> 15 then
    pulse_width = pulse_width_percent_new * 256 / 100
    hpwm 1,pulse_width,pulse_frequency
    SEROUT Serial_out,T9600,[10,13]
    else
    SEROUT Serial_out,T9600,[#pulse_width_percent,10,13]
    endif
    pulse_width_percent = pulse_width_percent_new
    endif

    If (command = 102) then
    SEROUT Serial_out,T9600,["Enter frequency cycle : "]
    pulse_frequency_new = 0
    input_char = 0
    while input_char<10
    pulse_frequency_new = (pulse_frequency_new * 10) + input_char
    gosub getchar
    wend
    if input_char <> 15 then
    pulse_frequency = pulse_frequency_new
    hpwm 1,pulse_width,pulse_frequency_new
    SEROUT Serial_out,T9600,[10,13]
    else
    SEROUT Serial_out,T9600,[#pulse_frequency,10,13]
    endif
    endif

    Goto mainloop

    getchar:
    serin Serial_in,T9600,input_char
    input_char = input_char - 48
    if input_char < 10 then
    serout Serial_out,T9600,[48+input_char]
    endif
    return

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    COOL! Glad you got it working!

    I do use hyperterminal much so I do not know for sure the command but here is something from way back. Looks like it says almost the same as you are trying???
    http://list.picbasic.com/forum/messa...tml?1054384602
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Sep 2009
    Location
    South Wales (UK)
    Posts
    62


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Thanks for your email. I saw that webpage before but couldn't get it to work. However, the problem was just familiarity with picbasic/this whole setup! I simply looked up the ascii codes and send them as you would expect, and it 'does' work

    serout Serial_out,T9600,[27,91,50,74]

    I'm just putting some error checking into the routine, to try and make it at least a little fool proof. It just feels so good now that I have comm's with the PIC. Feel like I've got over a full hurdle. Reliable comm's back and forth is so important to allow one to develop this stuff!

    I'll not add any further questions to this thread as I'm going off topic with my next issue. I'll see how it goes and then if desperation calls, I'll call you! I want to try to have 3 pic's all reading off the same RS232 RX line[and TXing on the same line]. Each PIC will be programmed separately with a unique identifier and will only address the TX line if it received data relevant to itself. But, I don't want to cover the board with 4 MHz xtals so I'm going to try to run 3 of them off the one xtal! Not sure how possible that is, but if it works, then great :P

    Anyway,

    Thanks to you and all for your help in getting me started on this PIC stuff. Feel like I've just been given my first car and have the freedom to go anywhere!

    J.

Similar Threads

  1. Interruptus Frustratus
    By Byte_Butcher in forum General
    Replies: 16
    Last Post: - 17th April 2009, 20:36
  2. Using SPI with External Interrupts
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th June 2008, 04:08
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31
  5. How to set external clock source in PBP
    By TurboLS in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 19th February 2005, 15:56

Members who have read this thread : 0

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

Posting Permissions

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