Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Timer 1 16-bit register write problem (ATmega328P)

2 posters

Go down  Message [Page 1 of 1]

Niall



I have seen an issue when configuring Timer 1 for fast PWM operation on an ATmega328P microcontroller. I am using mode 14 with the ICR1 register setting the count TOP value. I load the ICR1 register with 19999 (0x4E1F). However when simulating, the ICR1 register contains the value 19968 (0x4E00). It seems that the low byte is not written to the ICR1L register.

I have tested my code on a microcontroller board and the ICR1 register is configured as expected. My C code is shown below.

int main(void)
{
uint8_t x, ch;
int8_t LED_level = 0, motorLevel = 0;

/* Initializes MCU, drivers and middleware */
atmel_start_init();

/* Place GPIO and peripheral initialisation code here */
PRR &= 0xF7;
PORTB_set_pin_dir(LED, PORT_DIR_OUT);
PORTB_set_pin_dir(MOTOR, PORT_DIR_OUT);
TCCR1A = 0xA2;
ICR1 = 19999; //100Hz PWM, FCPU = 16MHz, N = 8
printf("ICR1: %d\n\r", ICR1);
OCR1A = LED_level * ICR1/10;
OCR1B = motorLevel * ICR1/10;
TCCR1B = 0x1A;



Last edited by Niall on Tue Mar 15, 2022 2:23 pm; edited 1 time in total (Reason for editing : Could not upload code file so I have added to post as text.)

arcachofo

arcachofo

Hi.
Which version are you using?

Niall



arcachofo wrote:Hi.
Which version are you using?

Hi,
I'm using version 0.4.15-SR7

arcachofo

arcachofo

Thanks.
I will have a look.

arcachofo

arcachofo

Hi.
I'm getting into this issue and there are some things:

Have you tried with 0.4.15-SR9?

If the issue also happens in SR9:
The code you posted is not complete.
You can zip the folder with the whole project and attach in your post.

In any case there are some questions:
- Does the PWM work as espected?
- How did you determine that ICR1 register contains the value 19968?


Niall



arcachofo wrote:Hi.
I'm getting into this issue and there are some things:

Have you tried with 0.4.15-SR9?

If the issue also happens in SR9:
The code you posted is not complete.
You can zip the folder with the whole project and attach in your post.

In any case there are some questions:
- Does the PWM work as espected?
- How did you determine that ICR1 register contains the value 19968?



Hi,
Thanks for the reply. The same issue exists with 0.4.15-SR9. I use a printf statement to see the ICR1 register value. I also monitor the ICR1H, ICR1L registers in the MCU monitor. These display as 0x4E and 0x00. 0x4E00 is 19968 decimal. The ICR1H register contains the correct value in the simulator but the ICR1L register should contain 0x1F. The simulated PWM frequency is 100.2Hz instead of the expected 100Hz - again due the ICR1L register not being loaded.

I have tested the same code on an ATmega328P micro clocked at 16MHz. I see the correct ICR1 register value of 19999 printed on a serial terminal and the PWM waveform shows as 100Hz on an oscilloscope.

I also had a look in the simulator at the OCR1A register which is also 16-bit. This register gets loaded correctly on simulation. The problem only appears to be with the ICR1 register.

I have attached my C file as a zip file. The code generates a 100Hz PWM waveform with 50:50 duty cycle on the timer 1 OC1A pin. The register configuration used is based on a 16MHz CPU clock. I was unable to upload my entire Atmel Studio Start project as the zipped folder was too large.

#define outPin 1

int main(void)
{
atmel_start_init();
PRR &= 0xF7;
PORTB_set_pin_dir(outPin, PORT_DIR_OUT);
TCCR1A = 0x82;
TCCR1C = 0;
ICR1 = 19999; //100Hz PWM, FCPU = 16MHz, N = 8
OCR1A = 10000;
TCCR1B = 0x1A; //Fast PWM Mode 14, Prescaler = 8

printf("ICR1: %u   0x%4x\n", ICR1, ICR1);
printf("OCR1A: %u   0x%4x\n", OCR1A, OCR1A);

while (1) {
}
}
Attachments
Timer 1 16-bit register write problem (ATmega328P) Attachmentmain.zip
You don't have permission to download attachments.
(1 Kb) Downloaded 1 times



Last edited by Niall on Fri Mar 25, 2022 12:49 pm; edited 1 time in total

arcachofo

arcachofo

Thanks.

Looks like that problem is already solved.
Soon I will updload 0.4.15-SR10.

Timer 1 16-bit register write problem (ATmega328P) Icr110

Niall



Thanks.

Sponsored content



Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum