nguyenkhang.khang2019@gma
Newbie
Hi guys . So right now am doing a project is: DS1307 RTC AVR atmega16 - Digital Clock (Use CodeVisionAVR)
But i have somes errors which is :
L490:Must declare first in block .
Can any one help me to undersatand where is wrong or what changes i should do?
Best regards
But i have somes errors which is :
L490:Must declare first in block .
Can any one help me to undersatand where is wrong or what changes i should do?
Best regards
C:
/*******************************************************
This program was created by the
CodeWizardAVR V3.12 Advanced
Automatic Program Generator
© Copyright 1998-2014 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 18/05/2022
Author :
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>
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=1
.equ __scl_bit=0
#endasm
#include <i2c.h>
#include <ds1307.h>
#include <alcd.h>
#define MODE PINB.0
#define UP PINB.1
#define DOWN PINB.2
#define OK PINB.3
bit AP;
bit sb;
bit alarm_en;
unsigned int h_set=0,m_set=0;
unsigned char hour,min,sec,day,date,month,year,mode,h,No_date;
void display_day(unsigned char x) {
switch (x) {
case 1 : lcd_putsf(" 2");
break;
case 2 : lcd_putsf(" 3");
break;
case 3 : lcd_putsf(" 4");
break;
case 4 : lcd_putsf(" 5");
break;
case 5 : lcd_putsf(" 6");
break;
case 6 : lcd_putsf(" 7");
break;
case 7 : lcd_putsf(" 8");
break;
default:
};
}
void check_conveter_day(void){
if (day ==8) day=1;
if (day ==0) day=7;
rtc_set_date(day, date,month,year);
}
void check_conveter_hour(void){
if (AP==0){
if (hour > 12) h = hour-12;
else h=hour;
}
else h = hour;
}
//Ham time LCD
void display_time_lcd(unsigned char x,unsigned char y){
lcd_gotoxy(x,y);
lcd_putchar (48+h/10);//chuc
lcd_putchar (48+h%10);//donvi
lcd_putsf(":");
lcd_putchar (48+min/10);
lcd_putchar (48+min%10);
lcd_putsf (":");
lcd_putchar (48+sec/10);
lcd_putchar (48+sec%10);
if (alarm_en==1) lcd_putsf("*");
else lcd_putsf(" ") ;
}
void display_date_lcd(unsigned char x,unsigned char y)
{
lcd_gotoxy(x,y);
lcd_putchar (48+date/10);
lcd_putchar (48+date%10);
lcd_putsf("/");
lcd_putchar (48+month/10);
lcd_putchar (48+month%10);
lcd_putsf ("/");
lcd_putchar (48+year/10);
lcd_putchar (48+year%10);
display_day(day);
}
void display_hengio_lcd(unsigned char x, unsigned char y)
{
lcd_gotoxy(x,y);
lcd_putchar (48+h_set/10);
lcd_putchar (48+h_set%10);
lcd_putsf(":");
lcd_putchar (48+m_set/10);
lcd_putchar (48+m_set%10);
if (alarm_en == 1 ) lcd_putsf("*" );
else lcd_putsf(" ");
}
//ham hien thi len LCD
void display_lcd(void){
lcd_gotoxy(0,0);
lcd_putsf("Time: ");
display_time_lcd(6,0);
//hien thi date
lcd_gotoxy(0,1);
lcd_putsf("Date: ");
display_date_lcd(6,1);
}
//kiemtra trang thai phim nhan
void status_button(void){
if (MODE&&UP&&DOWN&&OK&&sb){
sb=0;
}
}
//ham kiem tr so ngay trong thang
void Ndate_in_month(void){
if (month == 2 ){
if (year%4 == 0) No_date =29;
else No_date = 28 ;
}
else if (month ==1 || month ==3 || month ==5 || month ==7 || month ==8 || month ==10
|| month == 12 ) No_date = 31;
else No_date = 30;
}
//ham cai thoi gian bao thuc
void set_alarm_time (void){
status_button();
if ((OK == 0 ) && (sb ==0 )){
AP = ~AP;
sb =1 ;
}
status_button();
if (MODE==0 && sb==0){
if (mode == 9 ) {
mode = 0;
}
else {
mode++;
}
sb =1 ;
}
//mode=0
if (mode == 0){
lcd_gotoxy(0,0);
lcd_putsf("DO AN 1 ");
lcd_gotoxy(0,1);
lcd_putsf("khang_minh");
delay_ms (1000);
mode++;
}
//mode=1
if (mode==1){
display_lcd();
if((alarm_en == 1 )&& (hour == h_set) && (min == m_set)) {
PORTB.4 = 1;
status_button();
if( OK == 0 && sb== 0 ){
alarm_en = 0;
sb =1 ;
}
}
else {
PORTB.4 = 0;
}
}
//mode=2
if (mode == 2){
lcd_gotoxy(0,0);
lcd_putsf("Set Hour: ");
lcd_putsf(" ");
display_time_lcd(5, 1);
lcd_putsf(" ");
status_button();
if (UP == 0 && sb == 0){
if (hour == 23) hour = 0;
else hour++;
rtc_set_time(hour,min,sec);
sb = 1;
}
status_button();
if (DOWN == 0 && sb == 0){
if (hour ==0) hour = 23;
else hour--;
rtc_set_time(hour,min,sec);
sb = 1;}
}
//mode=3
if (mode == 3) {
lcd_gotoxy(0,0);
lcd_putsf("Set Minute : ");
lcd_putsf(" ");
display_time_lcd(5, 1);
lcd_putsf(" ");
status_button();
if (UP == 0 && sb == 0){
if (min == 59) min = 0;
else min++;
rtc_set_time(hour,min,sec);
sb = 1;
}
status_button();
if (DOWN == 0 && sb == 0){
if (min ==0) min = 59;
else min--;
rtc_set_time(hour,min,sec);
sb = 1;}
}
//mode4
if (mode == 4) {
lcd_gotoxy(0,0);
lcd_putsf("Set Date: ");
lcd_putsf(" ");
display_date_lcd(6, 1);
lcd_putsf(" ");
Ndate_in_month();
status_button();
if (UP == 0 && sb == 0){
if (day == 7) day = 1;
else day++;
// xac dinh ngay (date) khi chinh thu
if (date == No_date){
if (month == 12 ) {
date =1; month =1 ; year++;
}
else {
date =1 ; month++;
}
}
else date++;
rtc_set_date(day,date,month,year);
sb=1;
}
status_button();
if(DOWN == 0 && sb == 0){
if (day == 1) day = 7 ;
else day--;
if (date ==1){
if (month == 1){
month = 12 ; date = 31 ;year--;
}
else {
month--;
Ndate_in_month();
date = No_date;
}
}
else date--;
rtc_set_date(day, date,month,year);
sb=1;
}
}
//mode 5
if (mode == 5){
lcd_gotoxy(0,0);
lcd_putsf("Set Month :");
lcd_putsf(" ");
display_date_lcd(6, 1);
lcd_putsf(" ");
Ndate_in_month();
status_button();
if (UP == 0 && sb == 0) //phim UP duoc nhan
{
if (month == 12)
{
month =1;
if (year == 99)
year=0;
else year++;
}
else month++;
//xac dinh ngay khi chinh thang
if ( ( day + No_date%7) > 7)
day = day + No_date%7 - 7;
else day = day + No_date%7;
Ndate_in_month(); //cap nhat so ngay toi da cua thang vua dat lai
if (date > No_date)
date = No_date; //vd ngay 31/3 phai chuyen thanh 30/4 thi nhan UP
rtc_set_date(day, date, month, year);
sb = 1;
//PORTB.1 = sb;
}
status_button();
if (DOWN == 0 && sb == 0)
{
if (month == 1)
{
month = 12;
if (year == 0)
year=99;
else year--;
}
else month--;
Ndate_in_month(); //kiem tra, phai tinh theo so ngay cua thang truoc de xac dinh thu
if (day < No_date%7)
day = day - No_date%7 +7;
else day = day - No_date%7;
if(date > No_date)
date=No_date;
//vd tu ngay 31/3 phai chuyen thanh 29/2 khi nhan DOWN
rtc_set_date(day, date, month, year);
sb = 1;
//PORTB.1 = sb;
}
}
//che do mode=6 (chinh nam)
if (mode == 6)
{
lcd_gotoxy(0,0);
lcd_putsf("Set Year: ");
lcd_putsf(" ");
display_date_lcd(6,1);
//lcd_putsf("")
Ndate_in_month();
status_button();
if (UP == 0 && sb == 0)
{
//phim UP duoc nhan
//chinh ngay cua thang 2 khi chuyen tu nam nhau sang nam khong nhuan
if ((year%4==0) && (month=2) && (date==29))
{ //ngay 29/2 nam nhuan
date = 28;
}
//chinh thu khi nhan nut nam
if (year%4 == 0 )
{ //nam nhuan
if ((month == 1) || ((month == 2) && (date<29)))
{ if (day == 7) //ngaychunhat
day = 2; //thu 3
else day = day +2;
}
else day = day +1; //nam nhuan nhung thu chi tang 1 ngay
}
else day = day + 1; //khong phai nam nhuan
//chuyen nam
if(year == 99)
year = 0;
else year++;
rtc_set_date(day, date, month, year);
sb = 1;
//PORTB.1=sb;
}
status_button();
if (DOWN == 0 && sb == 0)
{ //chuyen nam
if (year%4 == 0)
{ //nam nhuan
if ((month > 2) || ((month == 2) && (date == 29)))
{
if (day == 1) //thu 2
day = 6; //thu 8
else day -= 2;
}
else day -= 1; //nam nhuan nhung thu chi lui 1 ngay
}
else day -= 1; //khong phai nam nhuan
//chinh ngay cua thang 2 khi chuyen tu nam nhuan sang nam khong nhuan
if ((year%4 == 0) && (month == 2) && (date == 29))
{ //ngay 29/2 nam nhuan
date=28;
}
//chinh nam
if (year == 0)
year=99;
else year--;
rtc_set_date(day, date, month, year);
sb = 1;
//PORTB.1 = sb;
}
//che do mode=7 chinh gio trong che do hen gio
if (mode == 7)
{
lcd_gotoxy(0,0);
lcd_putsf("Alarm: ");
display_hengio_lcd(8,0);
lcd_gotoxy(0,1);
lcd_putsf("Hour: ");
lcd_gotoxy(8,1);
lcd_putchar(h_set/10 + 48);
lcd_putchar(h_set%10 + 48);
lcd_putsf(" ");
status_button();
if ( UP == 0 && sb == 0)
{
if (h_set == 23)
h_set = 0;
else h_set++;
sb = 1;
//PORTB.1 = sb;
}
if (DOWN == 0 && sb == 0)
{
if (h_set == 0)
h_set = 23;
else h_set--;
sb = 1; //PORTB.1 = sb;
}
//status_button();
if (OK == 0 && sb == 0)
{
alarm_en = ~alarm_en;
//xac nhan cai dat bao thuc alarm en = 1 (0) -> có (không có báo thuc)
//lcd_gotoxy(15,0); //lcd_putsf("*");
sb = 1;
}
}
//che do mode = 8 (chinh phut trong che do hen gio)
if (mode == 8)
{
lcd_gotoxy(0,0);
lcd_putsf ("Alarm:");
display_hengio_lcd (8,0);
lcd_gotoxy(0,1);
lcd_putsf ("Minute:");
lcd_gotoxy(8,1);
lcd_putchar(m_set/10 + 48);
lcd_putchar (m_set%10 + 48);
lcd_putsf(" ");
status_button();
if (UP == 0 && sb == 0)
{
if (m_set == 59)
m_set = 0;
else m_set++;
sb = 1;
//PORTB.1 = sb;
}
//status_button();
if (DOWN == 0 && sb == 0)
{
if (m_set == 0)
m_set = 59;
else m_set--;
sb = 1;
// PORTB.1 = sb;
}
//status_button();
if (OK == 0 && sb == 0)
{
alarm_en = ~alarm_en; //xac nhan cai dat xong bao thuc alarm en = 1 (0) -> có (khong co) bao thuc
sb = 1;
//PORTB.1 = sb;
}
}
//Ham chinh
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 Funcl=In Func0=In
// State7=T State6=T State 55T State 4=T State 3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Funcl=Out Func0=Out
// State7=0 State6=0 State5=0 State 4=0 State 3=0 State2=0 Statel=0 State0=0
PORTB=0xEF; //1111_1111
DDRB=0x10; //1111_0000 void main(void) {
// Port C initialization
// Func7=Out Funcb=In Func5=In Func4=In Func3=In Func2=In Funcl=In Func0=In
// State7=0 State6=T State 55T State 4=T State 3=T State2=T Statel=T StateO=T
PORTC=0xFF;
DDRC=0x80; //1000_0000
// Port D initialization
// Func 7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Funcl=Out Func0=Out
// State7=0 State6=0 State 550 State 450 State 3=0 State2=0 Statel=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OCO output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter i initialization
// Clock source: System Clock
// Clock value: Timeri Stopped
// Mode: Normal top=0xFFFF
// OCA output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timeri 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
// INTO: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer (s)/Counter (s) Interrupt (s) initialization
TIMSK=0x00;
// 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;
// I2C Bus initialization
i2c_init();
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: Off
// SQW/OUT pin state: 0
rtc_init(0,0,0);
// Alphanumeric LCD initialization
// Connections specified in the
// Project Configure 1C Compiler Libraries | Alphanumeric LCD menu:
// RS - PORTD Bit o
// RD - PORTD Bit 1
// EN - PORTD Bit 2
// D4 - PORTD Bit 4
// D5 - PORTD Bit 5
// D6 - PORTD Bit 6
// D7 - PORTD Bit 7
// Characters/line: 16
lcd_init(16);
//cai dat mac dinh
rtc_set_time(0, 0, 0);
rtc_set_date(1, 16, 5, 16);
AP = 1; //mac dinh de che do la 24h
sb = 0; //mac dinh khong co nut bam
//alarm en = 0;
//PORTB.4 = 0;
while (1)
{
// Place your code here
rtc_get_time (&hour, &min,&sec); // Doc gio, phút, giay tu ds1307
rtc_get_date(&day, &date, &month, &year); // Doc thu, ngay, thang, nam tu ds 1307
check_conveter_day(); //kiem tra và kiem chuyen thu, che do gio ve dang chuan
check_conveter_hour();
status_button();
set_alarm_time(); // goi ctc thuc hien cai dat
//PORTB.0 = 0;
}
}
Last edited: