Nhập môn đệ tử
Tham gia ngày: Feb 2010
Bài gửi: 3
:
|
Hỏi về ngắt 16f877a
Em viết chuơng trình đèn giao thông có menu điều khiển sử dụng ngắt rb nhưng khi chạy mô phỏng kô thấy hoạt động,hix, ai giúp em với
Trích:
//************************************************** ***************************
// Author : ghost
// Project : Traffic lights
// Hardware: PIC16F877A
//************************************************** ***************************
#include <16F877A.h>
#include <def_877a.h>
#fuses NOWDT,PUT,XT,NOPROTECT
#use delay(clock=2000000)
#include <lcd_lib_4bit.h>
int h=12,m=0; //gio,phut
int g1=30,g2=30; //den xanh 1,den xanh 2
int r1=30,r2=30; //den do 1, den do 2 1 ung voi rsh, 2 ung voi nor
int y=5; //den vang
int d1=3,d2=3; //thoi gian tre 1,thoi gian tre 2
int y_b=22; //gio nhay den vang
int change; //bien nhan ngat
int menu,up,down,set; //cac bien nhan biet nut bam
int a=0; //trang thai menu
int a1,a2,a3,a4,a5,a6,a7,a10;
unsigned int a8,a9;
#int_rb
void ngat_rb()
{
if(rbif&&rbie)
{
porta=0;
porte=0;
change=1;
if(rb4)
{
menu=1;
a++;
if(a>10) a=0;
}
if(rb5) up=1;
if(rb6) down=1;
if(rb7) set=1;
}
}
void leddon(int tg,tv,tx,td)
{
int i;
while(true)
{
for(i=0;i<(tv+tx+td);i++)
{
delay_ms(100);
// cac den cho nguoi tham gia giao thong
if(i<tg)
porta=0b00100100; //d1 sang d2 sang
if((i>tg)&&(i<(tg+tx)))
porta=0b00100001; //d1sang x2 sang
if((i>(tg+tx)&&(i<(tg+tx+tv))))
porta=0b00100010; //d1 sang v2 sang
if((i>(tg+tx+tv)) && (i<(tg+tg+tx+tv)))
porta=0b00100100; //d1 sang do2 sang
if((i>(tg+tg+tx+tv)) && (i<(td+tx)))
porta=0b00001100; //x1 sang d2 sang
if((i>(tx+td)) && (i<(td+tx+tv)))
porta=0b00010100; //va1 sang d2 sang
// cac den danh cho nguoi di bo
if(tg>=5)
{
if((i<(tg-5)) || (i>tg+tv+tx+5))
portb=0b00000110;
if(((i>tg-5)&&(i<5)) || ((i>(2*tg+tv+tx-5))&&(i<tg+tv+tx+5)))
portb=0b00001010;
if((i>5) && (i<(2*tg+tv+tx-5)))
portb=0b00001001;
}
if(tg<5)
{
if((i<5) || ((i>(2*tg+tv+tx-5))&& (i<tg+tv+tx+5)) || (i>(td+tx+tv-5+tg)))
portb=0b00001010;
if((i>5) && (i<(2*tg+tv+tx-5)))
portb=0b00001001;
if((i>tg+tv+tx+5)&& (i<(td+tx+tv-5+tg)))
portb=0b00000110;
}
}
}
} // het ham leddon
void main()
{
set_tris_a(0);
set_tris_b(0xF0);
set_tris_c(0);
set_tris_d(0);
set_tris_e(0);
if(((h>=7)&&(h<=9))|((h>=17)&&(h<=19))) //Gio cao diem
leddon(d1,y,g1,r1);
if((h>=y_b)&&(h>=0)&&(h<=5)) //Gio nhay den vang
{
while(true)
{
delay_ms(100);
output_high(pin_a1);
output_high(pin_a4);
}
}
if(((h>=6)&&(h<=7))|((h>=9)&&(h<=17))|((h>=19)&&(h <=y_b))) //Gio binh thuong
leddon(d2,y,g2,r2);
enable_interrupts(global);
enable_interrupts(int_rb);
ext_int_edge(L_to_H);
lcd_init();
printf(lcd_putchar,"ghost");
if(change)
{
change=0;
if((menu==1)&&(a==1))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Gio :%d h",h);
a1=h; //bien tam
if(down==1)
{
a1--;
if(a1==0) a1=23;
printf(lcd_putchar, "Gio :%d h",a1);
down=0;
}
if(up==1)
{
a1++;
if(a1==24) a1=0;
printf(lcd_putchar, "Gio :%d h",a1);
up=0;
}
if(set==1)
{
h=a1;
set=0;
}
}
if((menu==1)&&(a==2))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Phut :%d m",m);
a2=m; //bien tam
if(down==1)
{
a2--;
if(a2==0) a1=59;
printf(lcd_putchar, "Phut :%d m",a2);
down=0;
}
if(up==1)
{
a2++;
if(a2==60) a2=0;
printf(lcd_putchar, "Phut :%d m",a2);
up=0;
}
if(set==1)
{
m=a2;
set=0;
}
}
if((menu==1)&&(a==3))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Den vang :%d s",y);
a3=y; //bien tam
if(down==1)
{
a3--;
if(a3==2) a3=10;
printf(lcd_putchar, "Den vang :%d s",a3);
down=0;
}
if(up==1)
{
a3++;
if(a3==11) a3=3;
printf(lcd_putchar, "Den vang :%d s",a3);
up=0;
}
if(set==1)
y=a3;
set=0;
}
if((menu==1)&&(a==4))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Do cao diem :%d s",r1);
a4=r1; //bien tam
if(down==1)
{
a4--;
if(a4==19) a4=90;
printf(lcd_putchar, "Do cao diem :%d s",a4);
down=0;
}
if(up==1)
{
a4++;
if(a4==91) a4=20;
printf(lcd_putchar, "Do cao diem :%d s",a4);
up=0;
}
if(set==1)
{
r1=a4;
set=0;
}
}
if((menu==1)&&(a==5))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Do thap diem :%d s",r2);
a5=r2; //bien tam
if(down==1)
{
a5--;
if(a5==19) a5=90;
printf(lcd_putchar, "Do thap diem :%d s",a5);
down=0;
}
if(up==1)
{
a5++;
if(a5==91) a5=20;
printf(lcd_putchar, "Do thap diem :%d s",a5);
up=0;
}
if(set==1)
{
r2=a5;
set=0;
}
}
if((menu==1)&&(a==6))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Xanh cao diem :%d s",g1);
a6=g1; //bien tam
if(down==1)
{
a6--;
if(a6==9) a6=70;
printf(lcd_putchar, "Xanh cao diem :%d s",a6);
down=0;
}
if(up==1)
{
a6++;
if(a6==71) a6=10;
printf(lcd_putchar, "Xanh cao diem :%d s",a6);
up=0;
}
if(set==1)
{
g1=a6;
set=0;
}
}
if((menu==1)&&(a==7))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Xanh thap diem :%d s",g2);
a7=g2; //bien tam
if(down==1)
{
a7--;
if(a7==9) a7=70;
printf(lcd_putchar, "Xanh thap diem :%d s",a7);
down=0;
}
if(up==1)
{
a7++;
if(a7==71) a7=10;
printf(lcd_putchar, "Xanh thap diem :%d s",a7);
up=0;
}
if(set==1)
{
g2=a7;
set=0;
}
}
if((menu==1)&&(a==8))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Do giao cao diem :%d s",d1);
a8=d1; //bien tam
if(down==1)
{
a8--;
if(a8==-1) a8=10;
printf(lcd_putchar, "Do giao cao diem :%d s",a8);
down=0;
}
if(up==1)
{
a8++;
if(a8==11) a8=0;
printf(lcd_putchar, "Do giao cao diem :%d s",a8);
up=0;
}
if(set==1)
{
d1=a8;
set=0;
}
}
if((menu==1)&&(a==9))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Do giao thap diem :%d s",d2);
a9=d2; //bien tam
if(down==1)
{
a9=d2;
if(a9==-1) a9=10;
printf(lcd_putchar, "Do giao thap diem :%d s",a9);
down=0;
}
if(up==1)
{
a9++;
if(a9==11) a9=0;
printf(lcd_putchar, "Do giao thap diem :%d s",a9);
up=0;
}
if(set==1)
{
d2=a9;
set=0;
}
}
if((menu==1)&&(a==10))
{
LCD_PutCmd(0x01);
printf(lcd_putchar, "Gio nhay den vang :%d s",y_b);
a10=y_b; //bien tam
if(down==1)
{
a10--;
if(a10==20) a10=23;
printf(lcd_putchar, "Gio nhay den vang :%d s",a10);
down=0;
}
if(up==1)
{
a10++;
if(a10==24) a10=21;
printf(lcd_putchar, "Gio nhay den vang :%d s",a10);
up=0;
}
if(set==1)
{
y_b=a10;
set=0;
}
}
}
}
|
Đây là mạch mô phỏng ạ
http://www.mediafire.com/?kxy1fuo1nyn
|