⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.i

📁 使用AVR单片机实现的万年历功能,其中使用了ds1302和1602
💻 I
📖 第 1 页 / 共 5 页
字号:
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.6 Professional
Automatic Program Generator
?Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mail:office@hpinfotech.com

Project : 
Version : 
Date    : 2008-6-28
Author  : F4CG                            
Company : F4CG                            
Comments: 


Chip type           : ATmega16
Program type        : Application
Clock frequency     : 4.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

// CodeVisionAVR C Compiler
// (C) 1998-2001 Pavel Haiduc, HP InfoTech S.R.L.

// I/O registers definitions for the ATmega16


#pragma used+
sfrb TWBR=0;
sfrb TWSR=1;
sfrb TWAR=2;
sfrb TWDR=3;
sfrb ADCL=4;
sfrb ADCH=5;
sfrw ADCW=4;      // 16 bit access
sfrb ADCSRA=6;
sfrb ADMUX=7;
sfrb ACSR=8;
sfrb UBRRL=9;
sfrb UCSRB=0xa;
sfrb UCSRA=0xb;
sfrb UDR=0xc;
sfrb SPCR=0xd;
sfrb SPSR=0xe;
sfrb SPDR=0xf;
sfrb PIND=0x10;
sfrb DDRD=0x11;
sfrb PORTD=0x12;
sfrb PINC=0x13;
sfrb DDRC=0x14;
sfrb PORTC=0x15;
sfrb PINB=0x16;
sfrb DDRB=0x17;
sfrb PORTB=0x18;
sfrb PINA=0x19;
sfrb DDRA=0x1a;
sfrb PORTA=0x1b;
sfrb EECR=0x1c;
sfrb EEDR=0x1d;
sfrb EEARL=0x1e;
sfrb EEARH=0x1f;
sfrw EEAR=0x1e;   // 16 bit access
sfrb UBRRH=0x20;
sfrb UCSRC=0X20;
sfrb WDTCR=0x21;
sfrb ASSR=0x22;
sfrb OCR2=0x23;
sfrb TCNT2=0x24;
sfrb TCCR2=0x25;
sfrb ICR1L=0x26;
sfrb ICR1H=0x27;
sfrb OCR1BL=0x28;
sfrb OCR1BH=0x29;
sfrw OCR1B=0x28;  // 16 bit access
sfrb OCR1AL=0x2a;
sfrb OCR1AH=0x2b;
sfrw OCR1A=0x2a;  // 16 bit access
sfrb TCNT1L=0x2c;
sfrb TCNT1H=0x2d;
sfrw TCNT1=0x2c;  // 16 bit access
sfrb TCCR1B=0x2e;
sfrb TCCR1A=0x2f;
sfrb SFIOR=0x30;
sfrb OSCCAL=0x31;
sfrb OCDR=0x31;
sfrb TCNT0=0x32;
sfrb TCCR0=0x33;
sfrb MCUCSR=0x34;
sfrb MCUCR=0x35;
sfrb TWCR=0x36;
sfrb SPMCR=0x37;
sfrb TIFR=0x38;
sfrb TIMSK=0x39;
sfrb GIFR=0x3a;
sfrb GICR=0x3b;
sfrb OCR0=0X3c;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-

// Interrupt vectors definitions


// Needed by the power management functions (sleep.h)
#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x40
	.EQU __sm_mask=0xB0
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0xA0
	.EQU __sm_ext_standby=0xB0
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif
#endasm


/***********************************************
PORTD.2为DS1302使能控制引脚
PORTD.3为DS1302时钟控制引脚
PORTD.3为DS1302数据控制引脚
***********************************************/

void DS1302WriteOneWord(unsigned char addr, unsigned char data);
unsigned char DS1302ReadOneWord(unsigned char addr);
void DS1302WriteTimeData(void);
void DS1302ReadTimeData(void);
void DS1302WriteRamData(void);
void DS1302ReadRamData(void);
void DS1302CheckDataOve(void);


