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

📄 dianzibiao.c

📁 avr控制的电子钟
💻 C
字号:
#include <stdint.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <compat/deprecated.h>
#define uchar unsigned char
#define uint unsigned int
#define PRINTF(format, ...) printf_P(PSTR(format), ## __VA_ARGS__)
// External crystal frequency
#define F_RTC           32768

// Define delay
#define PERIOD_MS       1000

#define USART1_BAUD         25000ul
#define USART1_UBBR_VALUE   ((F_CPU/(USART1_BAUD<<4))-1)
#define USART0_BAUD         19200ul
#define USART0_UBBR_VALUE   ((F_CPU/(USART0_BAUD<<4))-1)
uchar inbuf[4],ch;
uint count=0,i,date1,date2;
uchar fcount=0,fish_count=0,dir=1; 
uchar tempchar;
uchar nkeynum,menuf=0;
uchar f1=0, f2=1,f3,f4,n,k=16;
uchar t1,t2,t3;
char sec=0,hour=0,min=0;
uint8_t counts=0,maohao_f=0,time_f=1,settime_f=0,soundzhen_f=0;
#define LCD_cs1_0 cbi(PORTA,1)
#define LCD_cs1_1 sbi(PORTA,1) 
#define LCD_rst_0 cbi(PORTA,2)      
#define LCD_rst_1 sbi(PORTA,2) 
#define LCD_a0_0 cbi(PORTA,3)       
#define LCD_a0_1 sbi(PORTA,3) 
#define LCD_sclk_0 cbi(PORTA,4)       
#define LCD_sclk_1 sbi(PORTA,4)
#define LCD_di_0 cbi(PORTA,5)       
#define LCD_di_1 sbi(PORTA,5) 
#define zhendong_0 cbi(PORTA,6)       
#define zhendong_1 sbi(PORTA,6) 
#define sound_0 cbi(PORTA,7)       
#define sound_1 sbi(PORTA,7) 
#define nop() asm("nop")

