Mạch đếm sản phẩm 00 - 99 + ATmega 16


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

Project :
Version :
Date    : 3/30/2014
Author  : NeVaDa
Company :
Comments:


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

#include <mega16.h>
#include <delay.h>
#define  data PORTC
#define  led1 PORTD.0
#define  led2  PORTD.1
#define xung PIND.2
#define bao PORTB.0
#define den PORTB.1
unsigned char led7[10]={  0b11000000, // so "0"
                          0b11111001, // so "1"
                          0b10100100, // so "2"
                          0b10110000, // so "3"
                          0b10011001, // so "4"
                          0b10010010, // so "5"
                          0b10000010, // so "6"
                          0b11111000, // so "7"
                          0b10000000, // so "8"
                          0b10011000 // so "9"
                          };
int a,b,i=0;
void hienthi(int k)
{a=k/10;
 b=k%10;
 led1=1;
 led2=0;
 data=led7[a];
 delay_us(50);
 led1=led2=0;
 led1=0;
 led2=1;
 data=led7[b];
 delay_us(50);
 led1=led2=0;
}
// External Interrupt 0 service routine
int tran;
int count;
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
     i++;  
     if(i==99){
        i=0;
     }  
     bao = 0;  
     den = 1;

}

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x04;
// Place your code here
    tran++;
    if(tran == 5){
        tran =0;
        count++;
    }
}
// Declare your global variables here

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

// Port B 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
PORTB=0x00;
DDRB=0x03;

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

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

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x05;
TCNT0=0x04;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 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: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0x40;

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

// USART initialization
// USART disabled
UCSRB=0x00;

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

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Global enable interrupts
#asm("sei")
bao =1;
den =0;
while (1)
      {
      // Place your code here                
        hienthi(i);
             if(count == 4){
        bao = 1;
        count =0;    
        den = 0;
     }
      }
}



( Full code C + Simulation Protues)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút  hiện ra ở góc phải phía trên, nhấn vào để đến trang download

Nguồn banlinhkien.vn

0 Response to "Mạch đếm sản phẩm 00 - 99 + ATmega 16"

Đăng nhận xét