PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > PIC - Thiết kế và Ứng dụng

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

PIC - Thiết kế và Ứng dụng Ý tưởng cho các sản phẩm sử dụng PIC/dsPIC và các sản phẩm của Microchip

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 29-09-2011, 10:50 AM   #1
achilles86
Nhập môn đệ tử
 
Tham gia ngày: Apr 2008
Bài gửi: 4
:
Smile Cần giúp đỡ góp ý cho đoạn code sau (Đk động cơ DC dùng PIC)

Hiện tại em đang thực hiện đồ án : Điều khiển tốc độ động cơ DC sử dụng PIC 16F877A.
- Tóm tắt đề tài : + Sử dụng PIC 16F877A làm vi điều khiển trung tâm lập trình C.
+ Bàn phím gồm 16 phím đề nhập và điều khiển động cơ.( 10 phím số nhập tốc độ, 1 phím CLEAR, 1 phím SET, 3 phím điều khiển (STOP, FORWARD và REVERSE)
+ Hiển thị tốc độ dùng màn hình LCD 16x2.
+ Sử dụng mạch cầu H và IC L298 đề đảo chiều động cơ.
Code emđã viết xong nhưng gặp trục trặc khi sử dụng CCS để complie thì xuất hiện lỗi : "Out of ROM ". Em có tìm hiểu trên mạng thì biết lỗi này có lẽ xuất phát từ giải thuật viết code.Vì vậy em post đoạn code lên nhờ các sư huynh thông thạo xem xét góp ý và fix dùm. Em xin cảm ơn lắm lắm.

#include"E:/TL/DOAN2\cauH\MACH\15\main.h"
#include <LCD.C>
#use delay(clock=20000000)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
//==================khai bao ham con=============
int quetphim();
int checkphim(b);
void pwm();
void ghi_tocdo();
void ghi_thoigian();
void clear();
void read_rom();
//==================khai bao bien================
int8 i,t,a,b,c,d,sttphim,duty,ct,l,m;
int16 s_xung,s_vong,setpoint,error,luu,tg,tg1,dem;
//================ bien luu eeprom==============
int8 e0,e1,e2,e3,j,k;
int16 e;
//================chuong trinh quet phim==========
//quet phim so
int quetphim()
{
output_b(0xe0);// B4=0
a=0;
b=1;
checkphim(b);
if (a!=0)
{delay_ms(200);
return (sttphim);}
output_b(0xd0);// B5=0
a=0;
b=2;
checkphim(b);
if (a!=0)
{delay_ms(200);
return (sttphim);}
output_b(0xb0);// B6=0
a=0;
b=3;
checkphim(b);
if (a!=0)
{delay_ms(200);
return (sttphim);}
output_b(0x70);// B6=0
a=0;
b=4;
checkphim(b);
if (a!=0)
{delay_ms(200);
return (sttphim);}}

//============chuong trinh check phim=============
int checkphim(b)
{
switch (b)
{
case 1:
if(!input(pin_a0))
{sttphim=1;
a=1;}
else if(!input(pin_a1))
{sttphim=2;
a=1;}
else if(!input(pin_a2))
{sttphim=3;
a=1;}
else if(!input(pin_a3))
{ sttphim=10;//thuan
a=1;}
else {}
break;

case 2:
if(!input(pin_a0))
{sttphim=4;
a=1;}
else if(!input(pin_a1))
{sttphim=5;
a=1;}
else if(!input(pin_a2))
{sttphim=6;
a=1;}
else if(!input(pin_a3))
{sttphim=11;//nghich
a=1;}
else {}
break;
case 3:
if(!input(pin_a0))
{sttphim=7;
a=1;}
else if(!input(pin_a1))
{sttphim=8;
a=1;}
else if(!input(pin_a2))
{sttphim=9;
a=1;}
else if(!input(pin_a3))
{sttphim=12;//stop
a=1;}
else {}
break;

case 4:
if(!input(pin_a0))
{sttphim=0;//0
a=1;}
else if(!input(pin_a1))
{sttphim=15;//save
a=1;}
else if(!input(pin_a2))
{sttphim=14;//clear
a=1;}
else if(!input(pin_a3))
{sttphim=13;//set
a=1;}
else {}
break;}
return (sttphim);
}
// chuong trinh nhan xung tu encoder
//ngat ngoai, nhan xung tu encoder
#int_ext
void RB0_isr()
{
s_xung++;//dem so xung o chan RB0
}
//ngat timer1, tinh toan pwm va hien thi
#int_timer1
void timer1_isr()
{
set_timer1(-62500);
if (t==5){
S_vong=s_xung*2;//xung tren phut
//ht
lcd_gotoxy(1,1);
printf(lcd_putc," ");
if(ct==1)
{
lcd_gotoxy(1,1);
printf(lcd_putc,"TD_dat=%lu v/p",luu);}
else {
lcd_gotoxy(1,1);
printf(lcd_putc,"TG_dat=%lu s",tg);}
lcd_gotoxy(1,2);
printf(lcd_putc," ");
if (d==2)
{lcd_gotoxy(1,2);
printf(lcd_putc,"TD_tt=-%luv/p",s_vong);}
if (d==1)
{lcd_gotoxy(1,2);
printf(lcd_putc,"TD_tt= %luv/p",s_vong);}
//=========================
//xuat pwm
pwm();
//========================
s_xung=0;
s_vong=0;
t=0;
set_timer1(-62500);
}
else
t++;
set_timer1(-62500);
}
//ngat timer0, dat thoi gian
#int_timer0
void time0_irs()
{
set_timer0(-235);
if (dem>=tg1)
{
output_high(pin_e0);
delay_us(100);
output_low(pin_e0);
dem=0;
if (d==1)
{d=2;}
else if (d==2)
{d=1;}
else{}
set_timer0(-235);
}
else {
dem++;
set_timer0(-235);}
}
//chuong trinh chinh
void main()
{
i=0;
sttphim=0;
setpoint=0;tg=0;
duty=0;
c=0;
d=0;
ct=0;l=0;m=0;
e0=0;e1=0;e2=0;e3=0;e=0;k=0;
//================================================== ==========
//1:ngo vao;0: la ngo ra
set_tris_b(0b00001111);//4 chan RB4-RB7 xuat du lieu ra ban phim
set_tris_a(0b00001111);//4 chan RA0-RA3 nhan du lieu tu ban phim
set_tris_c(0b00000000);//2 chan RC0 va RC1 xuat PWM
set_tris_D(0b00000000);//port D la port xuat du lieu ra LCD
//================================================== ==========
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
/* timer1 la bo dinh thoi su dung xung noi,bo chia 1:8 thay doi moi 1600ns
Dung timer1 de ngat moi 0.1s do vay ta dat gia tri cho timer1 la :
0.1s/1600ns=62500(D)=F424(H) =>gia tri nap la FFFF-F424=BDB*/
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
enable_interrupts(int_ext);//khoi dong ngat ngoai
ext_int_edge(H_TO_L); // xung tu cao xuong thap
enable_interrupts(global);// khoi dong bit ngat GIE
setup_timer_2(T2_DIV_BY_4,249,1);
/*timer2 dung dinh thoi cho bo PWM
mode: bo chia thoi gian (prescale) cua timer2 1:4
period: gia tri nap chi thanh ghi PR2
postscale : bo chi ra,chon 1:1 PWM khong dung
Thach anh 20MHz, PWM fre: 10000Hz, thay doi duty cycle(%) de thay doi toc do*/
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(0);
set_pwm2_duty(0);
//================================================== =========
lcd_init()
lcd_send_byte(0,0x01);
lcd_gotoxy(1,1);
printf(lcd_putc,"CHUONG TRINH DK ");
lcd_gotoxy(1,2);
printf(lcd_putc," TD DONG CO DC ");
delay_ms(1000);
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc," ");
read_rom();
lcd_gotoxy(1,1);
printf(lcd_putc,"CHON CHE DO :_ ");
while (true)
{
while (c==0)
{
quetphim();
if (a!=0)
{
if (sttphim==1)
{ ct=1;
c=1;
lcd_gotoxy(1,1);
printf(lcd_putc,"CHON CHE DO :_%u",ct);}
if (sttphim==2)
{ ct=2;
c=1;
lcd_gotoxy(1,1);
printf(lcd_putc,"CHON CHE DO :_%u",ct);}
}
}
if (ct==1)
{lcd_gotoxy(1,1);
printf(lcd_putc,"TD_dat=_ v/p");
lcd_gotoxy(1,2);
printf(lcd_putc,"TD_luu=%lu v/p",e);}
else {
lcd_gotoxy(1,1);
printf(lcd_putc,"TD_dat=_ v/p");
lcd_gotoxy(1,2);
printf(lcd_putc,"TG_dat=_ s");}
While (c==1)
{
quetphim();

if (a!=0)
{
if (sttphim>=0 && sttphim<=9)//ban phim tu 0--->9
{
if (m==0)
{ghi_tocdo();}
if((ct==2)&&(m==1))
{ghi_thoigian();}
}
if (sttphim==14)
{clear();}
if ((sttphim==13)&&(i==0)&&(e==0))
{
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"Phai nhap TD_dat");
delay_ms(1000);
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"TD_dat=_");
}
if ((sttphim==15)&&(i!=0)) //luu vao eeprom
{
write_eeprom(0,e0);
delay_ms(100);
write_eeprom(1,e1);
delay_ms(100);
write_eeprom(2,e2);
delay_ms(100);
write_eeprom(3,e3);
delay_ms(100);
write_eeprom(4,i);
delay_ms(100);
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc," LUU THANH CONG ");
}
if ((sttphim==13)&&((i!=0)||(e!=0)))
{
if (ct==1)
{
c=2;
if (e!=0 && i==0)
luu=e;}
if ((ct==2)&&(l>=2))
{
c=2;}
m=1;
}}}
while (c==2)
{
quetphim();
if(a!=0)
{
if(sttphim==10)
{
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc," QUAY THUAN ");
if (ct==2)
{enable_interrupts(int_timer0);
set_timer1(-235);}
enable_interrupts(int_timer1);
enable_interrupts(global);
set_timer1(-62500);
set_pwm1_duty(duty);
d=1;
}
if (sttphim==11)
{
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc," QUAY NGHICH ");
if (ct==2)
{enable_interrupts(int_timer0);
set_timer1(-235);}
enable_interrupts(int_timer1);
enable_interrupts(global);
set_timer1(-62500);
set_pwm2_duty(duty);
d=2;
}
if (sttphim==12)//stop
{
disable_interrupts(int_timer0);
set_timer0(0);
disable_interrupts(int_timer1);
set_pwm1_duty(0);
set_pwm2_duty(0);
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc," STOP ");
duty=0;
d=0;
}
if (sttphim==14)
{
clear();
c=0;
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"CHON CHE DO :_ ");
}}}}}


void pwm()
{
if ((luu>s_vong)&& duty<250)
{error=luu-s_vong;
if (error>1000)
duty=duty+50;
else if (error>100)
duty=duty+20;
else if (error>30)
{duty=duty+5;}
else if (error>20)
duty=duty+1.5;
else if (error>10)
duty=duty+(0.05*error);
else {duty=duty+(0.025*error);}
}
if (luu<(s_vong-2)&& (duty>0))
{error=s_vong-luu;
if (error>=10)
duty=duty-(0.05*error);
else (duty=duty-(0.02*error));
}
if(luu==s_vong)
duty=duty;
if((duty>=250)&&(luu>s_vong))
{ lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc," TD_tt=MAX ");}
if (d==0)
{set_pwm1_duty(0);
set_pwm2_duty(0);}
else if (d==1)
{set_pwm1_duty(duty);
set_pwm2_duty(0);}
else
{set_pwm1_duty(0);
set_pwm2_duty(duty);}
}
//========chuong trinh con nhap toc do=======
void ghi_tocdo()
{
if(i>=0&&i<=3)
{
// giai thuat luu eeprom
if (i==0)
e0=sttphim;
else if (i==1)
e1=sttphim;
else if (i==2)
e2=sttphim;
else {e3=sttphim;}
//===============================
setpoint*=10;
setpoint+=sttphim;
i++;
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"TD_dat=%lu v/p",setpoint);
luu=setpoint;
}
else {
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"0<TD_dat<=9999");
delay_ms(1000);
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"TD_dat=%lu s",setpoint);
}}
//=========chuong trình con nhap thoi gian=========
void ghi_thoigian()
{
if(l>=0&&l<=3)
{
tg*=10;
tg+=sttphim;
l++;
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc,"TG_dat=%lu s",tg);
tg1=tg/0.12;
}
else {
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc," 0<TG_dat<=999 ");
delay_ms(1000);
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc,"TG_dat=%lu s",tg);
}}
//============chuong trinh con clear=========
void clear()
{
setpoint=0;
tg=0;
i=0;l=0;m=0;
e0=0;e1=0;e2=0;e3=0;
lcd_gotoxy(1,1);
printf(lcd_putc," ");
lcd_gotoxy(1,1);
printf(lcd_putc,"TD_dat=_");
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc,"TG_dat=_");
}
//=======chuong trinh con luu toc do vao epprom======
void read_rom()
{
k=read_eeprom(4);
for (j=0;j<k;j++)
{
e=e*10;
e=e+read_eeprom(j);
}}
achilles86 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 09:19 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam