PWM
value set to Output Compare Register or OCRx decides what will be pulse width modulated output.
Valid range for OCRx is 0-255
In the following code snippet, control(x,y) determines the value of voltage level at pin 4 and 21.
This is in context with driver controlled induction load operation.
#include<avr\io.h>
#include<util\delay.h>
#define mid0 10
#define mid1 17
#define mid2 52
void control(unsigned char x,unsigned char y)
{
TCCR0=0x6B;
TCCR2=0x6B;
OCR0=x; // Pin 4
OCR2=y; // Pin 21
}
void forward()
{
PORTB=0x01;
PORTD=0x01;
}
void delay_ms(unsigned char time_ms)
{
unsigned short delay_count = F_CPU / 4000;
unsigned short cnt;
asm volatile ("\n"
"L_dl1%=:\n\t"
"mov %A0, %A2\n\t"
"mov %B0, %B2\n"
"L_dl2%=:\n\t"
"sbiw %A0, 1\n\t"
"brne L_dl2%=\n\t"
"dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt) :"r"(time_ms), "r"((unsigned short) (delay_count)));
}
void delay(int a)
{
int i;
for(i=0;i<=a;i++)
delay_ms(100);
}
int main()
{
DDRB=0xFF;
DDRC=0xFF;
DDRD=0xFF;
while(1)
{
control(255,255); // for full speed
forward();
}
return 0;
}//end of main