⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm tiny.c

📁 Atmel tinny pwm source code
💻 C
字号:
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

volatile int pwm = 0; /* Note [1] */
volatile uint8_t direction;

SIGNAL (SIG_OVERFLOW0) /* Note [2] */
{
	if(pwm > 255) pwm = 255;
	if(pwm < 0) pwm = 0;
    OCR0A = pwm; /* Note [4] */
}

void ioinit (void) /* Note [5] */
{
    /* tmr0 is 8-bit PWM: */
	// WGM02.WGM01.WGM00 = 000 : Normal Mode, TOP = 0xFF
	// COM0A1.COM0A0 = 00 : OC0A disconected
	// CS02.CS01.CS00 = 001 : Clock source from Clk/1 (no prescaling)
    TCCR0A =  0;
    TCCR0B =  _BV(CS00);

    /* set PWM value to 0 */
    OCR0A = 0;

    /* enable OC0A (PB0) as output */
    DDRB = _BV (DDB0) | _BV (DDB1);

//    timer_enable_int(_BV (TOIE0));

    /* enable interrupts */
//    sei ();
}

void delay(void)
{
	unsigned int n;
	for(n=50000;n>0;--n);
//		_delay_ms(1.0);
}

int main (void)
{
    ioinit ();
    /* loop forever, the interrupts are doing the rest */

	pwm = 0;
    for (;;){
//		if(++pwm>255) pwm = 0;
		pwm = 128;
		delay();
	}

    return (0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -