I hope to light the LED in breathing mode by PWM drive through TCD in TINNY814.
But there is a problem for me: the duty ratio will not chang while I changing the CMPBSET.
I have tried the code from
It still keeping the duty ratio set in initialise function.
As the code shows:
/* * T817servo12b.c * * Created: 2/14/2018 20:18:49 * Author : J.Stampfl */ #define F_CPU 20000000ul #include <avr/io.h> #include <util/delay.h> volatile uint32_t knt; int main(void) { //Connect the servos and/or connect LEDs to pins PA4 & PA5 VPORTA.DIR = 0x30; // set PA4 & PA5 to output //setup TIMER TCD0.CTRLA = 0b01101100; //CLOCK PER, cnt clk = 4,sync clk = 4 as divisors //CLOCK PER IS NOT SET HERE. Just selected. Default is 6, Total divisor = 6*4*4 = 96 CPU_CCP = 0XD8; //Magic number for Configuration Change Protection. Chapt. 8 in Datasheet TCD0.FAULTCTRL = 0x30; //If you set the fuses, don't need this. TCD0.CMPACLR = 94; // ~0 degree on PA4 TCD0.CMPBSET = 4095 - 94; // ~0 degree on PA5 TCD0.CMPBCLR = 4095; // max count of timer // Start the TIMER TCD0.CTRLA |= 1; // Need |= here. while (1) { for (uint16_t ii = 94;ii < 510;ii=ii+10) //94 ~ 0 degrees, 509 ~180 degrees { TCD0.CMPACLR = ii; TCD0.CMPBSET = 4094 - ii; TCD0.CTRLE = 0b00000001; // Sync the registers _delay_ms(100); } for (uint16_t ii = 509;ii > 95;ii=ii-10) { TCD0.CMPACLR = ii; TCD0.CMPBSET = 4094 - ii; TCD0.CTRLE = 0b00000001; _delay_ms(100); } } }
We should see the duty ratio changing in PIN5,yet, NO.
I just want to use PWM B on PIN5, it is really simple for me:
#define F_CPU 3333333ul #include <avr/io.h> #include <util/delay.h> volatile uint32_t knt; int main(void) { //Connect the servos and/or connect LEDs to pins PA4 & PA5 VPORTA.DIR = 1<<5; // set PA5 to output //CLOCK PER IS NOT SET HERE. Just selected. Default is 6, Total divisor = 6*4*4 = 96 CPU_CCP = 0XD8; //Magic number for Configuration Change Protection. Chapt. 8 in Datasheet TCD0.FAULTCTRL = 0x20; //If you set the fuses, don't need this. TCD0.CMPBSET = 4095 - 94; // ~0 degree on PA5 TCD0.CMPBCLR = 4095; // max count of timer // Start the TIMER TCD0.CTRLA |= 1; // Need |= here. while (1) { for (uint16_t ii = 94;ii < 510;ii=ii+10) //94 ~ 0 degrees, 509 ~180 degrees { TCD0.CMPBSET = 4094 - ii; TCD0.CTRLE = 0b00000001; // Sync the registers _delay_ms(10); } for (uint16_t ii = 509;ii > 95;ii=ii-10) { TCD0.CMPBSET = 4094 - ii; TCD0.CTRLE = 0b00000001; _delay_ms(10); } } }
the ratio is keeping same : (TCD0.CMPBCLR - TCD0.CMPBSET)/TCD0.CMPBCLR = 94/4095
TCD is complex, I do not master it yet, and the datasheet need to be updated.
Actually there is no CMPC & CMPD, it wasted me much time to find where they output to till I read post of jstampfl.