//定义键盘扫描输入引脚和输出引脚
//定义键值
//定义按键状态

unsigned char keyMask;       //屏蔽码
unsigned char keyValue;      //输入的键值
unsigned char keyState;      //按键状态

void KeyInit(void);
unsigned char FindKeyValue (void);


/******************************************************
   PORTA 为液晶的数据输入输出口
******************************************************/


/**************************************************** 
   PORTB.0 为液晶使能引脚
   PORTB.1 为液晶读写引脚
   PORTB.2 为液晶数据指令选择引脚 
*****************************************************/


void LcdWriteData(unsigned char data);
void LcdWriteInstruction(unsigned char data);
unsigned char LcdReadInstruction(void);
unsigned char LcdReadData(void);

void LcdCheckBusy(void);
void LcdClear(void);
void LcdInit(void);
void LcdSetXY(unsigned char x, unsigned char y);
void LcdPutOneChar(unsigned char c);
void LcdPutFlashString(unsigned char flash *flashStr);
void LcdPutRamString(unsigned char *str);

/***********************************************************
存放音乐数据的数组的元素个数
***********************************************************/
/************************************************************
数组dataofOCR1A用于存放各个音符对应的OCR1A的值
最后一个为空音符标志
************************************************************/
flash unsigned int dataOfOCR1A[37] = 
{
	954, 903, 853, 804, 760, 716, 676, 638, 602, 568, 536, 506, 
	478, 452, 426, 402, 379, 358, 338, 319, 301, 284, 268, 253, 
	239, 226, 213, 201, 190, 179, 169, 160, 150, 142, 134, 127, 2500
};
/************************************************************
数组用于存放每个音符100ms需要比较匹配的次数
最后一个为空音符数据
************************************************************/
flash unsigned int counter[37] = 
{
	52, 55, 59, 62, 66, 70, 74, 78, 83, 88, 93, 99,
	105, 111, 117, 124, 132, 139, 148, 157, 166, 176, 186, 197,
	209, 221, 234, 248, 263, 279, 295, 313, 332, 351, 372, 394, 20
};
/***********************************************************
用于存放音乐数据的数组
***********************************************************/

