PDA

View Full Version : splitting output from a counter



astouffer
- 23rd September 2009, 21:25
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.


@ __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?

LinkMTech
- 24th September 2009, 05:09
Just a few things to start off with:


@ __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.

Archangel
- 24th September 2009, 05:12
Hi astouffer,
execute a subroutine . . .



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.

astouffer
- 24th September 2009, 21:21
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.



@ __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