AVR(ATtiny2313)による高速PWM動作を使ったRCサーボのテスト@C言語
#include<avr/io.h>
#include <avr/interrupt.h>
/***************関数******************/
void wait(uint16_t w){
while(w--){
volatile uint16_t i=200;
while(i--);
}
}
void servo_init(void){
TCNT1 = 0x00;
OCR1A = 0x00;
ICR1 = 0x9C4;
TCCR1A = 0b10000010;
TCCR1B = 0b00011010;
}
/**************メイン*****************/
int main(){
DDRB = 0xFF;
servo_init();
OCR1A = 0xFA;
while(1) {
OCR1A = 0x7D;
wait(1000);
OCR1A = 0xFA;
wait(1000);
}
}//==========================================================================
//Global variables and definition
#define PULSE_WIDTH 0x40
void pwm_start(){
OCR1AL = PULSE_WIDTH; //Load Pulse width
OCR1AH = 0;
DDRD |= (1<<5); //PortD.5 as o/p
TCCR1A = 0x81; //8-bit, Non-Inverted PWM
TCCR1B = 1; //Start PWM
}//==========================================================================
No comments:
Post a Comment