flash unsigned char music1[274] = {
16, 2, 17, 2, 19, 4, 19, 4, 19, 4, 12, 4, 14, 2, 12, 4, 16, 8, 14, 2,
12, 4, 12, 4,  9, 4, 12, 2,  9, 4,  7, 6, 36, 2, 16, 2, 17, 2,
19, 4, 19, 4, 19, 4, 12, 4, 14, 2, 12, 4, 16, 8, 14, 2, 
12, 4, 12, 1,  9, 1,  9, 1, 12, 4, 14,14, 16, 2, 14, 2,
12, 4, 12, 2, 14, 2, 12, 2,  9, 2,  9, 2,  7, 2,
 7, 1, 16, 4, 16, 5, 36, 2, 16, 2, 16, 2, 14, 2, 12, 2,  9, 2, 12, 2, 21, 4, 19, 18,
36, 16,
19, 4, 19, 2, 16, 2, 16, 4, 19, 4, 21, 2, 16, 2, 16, 2, 19, 10,
12, 4, 12, 2, 14, 2, 14, 2, 16, 4, 19,18, 21, 6, 21, 2, 19, 2, 16, 2, 16, 4,
19, 2, 21, 4, 12, 8, 16, 2, 14, 2, 16, 2, 14, 2, 12, 2, 12, 4,  9, 2, 12, 4, 14, 14,
19, 4, 19, 2, 16, 2, 16, 4, 19, 4, 21, 2, 16, 2, 16, 2, 19, 10,
12, 4, 24, 2, 23, 2, 21, 4, 16, 4, 19,20, 21, 6, 21, 2, 19, 2, 16, 2, 16, 4,
19, 2, 21, 4, 12, 8, 16, 2, 
14, 2, 16, 2, 14, 2, 12, 2, 12, 4,  9, 2, 14, 2, 12, 2, 12, 14,
21, 6, 21, 2, 19, 2, 16, 2, 16, 4, 19, 2, 21, 2, 16, 8, 16, 2,
14, 2, 16, 2, 19, 2, 21, 2, 16, 2, 12, 4,  9, 4, 12, 12
};
flash unsigned char music2[224] = {
 7, 8, 12, 4, 14, 4, 16, 6, 17, 2, 16, 4, 12, 4, 14, 8, 12, 4,  9, 4, 12, 16,
 7, 8, 12, 4, 14, 4, 16, 4, 16, 2, 17, 2, 19, 4, 12, 4, 17, 6, 16, 2, 19,  4, 14, 2, 16, 2, 16, 2, 14, 14,
16, 8, 19, 4, 23, 4, 23, 6, 21, 2, 21, 8, 19, 4, 19, 2, 21, 2, 23, 4, 21,  2, 19, 2, 16, 16,
17, 6, 17, 2, 19, 4, 21, 4, 19, 4, 17, 2, 16, 2, 14, 8, 11, 4, 11, 2,  9,  2,  7, 4,  9, 4, 12, 16,
24, 8, 21, 8, 17, 6, 19, 2, 21, 8, 23, 4, 23, 2, 23, 2, 23, 4, 21, 2, 19,  2, 16, 16,
24, 8, 21, 8, 17, 6, 19, 2, 21, 8, 21, 4, 21, 2, 21, 2, 21, 4, 17, 2, 16,  2, 14, 16,
 7, 8, 12, 4, 14, 4, 16, 6, 17, 2, 16, 4, 12, 2, 12, 2, 14, 6, 14, 2, 14,  4, 14, 2, 12, 2, 9, 4, 9, 12,
11, 8, 11, 6,  9, 2,  7, 4, 21, 4, 19, 4, 14, 2, 14, 2, 17, 6, 17, 2, 17,  4, 16, 2, 14, 2, 19, 16, 
21, 16, 24, 11, 36,1, 24, 4, 36, 1, 24, 36
};

flash unsigned char music3[138] = {
16, 2, 14, 2, 16, 2, 19, 2, 21, 2, 19, 2, 24, 2, 21, 2, 19, 2, 16, 2, 19, 8, 21, 4,
24, 2, 26, 2, 28, 4, 26, 2, 24, 2, 21, 2, 24, 2, 19, 16,
19, 2, 16, 2, 19, 8, 21, 4, 24, 4, 26, 2, 28, 2, 24, 2, 21, 2, 19, 4,
19, 4, 14, 4, 16, 2, 19, 2, 16, 2, 14, 2, 12, 2,  9, 2, 12, 8,
16, 2, 14, 2, 12, 4, 14, 6, 16, 2, 19, 4, 21, 2, 24, 4, 21, 4, 19, 4, 19, 2, 16, 2, 14, 4,
16, 2, 19, 2, 16, 2, 14, 2, 12, 2, 14, 2,  9, 8, 12, 4, 14, 6, 16, 2, 12, 2, 14, 2,
12, 2,  9, 2, 12, 2,  9, 2,  7, 8
};
	
flash unsigned char music4[124] = {
19, 4, 16, 2, 19, 2, 24, 8, 21, 4, 24, 4, 19, 8,
19, 4, 12, 2, 14, 2, 16, 4, 14, 2, 12, 2, 14, 8, 36, 8,
19, 4, 16, 2, 19, 2, 24, 6, 23, 2, 21, 4, 24, 4, 19, 8,
19, 4, 14, 2, 16, 2, 17, 6, 11, 2, 12, 8, 36, 8,
21, 4, 24, 4, 24, 8, 23, 4, 21, 2, 23, 2, 24, 8,
21, 2, 23, 2, 24, 2, 21, 2, 21, 2, 19, 2, 16, 2, 12, 2, 14, 8, 36, 8,
19, 4, 16, 2, 19, 2, 24, 6, 23, 2, 21, 4, 24, 4, 19, 8,
19, 4, 14, 2, 16, 2, 17, 6, 11, 2, 12, 8, 36, 8
};

