Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
seniorvos
PostPosted: Mar 09, 2010 - 09:36 AM
Newbie


Joined: Feb 01, 2010
Posts: 4


Hello freaks,
As a newbie I tried to measure a pulslength. According to the Atmel documentation this should be very easy. But I don't get it working. The only thing my program does is counting interrupts that are obviously generated by the program itself, because it does so even when I ground the inputpin.
I try to measure the length of pulses of about 75 to 300 msec. The accucacy is not important.
The programm displays 4 registers on a LCD which should make visible the pulslength and pulspause. The only thing that happen is counting up followed by a reset, for ever. The speed depends on the clock divide factor.
Who can tell me what is wrong in my program? Thanks!
My program:
Code:
; pulse.asm
.include "m162def.inc"       ; ATMEGA162 4Mc
.org $0000
   rjmp start
.org $0018
   rjmp TIM1_CAPT       ; timer1 event capture interrupt
.org $0030                     
start:
   ldi r16,low(RAMEND)
   out spl,r16
   ldi r16,high(RAMEND)
   out sph,r16           
;init timer1
   clr r16
   ldi r16,1<<TICIE1    ; set timer1 capture event interrupt
   out TIMSK,r16
   ldi r16,(1<<ICES1)|(1<<ICNC1)|(1<<CS10)|(1<<CS12) ;noise; rising; /1024
   out TCCR1B,r16
   ser r17              ; now rising edge r17=ff
   ser r16
   out DDRC,r16         ; port C output
   cbi DDRC,PC0         ; port C pin 0 capture
   sei
loop:
; this loop displays r18,r19,r20 and r21 on a LCD
;   rcall LCD
   rjmp  loop
;timer input capture interrupt
TIM1_CAPT:
   push r16
   in   r16,SREG        ; save SREG
   push r16
   in   r18,ICR1L       ; read ICR low
   in   r19,ICR1H       ; read ICR high
   sbrs r17,1           ; was rising(r17=ff)?skip
   rjmp pulslength
   rjmp pulspause             
pulslength:                  ; event was end of puls
      mov  r20,r18         ; r20/21 pulslength
   mov  r21,r19               
   clr  r16
   ldi  r16,(1<<ICNC1)|(1<<ICES1)|(1<<CS12)|(1<<CS10) ;nonoise; rising; /1024
   out  TCCR1B, r16
   ser  r17             ; remember rising r17=ff
   rjmp clearandexit
pulspause:                   ;event end of pause
                        ;r18/19 all on pauselenth
   clr  r16
   ldi  r16,(1<<ICNC1)|(0<<ICES1)|(1<<CS12)|(1<<CS10) ;nonoise, decreasing, /1024
   out  TCCR1B, r16
   clr  r17          ; remember decreasing r17=00
clearandexit:   
   clr  r16
   out  TCNT1H,r16             ; clear timerreg
   out  TCNT1L,r16             ; first high then low
   pop  r16
   out  SREG,r16
   pop  r16
   reti                       
; PM: include LCD display routine
 
 View user's profile Send private message  
Reply with quote Back to top
Bogdancev
PostPosted: Mar 09, 2010 - 10:47 AM
Hangaround


Joined: May 25, 2008
Posts: 177
Location: Dnipropetrovsk, Ukraine

Can you measure a period, from raising edge to raising edge?
Try first to simplify the program. Just capture only raising edges and show the period (quantity of you clock pulses) on LCD. Try to multiply that quantity with your AVR clock period (multiplied by pre-scaler). Is it the same as period of your input signal?
What pulse source do you use?
Do you have function generator?
Show you circuit.
Finally, you can use INT0 or INT1, they have option to trigger on both falling and raising edges at the same time. You don't need to readjust timer each time.
I would do (and I have done) like this: use INT0 or INT1, adjust it to trigger on both edges. Start the timer. When interrupt occurs: save value, zero timer, determine if it pulse or pause, show on LCD. Then wait for interrupt again.
 
 View user's profile Send private message  
Reply with quote Back to top
seniorvos
PostPosted: Mar 09, 2010 - 01:06 PM
Newbie


Joined: Feb 01, 2010
Posts: 4


What a pleasant surprise to get an answer so soon! I'l give more details. On my roof there are a number of sun collectors. They are based on heatpipes and genereate plenty of hot water. But this system needs a lot of controle. To mention some: the pump should only operate when needed. The system must be protected against frost and overheating. When there is too much hot water this should be used for the central heating of the house. There must be protection against legionella bacterial infection and so on. All this at this momentis done by hand. I intend to do this on behalf of an AVR microproceesor and therefore started to get used to AVR programming. I use AVR Studio-4 and a STK500. My first try was trying to get a LCD working. This went well. Now I try to get used to the timer and interrupts of the AVR. I took a DCF77 clock device (which gives nice pulses) and wrote a test program (see above).
This moment my first aim is rather to understand why my program brings the AtMega162 to generate event-interrupts all by itself then getting my program working, although that would be very nice.
Thanks for your advise!
 
 View user's profile Send private message  
Reply with quote Back to top
Bogdancev
PostPosted: Mar 10, 2010 - 06:08 AM
Hangaround


Joined: May 25, 2008
Posts: 177
Location: Dnipropetrovsk, Ukraine

Code:
cbi DDRC,PC0         ; port C pin 0 capture
Is this your capture pin?

If yes - it is wrong. In your case it must be PE0(ICP1/INT2)
With this pin modelling in AVR studio goes right. Without there are no interrupts occur at all. Your LCD just shows something unknown.
 
 View user's profile Send private message  
Reply with quote Back to top
seniorvos
PostPosted: Mar 11, 2010 - 07:46 AM
Newbie


Joined: Feb 01, 2010
Posts: 4


Yes, it was yes indeed! I misunderstood the AtMega 162 documentation. Now I have changed the pin assignment to ICP1 the program works fine although it needs some optimalisation because sometimes the program wrongly sees the length of a pause as if it had the length of the preceeding puls. Thank you for your help!
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits