DIY Electronic projects
Brojac Da li je uredu - Printable Version

+- DIY Electronic projects (https://forum.yu3ma.net)
+-- Forum: Mikrokontroleri (https://forum.yu3ma.net/forumdisplay.php?fid=52)
+--- Forum: Microchip PIC (https://forum.yu3ma.net/forumdisplay.php?fid=53)
+--- Thread: Brojac Da li je uredu (/showthread.php?tid=1586)



Brojac Da li je uredu - MILANNN - 05-02-2017

Pozdrav.Pronaso sam ovaj kod ali nemogu da ga probam u proteusu jer mi odmah ispise da je procesor zauzt 100% a odgovara mi nesto ovako prostije zbog lake izmene jer netreba mi kao pravi frekvencmetar vec da broji nesto drugo.Izmenio sam nesto oko displeja (pomesao neko portove pa ubacio i E port umesto D ) na ovom kodu ali nebitno to cu lako da ispravim,interesuje me samo da li ovo radi jer ako nece da nepravim plocicu uzalud.U proteusu mi samo pojavi neku crticu na displeju i pokoci se a komp mi uopste nije slab

'  Code for 16F877A to count frequency
'  Uses TMR0 and prescaler to gate 1 second counting time, internally clocked.
'  Uses Timer1, external clock, from PIN 15  
'  WDT, Analog, Comparitors OFF

clear
define loader_used 1
define osc 4

' Define LCD registers and bits
Define LCD_DREG PORTD
Define LCD_RSREG PORTD
Define LCD_RSBIT 0
Define LCD_EREG PORTD
Define LCD_EBIT 1
define LCD_RWREG PORTD
define LCD_RWBIT 2
define LCD_BITS 4
define LCD_LINES 2
'define LCD_COMMANDUS 2500
'define LCD_DATAUS 100

TRISC = %11110001    'using pin 15 as input for frequency
TRISD = 0            'PORTD all outputs
PORTB = 0
CMCON = 7            'disable comparators
ADCON1 = 7           'PORTA and E digital


'setup option register for TMR0
option_reg.7 = 0
option_reg.6 = 1     '1 for interrupts active
option_reg.5 = 0
option_reg.4 = 1     '1 for interrupts active 
option_reg.3 = 0     'Assign prescaller to TMR0
option_reg.2 = 1     'prescaler 0-15
option_reg.1 = 1     'with this setting TMRO-Rate = 1:256
option_reg.0 = 1

'Prescaller settings for option_reg
'option_reg 01011000 = 1:1 prescaler TMR0 roles over every 1mSec 
'option_reg 01010000 = 1:2 prescaler TMR0 roles over every 152 uSec
'option_reg 01010001 = 1:4 prescaler TMR0 roles over every 1mSec
'option_reg 01010010 = 1:8 prescaler TMR0 roles over every 2mSec
'option_reg 01010011 = 1:16 prescaler TMR0 roles over every 4mSec
'option_reg 01010100 = 1:32 prescaler TMR0 roles over every 8mSec
'option_reg 01010101 = 1:64 prescaler TMR0 roles over every 16mSec
'option_reg 01010110 = 1:128 prescaler TMR0 roles over every 32mSec
'option_reg 01010111 = 1:256 prescaler TMR0 roles over ever 64mSec

INTCON.7 = 0   'disable interrupt routine on overflow
INTCON.5 = 0   'disable interrupt routine on Timer0 overflow
INTCON.4 = 0   'disable RB0 external interrupt function
INTCON.3 = 0   'disable port change interrupt function
INTCON.1 = 0   'not really needed, but clear possible pin change flag

T1CON = %00110010 'Timer1 prescaler = 8 - osc off- sync on, external source


lcdout $fe,$c0
lcdout "Frequency Counter"
pause 1000


Tmr1flg var word
counting var byte
calc var byte
mhz var byte
khz var word
hz var word
TMR1 var byte
PORTD = 0     'We will be setting PORTD.0 high through the 1 second
              'gate-period to monitor it with Oscope
INTCON.2 = 0  'Clear Timer0 interrupt flag
TMR1 = 0      'Reset Timer0 counting value

' Main program loop
mainloop:
Tmr1flg = 0   'Clear variable which counts Timer1 overflows
TMR1H = 0     'clear Timer1 high register 
TMR1L = 0     'clear Timer1 low register
PIR1.0 = 0    'clear Timer1 overflow flag bit
INTCON.2 = 0  'Clear Timer0 interrupt flag
TMR1 = 0      'Reset Timer0 value
counting = 0
TRISB = 0
PORTD = 0
PORTB.0 = 1       'raise the 1 second timer flag
T1CON.0 = 1       'start Timer1

while counting < 15   'Timer0 has to overflow this many times for 1 second gate period
while INTCON.2 = 0    'watch for Timer1 overflow and count if it occurs.
if PIR1.0 = 1 then     'if Timer1 overflow flag is up then 
Tmr1flg = Tmr1flg + 1   'increment overflow count by 1. Each count represents value of 524280
PIR1.0 = 0              'Now reset the Timer1 overflow flag bit
endif
wend                    'end of inner while loop
INTCON.2 = 0            'Now reset the Timer0 overflow flag bit
counting = counting + 1  'increment counting for gate time
wend                     'end of outer while loop

T1CON.0 = 0 'stop Timer1
PORTB.0 = 0 'lower the 1 second timer flag representing the gate period


' calculations to get the numbers captured to the display
' we have to feed the number of Timer1 overflows into Mhz, Khz, hz
' Then we feed the Timer1 remaining count * 8 in as well
' and parse it out to mhz khz hz
hz = 0
khz = 0
mhz = 0

' Add Timer1 overflow values into Mhz, Khz, hz
if Tmr1flg > 0 then
for calc = 0 to Tmr1flg
khz = khz + 524
hz = hz + 280

while hz > 999
khz = khz + 1
hz = hz -1000
wend
while khz > 999
mhz = mhz + 1
khz = khz - 1000
wend
next
endif

' Add remaining Timer1 count * 8 into Mhz, Khz, hz
for calc = 0 to 7
hz = hz + TMR1

while hz > 999
khz = khz + 1
hz = hz -1000
wend

while khz > 999
mhz = mhz + 1
khz = khz - 1000
wend
next

'Display results
if mhz > 0 then 
lcdout $FE,1,#mhz,".",#khz,".",#hz
else
lcdout $FE,1,#khz,".",#hz
endif

pause 2000
goto mainloop   ' Go get another reading
End


RE: Brojac Da li je uredu - gorankg - 05-03-2017

Pogledaj ovu temu:
http://forum.yu3ma.net/showthread.php?tid=1473&page=5&highlight=counter

Tu imaš kod za brojač koji radi OK. Ako ti treba pomoć, tu sam.
Ovo što pokušavaš prepraviti meri frekvenciju.


RE: Brojac Da li je uredu - MILANNN - 05-03-2017

Hvaka gorankg.Meni i treba frekvencmetar samo za nesto specificno gde neznam koliko ce mi trebati da traje merenje a i nisam neki programer pa mi ovaj kod odgovara jer ga razumem i mogu da ga izmenim po potrebi kad vidim sta i kako stim sto kazem nisam neki programer pa neznam da li radi dok neprobam a nemogu u proteusu jer procesor odmah ide na 100% i tu je kraj simulacije.Ja sam ga iskopirao i bilo je nekih graskica sto znam da ispravim ali uzalud kad nemoze da se vidi u simulatoru.