BASIC4MCU | 모터 | 서보모터 | 11핀(GPIO 5 + 16비트 PWM 6)으로 서보모터 96개 구동
페이지 정보
작성자 키트 작성일2017-09-05 17:20 조회2,829회 댓글0건첨부파일
본문
잘 안보일 것 같아서 중요한 부분만 큰 사진으로 올립니다.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 //#define CVAVR // 코드비젼 사용자는 제일 앞에 막은 것을 삭제하면 됩니다.//#ifdef CVAVR#include <mega128.h>#include <delay.h>#else#define F_CPU 16000000UL // 16 MHz#define delay_ms _delay_ms#define delay_us _delay_us#include <avr/io.h> //16MHz#include <util/delay.h>#include <avr/interrupt.h>#endif//volatile unsigned int pwm[16][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},{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,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){#endifunsigned char sel[16]={0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};TCCR2=0;PORTA=sel[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>15)n=0;}//#ifdef CVAVRvoid main(void){#elseint main(void){#endifint i;unsigned char k,m;DDRA=0x1F; 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<16;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<16;k++)for(m=0;m<6;m++)pwm[k][m]=i; delay_ms(20); }}}cs PWM은 40ms 주기입니다.
댓글 0
조회수 2,829등록된 댓글이 없습니다.