BASIC4MCU | 모터 | 서보모터 | 9핀(GPIO3 + 16비트 PWM 6)으로 서보모터 48개 구동
페이지 정보
작성자 키트 작성일2017-09-05 17:19 조회2,864회 댓글0건본문
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 //#define CVAVR // 코드비젼 사용자는 제일 앞에 막은 것을 삭제하면 됩니다.//#ifdef CVAVR#include <mega128.h>#include <delay.h>#else#define F_CPU 16000000UL // 16 MHz#define delay_ms(x) _delay_ms(x)#include <avr/io.h> //16MHz#include <util/delay.h>#include <avr/interrupt.h>#endif//volatile unsigned int pwm[8][6]={{3600,2500,2500,2500,2500,2500},{2500,2500,2500,2500,2500,2500},{2500,2500,2500,2500,2500,2500},{2500,2500,2500,2500,2500,2500},{2500,2500,2500,2500,2500,2500},{2500,2500,2500,2500,2500,2500},{2500,2500,2500,2500,2500,2500},{2500,2500,2500,2500,2500,2500}};volatile unsigned char n=0;//#ifdef CVAVRinterrupt [TIM1_OVF] void timer1_ovf_isr(void){#elseISR(TIMER1_OVF_vect){#endifTCCR2=0x0C;}//#ifdef CVAVRinterrupt [TIM2_COMP] void timer2_comp_isr(void){#elseISR(TIMER2_COMP_vect){#endifTCCR2=0;PORTA=n;OCR1A=pwm[n][0]; OCR1B=pwm[n][1];OCR1CH=pwm[n][2]>>8; OCR1CL=pwm[n][2]&0xFF;OCR3AH=pwm[n][3]>>8; OCR3AL=pwm[n][3]&0xFF;OCR3BH=pwm[n][4]>>8; OCR3BL=pwm[n][4]&0xFF;OCR3CH=pwm[n][5]>>8; OCR3CL=pwm[n][5]&0xFF;if(++n>7)n=0;}//#ifdef CVAVRvoid main(void){#elseint main(void){#endifint i;unsigned char k,m;DDRA=0x07; DDRB=0xE0; DDRE=0x38;ICR1=4999; ICR3H=4999>>8; ICR3L=4999&0xFF; // 20ms/8=2.5msTCCR1A=0xAA; TCCR3A=0xAA;TCCR1B=0x1A; TCCR3B=0x1A;OCR2=149; TIMSK=0x84; //16000000/256/(1+149)=416.6667,2.4msSREG=0x80;while(1){for(i=1200;i<=4800;i+=20){ for(k=0;k<8;k++)for(m=0;m<6;m++)pwm[k][m]=i; delay_ms(20); }for(i=4800;i>=1200;i-=20){ for(k=0;k<8;k++)for(m=0;m<6;m++)pwm[k][m]=i; delay_ms(20); }}}cs OCR1C까지 서보모터로 출력하기 위해서타이머2를 동기시켜서 사용합니다.
댓글 0
조회수 2,864등록된 댓글이 없습니다.