arduino / arduino/ArduinoCore-megaavr

Arduino Wifi Rev2, bug when reading duty cycle value on pin 6

Open
#91 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
111
Forks
65
PR merge metrics
No merged PRs in 30d

Description

Hi,
I am trying to read the duty cycle set for pin 6 on the Arduino Wifi Rev2. By default, the timer used is TCB0, used in 8-bit PWM mode. The 16-bit Compare channel CCMP is used as two 8-bit registers, the lowest byte CCMPL is for the period, while the highest byte CCMPH is for the duty cycle.

However, something weird happens when I try to read the Compare channel register, the value of CCMPH is unexpecteadly copied into CCMPL. The same happens with pin 3 (which uses TCB1).

```
// PWM on pin 6, timer TCB0
//TCB_t* timer_B0 = (TCB_t *)&TCB0; // pointer on timer structure

void setup() {
pinMode(6, OUTPUT);
Serial.begin(9600);
}

void loop() {
setPWMValueOnPin6(20);
setPWMValueOnPin6(40);
setPWMValueOnPin6(60);
setPWMValueOnPin6(80);
}

void printCompareChannelValues()
{
Serial.print("Compare channel = ");
unsigned int r = TCB0_CCMP;
//byte r = timer_B0->CCMP;
Serial.println(r);

Serial.print("Compare channel L = ");
byte l = TCB0_CCMPL;
//byte l = timer_B0->CCMPL;
Serial.println(l);

Serial.print("Compare channel H = ");
byte h = TCB0_CCMPH;
//byte h = timer_B0->CCMPH;
Serial.println(h);
}

void setPWMValueOnPin6(byte value)
{
analogWrite(6, value);
printCompareChannelValues();
delay(2000);
}
```

Here is the serial monitor output:

Compare channel = 5375
Compare channel L = 255
Compare channel H = 20
Compare channel = 10260
Compare channel L = 20
Compare channel H = 40
Compare channel = 15400
Compare channel L = 40
Compare channel H = 60
Compare channel = 20540
Compare channel L = 60
Compare channel H = 80
Compare channel = 5200
Compare channel L = 80
Compare channel H = 20
Compare channel = 10260
Compare channel L = 20
Compare channel H = 40
Compare channel = 15400
Compare channel L = 40
Compare channel H = 60
Compare channel = 20540
Compare channel L = 60
Compare channel H = 80
Compare channel = 5200
Compare channel L = 80
Compare channel H = 20

This occurs even when removing the serial functions (I can see that by watching a led connected on pin 6).
Is that a normal behavior?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the supplied sketch and the TCB0/TCB1 CCMP, CCMPL, and CCMPH register accesses. Check the ATmega4809 timer documentation and the core's register definitions to determine whether the observed byte reads are expected; done means documenting or correcting the behavior with a focused reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.