flash unsigned char music5[62] = 
{
4, 8, 7, 6, 9, 2, 12, 6, 14, 2, 9, 2, 12, 2, 7, 4,
19, 6, 24, 2, 21, 3, 19, 1, 16, 2, 19, 2, 14, 8,

14, 8, 14, 6, 16, 2, 11, 4, 9, 4, 7, 6, 9, 2, 12, 4, 14, 4,
4, 4, 12, 4, 9, 3, 7, 1, 9, 2, 12, 2, 7, 16
};
flash unsigned char music6[196] = {
24, 4, 23, 4, 19, 4, 21, 12, 36, 4, 16, 2, 14, 2, 12, 4, 14, 4, 19, 4, 16, 20,
16, 8, 19, 4, 16, 2, 14, 14, 19, 2, 21, 2, 19, 4, 21, 4, 23, 2, 19, 4, 16, 18,
24, 4, 23, 4, 19, 4, 21, 12, 36, 4, 16, 2, 14, 2, 12, 4, 14, 4, 19, 4, 16, 16,
12, 2, 14, 2, 16, 4, 16, 2, 19, 6, 16, 4, 14, 12, 16, 6, 19, 4, 21, 24, 36, 12,
21, 4, 21, 4, 24, 4, 26, 4, 28, 4, 28, 4, 24, 4, 36, 4, 21, 4, 21, 4,  23, 4,
21, 4, 23, 2, 21, 2, 19, 2, 16, 6, 36, 4,
21, 4, 21, 4, 24, 4, 26, 4, 28, 4, 28, 4, 24, 4, 36, 4, 21, 4, 21, 4,  23, 4,
21, 4, 23, 2, 21, 2, 19, 2, 16, 6,
36, 4, 26, 2, 26, 2, 26, 4, 26, 4, 23, 4, 21, 4, 19, 2, 16, 6,
36, 4, 26, 2, 26, 2, 26, 4, 26, 4, 23, 4, 21, 4, 19, 2, 16, 6,
21, 4, 36, 2, 21, 26
};  

flash unsigned char music7[192] = {
 7, 2, 12, 6, 16, 2, 19, 6, 12, 2, 11, 6, 16, 2, 19, 4, 36, 2,
19, 2, 21, 2, 23, 4, 24, 6, 21, 2, 21, 2, 19, 14,
16, 2, 14, 2, 12, 6, 12, 2, 12, 4, 16, 2, 14,  2, 12, 6, 12, 2, 12, 4,
14, 2, 16, 2, 14, 6,  9, 2, 11, 4, 12, 2, 14,  2, 12, 12,
16, 2, 19, 2, 16, 6, 14, 2, 12, 4, 19, 4, 11, 12,
 9, 2, 11, 2,  9, 6, 11, 2,  9, 6,  7, 4, 16, 12,
19, 4, 16, 6, 14, 2, 12, 4, 19, 4, 11, 12,
 9, 2, 11, 2, 12, 6, 12, 2, 12, 4, 14,  2, 16, 2, 14, 12, 36, 2,
 7, 2, 12, 6, 16, 2, 19, 6, 12, 2, 11, 6, 16, 2, 19, 6, 
19, 2, 21, 6, 23, 2, 24, 6, 21, 2, 21, 2, 19, 10,
16, 2, 14, 2, 12, 6, 12, 2, 12, 4, 16, 2, 14,  2, 12, 6, 12, 2, 12, 4,
14, 2, 16, 2, 14, 6,  9, 2, 11, 4, 12, 2, 14,  2, 12, 12
};

