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

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 08-04-2012, 01:26 AM   #1
vanhoavh
Nhập môn đệ tử
 
Tham gia ngày: Mar 2012
Bài gửi: 3
:
Smile lập trình PIC16F877A đo tốc độ động cơ bước

chào các bác.
hiện e đang làm đồ án vi xử lý về đo tốc độ động cơ bước. nhưng code chạy mãi mà ko được, em nhờ các bac coi dùm và chỉ giúp e với...gấp quá rồi

#include "E:\do an\do an cau truc may tinh\lab2\doan\main.h"
#include <LCD.C>
//#include <math.h> //dung cac ham toan hoc
#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);
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);

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);
}}
//==========THE END========}

báo lỗi CCS la:

*** Error 71 "main.c" Line 509(0,1): Out of ROM, A segment or the program is too large MAIN
vanhoavh vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 08-04-2012, 01:27 AM   #2
vanhoavh
Nhập môn đệ tử
 
Tham gia ngày: Mar 2012
Bài gửi: 3
:
sửa giùm e với...
e cám ơn rất nhiều
vanhoavh vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 09-04-2012, 01:05 AM   #3
vanhoavh
Nhập môn đệ tử
 
Tham gia ngày: Mar 2012
Bài gửi: 3
:
giúp e với các bác, có đồ án nào đo encoder dùng pic 16f877a cho e xin với. có hậu tạ
vanhoavh vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 09-04-2012, 10:41 AM   #4
hoc_picpicpic
Đệ tử 6 túi
 
Tham gia ngày: Feb 2009
Bài gửi: 127
:
Trích:
Nguyên văn bởi vanhoavh View Post
giúp e với các bác, có đồ án nào đo encoder dùng pic 16f877a cho e xin với. có hậu tạ
lập trình đo encoder liên hệ tôi 01678315222
hoc_picpicpic vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 16-04-2012, 11:42 PM   #5
lambaotrung
Đệ tử 5 túi
 
Tham gia ngày: Jan 2010
Bài gửi: 96
:
bạn nếu múc địch

Trích:
Nguyên văn bởi vanhoavh View Post
sửa giùm e với...
e cám ơn rất nhiều
code dài khiếp. bạn có thể đơn giản hóa dc ko bạn.
chẳn hãng như bạn muốn làm gì với động cơ bước, cho nó chạy như nào và kiểm soát nó ra sao. tính số vòng quay trên giây hay đếm từng bước của động cơ.
lambaotrung vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 24-04-2012, 01:24 AM   #6
hangocminh1989
Đệ tử 6 túi
 
Tham gia ngày: Apr 2009
Bài gửi: 132
:
chia nhỏ chương trình main ra
hangocminh1989 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời


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à 10:10 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