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.

Help with pic to generate frequency

Status
Not open for further replies.
Nobody will just hand you the code, but you might find the following site useful. I know I did. MicroE
 
Following is the code I have written to generate 38kHz freq for IR detection.

Code:
;Brain for Robot 4 - Pradeep K. Shima
;scratch base ripped from Tutorial 1.2 by Nigel Goodwin 2002
 LIST p=16F628  ;tell assembler what chip we are using
 include "P16F628.inc" ;include the defaults for the chip
 __config 0x3D38   ;sets the configuration settings (oscillator type etc.)
 cblock  0x20    ;start of general purpose registers
  count1     ;used in delay routine
  counta     ;used in delay routine 
  countb     ;used in delay routine
 endc
 
 org 0x0000    ;org sets the origin, 0x0000 for the 16F628,
       ;this is where the program starts running 
 movlw 0x07
 movwf CMCON   ;turn comparators off (make it like a 16F84)
    bsf  STATUS,  RP0 ;select bank 1
    movlw  b'00000000'  ;set PortB all outputs
    movwf  TRISB
 movwf TRISA   ;set PortA all outputs
 movlw d'25'
 movwf PR2    ;PWM with freq~=38kHz (see [URL="https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html"]https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html[/URL])
 bcf  STATUS,  RP0 ;select bank 0
 movlw d'5'   ;duty ratio = 20%
 movwf CCPR1L
 movlw d'4'   ;Timer2 ON + 1:1 prescale
 movwf T2CON   
 movlw d'12'   ;set CCP module to PWM mode
 movwf CCP1CON

Main
 movlw 0x00
 movwf PORTA   ;set all bits on
 movwf PORTB
 call Delay   ;this waits for a while!
 movlw 0x00
 movwf PORTA
 movwf PORTB   ;set all bits off
 call Delay
 goto Main   ;go back and do it again
 
 
 
 

Delay movlw d'250'   ;delay 250 ms (4 MHz clock)
 movwf count1
d1 movlw 0xC7
 movwf counta
 movlw 0x01
 movwf countb
Delay_0
 decfsz counta, f
 goto $+2
 decfsz countb, f
 goto Delay_0
 decfsz count1 ,f
 goto d1
 retlw 0x00
 end
 
First drop that crystal value and get a nice and simple 4, 8, 10, or 20 MHZ because it makes all the difference for code calculation. Its easier to predict and calculate even multiples as opposed to 4.xxx and 8.xxx mhz.

I won't even comment on the F84.

As for the frequencies, they are simple delays and loops like the code below.
You, you the designer/developer/programmer must on you own decide if you want to allways stay inside a main loop, inside the actual delay loops, or inside some sort of interrupt loop. Trust me it made no difference what you choose. What matters is that once you choose a method you must augment the delays and loops to compensate for the over head of processing time. Learning how to compensate for the over head of processing time is the dogma.


Code:
MAIN:
	CALL	_100KHZ
	GOTO	$ 

;**********END 0F MAIN******************

_100KHZ:
	BSF	F_OUT
	NOP
	NOP
	BCF	F_OUT
	NOP
	GOTO	_100KHZ
	RETURN
;---------------------------------------
_90KHZ:
	BSF	F_OUT
	NOP
	NOP
	BCF	F_OUT
	NOP
	NOP
	GOTO	_90KHZ
	RETURN
;---------------------------------------
_80KHZ:	BSF	F_OUT
	NOP
	NOP
	NOP
	NOP
	BCF	F_OUT
	NOP
	NOP
	NOP
	NOP
	GOTO	_80KHZ
	RETURN
;---------------------------------------



Use 4, 8, 10, or 20 MHZ.
 
Last edited:
Stick to 4.864MHz crystal if you want exact 19KHz and 38KHz, which should be within +/-20Hz by the FM standard. Other required frequencies are off.

I don't know where the other frequencies come from but you can't have exact frequency output using a PIC for all of them. The best one can do is to use a 4.864x3=14.592MHz crystal, which gives exact frequencies for 19,38,57,& 114KHz, except 95KHz(either 93.5KHz or 96.0KHz) and 133KHz(either 130.3KHz or 135.1KHz).

The use of "nice" crystal frequencies like 4,8,16 or 20MHz with PIC won't give you exact frequency and you will be off by a fair amount on all required frequencies.

An AVR would do the job a bit better in this case.
 
15.96MHz should give you what you need for all the frequencies you mentioned. Nest all the bit toggles in a 210 instruction cycle loop. You may have trouble with 38k and 114k as they will have odd lengths, thus unequal on and off times. You may also have trouble with mapping some of the toggles to not interfere with others.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top