không hiểu sao em không đưa đươc ảnh lên.nhưng em mô phỏng thì khi em gán thứ 7 sau khi hết ngày đó thì nó lại chuyển sang thứ o.rồi tăng dần lên 1,2,3...7,0,1,2,3....
code của em.
#include <mega16.h>
#include <delay.h>
// I2C Bus functions
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>
// Alphanumeric LCD functions
#include <alcd.h>
#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 0xE0
unsigned char h,m,s,day,date,month,year;
// 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;
}
// Declare your global variables here
void putnum(unsigned char so)
{
unsigned char chuc,dv;
chuc=so/10;
dv=(so-10*chuc);
lcd_putchar(chuc+48);
lcd_putchar(dv+48);
}
void putnum1(unsigned char so1)
{
unsigned char tram,chuc,dv;
tram=so1/100;
chuc=(so1-100*tram)/10;
dv=(so1-10*chuc-100*tram);
lcd_putchar(tram+48);
lcd_putchar(chuc+48);
lcd_putchar(dv+48);
}
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0xff;
PORTC=0x00;
DDRC=0xff;
PORTD=0x00;
DDRD=0xff;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCB;
// I2C Bus initialization
// I2C Port: PORTC
// I2C SDA bit: 1
// I2C SCL bit: 0
// Bit Rate: 100 kHz
// Note: I2C settings are specified in the
// Project|Configure|C Compiler|Libraries|I2C menu.
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 1Hz
rtc_init(0,0,0);
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 0
// RD - PORTB Bit 1
// EN - PORTB Bit 2
// D4 - PORTB Bit 4
// D5 - PORTB Bit 5
// D6 - PORTB Bit 6
// D7 - PORTB Bit 7
// Characters/line: 16
lcd_init(16);
lcd_clear();
rtc_set_time(23,59,57);
rtc_set_date(7,3,5,13);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
rtc_get_time(&h,&m,&s);
rtc_get_date(&day,&date,&month,&year);
lcd_gotoxy(0,0);
putnum(h);
lcd_putsf(":");
putnum(m);
lcd_putsf(":");
putnum(s);
lcd_putsf(" Thu ");
putnum(day);
lcd_gotoxy(0,1);
putnum(date);
lcd_putsf("-");
putnum(month);
lcd_putsf("-");
lcd_putsf("20");
putnum(year);
lcd_putsf(" NT:");
putnum1(adc_data[0]);
delay_ms(100);
}
}
code của em.
#include <mega16.h>
#include <delay.h>
// I2C Bus functions
#include <i2c.h>
// DS1307 Real Time Clock functions
#include <ds1307.h>
// Alphanumeric LCD functions
#include <alcd.h>
#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 0xE0
unsigned char h,m,s,day,date,month,year;
// 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;
}
// Declare your global variables here
void putnum(unsigned char so)
{
unsigned char chuc,dv;
chuc=so/10;
dv=(so-10*chuc);
lcd_putchar(chuc+48);
lcd_putchar(dv+48);
}
void putnum1(unsigned char so1)
{
unsigned char tram,chuc,dv;
tram=so1/100;
chuc=(so1-100*tram)/10;
dv=(so1-10*chuc-100*tram);
lcd_putchar(tram+48);
lcd_putchar(chuc+48);
lcd_putchar(dv+48);
}
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0xff;
PORTC=0x00;
DDRC=0xff;
PORTD=0x00;
DDRD=0xff;
ADMUX=(FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff);
ADCSRA=0xCB;
// I2C Bus initialization
// I2C Port: PORTC
// I2C SDA bit: 1
// I2C SCL bit: 0
// Bit Rate: 100 kHz
// Note: I2C settings are specified in the
// Project|Configure|C Compiler|Libraries|I2C menu.
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 1Hz
rtc_init(0,0,0);
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 0
// RD - PORTB Bit 1
// EN - PORTB Bit 2
// D4 - PORTB Bit 4
// D5 - PORTB Bit 5
// D6 - PORTB Bit 6
// D7 - PORTB Bit 7
// Characters/line: 16
lcd_init(16);
lcd_clear();
rtc_set_time(23,59,57);
rtc_set_date(7,3,5,13);
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
rtc_get_time(&h,&m,&s);
rtc_get_date(&day,&date,&month,&year);
lcd_gotoxy(0,0);
putnum(h);
lcd_putsf(":");
putnum(m);
lcd_putsf(":");
putnum(s);
lcd_putsf(" Thu ");
putnum(day);
lcd_gotoxy(0,1);
putnum(date);
lcd_putsf("-");
putnum(month);
lcd_putsf("-");
lcd_putsf("20");
putnum(year);
lcd_putsf(" NT:");
putnum1(adc_data[0]);
delay_ms(100);
}
}
Comment