Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

1.25kHz 50% duty cycle wave from a PIC12F508

Status
Not open for further replies.

max0412

Full Member level 5
Full Member level 5
Joined
Oct 26, 2003
Messages
251
Helped
34
Reputation
66
Reaction score
5
Trophy points
1,298
Activity points
1,999
1.25khz oscillator

This is probably Amusingly simple but I'm just starting out with PIC's. So be kind.

Is it possible to generate a 1.25kHz 50% duty cycle wave from a PIC12F508. I come across examples where they use a NOP for 1uS intervals here.

Code:
PROCESSOR 12f508
   #include "p12f508.inc"
   __CONFIG  _MCLRE_OFF & _CP_OFF & _WDT_OFF & _XT_OSC
   #DEFINE PORT B'11111101'
	MOVF OSCCAL
	MOVLW PORT
	TRIS GPIO
		
   BEGIN
	BCF GPIO, 1     ;1uS
	NOP	        ;2uS each nop is 1uS long
	NOP		;3uS
	NOP		;4uS
	NOP		;5uS
	NOP		;6uS
	NOP		;7uS
	NOP		;8uS
	NOP		;9uS
	NOP		;10uS
	NOP		;11uS
	NOP		;12uS
	NOP		;13uS
	NOP		;14uS
	NOP		;15uS
	NOP		;16uS
	NOP		;17uS
	NOP		;18uS
	NOP	        ;19uS 
	BSF GPIO, 1 	;1uS Begin HIGH duty cycle
	NOP		;2uS
	NOP		;3uS
	NOP		;4uS
	NOP		;5uS
	GOTO BEGIN      ;2uS (26uS total for 38KHz)
	END

Is there a simple way to increase the NOP for my required waveform? Or is there an alternative way? I know this can be done with timers and logic but I’d prefer the PIC it lets me adjust the duty cycle with ease.
 

pic12f508

You can use macro language to ask MPASM to do it.
Code:
	RADIX DEC

#define fOSC 4000000

_waveC	macro	freq,duty,level
	variable tduty0,tduty1,tduty
tduty0 = (fOSC/4)/freq     
tduty1 = (tduty0*duty)/100
tduty0 -= tduty1
  if level
tduty = tduty1 
  else 
tduty = tduty0
  endif
  if tduty != 0 
    if (tduty & 1) != 0
      	NOP
    endif
tduty /= 2
    while tduty != 0
	GOTO	$+1	
tduty -= 1
    endw   
  endif
	endm
Then, use it to replace....
Code:
BEGIN
	BCF	GPIO,1
	_waveC	100000,50,0	;100K, 50% (0)
	BSF	GPIO,1
	_waveC	100000,50,1	;100K, 50% (1)
	GOTO	BEGIN
But the 1.25K is too slow for 1uS instruction time by code delay in 12F508.
To use 12F508's TMR0 to implement as:
Code:
#define nDefaultOPTION B'11000000'
	local	tTMR0,tduty0,tduty1
_setupT macro	freq,duty
	variable nPS
tTMR0 = (fOSC/4/2)/freq
nPS = 0
  while tTMR0 > 256
tTMR0 /= 2
nPS += 1
  endw
  if nPS < 8
	MOVLW	nDefaultOPTION+nPS
	OPTION
  else 
    error "ERROR: bad PS setting"
  endif 
tduty0 = tTMR0
tduty1 = (tduty0*duty)/100
tduty0 -= tduty1
	endm
_waveT	macro	level
	variable label
	variable tduty
  if level
tduty = tduty1 
  else 
tduty = tduty0
  endif
  if tduty > 0
	CLRF	TMR0
label = $
loop#v(label):
	MOVLW	tduty-2 ;*FIXME*
	SUBWF	TMR0,W
	BTFSS	STATUS,C
	GOTO	loop#v(label)
  endif  
	endm
Then, use it as:
Code:
	_setupT	1250,50		;1.25K, 50%
BEGIN
	BCF	GPIO,1
	_waveT	0		;1.25K, 50% (0)
	BSF	GPIO,1
	_waveT	1		;1.25K, 50% (1)
	GOTO	BEGIN
FYR
 

    max0412

    Points: 2
    Helpful Answer Positive Rating
12f508 using delay program

You can also use TMR0 with a prescaler of say 1:4 and every time it rolls over re-load it with 156 (adjust this value). That way TMR0 will roll over every 400us, which is half your required period. When it does, you toggle the GPIO bit for the output.
 

    max0412

    Points: 2
    Helpful Answer Positive Rating
pic12f508 osccal

Thanks for the help .

I just got the PIC DV164121 Development Tool and I'm muddling my way through the tutorials.

I'm placing an order this week ,now that I know it's possible to use the 12F508 I'll order a couple.

Could anyone recommend some good general purpose PIC's for learning the basics? The DV164121 board came with PIC16f887. What are the most widely used ones?

Thanks
 

tmr0 pic12f508

As much as I know, the most used ones are PIC12F508, PIC16F877A and PIC18F452.
But PIC16F887 is a good replacement for PIC16F877A! Basically is almost the same as PIC16F877A but it includes internal oscillator and has some minor differences in how to use its peripherals.
 

    max0412

    Points: 2
    Helpful Answer Positive Rating
tmr0 pic 12f508

I would say the 16F877(A) is the most widely used one. The 16F887 has some nice features compared with the 16F877, such as independently selectable analog inputs and weak pullups, but it lacks the parallel slave port. If that is important, go with the 877.
 

    max0412

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top