當前位置:編程學習大全網 - 源碼下載 - AVR單片機pwm波改變占空比

AVR單片機pwm波改變占空比

//ICC-AVR application builder : 2010-5-21 13:14:09

// Target : M16

// Crystal: 4.0000Mhz

#include <iom16v.h>

#include <macros.h>

void delay(unsigned int m)

{

unsigned int i,j;

for(i=0;i<m;i++)

for(j=0;j<570;j++);

}

void port_init(void)

{

PORTA = 0x00;

DDRA = 0x00;

PORTB = 0x00;

DDRB = 0xFF;

PORTC = 0x00; //m103 output only

DDRC = 0x00;

PORTD = 0x00;

DDRD = 0xFF;

}

//TIMER0 initialize - prescale:8

// WGM: PWM Phase correct

// desired value: 1mSec

// actual value: 1.020mSec (-2.0%)

void timer0_init(void)

{

TCCR0 = 0x00; //stop

TCNT0 = 0x00; //set count

OCR0 = 0x64; //set compare

TCCR0 = 0x62; //start timer

}

//call this routine to initialize all peripherals

void init_devices(void)

{

//stop errant interrupts until set up

CLI(); //disable all interrupts

port_init();

timer0_init();

MCUCR = 0x00;

GICR = 0x00;

TIMSK = 0x00; //timer interrupt sources

SEI(); //re-enable interrupts

//all peripherals are now initialized

}

//

void main(void)

{

init_devices();

while(1)

{

}

}

妳使OCR0的值 由大變小,即輸出的平均電壓變小,就可達到 燈由亮到滅

  • 上一篇:C++設計壹個用於人事管理的 People(人員)類。
  • 下一篇:《水滸傳》中108英雄的名字和綽號是什麽?
  • copyright 2024編程學習大全網