[solved] Problem accessing 16-bit registers (PWM)

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

Hi my friends,

I am trying to program PWM using timer1 but I have some problems with it. AVRstudio cannot properly simulate OCR1AH and OCR1BH (clear these values). I have heard AVRstudio 4 has problem with it. Anyway could you look at my code maybe I am doing this wrong. I had some break from programming AVRs.

	TCCR1A|= (1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
	TCCR1B|= (1<<WGM12)|(1<<WGM13);
	
		
	sei(); 
 
	
	uint32_t frequency=256;
	uint32_t target_timer_count=0;
	
          cli();
			
			
			target_timer_count=F_CPU/frequency; 
			ICR1L=(uint8_t)target_timer_count;
					
			ICR1H=(uint8_t)(target_timer_count>>8);
			
			OCR1AL= (uint8_t)(target_timer_count * 0.9); 
			OCR1AH= (uint8_t)((uint32_t)(target_timer_count * 0.9)>>8);

			OCR1BL= (uint8_t)(target_timer_count * 0.5); 
			OCR1BH= (uint8_t)((uint32_t)(target_timer_count * 0.5)>>8);
			TCCR1B|= (1<<CS10);
			
			
			sei();

I would be grateful for checking my code...

Thanks in advance

Last Edited: Mon. Oct 10, 2011 - 02:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have to write the HIGH byte register 1st, RTFM. You're using a COMPILER, why complicate things a bit by writing separately to 16 bit registers ? What PWM mode do you want ?

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

You should be able to just write:

OCR1A = target_timer_count * 0.9;

The compiler will handle the access order issues behind the scenes.

However, you may need to watch out for that 0.9. That would be recognized as something other than an integer. You might consider

 OCR1A = target_timer*count * 9 / 10;

That should carry out an integer division for you.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

Thank you indianajones11

I thought I need to assign them separately. Yes you are right I need to write HIGH byte first. I have read manual before posting but honestly I had to miss it.
I am using fast PWM now but maybe I will change it to correct phase according to needs.
I made simulation in Proteus everything work ok now.

Once again thank you...

Quote:
However, you may need to watch out for that 0.9. That would be recognized as something other than an integer. You might consider

Code:
OCR1A = target_timer*count * 9 / 10;

That should carry out an integer division for you.

Would it be better for compiler to write:

OCR1A = target_timer_count * 9 / 10; 

or use casting ???

OCR1A = (uint16_t)(target_timer_count * 0.9);

appreciate your help guys you are great...

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

Ifryd wrote:

Would it be better for compiler to write:

OCR1A = target_timer_count * 9 / 10; 

or use casting ???

OCR1A = (uint16_t)(target_timer_count * 0.9);


The first line uses all integer math while the second will result in floating point operations (much slower on the AVR).

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

Quote:

AVRstudio cannot properly simulate OCR1AH and OCR1BH (clear these values). I have heard AVRstudio 4 has problem with it

Perhaps because it says that in the user manual? It also says that "if your AVR is supported by Simulator 2 then ALWAYS use that in preference to the original simulator" - are you following that advice?

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

I read it thanks I wish there would be more devices supporting Simulator v.2 .
I also read your tutorial "Optimization and the importance of volatile in GCC " great article thank you for this...

Adam

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

Quote:

I wish there would be more devices supporting Simulator v.2

Have you picked up 4.19 yet? It's got quite a lot of the "old" models supported (they had to do this as it's the same simulator used in AS5 and in that there is no copy of "Sim 1"). For example Sim2 now has mega8, mega16 and mega32 added.

PS I looked back at your old posts - surely you are using 1284P? That's always been supported by Sim2 since it emerged.

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

Quote:

The first line uses all integer math while the second will result in floating point operations (much slower on the AVR).


True enough. However, the coder must consider that *9/10 will fail when target_timer_count is greater than ~7000. (Maybe half that when signed?)

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

theusch wrote:
Quote:

The first line uses all integer math while the second will result in floating point operations (much slower on the AVR).


True enough. However, the coder must consider that *9/10 will fail when target_timer_count is greater than ~7000. (Maybe half that when signed?)

Right, a programmer must always be aware of the intermediate values that result from any scaling operations. Another approach which avoids the problem is

scaled_val = starting_val - starting_val / 10;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Have you picked up 4.19 yet? It's got quite a lot of the "old" models supported (they had to do this as it's the same simulator used in AS5 and in that there is no copy of "Sim 1"). For example Sim2 now has mega8, mega16 and mega32 added.

Good to know thank you... I downloaded it and I will start working on it. I always hesitate before downloading new software. I lost few days once being up to date with the most recent Winavr versions. The Latest versions have many things fixed but also many unknown bugs.
But as far as I am concern Avrstudio 4 is discontinued so I believe 4.19 should be without serious unknown bugs and should be the most reliable.

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

Quote:
...so I believe 4.19 should be without serious unknown bugs and should be the most reliable.
Well... it's NOT ! It has an issue with correctly displaying the reg. view ( may be fixed now, though) , and it won't let my Dragon come out and play :cry: ! There's something about SP3 for 4.18 and integrated in 4.19, that jams up the Dragon.

I don't know what other major enough things, if any, may be wrong with it.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

IJ,

Are you talking about the beta 4.19 (which WAS seriously broken) or the final release (which seems fine so far)?

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

It must have been the beta. I stopped tracking it after beta left a bitter taste in my mouth, 2x :cry: , and I didn't know there was a final. I'll give the final a shot and it should work out then.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1