Hello,
I bought myself an Atmega 2560 some days ago.
I want to write my own analogWrite().
Anybody got an idea how to write analogWrite() without using any type of Arduino-functions?
Is there any instruction for writing analogWrite() ? I just need it for the ports A0 , A1 .
I'm sorry i have nearly no experience. I want to write the function for pins 6,7,11,12 .
I got the task to implement my own analogWrite() for the pins 6,7,11,12 but i really do not know where to start to do so.
It is homework and I don't want you to do my homework, I just want to ask if there is any kind of instruction that can help me with implementing the function cause for analogRead() there is some kind of basic instruction set.
I can't find the link I'm sry but it was like a 7step guide to implement the analogRead().
I'm reading right now ![]()
you can implement an analogWrite(p, v) with an interrupt routine. In psuedo code
byte state = 0;
byte val = 0;
void PWM(pin, v)
{
val = v;
}
void IRQ()
{
digitalWrite(pin1, state); // better use fast direct port IO
state = 1 - state;
if (state)
{
load interrupt timer with val;
}
else
{
load interrupt timer with 255-val;
}
}
the interrupt will count down to from v to zero OR from 255-v to zero. By writing another value to v the PWM changes. Doing multiple pins means your administration of pin states becomes bigger.
success
So i have to use Timer1 for pin 11 / 12 and Timer4 for pint 6/7 but how can i set the value for the timers ?
The in-built analogWrite() function relies on the fact that the Timers inside the Atmega chip automatically create PWM signals on specific pins - two pins for each Timer. That means that the generation of the PWM signal imposes almost no load on the microprocessor.
There is nothing to stop you writing code that causes a Timer to produce an interrupt and writing a line or two of code in the Interrupt Service Routine (ISR) that will change the state of any of the Arduino pins.
...R