simple PWM avr assembler atmega328pb not working

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

Hi Everyone,
I am trying to send an Infrared signal from PB1 on an arduino UNO board.
I have changed the crystal on the board to 3.6864 MHz and adjusted the fuses.

Just to verify my board is working, I am posting working code.

This code works on OC0A (PD6):

.nolist
.include "m328pbdef.inc"
.list

.cseg
.org 0
	rjmp	Reset

Reset:
	ldi	r16,HIGH(RAMEND)
	out	SPH,r16		
	ldi	r16,LOW(RAMEND)
	out	SPL,r16
	sbi	DDRD,DDRD6		;OC0A = output

	ldi	r16, (1 << COM0A0) | (1 << WGM01)	;toggle OC0A on match
	out	TCCR0A, r16

	ldi   	r16,(1 << CS00)	; CTC mode, no prescaling
	out   	TCCR0B,r16

	ldi	r16, 47
	out	OCR0A, r16

forever:
	rjmp	forever

I wanted to use OC1A (PB1) instead. This code is working but only at 14.38 KHz.
I have tried to change the OCR1A values but no matter
what I do it produces the same result, 14.38 KHz.

I got these values from avrcalc.exe.

Can someone PLEASE tell me what I am doing wrong?

Here is my code:

.nolist
.include "m328pbdef.inc"
.list

.cseg
.org 0
	rjmp	Reset

Reset:
	ldi	r16,HIGH(RAMEND)
	out	SPH,r16
	ldi	r16,LOW(RAMEND)
	out	SPL,r16

	sbi	DDRB,DDRB1		;OC1A = output

	ldi	r16, (1 << COM1A1) | (1 << WGM10)	;toggle OC1A on match
	sts	TCCR1A, r16

	ldi   	r16,(1 << WGM12) | (1<<CS10)	;CTC mode, no prescaling
	sts   	TCCR1B,r16

	clr	r16
	sts	OCR1AH, r16
	ldi	r16, 47			;38.4KHz (derived from avrcalc and working with OC0A code above)
	sts	OCR1AL, r16

forever:
	rjmp	forever

 

This topic has a solution.
Last Edited: Mon. Jul 13, 2020 - 01:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

your code selected pwm. According to the datasheet you want mode 4 - CTC. Set WGM12

 

WTH did you change the crystal??  No problems generating IR signals using the 16MHz source! 

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

Kartman wrote:

your code selected pwm. According to the datasheet you want mode 4 - CTC. Set WGM12

 

WTH did you change the crystal??  No problems generating IR signals using the 16MHz source! 

 

yes already done, look at the code above

        ldi   	r16,(1 << WGM12) | (1<<CS10)	;CTC mode, no prescaling
	sts   	TCCR1B,r16

I changed the crystal for another reason previously, just wanted to use this board temporarily.

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

You've set WGM10. 

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

Kartman wrote:

You've set WGM10. 

So in other words, I should NOT set WGM10?

 

I removed it and now there is no output from PB1 at all.

I guess I had it configured for mode 5.

Last Edited: Mon. Jul 13, 2020 - 01:53 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok I found the problem.  Thanks Kartman for steering me in the right direction.

The error is highlighted in bold.

This is the working code:

.nolist
.include "m328pdef.inc"
.list

.cseg
.org 0
	rjmp	Reset

Reset:
	ldi	r16,HIGH(RAMEND)
	out	SPH,r16
	ldi	r16,LOW(RAMEND)
	out	SPL,r16

	sbi	DDRB,1		                ;OC1A = output

;error here, should have been COM1A0 not COM1A1
;	ldi	r16, (1 << COM1A1) 		;toggle OC1A on match
	ldi	r16, (1 << COM1A0) 		;toggle OC1A on match
	sts	TCCR1A, r16

	ldi   	r16,(1 << WGM12) | (1<<CS10)	;CTC mode 4, no prescaling
	sts   	TCCR1B,r16

	clr	r16
	sts	OCR1AH, r16
	ldi	r16, 47
	sts	OCR1AL, r16

stop:
	rjmp	stop
Last Edited: Mon. Jul 13, 2020 - 01:53 PM