Mjerenje širine signala pomoću Tajmer0

Rasprava o PIC mikrokontrolerima, PIC projekti i drugo vezano za PIC-eve...

Moderators: pedja089, stojke369, [eDo], trax

Maki
Odlično uznapredovao
Odlično uznapredovao
Posts: 766
Joined: 02-07-2012, 12:54

Re: Mjerenje širine signala pomoću Tajmer0

Post by Maki »

Još jedno možda glupo pitanje, ali jel si definirao bitove za konfiguraciju glavnog signala takta (clk-a)?
Da ti kojim slučajem ne radi na internom?
User avatar
Black
Odlično uznapredovao
Odlično uznapredovao
Posts: 981
Joined: 17-12-2008, 15:44

Re: Mjerenje širine signala pomoću Tajmer0

Post by Black »

http://www.microcontrollerboard.com/pic ... orial.html


Ovkao nešto je blizu, dakle da napraviš da interput radi tek kad uradi overflow
vukboban
Pravo uznapredovao :)
Pravo uznapredovao :)
Posts: 212
Joined: 05-12-2008, 17:49
Location: Pancevo

Re: Mjerenje širine signala pomoću Tajmer0

Post by vukboban »

u onom kodu gde si koristio tmr1 ti ulazis u interrupt i proveravas da li je signal na rb5=1.ako jeste stavljas da je cnt=0 i startujes tmr1.izlazis iz interrupta,ali on istog tog trenutka ponovo ulazi u interrupt,jer je signal na rb5 jos uvek prisutan,opet cnt=0,opet tmr1 start i tako bezbroj puta dokle god je prisutan signal na rb5. zato i dobijas nebuloze na lcd.
Zato se koristi uzlazna i silazna ivica signala,a ne samo da li je signal 1 ili 0
majstor1
Pocetnik na forumu
Pocetnik na forumu
Posts: 47
Joined: 17-10-2014, 13:43

Re: Mjerenje širine signala pomoću Tajmer0

Post by majstor1 »

Pozdrav svima,
nisam odustao (od traženja) i našao sam kod koji u potpunosti rješava moj problem (LibStock Mikroelektronike). Nisam dalje razrađivao po formuli moju varijablu "broj" da ju preračunam u točno trajanje signala (u sec ili ms). Još jednom hvala svima na trudu i pomoći.

Code: Select all

program radi_s_MB18062018

 dim LCD_RS as sbit at RD4_bit
LCD_EN as sbit at RD5_bit
LCD_D4 as sbit at RD0_bit
LCD_D5 as sbit at RD1_bit
LCD_D6 as sbit at RD2_bit
LCD_D7 as sbit at RD3_bit

LCD_RS_Direction as sbit at TRISD4_bit
LCD_EN_Direction as sbit at TRISD5_bit
LCD_D4_Direction as sbit at TRISD0_bit
LCD_D5_Direction as sbit at TRISD1_bit
LCD_D6_Direction as sbit at TRISD2_bit
LCD_D7_Direction as sbit at TRISD3_bit
' Declarations section
dim  loopcount, broj as integer
broj_str as string [11]

' This 16F887 code works as follows
'     On the 16F887 RB0 when set as an input can be connected to timer1
'     When RB0 is high timer1 will count oscillator cycles
'     OPTION_REG.INTEDG can be set to generate an interrupt on either a rising or falling edge
'     The main programme is an endless loop which runs until there a signal on RB0
'     The arrival of a leading or falling edge on RB0 which triggers an interrupt
'     The interrupt service routine (ISR) starts or stops the timer
'     The code was developed for use with the Parallax Ping))) sensor
'
' Interrupt Service Routine (ISR)
sub procedure interrupt()           ' Interrupt routine
     if loopcount = 0 then          ' IF loopcount = 0 THEN its a leading edge so start timer, ELSE its a trailing edge
     TMR1H = 0x00                   ' Set initial value for the Timer1
     TMR1L = 0x00
     T1CON.TMR1ON = 1               ' Turn the Timer1 on
     INTCON.INTF = 0                ' Clear interupts
     loopcount = 1                  ' Set loopcount so that next time interrupt occurs, treat as a falling edge
     OPTION_REG.INTEDG = not OPTION_REG.INTEDG 'flip to check for trailing edge
     else
     T1CON.TMR1ON = 0               ' Turn the Timer1 off
     INTCON.INTF = 0                ' Clear interupts
     OPTION_REG.INTEDG = not OPTION_REG.INTEDG 'flip to check for trailing edge
     loopcount = 0                  ' Set loopcount so that next time interrupt occurs, treat as a leading edge
     end if
end sub


main:
' Configuration
  Lcd_Init() Lcd_Cmd(_LCD_CLEAR) Lcd_Cmd(_LCD_CURSOR_OFF)
' Set up I/O
      ANSEL   = 0        ' Set as digital
      ANSELH  = 0        ' Set as digital
      TRISA   = 0        ' Set as outputs
      TRISC   = 0        ' Set as outputs
      TRISD   = 0        ' Set as outputs
      TRISB = %00000001  ' Set RB5 RB0 (pin 33) as an input
      WPUB = 0xFF        ' Use internal weak pull up resistors
' Set up Interrupts
     INTCON.GIE = 1                  ' Enable interrupts
     INTCON.INTE = 1                 ' Enable RB0 external interrupt
     INTCON.PEIE = 0                 ' Disable all unmasked peripheral interrupts
     OPTION_REG.INTEDG = 1           ' Interrupt on a rising edge
' Set up Timer1
     TMR1H = 0x00                     ' Set initial value for the Timer1
     TMR1L = 0x00
     T1CON.TMR1CS  = 0                ' Timer1 counts pulses from oscillator
     T1CON.T1CKPS0 = 1                ' Assigned prescaler rate is 1:1
     T1CON.T1CKPS1 = 1                ' Assigned prescaler rate is 1:1
' Set up variables
     loopcount = 0                    ' Initialise counter  (0 = leading edge, 1 = trailing edge)

' Main program (endless loop)
start:
    while true
      broj=tmr1H
      broj=broj<<8
      broj=broj+tmr1l
      longinttostr (broj, broj_str)
      LCD_Out(3,1,broj_str)
    wend
goto start
end.
majstor1
Pocetnik na forumu
Pocetnik na forumu
Posts: 47
Joined: 17-10-2014, 13:43

Re: Mjerenje širine signala pomoću Tajmer0

Post by majstor1 »

Zaboravio sam napisati razlike od početnog posta.
Rješenje je nađeno preko TIMER1, a ne TIMER0 kako sam ja započeo. Pin za ulaz je RB0, a ne RB5, ostalo je isto 16F887, 8 MHz, EasyPic 5.
Signal prijemnika RC upravljanja je doveden preko NPN tranzistora BC549c (+5V na kolektor, signal preko otpornika 330oma na bazu tranzistora, a s emitera je signal odveden na RB0) na PIC. Na pinu RB0 je pull down otpornik 10K).
Ovim projektom modelari mogu proširiti četvrokanalnu stanicu na 9 kanala, a s obzirom na stabilnost rada vjerojatno se može i veći broj kanala postaviti.
Post Reply