flash unsigned char music8[250] = {
 9, 2, 12, 4,  9, 2, 16, 6, 14, 2, 14, 2, 16, 1, 14, 1, 12, 12,
11, 2,  9, 4, 11, 2, 14, 6, 16, 2,  9, 16,
16, 8, 21, 6, 16, 2, 21, 4, 19, 2, 17,  2, 16, 8,
12, 6, 14, 2, 16, 2, 14, 1, 16, 1, 17, 2, 16, 2, 14, 16,
 9, 4, 16, 4, 16, 4,  9, 4, 12, 12, 16, 4, 
14, 2, 11, 4, 16, 2, 14, 2,  9, 2, 12, 2, 14, 2, 16, 16,
16, 8, 21, 6, 16, 2, 21, 4, 19, 2, 17,  2, 16, 8,
19, 2, 14, 4, 17, 2, 16, 2, 14, 2, 12, 4, 14, 16,
14, 2, 11, 4, 16, 2, 11, 2,  9, 2,  7, 4,  9, 12,
16, 4, 19, 12, 16, 2, 19, 2, 21, 6, 24, 2, 23, 2, 21, 2, 19, 4, 21, 16,
24, 8, 23, 6, 21, 2, 19, 2, 21, 2, 19, 8, 19, 2, 21, 2, 16, 16,
24, 8, 23, 6, 21, 2, 19, 2, 21, 2, 19, 8, 19, 2, 21, 2, 16, 16,
 7, 2,  9, 4, 12, 2, 16, 6, 12, 2, 14, 2, 16, 2, 14, 12,
14, 2, 11, 4, 14, 2, 11, 2,  9, 2,  7, 4,  9, 16,
 7, 2,  9, 4, 12, 2, 16, 6, 12, 2, 14, 2, 16, 2, 14, 12,
 16, 4, 19, 8, 16, 4, 23, 6, 24, 2, 23, 2, 21, 2, 19, 4, 21, 32
};
flash unsigned char music9[302] = {
16, 4, 16, 4, 16, 4, 19, 4, 14, 8, 14, 4, 19, 4,
12, 4, 12, 4, 12, 4, 16, 4,  9, 8, 36, 8,
17, 4, 16, 4, 17, 4, 21, 4, 16, 8, 16, 4, 19, 4,
14, 8, 12, 4,  9, 4, 12, 4, 14, 12,
16, 4, 14, 4, 16, 4, 19, 4, 19, 4, 14, 4, 36, 4, 16, 4,
24, 8, 24, 4, 19, 4, 19, 4, 21, 12,
17, 4, 16, 4, 17, 4, 21, 4,  9, 4, 12, 2, 16, 10,
14, 4, 12, 12, 36, 8, 16, 4, 19, 2, 24, 2,
24, 4, 23, 4, 21, 4, 19, 4, 19, 8, 16, 4, 17, 2, 19, 2,
21, 4, 17, 4, 17, 4, 16, 4, 14, 8, 14, 4, 12, 2, 14, 4,
16, 4, 19, 2, 19, 2, 36, 4, 16, 4, 21, 4, 16, 4, 14, 4, 12, 4,
14, 8, 36, 4,  9, 2, 12, 2, 16, 4, 14, 4, 14, 4, 16, 4,
19,24, 24, 2, 23, 2, 21, 2, 23, 2,
21, 4, 19, 4, 19, 4, 12, 4, 16, 4, 19, 4, 19, 8,
21, 4, 21, 2, 21, 2, 17, 4, 16, 4, 14, 8, 16, 4, 16, 2, 24, 2,
19, 8, 16, 4, 16, 2, 19, 2, 12, 8, 36, 4, 16, 2, 16, 2, 
14, 4, 14, 2, 14, 2, 21, 4, 16, 4, 19, 8, 24, 2, 23, 2, 21, 2, 23, 2,
23, 4, 19, 4, 19, 4, 12, 4, 16, 4, 19, 4, 19, 8,
21, 4, 21, 2, 21, 2, 21, 4, 24, 4, 23, 8, 21, 4, 23, 4, 
24, 4, 19, 4, 19, 4, 16, 4, 19, 4, 21, 8, 19, 4,
19, 4, 17, 4,  9, 4, 11, 4, 11, 2, 12, 38
};
flash unsigned char music10[4] = {36, 10, 17, 10};
/***********************************************************
***********************************************************/
flash unsigned char *pMusic;                    //指向一首音乐数据首地址的指针
unsigned int maxMusic;                         //被指向音乐数据的个数
unsigned int iCompaCycle;                      //比较匹配的次数
unsigned int iMusicCycle;                      //循环变量,用于取音乐数据
unsigned char playCycle;                        //音乐播放次数

void MusicInit(void);   //音乐播放初始化函数

void PlayMusic(unsigned char musicChoose, unsigned char counterChoose);

void PlayMusic1(unsigned char counter1);
void PlayMusic2(unsigned char counter2);
void PlayMusic3(unsigned char counter3);
void PlayMusic4(unsigned char counter4);
void PlayMusic5(unsigned char counter5);
void PlayMusic6(unsigned char counter6);
void PlayMusic7(unsigned char counter7);
void PlayMusic8(unsigned char counter8);
void PlayMusic9(unsigned char counter9);
void PlayMusic10(unsigned char counter10);

/************************************************************
播放音乐使能TIME1之前必须完成以下工作
初始化*pMusic指针,使其指向某一首音乐
初始化maxMusic,使其等于*pMusic指针指向的音乐的数据的个数
初始化音乐播放的次数playCycle
初始化OCR1A,使其对应于第一个音符
初始化第一个音符比较匹配的次数iCompaCycle
初时话iMusicCycle为1
************************************************************/
/*****************************************************
数组DS1302Data[12]为万年历时钟日期数据存储变量
依次为农历年高位,农历年低位,农历月份,农历日期,国历年高位,国历年低位,国历月份,国历日期,星期,小时,分钟,秒
noiseData[8]为闹钟数据
*****************************************************/     
unsigned char DS1302Data[12];
unsigned char noiseData[9];
//unsigned char setTimeData[8];
/*****************************************************

数组findLunar[200]为国历转农历的数据
农历月份数据,每年4字节,从2001年开始,共50年 
数据解析: 
如果第一字节的bit7为1,则该年1月1日位于农历12月,否则位于11月 
第一字节去除bit7为该年1月1日的农历日期 
        第二字节                第三字节 
bit:     7  6  5  4  3  2  1  0  7  6  5  4  3  2  1  0 
农历月份:16 15 14 13 12 11 10 9  8  7  6  5  4  3  2  1 
农历月份指的是从该年1月1日的农历月份算起的顺序号 
农历月份对应的bit为1则该月为30日,否则为29日 
第四字节为闰月月份 
*****************************************************/
flash char findLunar[200] = 
{
		0x87,0x2a,0x56,0x04,
        0x12,0x14,0xae,0x00,
        0x1d,0x29,0x6d,0x00,
        0x8a,0x2a,0xb5,0x02,
        0x15,0x15,0xaa,0x00,
        0x82,0x36,0xaa,0x07,
        0x0d,0x2e,0x93,0x00,
        0x17,0x2d,0x26,0x00,
        0x86,0x35,0x27,0x05,
        0x11,0x2a,0x57,0x00,
        0x1b,0x14,0xb6,0x00,
        0x88,0x15,0x5a,0x04,
        0x14,0x2a,0xb5,0x00,
        0x81,0x2b,0x55,0x09,
        0x0b,0x17,0x4a,0x00,
        0x16,0x36,0x95,0x00,
        0x84,0x3a,0x95,0x06,
        0x0f,0x35,0x2b,0x00,
        0x1a,0x32,0x57,0x00,
        0x87,0x2a,0x5d,0x04,
        0x12,0x15,0x5a,0x00,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -