PWM
Moderátor: Moderátoři
PWM
Tu je program>
#include <C:\WinAVR-20090313\avr\include\avr\io.h>
#define increaseButton_PRESSED !(PIND & 0x40)
#define increaseButton_OPEN (PIND & 0x40)
#define decreaseButton_PRESSED !(PIND & 0x80)
#define decreaseButton_OPEN (PIND & 0x80)
#define COUNTER_MIN 0x0090
#define COUNTER_MAX 0x00f8
#define KROK 0x0008
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x08;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0xC0;
DDRD = 0x00;
}
void timer2_init(void)
{
TCCR2 = 0x61; //setup
OCR2 = COUNTER_MIN;
}
void init_devices(void)
{
//stop errant interrupts until set up
//CLI(); //disable all interrupts
port_init();
timer2_init();
//MCUCR = 0x00;
//GICR = 0x00;
//TIMSK = 0x00; //timer interrupt sources
//SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//***** DELAY OF 1 mSEC (appx.) *******
void delay_ms(int miliSec) //for 1 Mhz crystal
{
int i,j;
for(i=0;i<miliSec;i++)
for(j=0;j<100;j++)
{
asm("nop");
asm("nop");
}
}
//************************ main ***************************
void main(void)
{
unsigned int counter = COUNTER_MIN;
init_devices();
while(1)
{
}
if(increaseButton_PRESSED)
{
delay_ms(20);
if(increaseButton_OPEN)
while(increaseButton_PRESSED);
delay_ms(20);
if(counter >= COUNTER_MAX)
counter = COUNTER_MAX;
else
counter += KROK;
OCR2 = counter;
}
else
{
delay_ms(20);
if(decreaseButton_OPEN) ;
while(decreaseButton_PRESSED);
delay_ms(20);
if(counter <= COUNTER_MIN)
counter = COUNTER_MIN;
else
counter -= KROK;
OCR2 = counter;
}
}
//****************************** END ***************
Andrea píše:Tam, co jsi ten program opajcoval, mají ...
Přesně: http://images.robolabz.multiply.multipl ... =279223533
- vidím špatně udělanou funkci pro zpožďování. Převážná část kódu bude v těch cyklech for a ne v nop. Takže výsledek silně závisí na tom, jakou optimalizaci překladač provede. Pro zpožďování jsou určené funkce z avr-libc _delay_ms() a _delay_us().
PS: nezapomeň povolit globální přerušení sei() ;
- Přílohy
-
INTvektory.jpg- (60.78 KiB) Staženo 113 x