BASIC4MCU | 질문게시판 | 답변 : atmega128 코드 주석 문의요
페이지 정보
작성자 master 작성일2018-12-13 12:55 조회6,403회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=6816
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-12-13 오후 12:59:42// by Ok-Hyun Park//#include <avr/io.h>#include <avr/interrupt.h>#include "OK-128DCM.h"//#define Kp 0.1#define Ki 1.8#define Ts 0.01//float Wref_table[10]={+1500.,-1500.,+3000.,-3000.,+4500.,-4500.,+5500.,-5500.,0.,+5500.};volatile unsigned char m1,count,count0=0; // rotary encode pulsevolatile unsigned char direction; // motor directionvolatile unsigned char flag=0; // graphic flagunsigned char tableindex=0; // Wref data tablevolatile signed int PWM; // PWM output datavolatile unsigned int Wref_time=0; // time counter for Wref settingvolatile unsigned int step=0; // graphic step countvolatile float Ia; // motor currentvolatile float Wref,Wr,Werr,Werr0=0.; // motor speedvolatile float PIconstant,PI=0.; // PI variablesvolatile float Wrefx,Wrefx0; // reference speedvolatile float Wrx,Wrx0; // motor speedvolatile float Ia,Iax,Iax0; // motor current//void Graphic_clear(void){unsigned int x,y;for(y=21;y<=219;y++){ TFT_GRAM_address(27,y); for(x=27; x<=300; x++)TFT_data(Black); }}//void Graphic_line(void){unsigned int x,y;Rectangle(26,20,301,220,Cyan);for(x=76;x<=276;x+=50){ Line( x,18, x,22,Cyan); Line( x,218, x,222,Cyan); for(y=25;y<=215;y+=5)TFT_pixel(x,y,Silver); }for(y=45;y<=195;y+=25){ Line(24, y,28, y,Cyan); Line(299, y,303, y,Cyan); for(x=31;x<=296;x+=5)TFT_pixel(x,y,Silver); }Line(26,120,301,120,Cyan);}//void Graphic_scale(void){TFT_string(10,0,White,Magenta," RA-35GM 양방향 제어 ");TFT_string(0,0,Green,Black,"x100[rpm]");TFT_English_pixel( 0,213,'-'); TFT_English_pixel( 7,213,'6'); TFT_English_pixel( 15,213,'0');TFT_English_pixel( 0,188,'-'); TFT_English_pixel( 7,188,'4'); TFT_English_pixel( 15,188,'5');TFT_English_pixel( 0,163,'-'); TFT_English_pixel( 7,163,'3'); TFT_English_pixel( 15,163,'0');TFT_English_pixel( 0,138,'-'); TFT_English_pixel( 7,138,'1'); TFT_English_pixel( 15,138,'5');TFT_English_pixel( 15,113,'0');TFT_English_pixel( 0, 88,'+'); TFT_English_pixel( 7,88,'1'); TFT_English_pixel( 15, 88,'5');TFT_English_pixel( 0, 63,'+'); TFT_English_pixel( 7,63,'3'); TFT_English_pixel( 15, 63,'0');TFT_English_pixel( 0, 38,'+'); TFT_English_pixel( 7,38,'4'); TFT_English_pixel( 15, 38,'5');TFT_English_pixel( 0, 13,'+'); TFT_English_pixel( 7,13,'6'); TFT_English_pixel( 15, 13,'0');TFT_string(37,0,Magenta,Black,"[A]");TFT_English_pixel(305,213,'-'); TFT_English_pixel(312,213,'2');TFT_English_pixel(305,163,'-'); TFT_English_pixel(312,163,'1');TFT_English_pixel(305,113,'0');TFT_English_pixel(305, 63,'+'); TFT_English_pixel(312, 63,'1');TFT_English_pixel(305, 13,'+'); TFT_English_pixel(312, 13,'2');TFT_string(33,28,Cyan,Black,"[sec]");TFT_English_pixel( 22,224,'0');TFT_English_pixel( 66,224,'0'); TFT_English_pixel( 72,224,'.'); TFT_English_pixel( 78,224,'5');TFT_English_pixel(116,224,'1'); TFT_English_pixel(122,224,'.'); TFT_English_pixel(128,224,'0');TFT_English_pixel(166,224,'1'); TFT_English_pixel(172,224,'.'); TFT_English_pixel(178,224,'5');TFT_English_pixel(216,224,'2'); TFT_English_pixel(222,224,'.'); TFT_English_pixel(228,224,'0');}//ISR(TIMER1_COMPA_vect){ /*OC1A interrupt function(0.01s period)*/count=TCNT2; // read TCNT2if((PINE&0x80)==0x80)direction='F'; // read motor directionelse direction='R';m1=count-count0; // calculate m1(rotary encoder pulse)count0=count;Wr=(float)m1*60./52./0.01; // calculate Wr[rpm]if(direction=='R')Wr=-Wr;Werr=Wref-Wr; // PI controllerPI=PI+PIconstant*Werr-Kp*Werr0;Werr0=Werr;if(PI>99.)PI=99.; // PI control limiterelse if(PI<-99.)PI=-99.;if(PI>=0.){ PWM= PI+0.5; OCR3B=PWM; PORTG=0x01; } // output PWM value for forwardelse { PWM=-PI+0.5; OCR3B=PWM; PORTG=0x02; } // output PWM value for reverseWref_time++;flag=1; // set graphic flagstep++; // increment graphic stepWrefx0=Wrefx; Wrefx=Wref; Wrx0=Wrx; Wrx=Wr; Iax0=Iax; Iax=Ia; // store new value}//int main(void){unsigned char i,mode,polarity='+';signed int temp,sum;unsigned int y0,y;MCU_initialize(); Delay_ms(50);TFT_initialize(); Graphic_line(); Graphic_scale();TFT_string(4,3,Cyan,Black,"Kp="); TFT_unsigned_float(Kp,2,1);TFT_string(4,5,Cyan,Black,"Ki="); TFT_unsigned_float(Ki,2,1);if((PINB&0x40)==0x40){ mode='M'; TFT_string(32,0,Cyan,Black,"Manu"); } // manual modeelse { mode='A'; Wref=Wref_table[tableindex]; tableindex++; TFT_string(32,0,Cyan,Black,"Auto"); } // auto modePIconstant=Kp+Ki*Ts; // calculate PI constantDDRE=0x3C; // PORTE(DDRE4=1)//(OC3C=OC3B=OC3A=0)TCCR3A=0x22; TCCR3B=0x1A; ICR3=99; // fast PWM mode(14)// 16MHz/8/(1+99)=20kHzTCCR1B=0x0C; OCR1A=624; TIFR=0x10; TIMSK=0x10;TCCR2=0x06;sei();while(1){if((PINB&0x40)==0x40){if(mode=='A'){ mode='M'; TFT_string(32,0,Cyan,Black,"Manu"); }ADCSRA=0x84; ADMUX=0x43; Delay_us(150);sum=0; for(i=0;i<4;i++){ ADCSRA=0xD4; while((ADCSRA&0x10)!=0x10); sum+=ADCW; Delay_ms(1); }sum=sum>>2;if(sum>=0x200){ sum-=0x200; polarity='+'; }else { sum=0x200-sum; polarity='-'; }if(sum>511)sum=511;Wref=(float)sum*5500./511.;if(polarity=='-')Wref=-Wref;}else{if(mode=='M'){ mode='A'; TFT_string(32,0,Cyan,Black,"Auto"); }if(Wref_time>=300){ Wref_time=0; Wref=Wref_table[tableindex]; tableindex++; tableindex%=10; }}ADCSRA=0x87; ADMUX=0xE9; Delay_us(150);sum=0; for(i=0;i<4;i++){ ADCSRA=0xD7; while((ADCSRA&0x10)!=0x10); temp=ADCW; sum+=(temp>>6); Delay_ms(1); }sum=sum>>2;Ia=(float)sum*0.256/511./0.1;if(Ia>2.)Ia=2.;if(flag==1){ flag=0;if(step==1){ Iax0=Iax; Wrefx0=Wrefx; Wrx0=Wrx; }if((PINB&0x80)==0x00){y0=120.-Iax0*100./2.+0.5; y=120.-Iax*100./2.+0.5; Line(26+step-1,y0,26+step,y,Magenta);}y0=120.-Wrefx0*100./6000.+0.5; y=120.-Wrefx*100./6000.+0.5; Line(26+step-1,y0,26+step,y,White);y0=120.-Wrx0 *100./6000.+0.5; y=120.-Wrx *100./6000.+0.5; Line(26+step-1,y0,26+step,y,Green);if(step>=275){Graphic_clear(); Graphic_line();TFT_string(4,3,Cyan,Black,"Kp="); TFT_unsigned_float(Kp,2,1);TFT_string(4,5,Cyan,Black,"Ki="); TFT_unsigned_float(Ki,2,1);flag=0; step=0;}}}}
소스정렬을 잘 하면 가독성이 좋아집니다.
이해가 안가는 부분은 다시 분석해보시고요
복잡한 소스의 전체 기능을 파악해야지 답변을 달 수 있는 내용은 답변을 드리지 못합니다.
전체 주석도 마찬가지고요
짧은 시간에 분석이 가능한 내용만 질문해주세요
댓글 0
조회수 6,403등록된 댓글이 없습니다.