Mình đang làm một cái đồng hồ sử dụng atmega8. Nhưng nó không chạy có lẽ do code sai. moi người xem hộ mình với. Đây là code và mô phỏng.
PHP 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 : 26/05/2013
Author : NeVaDa
Company :
Comments:
Chip type : ATmega8L
Program type : Application
AVR Core Clock frequency: 4,000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
// I2C Bus functions
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=4
.equ __scl_bit=5
#endasm
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>
// Timer1 output compare A interrupt service routine
#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 1
unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x20
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
static unsigned char input_index=0;
// Read the 8 most significant bits
// of the AD conversion result
adc_data[input_index]=ADCH;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
input_index=0;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff))+input_index;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
}
// SPI functions
#include <spi.h>
// Declare your global variables here
// Declare your global variables here
/*------------------------------------------------------------------------------------------------*/
// khai báo bien, mang toan cuc
#define latch PORTB.2
#define MODE PINB.0
#define UP PINB.6
#define DOWN PINB.7
#define SET PINB.1
bit AP; // che do 24h (1), che do 12h(0)
bit sb; // sb=1: phim duoc nhan; sb=0; phim khong dc nhan
unsigned int a,b,c,d=0;
unsigned char hour,min,sec,temp,i,mode=0,h;
unsigned char led7seg[13]=
{
// MANG CÁC GIA TRI LED 7SEG TU SO 0- SO 9,ki tu "C" ,"N","NULL"
// Dau cham mac dinh khong sang nên bit 8 de gia tri"1"
0b11000000, // so "0"
0b11111001, // so "1"
0b10100100, // so "2"
0b10110000, // so "3"
0b10011001, // so "4"
0b10010010, // so "5"
0b10000010, // so "6"
0b11011000, // so "7"
0b10000000, // so "8"
0b10010000, // so "9"
0b11000110, // chu "C"
0b11001000, // chu "N"
0b11111111 // "NULL"
};
unsigned char array_led[6]; // mang chua 6 led 7seg can hien thi
/*------------------------------------------------------------------------------------------------*/
// chuyen doi che do gio 24h <=> 12h
// AP=1: che do 24h; AP=0: che do 12h
void check_conveter_hour(void)
{
// kiem tra che do gio: 24h or 12h
if(AP==0) // che do 12h
{
if(hour==0)
{
h=12;
}
else
{
if(hour>12)
{
h=hour-12;
}
else
{
h=hour;
};
};
}
else // che do 24h
{
h=hour;
};
}
void display_temp_lcd(void)
{
temp=0.49*adc_data[0]; // Nhiet do, giá tri 0,49 khi chon ADC 10bit
a=temp/1000;
b=(temp-1000*a)/100;
c=(temp-1000*a-100*b)/10;
d=(temp-1000*a-100*b-10*c);
}
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
// ctc chot du lieu 74HC595
void latch_data(void)
{
latch=0;
latch=1;
}
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
// ctc chuyen doi thoi gian sang ma 7seg
void bin2_7seg(void)
{
display_temp_lcd();
array_led[0]=led7seg[min%10]; // phan don vi cua phut
array_led[1]=led7seg[min/10]; // phan chuc cua phut
array_led[2]=led7seg[h%10]; // phan don vi cua gio
array_led[3]=led7seg[h/10]; // phan chuc cua gio
if(a==0&&b==0&&c==0) // Nêu nhiet do nho hon 10 thi hien thi 1 chu so
{
array_led[4]=led7seg[d]; // phan don vi cua nhiet do
array_led[5]=led7seg[12]; // phan chuc cua nhiet do
}
else
{
array_led[4]=led7seg[d]; // phan don vi cua nhiet do
array_led[5]=led7seg[c]; // phan chuc cua nhiet do
};
}
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
void display_led7seg(void) // ctc hien thi len led7seg
{
PORTD.3=0;
bin2_7seg(); // chuyen doi tu bin sang ma 7seg
spi(array_led[i]); // xuat du lieu ra 595 dua den led7seg thu i
latch_data(); // chot du lieu ra 595
PORTD=i;
PORTD.3=1; // enable 74hc138 chon led can hien thi
if(i==6)
{
i=0;
}
else
{
i++;
};
}
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
// chuong trinh con kiem tra trang thai cua phim nhan : bit trang thai sb
void status_button(void)
{
if(MODE&&UP&&DOWN&&SET)
{
sb=0; // kiem tra phim da nha hay chua?
}
}
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
// ctc test phim ( cai dat thoi gian, bao thuc )
void set_alarm_time(void)
{
/*****************************************************/
status_button(); // goi ctc kiem tra trang thai phim nhan
/*****************************************************/
// doan chuong trinh chuyen doi che do gio: 24h/12h
// sb=1: phim duoc nhan; sb=0; phim khong dc nhan
if(SET==0&&sb==0)
{
AP=~AP;
sb=1; // bat trang thai co phim nhan sb=1;
}
/*****************************************************/
// doan chuong trinh chon che do
if(MODE==0&&sb==0)
{
if(mode==5) // che do nguoi dung
{
mode=0;
}
else
{
mode++;
};
sb=1; // bao co phim nhan
}
/*****************************************************/
// doan chuong trinh cho che do 1(mode=1):
if(mode==1) // chinh gio
{
//////////////////////////////////////////////////////////////////////////////
if(UP==0&&sb==0) // phim "UP" nhan
{
if(hour==23)
{
hour=0;
}
else
{
hour++;
};
rtc_set_time(hour,min,sec); // dat lai gio
sb=1; // bao co phim nhan
}
////////////////////////////////////////////////////////////////////////////////
if(DOWN==0&&sb==0) // phim "DOWN" nhan
{
if(hour==0)
{
hour=23;
}
else
{
hour--;
};
rtc_set_time(hour,min,sec); // dat lai time
sb=1;
}
}
/*****************************************************/
// doan chuong trinh cho che do 2(mode=2):
if(mode==2) // chinh phut
{
////////////////////////////////////////////////////////////////////////////////
if(UP==0&&sb==0) // phim "UP" nhan
{
if(min==59)
{
min=0;
}
else
{
min++;
};
rtc_set_time(hour,min,sec); // dat lai gio
sb=1; // bao co phim nhan
}
////////////////////////////////////////////////////////////////////////////////
if(DOWN==0&&sb==0) // phim "DOWN" nhan
{
if(min==0)
{
min=59;
}
else
{
min--;
};
rtc_set_time(hour,min,sec); // dat lai gio
sb=1;
}
}
}
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
display_led7seg();
}
/*****************************************************/
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=In Func0=In
// State7=T State6=T State5=1 State4=1 State3=1 State2=1 State1=T State0=T
PORTB=0x3C;
DDRB=0x3C;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
PORTD=0xFF;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 62,500 kHz
// Mode: CTC top=OCR1A
// 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: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0B;
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: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;
// 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 Clock frequency: 1000,000 kHz
// ADC Voltage Reference: AREF pin
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCA;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 2*1000,000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x54;
SPSR=0x01;
// TWI initialization
// TWI disabled
TWCR=0x00;
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 1Hz
rtc_init(0,1,0);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
// Place your code here
rtc_get_time(&hour,&min,&sec); // Doc gio, phut, giay tu ds1307
check_conveter_hour();
set_alarm_time(); // goi ctc thuc hien cai dat
if(mode==0) // O che do hien thi binh thuong
{
display_led7seg(); // Hien thi thoi gian len lcd
}
else // chuyen sang che do set
{
set_alarm_time(); // goi ctc thuc hien cai dat
};
}
}