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.

PIC16F819 default oscillation frequency of internal oscillator?

Status
Not open for further replies.
Hi D@nny,

Producing sinusoidal waveforms with PWM is an effective technique, using a PIC 16F to generate the PWM limits the effective frequency of the sine wave to a just few hundred Hertz due to clocking limitations. Generation of 50Hz is more that doable the appnote below details the calculation necessary to implement an efficient and effective design as you can see from the example source code these PWM Sinusoidal Generators use a lookup table which you should be familiar with at this point. The example also uses a 2-pole low pass RC filter, which allows the fundamental harmonic to pass while effectively filtering any higher frequency harmonics.



MicroChip Appnote:

D/A Conversion Using PWM and R-2R Ladders to Generate Sine and DTMF Waveforms

Source for above appnote

Code:
;
;*********************************************************************
;       File:           SINE.ASM
;	Author:		Rob Stein
;       Date:           12/20/95
;	Assembler:	MPASM V01.40
;       Xtal:           20 Mhz
;       Inst Clk:       5 Mhz (200nSec)
;*********************************************************************
;       Description:    
;	Outputs a 60 Hz synthizied sine wave (32 step) via a general
;	purpose I/O pin (RB1) into a low pass filter. A software PWM
;	routine is used to create 32 seperate sinewave steps.	This
;	software was prototyped with the PICDEM1 board.
;
;	Circuit Diagram:
;		
;	           2.7k            2.7k	
;	RB1  ___/\  /\  /\______/\  /\  /\________ Analog Output
;	          \/  \/    |     \/  \/     |  
;	                    |                |
;	                  ----- 0.1uF      ----- 0.1uF
;	                  -----            -----
;	                    |                |
;	                   GND              GND
;
;	ROM Usage: 98 words
;
;	RAM Usage: 6 bytes
;
;************************* Constant Definition   *********************

FXTAL		EQU     .20000000	; Crystal Frequency
FINST  		EQU     FXTAL/4		; Instruction Cycle Frequency
FSINE  		EQU     .60		; Sine function frequency     
STEP#		EQU	.32		; Number of steps
FSTEP  		EQU     FSINE * STEP#	; Step frequency

;*************************  Register Definition  *********************

TEMPW		EQU	0x20		; Temporary interupt storage for W
DELAYCNT1	EQU	0x21		; Delay routine counter low
DELAYCNT2  	EQU     0x22		; Delay routine counter high
STEPCOUNT	EQU	0x23		; Sine step counter
OUTLOW 		EQU     0x24		; PWM low cycle load for TMR0
OUTHIGH		EQU	0x25		; PWM high cycle load for TMR0

;*************************     Bit Definition    *********************

PWM	EQU	0x01			; RB1 used for PWM output

;*********************************************************************
;				Reset Vector
;*********************************************************************

        org     0x000           
        goto    Start		; Begining of Program

;*********************************************************************
;                Interupt Vector and Service Routine
;	This interupt routine is entered via an overflow of TMR0 from
;	0xFF to 0x00.  A test of RB1 determines if the next time state
;	is a high or low cycle.  The next interupt will occure based the 
;	TMR0 reload value (OUTLOW or OUTHIGH).  
;
;	The interupt routine was designed to use a minimial number of 
;	instruction cycles.  This was done to maximize the PWM duty cycle
;	range (ie. a 5 % to 95 % range is achievable with this ISR).  Note 
;	that 'swapf' instructions are used to perform register moves without
;	effecting the STATUS flags (this saves instruction cycles by
;	eliminating the need to	temporarily save the STATUS register).
; 
;*********************************************************************

	org     0x004		; Interupt vector location
IntVector
	movwf	TEMPW		; Temporarily save W
	btfsc	PORTB,PWM	; Was this a Low cycle ?
	goto	PWMLow		; No ... 
PWMHigh
	swapf	OUTHIGH,W	; Yes... Load high time without affecting STATUS flags
	bsf	PORTB,PWM
	nop			; Delay to equalize high/low TMR0 load cycles
	movwf	TMR0		; Load next edge interupt time
	bcf	INTCON,T0IF	; Clear TMR0 overflow flag
	swapf	TEMPW,F		; Swap saved W
	swapf	TEMPW,W		; Restore W
