Timer1 in Atmega16

6 posts / 0 new
Last post
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I want to generate PWM using Timer1
Crystal oscillator= 8MHz
Timer1 Prescale=8

so one timer tick is of 1us.

for 2ms high pulse, 2ms/1us= 2000
so S1=65535-2000=63535

for 18ms low pulse, 18ms/1us=18000
so prescale= 65535-18000=47535

By using S1, and prescale value, i wrote the code. When i check the simulation on ISIS protesus, i m getting 8 times high and low pulse.
Can anybody help?

the code is


$regfile = "m16def.dat"
$crystal = 8000000

Config Timer1 = Timer , Prescale = 8

On Timer1 Servoirq

Config Portd.0 = Output
Portd.0 = 0


Dim Preload As Word


Dim S1 As Word
Dim I As Byte


  Preload = 63285
   S1 = 47535


Enable Timer1                                               'required in any case
Enable Interrupts                                           'required in any case

Do

Loop
End


Servoirq:
If Portd.0 = 0 Then
  Timer1 = S1
  Portd.0 = 1
Else
  Portd.0 = 0
  Timer1 = Preload
End If
Return
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A mega16 by default clocks at 1MHz. Presumably there's a setting in Proteus where you tell it that the chip is actually running at 8MHz? Have you changed this option?

 

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

hmmm. I was thinking that statement $crystal = 8000000 will do this job, but i was wrong.
In proteus, when i set CKSEL Fuses to Int. RC[8MHz]. i get right 2ms and 18ms pulses.

Thanks

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

Quote:
I want to generate PWM using Timer1
Then why not use PWM mode?

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:

hmmm. I was thinking that statement $crystal = 8000000 will do this job, but i was wrong.

It is common that you need to have one setting telling your tool chain (compiler, assemblerm whatever..) the CPU speed so that it can generate correct code (e.g. calculating correct UBBRnx values for USART baud rates), AND one setting to tell the actual system what clock it should use. For the latter there is e.g. a setting in AVR Simulator, on real chips it is determined in part by fuses etc...

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington]

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

Warning: In another thread OP has already admitted to using a stolen copy of Bascom so it seems likely the Proteus is also stolen. As such I'll leave it for you to decide if you still want to provide support in this thread.

Moderator.