More PWM stuff

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

I am lost here

I am trying to use the mega 8515's timer0 for PWM. it must operate all by itself and output a pulse of 1/8 of a second and be still for 1 second

Once again, my code sucks and I am ready to start over.

Any assembler suggestions?

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

So you want an output form of 125ms high output followed by 1000ms low output, and continuously repeat that form. Is that correct? At what speed will your device be running?

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

right now i am using the 3.68mhz on the stk, but it will eventually run at 8mhz. the key right now is to just get the thing to allow mr to vary the pulse widths etc.

Thx

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

The 8-bit timer will roll-over faster than your desired output frequency. At 8Mhz with a maximum 1024 prescalar and a maximum 256 count, the period of the timer will be 32.768ms.

Personally, for such a slow rate and for maximum flexibility, I'd just keep track of time in a periodic interrupt. For many of my designs, I have timer0 interrut every 5-10ms to handle background processing (or at least flag for the main routine that a process is due to be performed). Mainly, the 5-10ms comes because I tend to use that as a button debouncing period.

For your desired output frequency, you can either use the 16-bit timer1, use a slower system clock speed, or keep track of time yourself and just change the output pin after counting 125ms then again after counting 1000ms.

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

I would like to use the timer1 which is 16bit. I am using timer0 to generate a 32khz square wave to run a a/d converter

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

The simplest approach would be to use timer1 in CTC mode with a prescalar of 64 and a CTC value of 15625. At 8MHz, that'd give you periodic interrupts at 125.0ms. You then raise your output for 1 interrupt and keep it low for the next 8 interrupts.

If you wanted to produce your desired signal without interrupts or bit-banging the output pin, then you can use a larger prescalar, set OCR1A to a value for your 125ms high and OCR1B for half of your 1125ms overall period, then OR those two output lines. You'd have to carefully setup your PWM mode so that OC1B is high for the 125ms that OC1A will be high, but then not high again until 1125ms had passed from when it last became high.

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

This is starting to sound way more complicated than it should.

What sucks is that I am currently doing everything in assembler. Most freaks use 'c'. I am at a disadvantage

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Quote:

The simplest approach would be to use timer1 in CTC mode with a prescalar of 64 and a CTC value of 15625. At 8MHz, that'd give you periodic interrupts at 125.0ms. You then raise your output for 1 interrupt and keep it low for the next 8 interrupts.

That is certainly straightforward, >>if<< OP wants to keep them an even multiple. But there is the not about varying the pulse widths. [I don't know what an "mr" refers to.]

Quote:

If you wanted to produce your desired signal without interrupts or bit-banging the output pin, ...

A good goal.

Quote:

...then you can use a larger prescalar, set OCR1A to a value for your 125ms high and OCR1B for half of your 1125ms overall period, then OR those two output lines. You'd have to carefully setup your PWM mode so that OC1B is high for the 125ms that OC1A will be high, but then not high again until 1125ms had passed from when it last became high.

But that seems like a lot of work. A quick session with AVRCalc shows that with a /64 prescaler 125ms is 0x1c20 -1 = 0x1c1f, and the full 1125ms period is 0xfd20 - 1 = 0xfd1f.

If you want to use OCR1B, you can use mode 14 or mode 15. Let's say mode 14. Then you load ICR1 with the period, and OCR1A or OCR1B with the on-time. Vary the pulse width with OCR1A/B and the period with ICR1.

Nary a clock cycle for continuous operation nor any tied pins in sight.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Lee
I am still a tad confused here.
ICR1 sets the pulse width?
and oca/b set the time between the pulses?

mr was supposed to read "me" not 'mr' bad typing

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

JChristoff
Illinois

Last Edited: Thu. Jan 6, 2011 - 06:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
ICR1 sets the pulse width?
and oca/b set the time between the pulses?

You have that reversed. OCRA/B set the pulse Width, ICR1 sets the frequency (really, the time between the beginning of one pulse and the next). Though this really depends on what mode you have. Some use OCRA as top and OCRB as pulse width.

Regards,
Steve A.

The Board helps those that help themselves.

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

theusch wrote:
If you want to use OCR1B, you can use mode 14 or mode 15.
Thanks for chiming in, Lee. I thought there should be an easier way to implement a duty cycle than my suggestions.

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

Quote:

What sucks is that I am currently doing everything in assembler. Most freaks use 'c'. I am at a disadvantage

Setting up the timers is nothing more than loading values into I/O registers. kmr >>may<< even be able to do it in C++. ;)

For excellent worked timer examples in ASM, visit www.avrbeginners.net .

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Here is a new twist.
When I set the wgm11, and wgm10 bits the ocr1h byte is no longer writeable. even if I write to the registers before anything else, as soon as I set those wto bits, the register is cleared and that is it.

It almost seems that te software thinks the timerr is 8bit

Hee is the code:

.include "m8515def.inc"

.def temp =r16
.def tempa =r17

begin:
ldi temp,0xff
out ddrd,temp
cli

ldi temp,0x18
out tccr1b,temp
ldi temp,(1<<com1a1|1<<wgm11|1<<wgm10)
out tccr1a,temp
ldi temp,0x18
out tccr1b,temp
ldi temp,0x00
ldi tempa,0xf0
out ocr1ah,tempa
out ocr1al,temp
ldi temp,0x19
out tccr1b,temp

done: rjmp done

I have no clue at this point

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

How do you 'know' it is going to 0?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

when you run the code in simulator using the step function you can watch it. I noticed that there is an issue with the 16bit registers in avrstudio as a known issue. will need to look into that....gonna try the dragon tonight

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user