atmega128 코드 주석 문의요
페이지 정보
작성자 여러주 작성일18-12-13 12:30 조회7,318회 댓글0건본문
#include <avr/io.h>
#include <avr/interrupt.h>
#include "OK-128DCM.h"
void Graphic_line(void);
void Graphic_scale(void);
void Graphic_clear(void);
#define Kp 0.1
#define Ki 1.8
#define Ts 0.01
volatile float Ia; // motor current
volatile unsigned char m1, count, count0 = 0; // rotary encode pulse
volatile float Wref, Wr, Werr, Werr0 = 0.; // motor speed
volatile unsigned char direction; // motor direction
volatile float PIconstant, PI = 0.; // PI variables
volatile signed int PWM; // PWM output data
volatile unsigned int Wref_time = 0; // time counter for Wref setting
unsigned char tableindex = 0; // Wref data table
float Wref_table[10] = {+1500., -1500., +3000., -3000., +4500., -4500., +5500., -5500., 0., +5500.};
volatile float Wrefx, Wrefx0; // reference speed
volatile float Wrx, Wrx0; // motor speed
volatile float Ia, Iax, Iax0; // motor current
volatile unsigned int step = 0; // graphic step count
volatile unsigned char flag = 0; // graphic flag
ISR(TIMER1_COMPA_vect) /* OC1A interrupt function(0.01s period) */
{
count = TCNT2; // read TCNT2
if((PINE & 0x80) == 0x80) direction = 'F'; // read motor direction
else 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 controller
PI = PI + PIconstant*Werr - Kp*Werr0;
Werr0 = Werr;
if(PI > 99.) PI = 99.; // PI control limiter
else if(PI < -99.) PI = -99.;
if(PI >= 0.) // output PWM value for forward
{ PWM = PI + 0.5;
OCR3B = PWM;
PORTG = 0x01;
}
else // output PWM value for reverse
{ PWM = -PI + 0.5;
OCR3B = PWM;
PORTG = 0x02;
}
Wref_time++;
flag = 1; // set graphic flag
step++; // increment graphic step
Wrefx0 = Wrefx; // store old value of Wref
Wrefx = Wref; // store new value of Wref
Wrx0 = Wrx; // store old value of Wr
Wrx = Wr; // store new value of Wr
Iax0 = Iax; // store old value of Ia
Iax = Ia; // store new value of Ia
}
int main(void)
{
unsigned char i, mode, polarity = '+';
signed int temp, sum;
unsigned int y0, y;
MCU_initialize(); // initialize MCU and kit
Delay_ms(50); // wait for system stabilization
TFT_initialize(); // initialize TFT-LCD module
Graphic_line(); // draw line of graphic screen
Graphic_scale(); // display graphic scale
TFT_string(4,3,Cyan,Black,"Kp="); // display Kp(xx.x)
TFT_unsigned_float(Kp,2,1);
TFT_string(4,5,Cyan,Black,"Ki="); // display Ki(xx.x)
TFT_unsigned_float(Ki,2,1);
if((PINB & 0x40) == 0x40) // manual mode
{ mode = 'M';
TFT_string(32,0,Cyan,Black,"Manu");
}
else // auto mode
{ mode = 'A';
Wref = Wref_table[tableindex];
tableindex++;
TFT_string(32,0,Cyan,Black,"Auto");
}
PIconstant = Kp + Ki*Ts; // calculate PI constant
DDRE = 0x3C; // PORTE (DDRE4 = 1)
PORTE = 0x00; // (OC3C = OC3B = OC3A = 0)
TCCR3A = 0x22; // Timer3 = fast PWM mode(14), output OC3B
TCCR3B = 0x1A; // 16MHz/8/(1+99) = 20kHz
TCCR3C = 0x00;
ICR3 = 99;
OCR3B = 0;
TCCR1A = 0x00;
TCCR1B = 0x0C;
TCCR1C = 0x00;
OCR1A = 624;
TCNT1 = 0x0000;
TIFR = 0x10;
TIMSK = 0x10;
sei();
TCCR2 = 0x06;
TCNT2 = 0;
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;
}
}
}
}
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');
}
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);
}
}
댓글 : 0
조회수 : 7,318
등록된 댓글이 없습니다.