unsigned char maohao_off[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
unsigned char maohao[]=
{
0x00,0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,0x00,
};

unsigned char  number[10][16]=
{
{//0
0x00,0xE0,0xF0,0x18,0x08,0x08,0x18,0xF0,0x00,0x0F,0x1F,0x30,0x20,0x20,0x30,0x1F
},
{//1
0x00,0x20,0x30,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00
},
{//2
0x00,0x60,0x70,0x18,0x08,0x18,0xF0,0xE0,0x00,0x30,0x38,0x2C,0x26,0x23,0x21,0x20

},
{//3
0x00,0x30,0x38,0x08,0x08,0x08,0xF8,0xF0,0x00,0x18,0x38,0x20,0x21,0x21,0x3F,0x1E
},
{//4
0x00,0x00,0x00,0xC0,0xE0,0x30,0xF8,0xF8,0x00,0x06,0x07,0x05,0x04,0x04,0x3F,0x3F
},
{//5
0x00,0xF8,0xF8,0x48,0x48,0x48,0xC8,0x88,0x00,0x18,0x38,0x20,0x20,0x20,0x3F,0x1F

},
{//6
0x00,0xE0,0xF0,0x98,0x88,0x88,0xB8,0x30,0x00,0x0F,0x1F,0x31,0x20,0x20,0x3F,0x1F
},
//7
{0x00,0x08,0x08,0x08,0x08,0xC8,0xF8,0x38,0x00,0x00,0x00,0x30,0x3E,0x0F,0x01,0x00},
//8
{0x00,0x60,0xF0,0x98,0x08,0x08,0x98,0xF0,0x00,0x0C,0x1E,0x33,0x21,0x21,0x33,0x1E},
//9
{0x00,0xF0,0xF8,0x08,0x08,0x18,0xF0,0xE0,0x00,0x19,0x3B,0x22,0x22,0x33,0x1F,0x0F}
};

SIGNAL(SIG_OVERFLOW0) //Timer/Counter1 Overflow 
{    TCNT0 = 0X00;
 counts++;
   if(counts==27)
    { time_f=1;
	  sec++;
     if(sec>59)
		{
			sec=0;
			min++;
		}
	if(min>59)
		{
			hour++;
			min=0;
		}	
	if(hour>23)
			hour=0;
       counts=0;
	}
  
} 

void DLYnumberatxx(uchar tpage,uchar add,uchar content[16])//tpage:0-3;add:0-3
{
   uchar i;
   uchar column,page;
   column=add*8;
   page=tpage *2;
   LCMlcpinit(page,column);
		for(i=0;i<16;i++)
					{
						wdata(content[i]);
							if(i==7)
						 LCMlcpinit((page+1),column);
					}
}

void LCMlcpinit(uchar tpage,uchar tcolumn)        
{
   uchar temp,i;
    i=0;
   w_com(0xb0+tpage);                                //配置页
   w_com(0x40);                                      //配置页里的行
   temp=((tcolumn+i)>>4)|0x10;
   w_com(temp);                                       //配置列
   //temp=tcolumn;
   w_com((tcolumn+i)&0x0f);//config column
}
void w_com(unsigned char x) 
 {  w_coms(x); }  
void wdata(unsigned char dat) 

{ wdatas(dat);} 

void w_coms(unsigned char x)                     //写命令
{ 
   LCD_a0_0; 
   LCD_cs1_0; 
   data_send(x); 
} 
void wdatas(unsigned char dat)                   //写数据
{ 
   LCD_a0_1; 
   LCD_cs1_0; 
   data_send(dat); 
} 

void data_send(unsigned char dat)                  //串行模式发送数据
 {  
   unsigned char s,temp; 
   uchar i; 
   LCD_sclk_0; //sclk=0
   s=dat; 
   for(i=8;i>0;i--) 
	   {
	   LCD_sclk_0;
	   nop(); 
	   nop(); 
	   temp=s & 0x80; 
	   if(temp) 
		   {
		   LCD_di_1;
		   } 
	   else 
			{
			LCD_di_0;
			} 
		   LCD_sclk_1;
	   s=s<<1;   
	   } 
 }  




void dispclk()                    //显示时钟
{
   unsigned char i,j,k,l,m;
   j=min/10;
   k=min%10;
   i=sec/10;
   l=sec%10;
   time_f=0;
   if(maohao_f==0)
   { DLYnumberatxx(0,7,maohao_off);   //秒针闪烁
	maohao_f=1;}
	else
	{DLYnumberatxx(0,7,maohao);
	maohao_f=0;}
   DLYnumberatxx(0,5,number[j]); 
   DLYnumberatxx(0,6,number[k]); 
   DLYnumberatxx(0,8,number[i]); 
   DLYnumberatxx(0,9,number[l]);
   
}
void delay(uint us)
{
    uint i,j;
	for(i=0;i<us;i++)
	   {
	   for(j=0;j<1141;j++);
       }
}
void lcd_init()                           
{      
        DDRA = 0xff; 
        PORTA = 0x3f;
        LCD_rst_0; 
        nop(); 
        nop(); 
        LCD_rst_1;
        w_com(0xaf);        //ON DISPLAY 
        w_com(0x40);        //STAR DISPLAY 
        w_com(0xa0);        //ADC NORMAL       若要反向显示,设为0xa1,0xc0
        w_com(0xa6);        // 
        w_com(0xa4);        //CLEAR  
        w_com(0xa2);       //1/9BIAS 
        w_com(0xc8);       //COMMON OUTPUT DIRECTION 
        w_com(0x2f);        //POWER CONTROL 
        w_com(0x24);      //RESISTER RATIO
        w_com(0x81);       //VOLUM MODE SET 
        w_com(0x24);       //RESISTER RATIO 
        
}
void TMR_vInit(void)
{
    /* Start timer 0 with clock prescaler CLK/1024 and CTC Mode ("Clear Timer on Compare")*/
    /* Resolution is 32.25 ms */
    TCCR0 = (0<<FOC0)|(0<<WGM00)|(0<<COM01)|(0<<COM00)|(1<<WGM01)|(1<<CS02)|(1<<CS01)|(1<<CS00);

    // Reset time
    TCNT0 = 0X00;

    // Calculate and set period
    //OCR0  = (uint16_t)(((F_RTC/1024)*PERIOD_MS)/1000) - 1;

    // Enable interrupt on compare match
     TIMSK=(1<<TOIE0);
	sei(); //总中断标志置位 

    // Select asynchronous timer 0 operation to use external 32.768 kHz crystal
   // ASSR |= (1<<AS0);
}
void FillLCD(uchar content)                       //屏幕清空
{
    uchar i,j;
    for(j=0;j<8;j++)
	{
	///wcomm(0x40+j*8);
	w_com(0xb0+j);
	w_com(0x10);
	w_com(0x00);
	for(i=0;i<132;i++)
	wdata(content);
	}
}
void main()
{
	lcd_init();
	FillLCD(0x00);
 	TMR_vInit();
	while(1)
	{

	 
	dispclk();
	//delay(100);

	}
}

⌨️ 快捷键说明

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