PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   PIC - Thiết kế và Ứng dụng (http://www.picvietnam.com/forum/forumdisplay.php?f=23)
-   -   Ngắt RB0 Trên Led 7 đoạn! (http://www.picvietnam.com/forum/showthread.php?t=7607)

troiuuuu 25-12-2010 10:15 PM

Ngắt RB0 Trên Led 7 đoạn!
 
1 Attachment(s)
Chào cả nhà!Hnay đi thực hành ông thầy cho bài thực hành,huu chưa bit gì hết mà làm thực hành,loay hoay mãi mà ko viết dc code rồi tìm tài liệu lên mạng search ....nói chung là chỗ nào cũng lùng hết mà vẫn ko chạy.
Hnay post bài này lên mong mọi người giúp đỡ.
Đề bài:
Thiết kế như hình vẽ và yêu cầu là mỗi lần nhấn nút button thì trên 2 led 7 đoạn sẽ hiện thị từ 00--->99.
Chân RB0 nối ra nút button,còn PortD thì nối ra 2 Led 7 đoạn,
còn đây là code của mình:

//Chuong trinh LED
//Nhom thuc hien:Nhom 2
//Ngay thuc hien:25/12/2010
//************************************************** *********************

#include<16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#use fast_io(b)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=4000000)
#use fast_io(b)
#use fast_io(d)
#byte portb=0x06
#byte portd=0x08
const unsigned char digital[]={0b00000001,0b00000010,0b00000011,0b00000100,0b00 000101,0b00000110,0b00000111,0b00001000,0b00001001 };
//Chuong trinh ngat
#int_ext
void ngat_RB0()
{
unsigned int press_count;
if(press_count==10)
{
output_D(0x00010000);
}
else if(press_count==20)

output_D(0x00100000);

else if(press_count==30)

output_D(0x00110000);

else if(press_count==40)

output_D(0x01000000);

else if(press_count==50)

output_D(0x01010000);

else if(press_count==60)

output_D(0x01100000) ;

else if(press_count==70)

output_D(0x01110000);

else if(press_count==80)

output_D(0x10000000);

else if(press_count==90)

output_D(0x10010000);

else
portD=digital[press_count];
}



main()
{
set_tris_B(0x00000001);
set_tris_D(0x00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge(H_TO_L);
output_D(0x00);
while (true)
{

}

tdm 26-12-2010 06:26 PM

1 Attachment(s)
Trích:

Nguyên văn bởi troiuuuu (Post 41081)
Chào cả nhà!Hnay đi thực hành ông thầy cho bài thực hành,huu chưa bit gì hết mà làm thực hành,loay hoay mãi mà ko viết dc code rồi tìm tài liệu lên mạng search ....nói chung là chỗ nào cũng lùng hết mà vẫn ko chạy.
Hnay post bài này lên mong mọi người giúp đỡ.
Đề bài:
Thiết kế như hình vẽ và yêu cầu là mỗi lần nhấn nút button thì trên 2 led 7 đoạn sẽ hiện thị từ 00--->99.
Chân RB0 nối ra nút button,còn PortD thì nối ra 2 Led 7 đoạn,
còn đây là code của mình:

//Chuong trinh LED
//Nhom thuc hien:Nhom 2
//Ngay thuc hien:25/12/2010
//************************************************** *********************

#include<16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#use fast_io(b)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=4000000)
#use fast_io(b)
#use fast_io(d)
#byte portb=0x06
#byte portd=0x08
const unsigned char digital[]={0b00000001,0b00000010,0b00000011,0b00000100,0b00 000101,0b00000110,0b00000111,0b00001000,0b00001001 };
//Chuong trinh ngat
#int_ext
void ngat_RB0()
{
unsigned int press_count;
if(press_count==10)
{
output_D(0x00010000);
}
else if(press_count==20)

output_D(0x00100000);

else if(press_count==30)

output_D(0x00110000);

else if(press_count==40)

output_D(0x01000000);

else if(press_count==50)

output_D(0x01010000);

else if(press_count==60)

output_D(0x01100000) ;

else if(press_count==70)

output_D(0x01110000);

else if(press_count==80)

output_D(0x10000000);

else if(press_count==90)

output_D(0x10010000);

else
portD=digital[press_count];
}



main()
{
set_tris_B(0x00000001);
set_tris_D(0x00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge(H_TO_L);
output_D(0x00);
while (true)
{

}

vào ngắt mà không tăng biến press_count.
tham khảo code sau:
Code:

#include<16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#use fast_io(b)
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=4000000)
#use fast_io(b)
#use fast_io(d)
#byte portb=0x06
#byte portd=0x08
int8 press_count,chuc,don_vi;
void ngat_RB0()
{
press_count++;
if(press_count==100)
 {
 press_count=0x00;
 }
  chuc=press_count/10;
  don_vi=press_count%10;
  chuc<<=4;//dao lam 4bit cao
  chuc=chuc|don_vi;//or
  output_D(chuc);
}



main()
{
set_tris_B(0x00000001);
set_tris_D(0x00);
press_count=0x00;
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge(H_TO_L);
 output_D(0x00);
while (true)
{
}
}


troiuuuu 26-12-2010 09:47 PM

1 Attachment(s)
Ko chạy bạn ơi!hix,sao bài mình viết chỉ chạy từ 0-->9 thôi còn chuyển sang led hàng chục thì nó ko chạy nữa?
Bạn coi thử mình viết cách này nó sai chỗ nào?khi bấm cái button từ 1-->9 thì Trên Led hàng đơn vị chạy ok từ 0--->9,còn bấm cái thứ 10 thì nó ko chạy nữa.
================================================== ==================
//Chuong trinh LED
//Nhom thuc hien:Nhom 2
//Ngay thuc hien:25/12/2010
//************************************************** ****************
#include<16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=4000000)
#use fast_io(B)
#use fast_io(D)
#byte portB=0x06
#byte portD=0x08
const unsigned char digital[]={0b00000001,0b00000010,0b00000011,0b00000100,0b00 000101,0b00000110,0b00000111,0b00001000,0b00001001 };
//Chuong trinh ngat
#int_ext
void ngat_RB0()
{
unsigned int press_count;
if(press_count<10)
{
portD=digital[press_count];
++ press_count;
}
if (press_count==10)
{
press_count==0x00010000;
output_d(press_count);
}
if(press_count==20)
{
portD=0x00100000;
}
if(press_count==30)
{
portD=0x00110000;
}
if(press_count==40)
{
portD=0x01000000;
}
if(press_count==50)
{
portD=0x01010000;
}
if(press_count==60)
{

portD=0x01100000;
}

if(press_count==70)
{
portD=0x01110000;
}
if(press_count==80)
{
portD=0x10000000;
}
if(press_count==90)
{
portD=0x10010000;
}

}
//Chuong trinh chinh
main()
{
set_tris_B(0x00000001);
set_tris_D(0);
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge(H_To_L);
portD=0x00;
while(true);
{
}
}

nhiot081290 27-12-2010 03:51 PM

mày hả danh chưa xong ha?.

tdm 27-12-2010 09:21 PM

Trích:

Nguyên văn bởi troiuuuu (Post 41092)
Ko chạy bạn ơi!hix,sao bài mình viết chỉ chạy từ 0-->9 thôi còn chuyển sang led hàng chục thì nó ko chạy nữa?
Bạn coi thử mình viết cách này nó sai chỗ nào?khi bấm cái button từ 1-->9 thì Trên Led hàng đơn vị chạy ok từ 0--->9,còn bấm cái thứ 10 thì nó ko chạy nữa.
================================================== ==================
//Chuong trinh LED
//Nhom thuc hien:Nhom 2
//Ngay thuc hien:25/12/2010
//************************************************** ****************
#include<16f877a.h>
#fuses NOWDT,PUT,HS,NOPROTECT
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=4000000)
#use fast_io(B)
#use fast_io(D)
#byte portB=0x06
#byte portD=0x08
const unsigned char digital[]={0b00000001,0b00000010,0b00000011,0b00000100,0b00 000101,0b00000110,0b00000111,0b00001000,0b00001001 };
//Chuong trinh ngat
#int_ext
void ngat_RB0()
{
unsigned int press_count;
if(press_count<10)
{
portD=digital[press_count];
++ press_count;
}
if (press_count==10)
{
press_count==0x00010000;//thừa dấu bằng
output_d(press_count);
}
if(press_count==20)
{
portD=0x00100000;
}
if(press_count==30)
{
portD=0x00110000;
}
if(press_count==40)
{
portD=0x01000000;
}
if(press_count==50)
{
portD=0x01010000;
}
if(press_count==60)
{

portD=0x01100000;
}

if(press_count==70)
{
portD=0x01110000;
}
if(press_count==80)
{
portD=0x10000000;
}
if(press_count==90)
{
portD=0x10010000;
}

}
//Chuong trinh chinh
main()
{
set_tris_B(0x00000001);
set_tris_D(0);
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge(H_To_L);
portD=0x00;
while(true);//thừa dấu ;
{
}
}

nó chạy từ 0....9 rồi không tăng nữalà phải rồi (xem giải thích trong code)
Code:

//Chuong trinh ngat
#int_ext
void ngat_RB0()
{   
    unsigned int press_count;
    if(press_count<10)  //tại đây hiển thị từ 0...9
    {
    portD=digital[press_count];
    ++ press_count;
    }                                   
    if (press_count==10)
    {
    press_count=0x00010000;//tại đây  lúc này giá trị (press_count là 16) khi vào ngắt lần nữa giá trị (press_count là 16)
không thỏa các điều kiện bạn đưa ra lên nó luôn thoát khỏi ngắt.
    output_d(press_count);
    }
      if(press_count==20)
    {
    portD=0x00100000;
    }
    if(press_count==30)
    {   
    portD=0x00110000;
    }
    if(press_count==40)
    {
    portD=0x01000000;
    }
    if(press_count==50)
    {
    portD=0x01010000;
    }
    if(press_count==60)
    {
       
    portD=0x01100000;
    }
                       
    if(press_count==70)
    {
    portD=0x01110000;
    }
    if(press_count==80)
    {
    portD=0x10000000;
    }
    if(press_count==90)
    {       
    portD=0x10010000;
    }
                                       
}


daotruong 29-12-2010 04:04 AM

các bác giúp em về led7doan với
 
1 Attachment(s)
em chỉnh sửa mãi mà vẫn không đúng, nó cứ chạy sai,
bác nào giúp em với nhé .
cảm ơn cac bác nhiều:
bác nào xem xong biết chỗ sai chi em với :gmail:daotruongmo.90@gmail.com

stupid11 17-03-2011 09:59 PM

1 Attachment(s)
Mấy bạn viết gì mà phức tạp thế.


Múi giờ GMT. Hiện tại là 01:00 AM.

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam