📄 128riliok.c
字号:
//632896
//Target : atmega128
//crystal:11.0592MHZ
//#define F_CPU 11059200UL
#include <inttypes.h>
#include <string.h>
//#include <avr/signal.h> //2005
#include <avr/interrupt.h> //2006
//#include <avr/delay.h> //2005
#include <util/delay.h> //2006
//#include <avr/wdt.h>
#include <avr/io.h>
#include <avr/sfr_defs.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#define TRUE 1
#define FALSE 0
/******************timerone***********************/
#define EN_TIMERONE_INT (TIMSK = _BV(TOIE1))
#define DIS_TIMERONE (TCCR1B = (TCCR1B & 0xf8))
#define EN_TIMERONE (TCCR1B = (TCCR1B | 0x03))
#define DELAY100H 0xDE //50ms
#define DELAY100L 0xCF
/******************count clock*******************/
//#define ONE_MINUTES 60
#define SECONDS 20
//#define ONE_HOURS 24
/******************Usart init*******************/
#define BAUDRATEH 0X00 //9600
#define BAUDRATEL 0X19
/******************port init*******************/
#define SB_RW PORTG |= (1<<PG0)
#define CB_RW PORTG &= ~(1<<PG0)
#define SB_RS PORTC |= (1<<PC5)
#define CB_RS PORTC &= ~(1<<PC5)
#define SB_E PORTC |= (1<<PC7)
#define CB_E PORTC &= ~(1<<PC7)
uint16_t Count=0;
uint8_t Switch_in;
uint16_t count=0;
uint8_t miao;
uint8_t fen;
uint8_t shi;
uint8_t yue;
uint8_t xq;
uint8_t nian;
uint8_t ri;
uint8_t Key;
uint8_t ret;
uint8_t s1num;
uint8_t exit_set_stutas;
uint8_t Key1;
uint8_t Key2;
uint8_t delay2miao=0;
//uchar table[]=" 2008-08-20 03";//年月日 星期
//uchar table1[]=" 12:00:00"; //时分秒
unsigned char table[]="09-01-10 03";//年月日 星期
unsigned char table1[]="12:58:00"; //时分秒
void DelayMs(unsigned int t) //1ms
{
unsigned int i;
for(i=0;i<t;i++)
{
_delay_loop_2(2765);
}
}
static void Timerone_Init(void)
{
TCCR1A = 0;
TCCR1B = _BV(CS12);//|_BV(CS10);
SFIOR = _BV(PSR321);
TIFR = _BV(TOV1);
//TCNT1H = DELAY100H;
//TCNT1L = DELAY100L; //50ms
TCNT1H=0xAB;//(65536-50000)/256;
TCNT1L=0xA0;//(65536-50000)%256;
TIMSK = _BV(TOIE1);
}
void Port_Init(void)
{
DDRA = 0xFF;
PORTA = 0x00;
DDRB = (1<<DDB3)|(1<<DDB2);
PORTB = 0x00;
DDRC = 0xFF;
PORTC = (1<<PD7)|(1<<PD5);
DDRG = (1<<DDG0);
PORTG = 0x00;
DDRE = 0xFF;
PORTE=0xFF;
//DDRF = (1<<DDF4)|(1<<DDF3)|(1<<DDF2);
PORTF =0xFF;
}
void LCD_Com(uint8_t Command)
{
uint8_t i=0;
CB_RS;
CB_RW;
SB_E;
PORTA = Command;
while(i<6)
{
i++;
}
CB_E;
i=0;
SB_RW;
while(i<6)
{
i++;
}
SB_E;
}
void LCD_Init(void)
{
DelayMs(15);
LCD_Com(0x38); //set 8-bit data.
DelayMs(5);
LCD_Com(0x38); //set 8-bit data.
DelayMs(1);
LCD_Com(0x38); //set 8-bit data.
DelayMs(1);
LCD_Com(0x08); //set 8-bit data,2 hang,5*10 char
DelayMs(1);
//LCD_Com(0x0C); //set display on.
LCD_Com(0x0F);
DelayMs(1);
LCD_Com(0x01); //clear the sreen
DelayMs(2);
LCD_Com(0x06); //set moving.06 ++ 04--
//DelayMs(1);
}
void LCD_Write(unsigned char Data,uint8_t Command_Data) //row is the 80h-87h,90h-97h
{
uint8_t i=0;
if(Command_Data == 0) // write the Command and add.
{
CB_RS; //写指令
}
if(Command_Data == 1) // write the data.
{
DelayMs(1);
SB_RS; //写数据
}
CB_RW;
CB_RW;
SB_E; //0x0C显示开关控制0C写入,80是地址。一次写一个数据先写DATA[0]">" 再写data[1]‘1’再写data[2]‘1’
PORTA = Data; //片选下降沿触发表示显示开关控制0C写入 再写data[8]‘1’,再写data[1]“0“
while(i<6)
{
i++;
} //延时
CB_E;
i=0;
SB_RW; //片选下降沿触发表示显示开关控制0C写入
while(i<6)
{
i++;
}
SB_E; //片选为高等待下一次写入。
}
void LCD_Display(unsigned char Data[8],uint8_t Row)
{
uint8_t i;
LCD_Write(0x0c,0); //显示方式控制
DelayMs(1);
if(Row == 1)
{
// LCD_Write((0x80),0); //第一行第一个
for(i=0;i<=7;i++)
{
LCD_Write((0x80+i),0);
LCD_Write(Data[i],1);
}
}
if(Row == 2)
{
// LCD_Write((0x88),0);
for(i=0;i<=7;i++)
{
LCD_Write((0xA8+i),0);
LCD_Write(Data[i],1);
}
}
i=0;
}
//写时分秒数据子程序
void Write_Sfm(unsigned char add,unsigned char date)
{
uint8_t shi,ge;
shi=date/10;
ge=date%10;
LCD_Write(0xA8+add,0);
//LCD_Write(0xA8,0);
LCD_Write(0x30+shi,1);
LCD_Write(0x30+ge,1);
}
//写年月日星期数据子程序
void Write_Nyr(unsigned char add,unsigned char date)
{
uint8_t shi,ge;
shi=date/10;
ge=date%10;
LCD_Write(0x80+add,0);
LCD_Write(0x30+shi,1);
LCD_Write(0x30+ge,1);
}
void miao_shuju(void)
{
uint8_t miao_shi;
uint8_t miao_ge;
miao_shi=miao/10;
miao_ge=miao%10;
table1[6]=0x30+miao_shi;
table1[7]=0x30+miao_ge;
LCD_Display(table1,2);
DelayMs(200);
table1[6]=0x20+miao_shi;
table1[7]=0x20+miao_ge;
LCD_Display(table1,2);
DelayMs(200);
table1[6]=0x30+miao_shi;
table1[7]=0x30+miao_ge;
LCD_Display(table1,2);
}
void fen_shuju(void)
{
uint8_t fen_shi;
uint8_t fen_ge;
fen_shi=fen/10;
fen_ge=fen%10;
table1[3]=0x30+fen_shi;
table1[4]=0x30+fen_ge;
LCD_Display(table1,2);
}
void shi_shuju(void)
{
uint8_t shi_shi;
uint8_t shi_ge;
shi_shi=shi/10;
shi_ge=shi%10;
table1[0]=0x30+shi_shi;
table1[1]=0x30+shi_ge;
LCD_Display(table1,2);
}
void ri_shuju(void)
{
uint8_t ri_shi;
uint8_t ri_ge;
ri_shi=ri/10;
ri_ge=ri%10;
table[6]=0x30+ri_shi;
table[7]=0x30+ri_ge;
LCD_Display(table,1);
}
void yue_shuju(void)
{
uint8_t yue_shi;
uint8_t yue_ge;
yue_shi=yue/10;
yue_ge=yue%10;
table[3]=0x30+yue_shi;
table[4]=0x30+yue_ge;
LCD_Display(table,1);
}
void nian_shuju(void)
{
uint8_t nian_shi;
uint8_t nian_ge;
nian_shi=nian/10;
nian_ge=nian%10;
table[0]=0x30+nian_shi;
table[1]=0x30+nian_ge;
LCD_Display(table,1);
}
void Key_Scan(void)
{
uint8_t Key1;
uint8_t Key2;
Key1 = PIND;
Key1 = Key1 & 0x1F;
DelayMs(10);
Key2 = PIND;
Key2 = Key2 & 0x1F;
if((Key1==Key2)&&(Key1!=0))
{
//Key_Work_Flag=1;
Key=Key1;
if(Key==0x1D) //TRANS
{
ret=2;
}
if(Key==0x1B) //STO
{
ret=3;
}
if(Key==0x17) //MD
{
ret=4;
}
//等待弹起
/*while(1)
{
Key1 = PIND;
Key1 = Key1 & 0x1F;
if (Key1==0)
{
DelayMs(5);
break;
}
}*/
}
//return Key_Work_Flag;
//return ret;
}
void delay_2time(uint8_t t)
{
uint8_t i;
for(i=t;i<=t;i++)
{
PORTE&=!_BV(PE2);
DelayMs(50);
PORTE|=_BV(PE2);
DelayMs(50);
}
}
void exit_set_status(void)
{
while(delay2miao<=200)
{
DelayMs(20);
Key_Scan();
if(Key2!=0)
{
delay2miao=0;
break;
}
if(Key2==0)
{
if(delay2miao!=200)
{
delay2miao++;
break;
}
else
{
exit_set_stutas=100;
LCD_Write(0x0c,0); //按键4次后 开显示 光标不开不闪烁
TIMSK =_BV(TOIE1);
delay2miao=0; //如果没有这一行就不会退出到主程序那么只能设置第一次,第二次就设置不了。
ret=0;
s1num=0;
break;
}
}
}
}
void Key_tanqi(void)
{
while(1)
{
Key_Scan();
if(Key2==0)
break;
}
}
void Key_Handing(void)
{
if(ret==2)
{
DelayMs(80);
for(exit_set_stutas=0;exit_set_stutas<100;exit_set_stutas++)
{
Key_Scan();
if(ret==2)
{
s1num++;
delay_2time(1);
while(!ret);
if(s1num==1)
{
Key_tanqi();
TIMSK = 0;//_BV(TOIE1);
LCD_Write(0xA8+6,0); //调节秒的数据指针设置在(1,10)坐标处 理解0x80+0x40+10这条指令
LCD_Com(0x0F);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -