Professional Documents
Culture Documents
#include<at89x51.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
//========================= #DEFINE LCD
#define RS P3_5
#define RW P3_6//RW=0 => ghi
#define EN P3_7//RW=1 => doc
//RS=0 => code
//RS=1 => data
#define LCD_PORT P1
//========================= #DEFINE ADC
#define INTR P3_2
#define RD P3_3
#define WR P3_4
//========================================================
====== DELAY
void delay_ms(int n)
{
int k,j;
for(k=0;k<n;k++)
{
for(j=0;j<500;j++);
}
}
//==========================
void delay_5ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<4;j++){}
}
//===========================
void delay_15ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<100;j++){}
}
//========================================================
====== LCD
//============================== GUI LENH CHO LCD
void LCD_CODE(unsigned char c)
{
RS=0;//code
RW=0;//ghi
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();
}
//=============================== KHOI TAO LCD
void LCD_INIT()
{
delay_15ms();
LCD_CODE(0x38); //che do 8bit,2 hang,kieu ky tu 5x8 diem anh.
LCD_CODE(0x0C); //hien thi man hinh,c con tro, con tro nhp' nhy.
LCD_CODE(0x01); // Xoa man hinh LCD
}
//============================== IN KY TU
void LCD_DATA(unsigned char c)
{
RS=1;//data
RW=0;//ghi
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();
}
//=============================== IN CHUOI KY TU
void LCD_STRING(unsigned char *s)
{
while(*s) //den NULL thi thoi
{
LCD_DATA(*s);
s++;
}
}
//========================================================
====== KHOI TAO RS232
void SetupSerial()
{
TMOD=0x20; /* timer 1 che do 2: 8-Bit tu dong nap lai.
*/
TH1=0xFD; /* toc do 9600 baud
*/
TL1=0xFD;
SCON=0x52; /* Che do 1: 8-bit UART, cho phep truyen */
TR1=1;
/* timer 1 run
*/
TI=0;
/* co ngat nha^n.=0*/
RI=0;
/* co ngat' truye^n =0*/
ES=1;
/* cho phep ngat noi tiep */
}
//========================================================
====== HIEN THI GIA TRI ADC LEN LCD
void HIENTHI_ADC(unsigned char t)
{
unsigned char v;
if(t<10)
LCD_DATA(t+48);
else
if(t<100)
{
LCD_DATA(t/10+48);
LCD_DATA(t%10+48);
}
else
{
v=t/10;
LCD_DATA(v/10+48);
LCD_DATA(v%10+48);
LCD_DATA(t%10+48);
}
}
//========================================================
====== HAM GUI SO LIEU LEN VB
void send(unsigned char a)
{
if(a<10)
{
SBUF=a+48;
while(TI==0){}
TI=0;
}
if(a>9&&a<100)
{
unsigned char c=a/10;
unsigned char d=a%10;
SBUF=c+48;
while(TI==0){}
TI=0;
SBUF=d+48;
while(TI==0){}
TI=0;
}
if(a>99)
{
unsigned char t=a/100;
unsigned char c=a/10-10*t;
unsigned char d=a%10;
SBUF=t+48;
while(TI==0){}
TI=0;
SBUF=c+48;
while(TI==0){}
TI=0;
SBUF=d+48;
while(TI==0){}
TI=0;
}
}
//========================================================
====== NGAT NOI TIEP
{
if(TI==0)
LCD_CODE(0x01);
LCD_DATA(*c);
TI=1; //de cho chuong trinh ko thoat khoi ham ngat,phuc vu viec
truyen ky tu len LCD.
count=1; //danh dau bat dau viec truyen ky tu len LCD
}
}
}
//========================================================
====== MAIN
void main()
{
SetupSerial(); //Khoi tao cac thong so cho truyen thong noi tiep
LCD_INIT();
//LCD_CODE(0x80);//hien thi 2 hang : dia chi hien thi + 80
EA = 1;
//Cho phep ngat nhung chi? c ngat noi tiep duoc dung trong
code nay
while(1)
{
//if(P0_3==1)
{
P0_0=P0_1=P0_2=0;
WR=0;
//Bat dau chuyen doi gia tri tu ADC
delay_ms(10);
//Tao tre de cap nhat du lieu tu ADC
WR=1;
//Ket thuc chuyen doi
RD=0;
//Chot du lieu da duoc chuyen doi: P2
LCD_CODE(0x01);
LCD_putstr("Nhiet do:");
HIENTHI_ADC(P2);
LCD_STRING(" oC");
delay_ms(300);
}
}
}
c=getc();
switch(c)
{
case '0': //xoa man hinh LCD
{
LCD_putcmd(0x01);
}
break;
//o day co the su dung ma thap phan! (ngoai tru cac ky tu dieu khien)
case '-': //lui con tro hien thi LCD 1 don vi.
{
LCD_putcmd(0x10);
}
break;
case '1': //truyen len may tinh: gia tri do duoc.
{
Set_ADC_channel(0); //kenh 0 chan so2
delay_us(10);
for(i=0;i<10;i++)
{
vl[i]=read_adc();
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/20.4
8;
send(value);
}
break;
case '2': //truyen len may tinh: gia tri do duoc.
{
Set_ADC_channel(1); //kenh 1 chan so3
delay_us(10);
if(read_adc()==0)
{
for(i=0;i<10;i++)
{
vl[i]=read_adc();
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/
10;
}
else
{
value=read_adc()+6;
for(i=0;i<10;i++)
{
vl[i]=read_adc()+6;
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/
10;
}
send(value);
}
break;
case '9': //thoat khoi ham ngat, cho ADC lm viec.
{
count=0; //ket thuc viec truyen ky tu len LCD
LCD_putcmd(0x01);
}
break;
default : //truyen ky tu xuong LCD ^^.
{
count++;
if(count==1) LCD_putcmd(0x01);
if(count==16) LCD_putcmd(0xC0);
if(count==32)
{
LCD_putcmd(0x01);
count=0;
}
LCD_putchar(c);
}
}
}
//========================================================
===============
void main()
{
//trisD=0x0C; //D0,D1 LA CONG VAO, D2-D7 LA CONG RA.
enable_interrupts(int_rda); //cho phep ngat noi tiep nhan.
enable_interrupts(GLOBAL);
for(i=0;i<10;i++)
{
vl[i]=read_adc()+6;
delay_us(10);
}
value=(vl[0]+vl[1]+vl[2]+vl[3]+vl[4]+vl[5]+vl[6]+vl[7]+vl[8]+vl[9])/
10;
}
LCD_PutCmd(0xC0);
LCD_putchar("DIEN AP : ");
LCD_putcmd(0xC9);
//HIENTHI_LCD(value);
HIENTHI_LCD_float(value);
LCD_putcmd(0xCD);
LCD_putchar("V ");
}
}
}
//========================================================
===============
void HIENTHI_LCD(int8 t)
{
unsigned char v;
if(t<10)
LCD_putchar(t+48);
else
if(t<100)
{
LCD_putchar(t/10+48);
LCD_putchar(t%10+48);
}
else
{
v=t/10;
LCD_putchar(v/10+48);
LCD_putchar(v%10+48);
LCD_putchar(t%10+48);
}
}
void send(int8 a)
{
if(a<10)
{
putc(a+48);
}
if(a>9&&a<100)
{
unsigned char c=a/10;
unsigned char d=a%10;
putc(c+48);
putc(d+48);
}
if(a>99)
{
unsigned char t=a/100;
unsigned char c=a/10-10*t;
unsigned char d=a%10;
putc(t+48);
putc(c+48);
putc(d+48);
}
}
void HIENTHI_LCD_float(int8 t)
{
int8 v;
if(t<10)
{
LCD_putchar(48);
LCD_putchar(46);
LCD_putchar(t+48);
LCD_putchar(32);
}
else
if(t<100)
{
LCD_putchar(t/10+48);
LCD_putchar(46);
LCD_putchar(t%10+48);
LCD_putchar(32);
}
else
{
v=t/10;
LCD_putchar(v/10+48);
LCD_putchar(v%10+48);
LCD_putchar(46);
LCD_putchar(t%10+48);
}
}
/*
void send_float(float a)
{
if(a<10)
{
putc(a+48);
}
if(a>9&&a<100)
{
unsigned char c=a/10;
unsigned char d=a%10;
putc(c+48);
putc(d+48);
}
if(a>99)
{
unsigned char t=a/100;
unsigned char c=a/10-10*t;
unsigned char d=a%10;
putc(t+48);
putc(c+48);
putc(d+48);
}
}*/
//========================================================
===============
S hot ng ca robot t ng
1.2.1 Mch ng vo (cm bin, nt n, cng tc hnh trnh)
Vi robot n gin, ng vo thng l mc logic ly t cm bin quang (quang
tr, quang diode), nt n hoc cng tc hnh trnh. T mch vi iu khin x
l cc tn hiu ny xut ng ra (thng l ng c DC) choph hp.
Cm bin quang phi c che chn cn thn hn ch nh hng t
ccngun nh sng bn ngoi.
Mch vi iu khin
1.2.3 Mch cng sut iu khin ng c DC
Mt s mch thng dng:
#include <16f877A.h>
#include <def_877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
/* NH NGHA CC CHN V PORT */
#define DIR_LEFT RC0
#define EN_LEFT
RC1
#define DIR_RIGHT RC3
#define EN_RIGHT RC2
#define SENSOR PORTD
/* KHAI BO CC CHNG TRNH CON */
void motor_left_forward();
void motor_left_reverse();
void motor_left_stop();
void motor_right_forward();
void motor_right_reverse();
void motor_right_stop();
void forward();
void reverse();
void stop();
void turn_left();
void turn_right();
/* CC CHNG TRNH CON */
// ng c tri chy thun
void motor_left_forward()
{
DIR_LEFT=1; // chiu thun
EN_LEFT=1; // cho php chy
}
// ng c tri chy ngc
void motor_left_reverse()
{
DIR_LEFT=0; // chiu ngc
EN_LEFT=1; // cho php chy
}
// ng c tri dng
void motor_left_stop()
{
EN_LEFT=0; // khng cho php chy
}
// ng c phi chy thun
void motor_right_forward()
{
DIR_RIGHT=1;// chiu thun
EN_RIGHT=1; // cho php chy
}
// ng c phi chy ngc
void motor_right_reverse()
{
DIR_RIGHT=0;// chiu ngc
EN_RIGHT=1; // cho php chy
}
// ng c phi dng
void motor_right_stop()
{
EN_RIGHT=0; // khng cho php chy
}
// Chy thng
void forward()
{
motor_left_forward();
motor_right_forward();
}
// Chy li
void reverse()
{
motor_left_reverse();
motor_right_reverse();
}
// Dng
void stop()
{
motor_left_stop();
motor_right_stop();
}
// Quay tri
void turn_left()
{
motor_left_forward();
motor_right_reverse(); // hoc motor_right_stop();
}
// Quay phi
void turn_right()
{
}
void left_motor_reverse(int value)
{
MOTOR_LEFT_DIR=1;
setup_timer_2(T2_DIV_BY_4,124,1);
// Dieu xung 10kHz
setup_ccp2(CCP_PWM);
set_pwm2_duty(value);
}
void right_motor_reverse(int value)
{
MOTOR_RIGHT_DIR=1;
setup_timer_2(T2_DIV_BY_4,124,1);
// Dieu xung 10kHz
setup_ccp1(CCP_PWM);
set_pwm1_duty(value);
}
void left_motor_stop()
{
setup_ccp1(CCP_OFF);
}
void right_motor_stop()
{
setup_ccp1(CCP_OFF);
}
// Chng trnh x l tc 2 ng c
// 0:Stop,100:FORWARD 100%,-100:Reverse 100%
void speed (signed int left_motor_speed, signed int right_motor_speed)
{
int left_pwm_value=0,right_pwm_value=0;
/* Left motor */
if( left_motor_speed >= 0 )
{
left_pwm_value = 1.25*left_motor_speed; // (125*left_motor_speed/100)
left_motor_forward(left_pwm_value);
}
else
{
left_motor_speed = -left_motor_speed;
left_pwm_value = 1.25*left_motor_speed; // (125*left_motor_speed/100)
left_motor_reverse(left_pwm_value);
}
/* Right motor */
if( right_motor_speed >= 0 )
{
right_pwm_value
=
1.25*right_motor_speed;
(125*left_motor_speed/100)
right_motor_forward(right_pwm_value);
}
else
{
right_motor_speed = -right_motor_speed;
right_pwm_value
=
1.25*right_motor_speed;
(125*left_motor_speed/100)
right_motor_reverse(right_pwm_value);
}
}
//
//
Code:
//KIEM TRA VACH NGANG: 1: co vach, 0:khong co vach
int check_cross_line()
{
int temp_sensor=0,led_in_line=0,i=0;
temp_sensor=SENSOR;
for (i=0;i<8;i++)
{
if ((temp_sensor&0x01)==0x01) led_in_line++;
if (led_in_line==4) break;
temp_sensor=temp_sensor>>1;
}
if (led_in_line==4)
return 1;
else
return 0;
}
Do chng trnh nhn vch ngang c gi lin tc kim tra c vch ngang
xut hin hay khng nn s dn n tnh trng khi robot n vch ngang, bin
m s vch tng thm 1. Sau , khi robot cha kp chy qua vch ngang m
hm kim tra c gi dn n vic bin m tng lin tc.
Vic ny khin cho robot thc hin sai cng vic.
Hng gii quyt tnh hung ny nh sau:
o Khi robot gp vch ngang: chy thng
o Khi ht vch ngang: bin m s vch tng thm 1
o Thc hin cng vic tng ng
Code:
while (check_cross_line() == 1) // gp vch ngang
{
speed(100,100); // chy thng
}
number_cross_line++; // tng bin m s vch ngang thm 1
3.3 X l khi robot lch hon ton khi vch
Trng hp robot lch ra khi vch (quay qu mnh hoc b va chm), gi
tr cm bin c c l 0x00, nh vy robot s b mt phng hng.
gii quyt trng hp ny, ta t mt bin trng thi l bin ton cc.
Gi bin ny l line_status:
o Line_status=0: gia vch;
o Line_status=1: lch tri;
o Line_status=2: lch phi;