Học viện kỹ thuật quân sự
Hãy đăng nhập để có thể dùng đầy đủ chức năng của forum
Học viện kỹ thuật quân sự

100 Hoàng Quốc Việt, Cầu Giấy, Hà Nội


You are not connected. Please login or register

Topic hướng dẫn làm bt môn Thiết kế hệ thống số

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

Bài 1: Ấn nút bên trái thì đèn bật, ấn nút bên phải đèn tắt

Code:

#include <mega32.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=P
PORTA=0x03;  //0b00000011
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTC=0x00;
DDRC=0x01; //0b00000001

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

while (1)
      {
      // Place your code here
      if (PINA.0==0) {
      PORTC.0=1;
      }
      else if (PINA.1==0) {
      PORTC.0=0;
      }
            };
}


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz
Bài 2: Led chạy từ 00 đến 99


Code:

#include <mega32.h>
#include <delay.h>
unsigned int k,dem = 0;    //vi DEM_SO_NGAT se dem den 500 nen phai chon kieu int chu ko phai kieu char (char 0->255)

#define NUM_0 0xDF
#define NUM_1 0x86
#define NUM_2 0xBB
#define NUM_3 0xAF
#define NUM_4 0xE6
#define NUM_5 0xED
#define NUM_6 0xFC
#define NUM_7 0x87
#define NUM_8 0xFF
#define NUM_9 0xE7 

unsigned char MANG_NUM[10]={NUM_0,NUM_1,NUM_2,NUM_3,NUM_4,NUM_5,NUM_6,NUM_7,NUM_8,NUM_9};
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)    //thay TIM0_OVF bang 12 cung duoc
{
// Place your code here
  // #asm("clr") //dung toan bo ngat khac ko duoc thuc hien

    dem++;
    if(dem>500) {dem=0;k++;};
    if(k>99) k=0;
    TCNT0=6; //de thanh ghi dem bat dau dem tu 6 -> 255 tuong duong voi 2ms ngat 1 lan
    #asm("sei")    // cho phep ngat... chuong trinh ngat nam o giua 2 cau lenh asm
   
}

// Declare your global variables here
void hienthi(void)
{
    PORTB.0=0;
    PORTA=MANG_NUM[k%10];delay_ms(10);PORTB.0=1;
    PORTB.1=0;
    PORTA=MANG_NUM[k/10];delay_ms(10);PORTB.1=1;
}
void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=0x00;              //thanh ghi nay dem tu 0 -> 255, sau khi tran thi thuc hien ham interrupt o tren
OCR0=0x00;  // dung cho so sanh khop (compare match) -> khong dung

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
            // Place your code here
hienthi();
        }
}


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz
Bài 2 (cách 2)
Code:
#include <mega32.h>
#include <delay.h>
unsigned int DEM_SO_NGAT = 0;    //vi DEM_SON_NGAT se dem den 500 nen phai chon kieu int chu ko phai kieu char (char 0->255)
unsigned char DEM_DONVI = 0;
unsigned char DEM_CHUC = 0;
#define NUM_0 0xDF
#define NUM_1 0x86
#define NUM_2 0xBB
#define NUM_3 0xAF
#define NUM_4 0xE6
#define NUM_5 0xED
#define NUM_6 0xFC
#define NUM_7 0x87
#define NUM_8 0xFF
#define NUM_9 0xE7 

unsigned char MANG_NUM[10]={NUM_0,NUM_1,NUM_2,NUM_3,NUM_4,NUM_5,NUM_6,NUM_7,NUM_8,NUM_9};
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)    //thay TIM0_OVF bang 12 cung duoc
{
// Place your code here
  // #asm("clr") //dung toan bo ngat khac ko duoc thuc hien
    DEM_SO_NGAT++;
    if (DEM_SO_NGAT==500) {
        DEM_DONVI++;
        DEM_SO_NGAT=0;
        if (DEM_DONVI==10) {
        DEM_DONVI=0;
        DEM_CHUC++;
          if (DEM_CHUC==10) {
            DEM_CHUC=0;
          }
        }
    }
    TCNT0=6; //de thanh ghi dem bat dau dem tu 6 -> 255 tuong duong voi 2ms ngat 1 lan
    #asm("sei")    // cho phep ngat... chuong trinh ngat nam o giua 2 cau lenh asm
   
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x03;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=0x00;              //thanh ghi nay dem tu 0 -> 255, sau khi tran thi thuc hien ham interrupt o tren
OCR0=0x00;  // dung cho so sanh khop (compare match) -> khong dung

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      PORTB.1=1;
      PORTB.0=0;
      PORTA = MANG_NUM[DEM_DONVI]; 
      delay_ms(10);
      PORTB.0=1;
      PORTB.1=0;
      PORTA = MANG_NUM[DEM_CHUC];
      delay_ms(10);
      };
}


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz
Bài 2 (cách 3)
Code:

#include <mega32.h>
#include <delay.h>
unsigned int DEM_SO_NGAT = 0;    //vi DEM_SON_NGAT se dem den 500 nen phai chon kieu int chu ko phai kieu char (char 0->255)
unsigned char DEM_DONVI = 0;
unsigned char DEM_CHUC = 0;
#define NUM_0 0xDF
#define NUM_1 0x86
#define NUM_2 0xBB
#define NUM_3 0xAF
#define NUM_4 0xE6
#define NUM_5 0xED
#define NUM_6 0xFC
#define NUM_7 0x87
#define NUM_8 0xFF
#define NUM_9 0xE7 

unsigned char MANG_NUM[10]={NUM_0,NUM_1,NUM_2,NUM_3,NUM_4,NUM_5,NUM_6,NUM_7,NUM_8,NUM_9};
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)    //thay TIM0_OVF bang 12 cung duoc
{
// Place your code here
  // #asm("clr") //dung toan bo ngat khac ko duoc thuc hien
    DEM_SO_NGAT++;
    if (DEM_SO_NGAT==500) {
        DEM_DONVI++;
        DEM_SO_NGAT=0;
        if (DEM_DONVI==10) {
        DEM_DONVI=0;
        DEM_CHUC++;
          if (DEM_CHUC==10) {
            DEM_CHUC=0;
          }
        }
    }
    TCNT0=6; //de thanh ghi dem bat dau dem tu 6 -> 255 tuong duong voi 2ms ngat 1 lan
    #asm("sei")    // cho phep ngat... chuong trinh ngat nam o giua 2 cau lenh asm
   
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x03;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=0x00;              //thanh ghi nay dem tu 0 -> 255, sau khi tran thi thuc hien ham interrupt o tren
OCR0=0x00;  // dung cho so sanh khop (compare match) -> khong dung

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      if (DEM_SO_NGAT%2==0) {
      PORTB.1=1;
      PORTB.0=0;
      PORTA = MANG_NUM[DEM_DONVI];    //hien gia tri bien dem len led7seg
      //delay_ms(10);
      }
      else {
      PORTB.0=1;
      PORTB.1=0;
      PORTA = MANG_NUM[DEM_CHUC];
      //delay_ms(10);
      }
      };
}


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz
Ai hỏi gì thì post câu hỏi ở đây.


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz

lamsheva


Hạ sĩ 1
Hạ sĩ 1
ngon nhưng mà chưa cài Laughing

Xem lý lịch thành viên

zking


Trung sĩ 6
Trung sĩ 6
Nhìn ngon thật đấy Laughing
quả này lớp mềnh có cao thủ rồi =)
Thịnh fuk =)

