Tình hình là em tự mò mẫm học PIC, viết bằng C.
Em viết được chương trình với lưu đồ cơ bản thế này:

code của em như sau:
Ý em muốn bấm chọn số bước, rồi bấm start thì động cơ chạy, trừ dần số bước đến = 0 thì dừng.
Mà không hiểu sao vừa bấm chọn bước thì từ 0000 nó nhảy lên 6k mấy
Anh/chị nào chỉ em sai ở đâu với
Đàn em vừa vào nghề, mong anh/chị chỉ giáo
File code + mô phỏng: Chia sẻ file trên Zing Me
Em viết được chương trình với lưu đồ cơ bản thế này:

code của em như sau:
Code:
// Truong trINH dieu khien dong co buoc 1.8*
// a1 = B0
// a2 = B1
// b1 = B2
// b2 = B3
// LCD:
// D7 = RD7
// D6 = RD6
// D5 = RD5
// RS = RD2
// RW = GND
// E = RD3
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<16f877a.h>
#include<def_877a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=12000000)
#include<lcd_lib_4bit.h>
#byte PORTA=0x05
#byte PORTB=0x06
#byte PORTC=0x07
#byte PORTD=0x08
#byte PORTE=0x09
#use fast_io(b)
#use fast_io(d)
int8 tram,chuc,donvi,nghin;
int8 start=0;
int16 temp;
int16 i=0; // i = buoc
void hien_thi(int16 i);
void thiet_lap(int8 start);
void main()
{
LCD_init();
set_tris_d(0);
while(true)
{
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
// QUAY THUAN
set_tris_b(0);
if(start==0)
{
thiet_lap((int8) start);
}
if(i==0)
{
start=0;
thiet_lap((int8) start);
}
for(;i>0;)
{
output_low(PIN_B0);
output_low(PIN_B2);
i--;
hien_thi((int16) i);
delay_ms(50);
output_high(PIN_B0);
i--;
hien_thi((int16) i);
delay_ms(50);
output_low(PIN_B1);
i--;
hien_thi((int16) i);
delay_ms(50);
output_high(PIN_B2);
i--;
hien_thi((int16) i);
delay_ms(50);
output_low(PIN_B3);
i--;
hien_thi((int16) i);
delay_ms(50);
output_high(PIN_B1);
i--;
hien_thi((int16) i);
delay_ms(50);
output_low(PIN_B0);
i--;
hien_thi((int16) i);
delay_ms(50);
output_high(PIN_B3);
i--;
hien_thi((int16) i);
}
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
void hien_thi(int16 i)
// TACH SO
{
if(start==1)
{
for(;i<=0;)
{
LCD_putcmd(0x01);
while(TRUE)
{
LCD_putcmd(134);
Printf(LCD_putchar"XONG");
}
}
}
nghin = i / 1000;
temp = i % 1000;
tram = temp / 100;
temp = temp % 100;
chuc = temp / 10;
donvi = temp % 10;
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
// HIEN SO
LCD_putcmd(128);
Printf(LCD_putchar"STEP=%u",nghin);
LCD_putcmd(134);
Printf(LCD_putchar"%u",tram);
LCD_putcmd(135);
Printf(LCD_putchar"%u",chuc);
LCD_putcmd(136);
Printf(LCD_putchar"%u",donvi);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
void thiet_lap(int8 start)
{
while(TRUE)
{
set_tris_a(0xff);
if(input(PIN_A0)==0)
{
delay_ms(20);
if(input(PIN_A0)==0)
{
i++;
while(input(PIN_A0)==0)
{
// khong lam gi, cho buong nut nhan ra
}
}
}
hien_thi((int16) i);
if(input(PIN_A1)==0);
{
while(input(PIN_A1)==0);
delay_ms(20);
start=1;
return;
}
}
}
// END
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
Mà không hiểu sao vừa bấm chọn bước thì từ 0000 nó nhảy lên 6k mấy

Anh/chị nào chỉ em sai ở đâu với

Đàn em vừa vào nghề, mong anh/chị chỉ giáo

File code + mô phỏng: Chia sẻ file trên Zing Me

Comment