splitting output from a counter


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    41

    Default splitting output from a counter

    Hello all, I'm trying to figure out a way to have a 12F683 take a variable frequency square wave in and generate a pulse train out from two pins. The input would never go above 3Khz and the output would look like this:


    pin3-pin2-pin2-pin2-pin2-pin3-pin2-pin2-pin2-pin2-pin3

    One pulse from pin 3 and then four pulses from pin 2 all depending on the incoming frequency. The code so far doesn't seem to work very well.

    Code:
    @	__CONFIG _INTOSCIO & _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _BOD_OFF & _CP_OFF & _MCLRE_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF
    
    DEFINE OSC 8           ' Internal 8MHz
    'DEFINE ADC_BITS 8      ' 8-bit resolution
    'DEFINE ADC_CLOCK 2     ' Set clock source to Frc/32
    'DEFINE ADC_SAMPLEUS 50 ' Sample time in uS before A/D conversion is started
    
    CMCON0 = 7   			'Comparators off
    ADCON0 = %00000000		
    ANSEL = %00000000		
    INTCON = 0			'INTERRUPTS off
    OSCCON = %01110000		'8 Mhz
    TRISIO = %000100		
    GPIO = %00000100 		
    OPTION_REG = %00101000	
    
    A	VAR	byte
    
    
    
    TMR0= 0      ' clear count
    low GPIO.4
    low GPIO.5
    input GPIO.2
    A=0
    
    main:
    while TMR0 <= 5
    wend
    TMR0= 0      ' clear count 
    A= A+1
    if A = 4 then largepulse
    pulsout GPIO.5, 100		'500 uS short pulses pin 2
    gosub main
    
    
    largepulse:
    pulsout GPIO.4, 100		'500 uS big pulse pin 3
    A=0
    return
    Am I way off here or on the right track?

  2. #2
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default

    Just a few things to start off with:
    Code:
    @	__CONFIG _INTOSCIO & _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _BOD_OFF & _CP_OFF & _MCLRE_ON & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF
    Choose one 
    
    DEFINE OSC 8           ' Internal 8MHz
    'DEFINE ADC_BITS 8      ' 8-bit resolution
    'DEFINE ADC_CLOCK 2     ' Set clock source to Frc/32
    'DEFINE ADC_SAMPLEUS 50 ' Sample time in uS before A/D conversion is started
    
    CMCON0 = 7   			'Comparators off
    ADCON0 = %00000000		
    ANSEL = %00000000		
    INTCON = 0			'INTERRUPTS off
    OSCCON = %01110000		'8 Mhz
    TRISIO = %000100		
    GPIO = %00000100 		
    OPTION_REG = %00101000	
    
    A	VAR	byte
    
    
    
    TMR0= 0      ' clear count
    low GPIO.4
    low GPIO.5
    input GPIO.2
    A=0
    
    main: 'You'll probably want to "see" what's happening on GPIO.2 once you get this part done I reckon
    while TMR0 <= 5
    wend
    TMR0= 0      ' clear count 
    A= A+1
    pulsout GPIO.5, 100		'500 uS short pulses pin 2
    if A = 4 then GOSUB largepulse   ' lets the four pulses go first then jumps to "large pulse"
    GOTO main   ' GOTO here instead
    
    
    largepulse:
    pulsout GPIO.4, 100		'500 uS big pulse pin 3
    A=0
    return
    There's probably more but that's what I see for now.
    Louie

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi astouffer,
    execute a subroutine . . .

    Code:
    counter var byte
    . . . . . your code . . . . 
    gosub goesout
    . . . . more code . . . . 
    goesout:
    Pulseout 10, pin3
    for counter = 0 to 3
    pulseout 10, pin2
    next counter
    return
    Please forgive if the pulseout is wrong as I am not home and have no manual here . . .
    But you get the Idea.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    Nov 2008
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    Ok we finally got the code working. This picture explains it much better. The yellow trace is a 1.5Khz square wave. The purple and green traces are the resulting outputs.


    Code:
    @	__CONFIG _INTOSCIO & _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _BOD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF
    
    DEFINE OSC 8           ' Internal 8MHz
    
    CMCON0 = 7   			'Comparators off
    ADCON0 = %00000000		'ADC enabled and right justified
    ANSEL = %00000000		'GPIO.0 and GPIO.1 analog input
    INTCON = 0				'INTERRUPTS off
    OSCCON = %01110000		'8 Mhz
    TRISIO = %000100		'GPIO2 input
    GPIO = %00000100 		'All outputs = 0 on boot
    OPTION_REG = %00101000	'
    INCLUDE "Modedefs.bas"
    
    
    
    A	VAR	word
    A=0
    TMR0= 0      						
    input GPIO.2						
    main:
    while TMR0 <= 5
    wend
    A = A+1
    TMR0= 0
    if A <> 5 then pulsout GPIO.5, 400		
    if A=5 then onepulse
    'TMR0= 0  								 pause		     						
    gosub main
    
    onepulse:
    TMR0= 0
    pulsout GPIO.4, 400						
    A=0
    gosub main
    Attached Images Attached Images  

Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 06:14
  2. 20 Digit Virtual LED Counter
    By T.Jackson in forum Code Examples
    Replies: 9
    Last Post: - 19th November 2007, 05:02
  3. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  4. Serious Serial Situation Setbacks...
    By Dansdog in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th February 2007, 03:46
  5. HSEROUT Newbie question/problem
    By Mark Scotford in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 11th July 2006, 14:44

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