Xem lý lịch thành viên
cao thủ j`, bài này đơn giản mà, đến các bài khó thì thôi ko post nữa lol
Làm nhiều cách sau này chẳng may có bài tập lớn thầy TX.Nam còn kiếm chác đc tí


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz

no_love90


Thượng sĩ 6
Thượng sĩ 6
sao kô có slide của thầy hả Thịnh

Xem lý lịch thành viên http://acmepeers.com/?u=bqkd
no_love90 đã viết:sao kô có slide của thầy hả Thịnh

Code:
http://hvktqs.1forum.biz/t1051-topic#9011
Post rồi mà, thầy gửi đó


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz
Đồng hồ (chưa chỉnh được giờ phút giây, mới chỉ chạy xuông từ 00:00:00 đến 23:59:59), đang nghiên cứu nốt

Code:

#include <mega32.h>
#include <delay.h>

#define NUM_0                  0XDF
#define NUM_1                  0X86
#define NUM_2                  0XBB
#define NUM_3                  0XAF
#define NUM_4                  0XE6
#define NUM_5                  0XED
#define NUM_6                  0XFC
#define NUM_7                  0X87
#define NUM_8                  0XFF
#define NUM_9                  0XE7

#define CHUCH                PORTB.0
#define DONVIH              PORTB.1
#define CHUCM                PORTB.2
#define DONVIM              PORTB.3
#define CHUCS              PORTB.4
#define DONVIS              PORTB.5

// Declare your global variables here
unsigned char MANG_NUM[10]  = {NUM_0, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5,
                                NUM_6, NUM_7, NUM_8, NUM_9};

unsigned int DEM = 0;

unsigned char NUM_DEMS = 0, NUM_DEMM=0, NUM_DEMH=0, NUM_CHUCS, NUM_DONVIS, NUM_CHUCM, NUM_DONVIM, NUM_CHUCH, NUM_DONVIH;

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
    // Place your code here
    #asm("cli")

    DEM++;
    if (DEM == 500)  //500 la 1s
    {
        DEM            = 0; 
        NUM_DEMS++;
        if (NUM_DEMS == 60)
        {
            NUM_DEMS    = 0;
            NUM_DEMM++;
            if (NUM_DEMM == 60) {
                NUM_DEMM = 0;
                NUM_DEMH++;
                if (NUM_DEMH == 24) {
                    NUM_DEMH=0;
                }
            }
           
        }
    }

    TCNT0      = 6;

    #asm("sei")
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=6;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

    while (1)
    {
        // Place your code here
        NUM_CHUCS            = NUM_DEMS/10;
        NUM_DONVIS          = NUM_DEMS%10;
                NUM_CHUCM            = NUM_DEMM/10;
        NUM_DONVIM          = NUM_DEMM%10;
                NUM_CHUCH            = NUM_DEMH/10;
        NUM_DONVIH          = NUM_DEMH%10;
       
        //DIEU KHIEN SANG LED
        PORTA              = 0x00;
        CHUCH              = 0;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              =1;
        CHUCS              =1;
        DONVIS              =1;
        PORTA              = MANG_NUM[NUM_CHUCH];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 0;
        CHUCM              = 1;
        DONVIM              =1;
        CHUCS              =1;
        DONVIS              =1;
        PORTA              = MANG_NUM[NUM_DONVIH];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH            = 1;
        DONVIH          = 1;
        CHUCM              = 0;
        DONVIM              =1;
        CHUCS              =1;
        DONVIS              =1;
        PORTA              = MANG_NUM[NUM_CHUCM];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              =0;
        CHUCS              =1;
        DONVIS              =1;
        PORTA              = MANG_NUM[NUM_DONVIM];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              =1;
        CHUCS              =0;
        DONVIS              =1;
        PORTA              = MANG_NUM[NUM_CHUCS];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              =1;
        CHUCS              =1;
        DONVIS              =0;
        PORTA              = MANG_NUM[NUM_DONVIS];
        delay_ms(5);
    };
}


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz
Thêm 3 cái nút bấm, nút trên cùng chỉnh giờ, thứ 2 chỉnh phút, thứ 3 chỉnh giây


Code:

/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : Digital Clock
Version : 1.0
Date    : 2/26/2012
Author  : Thinh
Company : DTVT7A
Comments:


Chip type          : ATmega32
Program type        : Application
Clock frequency    : 8.000000 MHz
Memory model        : Small
External RAM size  : 0
Data Stack size    : 512
*****************************************************/

#include <mega32.h>
#include <delay.h>

#define NUM_0                  0XDF
#define NUM_1                  0X86
#define NUM_2                  0XBB
#define NUM_3                  0XAF
#define NUM_4                  0XE6
#define NUM_5                  0XED
#define NUM_6                  0XFC
#define NUM_7                  0X87
#define NUM_8                  0XFF
#define NUM_9                  0XE7

#define CHUCH              PORTB.0
#define DONVIH              PORTB.1
#define CHUCM              PORTB.2
#define DONVIM              PORTB.3
#define CHUCS              PORTB.4
#define DONVIS              PORTB.5

// Declare your global variables here
unsigned char MANG_NUM[10]  = {NUM_0, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5,
                                NUM_6, NUM_7, NUM_8, NUM_9};

unsigned int DEM = 0;

unsigned char NUM_DEMS = 0, NUM_DEMM=0, NUM_DEMH=0, NUM_CHUCS, NUM_DONVIS, NUM_CHUCM, NUM_DONVIM, NUM_CHUCH, NUM_DONVIH;

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
    // Place your code here
    #asm("cli")

    DEM++;
    if (DEM == 500)  //500 la 1s
    {
        DEM            = 0; 
        NUM_DEMS++;
        if (NUM_DEMS == 60)
        {
            NUM_DEMS    = 0;
            NUM_DEMM++;
            if (NUM_DEMM == 60) {
                NUM_DEMM = 0;
                NUM_DEMH++;
                if (NUM_DEMH == 24) {
                    NUM_DEMH=0;
                }
            }
           
        }
    }

    TCNT0      = 6;

    #asm("sei")
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xFF;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0xFF;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=6;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// Global enable interrupts
#asm("sei")

    while (1)
    {
        // Place your code here
        NUM_CHUCS            = NUM_DEMS/10;
        NUM_DONVIS          = NUM_DEMS%10;
                NUM_CHUCM            = NUM_DEMM/10;
        NUM_DONVIM          = NUM_DEMM%10;
                NUM_CHUCH            = NUM_DEMH/10;
        NUM_DONVIH          = NUM_DEMH%10;
       
        //DIEU KHIEN SANG LED
        PORTA              = 0x00;
        CHUCH              = 0;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              =1;
        CHUCS              =1;
        DONVIS              =1;
        PORTA              = MANG_NUM[NUM_CHUCH];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 0;
        CHUCM              = 1;
        DONVIM              = 1;
        CHUCS              = 1;
        DONVIS              = 1;
        PORTA              = MANG_NUM[NUM_DONVIH];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 1;
        CHUCM              = 0;
        DONVIM              = 1;
        CHUCS              = 1;
        DONVIS              = 1;
        PORTA              = MANG_NUM[NUM_CHUCM];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              = 0;
        CHUCS              = 1;
        DONVIS              = 1;
        PORTA              = MANG_NUM[NUM_DONVIM];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              = 1;
        CHUCS              = 0;
        DONVIS              = 1;
        PORTA              = MANG_NUM[NUM_CHUCS];
        delay_ms(5);
       
        PORTA              = 0x00;
        CHUCH              = 1;
        DONVIH              = 1;
        CHUCM              = 1;
        DONVIM              = 1;
        CHUCS              = 1;
        DONVIS              = 0;
        PORTA              = MANG_NUM[NUM_DONVIS];
        delay_ms(5);
       
        if (PIND.0 == 0) {
        delay_ms(200);
          NUM_DEMH++;
          if (NUM_DEMH == 24) {
                NUM_DEMH = 0;
          }
        }
       
        if (PIND.1 == 0) {
        delay_ms(200);
          NUM_DEMM++;
          if (NUM_DEMM == 60) {
                NUM_DEMM = 0;
          }
        }
       
        if (PIND.2 == 0) {
        delay_ms(200);
          NUM_DEMS++;
          if (NUM_DEMS == 60) {
                NUM_DEMS = 0;
          }
        }
    };
}


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz
Số ở dòng dưới tương ứng với giá trị thay đổi của i


Code:
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : LCD DTVT7A
Version : 1.0
Date    : 2/24/2012
Author  : MrThinh
Company : MTA
Comments: No comments!


Chip type          : ATmega32
Program type        : Application
Clock frequency    : 8.000000 MHz
Memory model        : Small
External RAM size  : 0
Data Stack size    : 512
*****************************************************/

#include <mega32.h>
#include <delay.h>
#include <stdio.h>

// Alphanumeric LCD Module functions
#asm
  .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

// Declare your global variables here
unsigned char i=0;

void leftright(void) {
char strbuff[20];
    i++;
   
      sprintf(strbuff,"%d",i);
      if (i==10) {
        i=0;
      }
      // Place your code here
      lcd_gotoxy(i,0);
      lcd_putsf("DTVT7A");
     
//      lcd_gotoxy(10-i,1);
//      lcd_putsf("DTVT7A");
        lcd_gotoxy(5,1);
 lcd_puts(strbuff);
     
      delay_ms(200);
      lcd_clear();
}
void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

while (1)
      {
          leftright();
      };
}
Code:
Tham khảo:
http://elecrom.wordpress.com/2009/07/14/codevisionavr-tip-how-to-display-formatted-numbers-on-lcd/


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz

14 LCD clock on Sat Mar 03, 2012 10:09 pm

Project của bài tập lớn đây

Code:
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date    : 3/2/2012
Author  :
Company :
Comments:


Chip type              : ATmega32
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size      : 0
Data Stack size        : 512
*****************************************************/

#include <mega32.h>
#include <delay.h>

// Alphanumeric LCD Module functions
#asm
  .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

unsigned char gio=0,phut=0,giay=0;
unsigned int dem;
unsigned char vitri=2;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
#asm("cli")

    dem++;
    if (dem == 500)  //500 la 1s
    {
        dem            = 0; 
        giay++;
        if (giay == 60)
        {
            giay    = 0;
            phut++;
            if (phut == 60) {
                phut = 0;
                gio++;
                if (gio == 24) {
                    gio=0;
                }
            }
           
        }
    }

    TCNT0      = 6;

    #asm("sei")
}

// Declare your global variables here
void display(void) {
    //hien thi gio
      lcd_gotoxy(vitri,0);
      lcd_putchar(48+gio/10);
      lcd_gotoxy(vitri+1,0);
      lcd_putchar(48+gio%10);
      lcd_gotoxy(vitri+2,0);
      lcd_putsf(":");
      //hien thi phut
      lcd_gotoxy(vitri+3,0);
      lcd_putchar(48+phut/10);
      lcd_gotoxy(vitri+4,0);
      lcd_putchar(48+phut%10);
      lcd_gotoxy(vitri+5,0);
      lcd_putsf(":");
      //hien thi giay
      lcd_gotoxy(vitri+6,0);
      lcd_putchar(48+giay/10);
      lcd_gotoxy(vitri+7,0);
      lcd_putchar(48+giay%10);
   
}
void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0xFF;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x03;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here
      display();
      if (PIND.0==0) {
        delay_ms(200);
        gio++;
      }
      if (PIND.1==0) {
        delay_ms(200);
        phut++;
      }
      if (PIND.2==0) {
        delay_ms(200);
        giay++;
      }

      };
}


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz

thanhcong_39


Học viên 2
Học viên 2
admin đã viết:Bài 1: Ấn nút bên trái thì đèn bật, ấn nút bên phải đèn tắt

Code:

#include <mega32.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=P
PORTA=0x03;  //0b00000011
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTC=0x00;
DDRC=0x01; //0b00000001

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

while (1)
      {
      // Place your code here
      if (PINA.0==0) {
      PORTC.0=1;
      }
      else if (PINA.1==0) {
      PORTC.0=0;
      }
            };
}

Xem lý lịch thành viên
cho bừa vào ý mà, mô phỏng mà big grin thực tế thì dùng con 1k thôi thì phải big grin


_________________
Alienware.com
Xem lý lịch thành viên http://hvktqs.1forum.biz

Sponsored content


Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết