đây là một chương trình mẫu ,đã qua chạy thử ,mong các bạn hãy đưa box avr lớn mạch hơn,thân ái.
//----------------------------------------------------------------------
//LAP TRINH : HIEN THI 4 LED 7SEG
//TH : PHAN THE BINH
//CN : DIEU KHIEN TU DONG
//NTH : 19/10/2007
//NHT : 19/10/2007
//-----------------------------
//-----------------------------
#include <mega8.h>
#include <delay.h>
//-----------------------------
#define led1 PORTC.3
#define led2 PORTC.2
#define led3 PORTC.1
#define led4 PORTC.0
//-----------------------------
#define led_7seg PORTD
//-----khai bao cac bien-------
int h_don_vi,h_chuc,h_tram,h_nghin;
int sodem,n_dv,n_c,n_t,n_n,n_7seg;
//-----chuong trinh con--------
void giai_ma_7_seg(int n_7seg);
//----chuong trinh chinh-------
void main(void)
{
//------khai bao cong ra-------
DDRC =0xff;
DDRD =0xff;
//----nhap gia tri hien thi----
sodem=1357;
//-----------------------------
//xu li giai ma so dem
h_don_vi=sodem % 10;
n_dv=sodem / 10;
h_chuc=n_dv % 10;
n_c=n_dv / 10;
h_tram=n_c % 10;
n_t=n_c / 10;
h_nghin=n_t%10;
n_n=n_t/10;
//-----------------------------
//----vong lap hien thi so----
while (1)
{
//------------------
n_7seg=h_don_vi;
giai_ma_7_seg(n_7seg);
led1=1;
led2=1;
led3=1;
led4=0;
delay_ms(5);
//------------------
n_7seg=h_chuc;
giai_ma_7_seg(n_7seg);
led1=1;
led2=1;
led3=0;
led4=1;
delay_ms(5);
//------------------
n_7seg=h_tram;
giai_ma_7_seg(n_7seg);
led1=1;
led2=0;
led3=1;
led4=1;
delay_ms(5);
//-------------------
n_7seg=h_nghin;
giai_ma_7_seg( n_7seg);
led1=0;
led2=1;
led3=1;
led4=1;
delay_ms(5);
};
}
//-------------------------------
//--------giai ma 7seg-----------
//-------------------------------
void giai_ma_7_seg(int n_7seg)
{
switch (n_7seg)
{
case 0:
led_7seg=0xa0;
break;
case 1:
led_7seg=0xbb;
break;
case 2:
led_7seg=0x62;
break;
case 3:
led_7seg=0x2a;
break;
case 4:
led_7seg=0x39;
break;
case 5:
led_7seg=0x2c;
break;
case 6:
led_7seg=0x24;
break;
case 7:
led_7seg=0xba;
break;
case 8:
led_7seg=0x20;
break;
case 9:
led_7seg=0x28;
break;
}
}
//----------------------------
//---KET THUC CHUONG TRINH----
//----------------------------
//----------------------------------------------------------------------
//LAP TRINH : HIEN THI 4 LED 7SEG
//TH : PHAN THE BINH
//CN : DIEU KHIEN TU DONG
//NTH : 19/10/2007
//NHT : 19/10/2007
//-----------------------------
//-----------------------------
#include <mega8.h>
#include <delay.h>
//-----------------------------
#define led1 PORTC.3
#define led2 PORTC.2
#define led3 PORTC.1
#define led4 PORTC.0
//-----------------------------
#define led_7seg PORTD
//-----khai bao cac bien-------
int h_don_vi,h_chuc,h_tram,h_nghin;
int sodem,n_dv,n_c,n_t,n_n,n_7seg;
//-----chuong trinh con--------
void giai_ma_7_seg(int n_7seg);
//----chuong trinh chinh-------
void main(void)
{
//------khai bao cong ra-------
DDRC =0xff;
DDRD =0xff;
//----nhap gia tri hien thi----
sodem=1357;
//-----------------------------
//xu li giai ma so dem
h_don_vi=sodem % 10;
n_dv=sodem / 10;
h_chuc=n_dv % 10;
n_c=n_dv / 10;
h_tram=n_c % 10;
n_t=n_c / 10;
h_nghin=n_t%10;
n_n=n_t/10;
//-----------------------------
//----vong lap hien thi so----
while (1)
{
//------------------
n_7seg=h_don_vi;
giai_ma_7_seg(n_7seg);
led1=1;
led2=1;
led3=1;
led4=0;
delay_ms(5);
//------------------
n_7seg=h_chuc;
giai_ma_7_seg(n_7seg);
led1=1;
led2=1;
led3=0;
led4=1;
delay_ms(5);
//------------------
n_7seg=h_tram;
giai_ma_7_seg(n_7seg);
led1=1;
led2=0;
led3=1;
led4=1;
delay_ms(5);
//-------------------
n_7seg=h_nghin;
giai_ma_7_seg( n_7seg);
led1=0;
led2=1;
led3=1;
led4=1;
delay_ms(5);
};
}
//-------------------------------
//--------giai ma 7seg-----------
//-------------------------------
void giai_ma_7_seg(int n_7seg)
{
switch (n_7seg)
{
case 0:
led_7seg=0xa0;
break;
case 1:
led_7seg=0xbb;
break;
case 2:
led_7seg=0x62;
break;
case 3:
led_7seg=0x2a;
break;
case 4:
led_7seg=0x39;
break;
case 5:
led_7seg=0x2c;
break;
case 6:
led_7seg=0x24;
break;
case 7:
led_7seg=0xba;
break;
case 8:
led_7seg=0x20;
break;
case 9:
led_7seg=0x28;
break;
}
}
//----------------------------
//---KET THUC CHUONG TRINH----
//----------------------------
Comment