IntEndHi
        retfie                  ; Return from Interupt
PWMLow	
	bcf	PORTB,PWM
	swapf	OUTLOW,W	; Load low time
	movwf	TMR0		; Load next edge interupt time
	bcf	INTCON,T0IF	; Clear TMR0 overflow flag
	swapf	TEMPW,F		; Swap saved W
	swapf	TEMPW,W		; Restore W
IntEndLo
        retfie                  ; Return from Interupt

;*********************************************************************
;                            Main Routine
;*********************************************************************
Start
        clrf    STATUS		; Intitialize STATUS & select bank 0
        bsf     STATUS,RP0      ; Select register bank 1
	movlw	0x88		
	movwf	OPTION_REG	; 1:1 TMR0 prescaler, PORTB pull-ups disabled
        movlw   0xFF
        movwf   TRISA           ; Set Port_A as inputs
        clrf    TRISB           ; Set Port_B as outputs
        bcf     STATUS,RP0      ; Select register bank 0
	movwf	PORTB		; PORT_B pins high
	clrf	TMR0		; Initialize TMR0
	movlw	0xA0		
	movwf	INTCON		; Enable TMRO and global interupt
ResetStep
        movlw   STEP#
        movwf   STEPCOUNT	; Load counter for 32 steps
StepLoop
        call    Delay		; Software delay
        movf    STEPCOUNT,W	; Pass table offset via W
        call    SineTable	; Get table value 
	call	SetPWM		; Set-up low & high PWM values
        decfsz  STEPCOUNT,F	; Next step
        goto    StepLoop
        goto    ResetStep

;*********************************************************************
;                       	Set PWM Subroutine
;	The following calculates the next low and high PWM time values.
;	The two time values, OUTLOW and OUTHIGH, will be passed to the 
;	interupt service routine.   
;*********************************************************************
SetPWM
	bcf	INTCON,GIE	; Disable interupts to protect ISR from...
				; corrupting OUTLOW & OUTHIGH values
	movwf	OUTLOW		; Set PWM Duty Cycle
	comf	OUTLOW,W
	
	addlw	IntEndHi-IntVector ; Adjust for Int Service time
	movwf	OUTHIGH
	movf	OUTLOW,W
	addlw	IntEndHi-IntVector ; Adjust for Int Service time
	movwf	OUTLOW

	swapf	OUTLOW,F	; Swap nibbles so that interupt service...
	swapf	OUTHIGH,F       ; will not corrupt STATUS
	bsf	INTCON,GIE	; Re-enable interupts
	return
         
;*********************************************************************
;   			Look-up Table for Sine Wave
;	This 32 entry table was generated to produce a 0.1*Vdd to 
;	0.9*Vdd (typicaly 0.5 to 4.5 volt) sine function. 
;*********************************************************************
SineTable
	addwf	PCL,F		; Increment into table
        retlw   .0  		; Dummy table value
        retlw   .128		; 0 degree, 2.5 volt
        retlw   .148
        retlw   .167
        retlw   .185
        retlw   .200
        retlw   .213
        retlw   .222
        retlw   .228
        retlw   .230		; 90 degree, 4.5 volt
        retlw   .228
        retlw   .222
        retlw   .213
        retlw   .200
        retlw   .185
        retlw   .167
        retlw   .148
        retlw   .128		; 180 degree, 2.5 volt
        retlw   .108
        retlw   .89
        retlw   .71
        retlw   .56
        retlw   .43
        retlw   .34
        retlw   .28
        retlw   .26		; 270 degree, 0.5 volt
        retlw   .28
        retlw   .34
        retlw   .43
        retlw   .56
        retlw   .71
        retlw   .89
        retlw   .108

;*********************************************************************
;                       Time Delay Sub-routine
;	The time delay is used to create the precision 32 steps.  The
;	32 step times totaled together add up to a 60 Hz rate. Note that
;	constants DELAYCNT# are used so that other frequencies can easily 
;	generated (example: FSINE equ .50 for a 50 Hz sinewave).
;*********************************************************************
TDELAY		EQU	FINST/FSTEP	; # of delay count cycles 
ADJTDELAY	EQU	TDELAY/3 - 55	; Adjust for main routine cycles
TDELAYHI	EQU	high ADJTDELAY	; Most Significant Byte of TDELAY
TDELAYLO	EQU	low ADJTDELAY	; Least Sig. Byte of TDELAY

Delay
	movlw	TDELAYHI
	movwf	DELAYCNT2	; Load high byte delay counter
	clrf	DELAYCNT1
LoopD1
        decfsz  DELAYCNT1,F	; Finished with 256 loops ?
        goto    LoopD1		; No ... keep going
	decfsz	DELAYCNT2,F	; Yes... Done with TDELAYHI loops ?
	goto	LoopD1		; No ...

	movlw	TDELAYLO	; Yes... Load low byte with adjust for...
        movwf   DELAYCNT1	; main routine cycles.
LoopD2
        decfsz  DELAYCNT1,F	; Finished with TDELAYLO loops ?
        goto    LoopD2		; No ... keep going
        return			; Yes... Finished

        END			; That's all Folks !


If you have an additional questions concerning sinusoidal wave generation after reading the appnote, feel free to ask. I have considerable reference material on the subject.
 

i have insert my 50Hz pic16f819 in inverter for dc12v to 220vac. Amazing. The 50hz increased to 200 hertz and when i tried to check output with analouge metre it burns and the second shous at 250 to 30 volts with very trimbling pin. Why frequency increased to 200 hertz?
Transistors are TIP741 4 transistors 2 each side.
below the pic what i have actually done. consider each transistor 2 as i have combined their all three pins together.
TIP741.JPG
 
Last edited:

I'm having a hard time understanding you last posting. You inserted your PIC into an inverter? And you burned up your meter?
 

ya i have burnt my two analogue volt meter when check output 220 volts. the meter were set at 1000 volt. and he frequency counter increased to 203 hertz from 49 hertz.
 

I'm sorry to hear the bad news.

If I'd known you were going to build an inverter, I would have given you some valuable appnotes concerning the design.

I hope the meter is repairable
 

i dont care meter. The problem is that why it happens?
 

processor PIC16F819
#include <p16f819.inc>
__CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO
J equ 20h
K equ 21h
org 0
goto main
main:
banksel OSCCON
movlw B'01110000'
movwf OSCCON
banksel TRISB
movlw B'00000000'
movwf TRISB
banksel PORTB
Flasher:
bcf PORTB , 1
bsf PORTB , 0
nop
nop
call delay
bcf PORTB , 0
bsf PORTB , 1
call delay
goto Flasher
fin:
goto fin
delay:
movlw D'10'
movwf K
clrf J
delay1:
nop
decfsz J , f
goto delay1
delay2:
nop
decfsz J , f
goto delay2
decfsz K , f
goto delay1
return
end


Simplest 50Hz frequency for 12vdc to 220vac picture below.
Will this work if i use it to make inverter?
can i control duty cycle of this frequecy with pwm in pic16f819 or it is not possible? Their is another method to do this with pwm? i mean is the 50hz pwm will be realtime or the frequency i made with code can be attached with pwm?

If this is the code you used, in an attempt to produce a sinusoidal wave to feed to the inverter circuit?

If so, it is an incorrect approach.

I gave you links to an appnote and code, which when implemented correctly will produce a very nice sine wave at 50Hz.

Study them both. I'll also dig up some appnotes concerning the design of inverters.
 

here is video whay happens with this code:
processor PIC16F819
#include <p16f819.inc>
__CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO
J equ 20h
K equ 21h
org 0
goto main
main:
banksel OSCCON
movlw B'01110000'
movwf OSCCON
banksel TRISB
movlw B'00000000'
movwf TRISB
banksel PORTB
Flasher:
bcf PORTB , 1
bsf PORTB , 0
nop
nop
call delay
bcf PORTB , 0
bsf PORTB , 1
call delay
goto Flasher
fin:
goto fin
delay:
movlw D'10'
movwf K
clrf J
delay1:
nop
decfsz J , f
goto delay1
delay2:
nop
decfsz J , f
goto delay2
decfsz K , f
goto delay1
return
end

see trembling of voltmetre while set at 1000 vac. The hertz increased 147 this time. I conclude that the pic is restarting due to any surge?


---------- Post added at 06:16 ---------- Previous post was at 05:49 ----------

or should i change my square wave to modified square wave? as in pic below
**broken link removed**
i have green colour wave. should i change it to blue colour wave to stop frequency increasing?
 

A voltage surge could very well be the reason, although if I had to guess it might be a votage sag rather than a surge.

Everytime a pulse ends the inductor creates Back EMF, caused by the collapse of the magnetic field, which will be in the opposite direction of the polarity of the battery. So you have momentary voltage sags which drop the supply voltage to the voltage regulator below it's operational threshold, effectively powering down the PIC, which then reboots once the voltage regulator comes back online.

Your inverter design produces what is known as a modified sine wave, which really doesn't look like a sine wave at all. It has its uses, driving motors, lighting lightbulbs, etc, but I'd think twice about powering my stereo system with that type of power output.

A similar, but better design of a modified sine wave inverter:

**broken link removed**

I put together some pure sine wave inverter designs for you later tonight or in the morning.

Meanwhile, read the appnote and study it's code, so that you can understand the technique of producing pure sine waves from PWM. A pure sine wave inverter takes this technique to a larger scale, so to speak.

Ciao
 
Last edited:

OK.
Is this now modified sine wave or i should increase the duty cycle from 8 ms to 9 ms or 10 ms and wait 2 ms


processor PIC16F819
#include <p16f819.inc>
__CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO
J equ 20h
K equ 21h
org 0
goto main
main:
banksel OSCCON
movlw B'01110000'
movwf OSCCON
banksel TRISB
movlw B'00000000'
movwf TRISB
banksel PORTB
Flasher:
bcf PORTB , 1
call delay2ms
bsf PORTB , 0
call delay8ms
nop
nop
bcf PORTB , 0
call delay2ms
bsf PORTB , 1
call delay8ms
goto Flasher
fin:
goto fin
delay2ms:
movlw D'2'
movwf K
clrf J
delay1:
nop
decfsz J , f
goto delay1
delay2:
nop
decfsz J , f
goto delay2
decfsz K , f
goto delay1
return
delay8ms:
movlw D'8'
movwf K
clrf J
delay11:
nop
decfsz J , f
goto delay11
delay22:
nop
decfsz J , f
goto delay22
decfsz K , f
goto delay1
return
end
 

The example inverter of the links I just posted use two staggered PWM outputs with period of 20ms with 25% duty cycle or 5ms pulse.

The PIC16F628A is programmed to produce a logic 5v signal for 5ms at pin 17 then 15ms off. Then the same at pin 18, 5ms on then 15ms off (4.17ms for 60Hz). That is one cycle which is then looped. This results in the signals below on the oscilloscope. (2 channel view) You can see the two 5v pulses from pins 17 & 18.

But, you'll need to make some design changes to prevent the Back EMF from power cycling the PIC.
 

The PIC16F628A is programmed to produce a logic 5v signal for 5ms at pin 17 then 15ms off. Then the same at pin 18, 5ms on then 15ms off (4.17ms for 60Hz). That is one cycle which is then looped. This results in the signals below on the oscilloscope. (2 channel view) You can see the two 5v pulses from pins 17 & 18. this decreases frequency from 50hz to 25 hertz
 

this decreases frequency from 50hz to 25 hertz

Actually it doesn't, each pulse represent one half cycle of the resulting sine wave, one per crest of the wave so to speak.

Two pulse per 20ms period, is a frequency of 50Hz. You are concerned with the period of the pulse train, not the number of pulses per period. When you produce a pure sine wave with PWM, you could have hundreds or thousands of pulses per period depending on the frequency of the resulting sine wave. When producing a pure sine wave with PWM with a resulting frequency of 50Hz you'll be dealing with 25 to 100 pulses per 20ms period.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top