ATMEGA 2560 Timer and PWM same time

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm new to AVR programming . I want to use ATMEGA 2560 for meausre the motor speed using shaft encoder and display on LCD. This is my approach

1) I use Timer (Timer0) to calculate 1 second delay

2) every time Shaft encoder detect a hole it will generate an interrupt (interrupt 0 ) and 1 to the register 

3) after 1 second delay i get the value of the register and show it in LCD

 

This works fine when i connect motor to direct 5v current

but if i connect this to PWM pin (Timer5) LCD acts wired .What could be the problem ? (please see the bellow code for my initialisation)

 

ldi temp,(2<<ISC00)      ; set INT0 as falling edge triggered interrupt
sts EICRA,temp

in temp,EIMSK            ; enable INT0
ori temp,(1<<0)
out EIMSK,temp


ldi temp, 0b00000000
out TCCR0A, temp
ldi temp, 0b00000101         ; Prescaling value=1024
out TCCR0B, temp 
ldi temp, 1<<TOIE0           ; =1024*16 microseconds
sts TIMSK0, temp 
sei   
                    ;enable global interrupt


ldi temp, 0b00001000 
sts DDRL, temp ; Bit 3 will function as OC5A. 
clr temp  ; the value controls the PWM duty cycle 
sts OCR5AH, temp 
ldi temp, 0x4A
 sts OCR5AL, temp ; Set Timer5 to Phase Correct PWM mode. 
 ldi temp, (1 << CS50) 
 sts TCCR5B, temp 
 ldi temp, (1<< WGM50)|(1<<COM5A1) 
 sts TCCR5A, temp

 

Last Edited: Fri. Oct 13, 2017 - 06:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why do you think the problem is with this code? You also force us to guess as you use ‘temp’ without telling us what that is. You might want to show us the whole code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you can, use C languange.
And schematics and complete code would be easier to trace the problem.
.
MG

I don't know why I'm still doing this hobby

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Why do you think the problem is with this code?
+1

 

The thing you said was "weird" was the LCD but the code you show has nothing to do with LCD driving??

 

At a guess I'd say you are probably hanging in an ISR for too long so that it's disrupting timing of signals to the LCD or something.