TXT

CODE

By Louise Webb,2014-10-31 18:25
21 views 0
CODE

    //====================================================== ////自编的全部程序文件包括!

    ////minilcd.h 12864lcd驱动

    ////minilcd.c "

    ////ds1302.h ds1302驱动

    ////ds1302.c "

    ////miniclock.h 字模数组

    ////miniclock.c 主程序

    //====================================================== //++++++++++++++++++++++++

    //======miniclock.h========

    // 字模表!88点阵,1216点阵数字,1616点阵汉字和小鸡图案

//================以下存放在FLASH

#include <avr/pgmspace.h>

//08数字

    const char num88[][8] PROGMEM={{0x00,0x7C,0xFE,0x82,0x82,0xFE,0x7C,0x00},

    {0x00,0x00,0x84,0xFE,0xFE,0x80,0x00,0x00},//1 {0x00,0xC4,0xC6,0xA2,0x92,0x9E,0xCC,0x00},//2 {0x00,0xC4,0xC6,0x92,0x92,0xFE,0x6c,0x00},//3 {0x00,0x60,0x50,0x48,0x44,0xFE,0xFE,0x40},//4 {0x00,0xDE,0xDE,0x8A,0x8A,0xFA,0x72,0x00},//5 {0x00,0x7C,0xFE,0x92,0x92,0xF6,0x76,0x00},//6 {0x00,0x06,0x06,0xC2,0xE2,0x1E,0x0E,0x00},//7 {0x00,0x6c,0xFE,0x92,0x92,0xFE,0x6c,0x00},//8 {0x00,0x5c,0xDE,0x92,0x92,0xFE,0x7c,0x00},//9 {0x00,0x78,0x24,0x22,0x22,0x24,0x78,0x00},//A {0x00,0x42,0x7E,0x4A,0x4A,0x4A,0x34,0x00},//B {0x00,0x3C,0x42,0x42,0x42,0x42,0x66,0x00},//C {0x00,0x42,0x7E,0x42,0x42,0x42,0x3C,0x00},//D {0x00,0x42,0x7E,0x4A,0x4A,0x42,0x66,0x00},//E {0x00,0x02,0x7E,0x12,0x12,0x12,0x02,0x00},//F {0x00,0x00,0x00,0xCC,0xCC,0x00,0x00,0x00},//- {0x00,0x00,0x30,0x30,0x30,0x30,0x00,0x00},//: {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}; //SPACE

//12*16数字

    const char num1216[][24]

    PROGMEM={{0x00,0xFC,0xFE,0x06,0x06,0x86,0x46,0x26,0x16,0xFE,0xFC,0x00,0x00,0x3F,

    0x7F,0x68,0x64,0x62,0x61,0x60,0x60,0x7F,0x3F,0x00},//0 {0x00,0x00,0x00,0x00,0x08,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

    ,0x00,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00}, //1

    {0x00,0x3C,0x3E,0x06,0x06,0x06,0x06,0x86,0xC6,0x7E,0x3C,0x00,0x00,0x60,0x70,0x78,0x6C,0x66,0x63,0x61,0x60,0x60,0x60,0x00}, //2

    {0x00,0x3C,0x3E,0x06,0x06,0xC6,0xC6,0xC6,0xC6,0xFE,0x3C,0x00,0x00,0x3C,0x7C,0x60,0x60,0x60,0x60,0x60,0x60,0x7F,0x3F,0x00}, //3

    {0x00,0x00,0x80,0xE0,0x78,0x1E,0x06,0xFE,0xFE,0x00,0x00,0x00,0x00,0x1E,0x1F,0x19,0x18,0x18,0x18,0x7F,0x7F,0x18,0x18,0x00}, //4

    {0x00,0xFE,0xFE,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x86,0x00,0x00,0x3C,0x7C,0x60,0x60,0x60,0x60,0x60,0x60,0x7F,0x3F,0x00}, //5

    {0X00,0xFC,0xFE,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xCE,0x8C,0x00,0x00,0x3F,0x7F,0x60,0x60,0x60,0x60,0x60,0x60,0x7F,0x3F,0x00}, //6

    {0x00,0x1E,0x1E,0x06,0x06,0x06,0x06,0x86,0xE6,0x7E,0x1E,0x00,0x00,0x00,0x00,0x00,0x60,0x78,0x1E,0x07,0x01,0x00,0x00,0x00}, //7

    {0x00,0x7C,0xFE,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xFE,0x7C,0x00,0x00,0x3F,0x7F,0x60,0x60,0x60,0x60,0x60,0x60,0x7F,0x3F,0x00}, //8

    {0x00,0xFC,0xFE,0x06,0x06,0x06,0x06,0x06,0x06,0xFE,0xFC,0x00,0x00,0x31,0x73,0x63,0x63,0x63,0x63,0x63,0x63,0x7F,0x3F,0x00}, //9

    {0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00}, //-

    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, //space

    {0x00,0x00,0x00,0x00,0x18,0x3C,0x3C,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}; //.

//小鸡图案

    const unsigned char chik1[32]

    PROGMEM={0x00,0x00,0x00,0x80,0x80,0x80,0x80,0xE0,0x70,0x18,0x18,0x18,0x18,0xF8,0xFC,0xCC,

    0x00,0x4E,0x31,0x20,0x24,0x10,0x19,0x19,0x16,0x90,0x50,0x70,0x78,0x5E,0x1F,0x07};//小鸡1

const unsigned char chik2[32]

    PROGMEM={0x80,0x80,0x40,0x20,0x20,0x20,0xC0,0xE0,0x70,0x18,0x18,0x18,0x18,0xF0,0xE0,0x60,

    0x01,0x03,0x04,0x09,0x08,0x18,0x50,0x59,0x76,0x90,0x50,0x70,0x78,0x5E,0x07,0x00};//小鸡2

//16*16汉字

    const unsigned char HANZ[][32]

    PROGMEM={{0x00,0x00,0x00,0xFE,0xFE,0x82,0x82,0x82,0x82,0x82,0xFE,0xFF,0x02,0x00,0x00,0x00,

    0x00,0x00,0x00,0x7F,0x7F,0x20,0x20,0x20,0x20,0x20,0x7F,0x7F,0x00,0x00,0x00,0x00}, //日!

    {0x00,0xFC,0xFC,0x84,0x84,0xFE,0xFE,0x14,0x90,0x10,0x10,0xFF,0xFF,0x10,0x18,0x00,

    0x00,0x3F,0x1F,0x10,0x10,0x3F,0x3F,0x00,0x00,0x23,0x60,0xFF,0x7F,0x00,0x00,0x00}, //时!

    {0x80,0x40,0x70,0xCF,0xC8,0x48,0x00,0xF8,0xF0,0x10,0xFF,0xFF,0x10,0xF8,0xF0,0x10,

    0x00,0x02,0x22,0x7F,0x3F,0x12,0x08,0x07,0x03,0x02,0xFF,0xFF,0x02,0x07,0x07,0x00}, //钟!

    {0x00,0x20,0x18,0xCF,0xC7,0x44,0x44,0xFC,0xFC,0x44,0x44,0x64,0x46,0x04,0x00,0x00,

    0x04,0x04,0x04,0x07,0x07,0x04,0x04,0xFF,0xFF,0x04,0x04,0x04,0x04,0x06,0x04,0x00}, //

    {0x00,0x00,0x00,0x00,0xFF,0xFE,0x22,0x22,0x22,0x22,0x22,0xFF,0xFE,0x04,0x00,0x00,

    0x00,0x80,0x40,0x70,0x1F,0x07,0x02,0x02,0x02,0x42,0x42,0xFF,0x7F,0x00,0x00,0x00}, //

    {0x00,0xC0,0x60,0x38,0x9E,0x8E,0x80,0x80,0x83,0xCE,0x9C,0xB0,0xE0,0xC0,0xC0,0x80,

    0x01,0x80,0x40,0x60,0x38,0x1F,0x07,0x40,0x80,0x7F,0x3F,0x00,0x00,0x01,0x00,0x00}, //

    {0x00,0x00,0x00,0x80,0xE0,0x00,0xFE,0xFE,0x02,0x20,0x40,0x80,0x00,0x00,0x00,0x00,

    0x00,0x08,0x06,0x03,0x10,0x30,0x7F,0x7F,0x00,0x00,0x00,0x01,0x03,0x0E,0x00,0x00}, //

    {0x00,0x00,0xFE,0xFE,0x42,0x42,0x42,0x42,0xFE,0xFE,0x42,0x42,0xC3,0xE2,0x40,0x00,

    0x40,0x30,0x0F,0x83,0x40,0x30,0x18,0x0E,0x07,0x21,0x40,0xC0,0x7F,0x3F,0x00,0x00}}; //

    //============================================================================== //

    //========== miniLcd.h ============

    //头文件包括引脚操作宏定义以及液晶有关的函数说明,以供主程序调用

    //

    #include <avr/io.h>

    #include <avr/pgmspace.h>

    #include <util/delay.h>

#define CS_H PORTD|=(1<<PD0) //ena PD0高低控制 !片选线

    #define CS_L PORTD&=~(1<<PD0)

#define RST_H PORTD|=(1<<PD1) //rst PO1高低控制!复位线

    #define RST_L PORTD&=~(1<<PD1)

#define AO_H PORTD|=(1<<PD2) //com/data PD2高低控制!命令-数据线

    #define AO_L PORTD&=~(1<<PD2)

    #define SCL_H PORTD|=(1<<PD3) //clock PD3高低控制!时钟线 #define SCL_L PORTD&=~(1<<PD3)

    #define SI_H PORTD|=(1<<PD4) //seriel data PD4高低控制!数据线 #define SI_L PORTD&=~(1<<PD4)

    #define uchar unsigned char #define uint unsigned int

//以下都是这个LCD的有关驱动函数申明

    void Lcd_ini();

    void LcdWriByte(uchar n) ; void LcdWriCommand(uchar comm); void LcdWriData(uchar dat); void LcdSetxy(uchar x,uchar y); void LcdCls(char val);

    void LcdReset(void) ;

    void LcdOff(void);

void LcdShow88(uchar x,uchar y,const char *p);

    void LcdShow1616(uchar x,uchar y,const char *p) ;

    void LcdShow1216(uchar x,uchar y,const char *p) ;

/////////////////////////////////////////////////////////////////////

    /////////////////////////// //====== miniLcd.c=========== #include"miniLcd.h"

void Lcd_ini() //lcd初始化

    {

     LcdWriCommand(0xaf); //显示开

     LcdWriCommand(0x40); //起始行0

     LcdWriCommand(0xa0); //正常方向!左-

     LcdWriCommand(0xa6); //正常显示;白背景

     LcdWriCommand(0xa4); //正常关全屏显示

     LcdWriCommand(0xa2); //1/9偏压

     LcdWriCommand(0xc8); //0行在上,正常由上往下显示

     LcdWriCommand(0x2f); //内部电源设置默认值

     LcdWriCommand(0x24); //分压比!对比度默认值

     LcdWriCommand(0x81); //亮度默认值

     LcdWriCommand(0x20); //VOLUME REGISTER SET默认值

    }

    ////////////////////////////

    void LcdWriByte(uchar nn) //lcd串写1字节从最高位开始 {

     int i;

     SCL_L;

     for(i=8;i>0;i--)

     {

     SCL_L;

     if(nn&0X80)

     SI_H;

     else

     SI_L;

     SCL_H;

     nn<<=1;

     }

}

    ////////////////////////////

    void LcdWriCommand(uchar comm) //lcd写命令 {

     CS_L;

     AO_L;

     LcdWriByte(comm);

     CS_H;

    }

    ////////////////////////////

    void LcdWriData(uchar data) //lcd写数据 {

     CS_L;

     AO_H;

     LcdWriByte(data);

     CS_H;

    }

    //////////////////////////////

void LcdSetxy(uchar x,uchar y) //lcd设置显示坐标x:0-127;y:0-7

    {

     uchar tmp=0;

     y&=0x0f; //y0xb0组合

     y|=0xb0;

     tmp=x; //x组合成新的高4

     tmp>>=4;

     tmp|=0x10;

     x&=0x0f; //x取低4

     LcdWriCommand(y);

     LcdWriCommand(tmp); //x 4

     LcdWriCommand(x); //x 4

    }

    //////////////////////////////

    void LcdCls(char val) //lcd清屏,一般取val=0 {

     uchar i,j;

     for(i=0;i<9;i++)

     {

     LcdSetxy(0,i);

     for(j=0;j<128;j++)

     {

     LcdWriData(val);

     }

     }

    }

    ///////////////////////////////

void LcdReset(void) //lcd软复位

    {

     LcdWriCommand(0xe2);

}

    //////////////////////////////// void LcdOff(void) //lcd

    {

     LcdWriCommand(0xae);

}

    ///////////////////////////////// void LcdShow88(uchar x,uchar y,const char *p) //显示88点阵字符 { uchar i; char mid;

     LcdSetxy(x,y); //设置坐标

     for(i=0; i<8; i++) //写字符

     {

     mid=pgm_read_byte(p+i); //读出flash88字模表

     LcdWriData(mid); //显示

     }

}

    ////////////////////////////////// void LcdShow1616(uchar x,uchar y,const char *p) //显示1616点阵 {

     uchar i,mid;

     LcdSetxy(x,y);

     for(i=0; i<16; i++) //设置坐标

     //写上半个点阵

     {

     mid=pgm_read_byte(p+i);

     LcdWriData(mid);

     }

     LcdSetxy(x,y+1); //设置坐标(页地址加1)

     for(i=16; i<32; i++) //写下半点阵

     {mid=pgm_read_byte(p+i);

     LcdWriData(mid);}

}

    ////////////////////////////////// void LcdShow1216(uchar x,uchar y,const char *p) //显示1216点阵 {

     uchar i; char mid;

     LcdSetxy(x,y); //设置坐标

     for(i=0; i<12; i++) //写上半个点阵

     {

     mid= pgm_read_byte(p+i);

     LcdWriData(mid);

     }

     LcdSetxy(x,y+1); //设置坐标(页地址加1)

     for(i=0; i<12; i++) //写下半个点阵

     {

     mid= pgm_read_byte(p+i+12);

     LcdWriData(mid);

     }

}

    //////////////////////////////////

    //============================== //======= DS1302.h ========== #include <avr/io.h>

    #include <util/delay.h>

    #define rst_L PORTB&=~(1<<PB0) //M8控制 DS1302 的引脚定义

宏!PB0,PB1,PB2

    #define rst_H PORTB|=(1<<PB0)

    #define sclk_L PORTB&=~(1<<PB1) #define sclk_H PORTB|=(1<<PB1) #define data_out_L PORTB&=~(1<<PB2) #define data_out_H PORTB|=(1<<PB2)

    #define BCD2DEC(X) (((X&0x70)>>4)*10 + (X&0x0F)) //用于将BCD码转成十进制的宏 #define DEC2BCD(X) ((X/10)<<4 | (X%10)) //用于将十进制转成BCD码的宏

#define uchar unsigned char

    void writeDS_Data(uchar addr,uchar data); //1302 uchar readDS_Data(uchar addr); //1302 void set_DS_ini(void); //1302初始化

//////////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////////////////

    //================================ //========= DS1302.C 源文件

#include "DS1302.h"

    void write_byte_DS(uchar abyte) // 串行写1302 1字节 {

     uchar i,mid;

     PORTB=0XF1; //使 RST H PB0PB1 PB2 L

     DDRB=0X0F;

     sclk_L; //DS1302.H宏定义,先拉低sclk

     for(i=0;i<8;i++) //data_out串行送出8位数据。最低位开始

     {

     mid=abyte&0x01; //取数据最低位

     if(!mid) //data_out电平

     data_out_L;

     else

     data_out_H;

     sclk_H; //sclk拉高

     sclk_L; //sclk拉低

     abyte>>=1; //左移一位

     }

     _delay_us(4);

}

    /////////////////////////////// uchar read_byte_DS(void) //读出1302 1字节 {

     uchar i,mid=0;

     PORTB|=(1<<PB2);

     DDRB&=~(1<<PB2); //data_out 就是PB2 ,上拉输入

     sclk_L; //sclk

     for (i=0;i<8;i++) //PB2 7

     {

     mid>>=1;

     sclk_H;

     if(PINB&(1<<PB2)) //sclk高时读

     mid|=0x80; //读出放在mid高位,然后左移

     sclk_L;

     }

     return mid;

     _delay_us(4);

     DDRB|=(1<<PB2); //读完后PB2恢复为输出

}

    /////////////////////////////// void writeDS_Data(uchar addr,uchar data) //针对地址写1302 {

     PORTB=0xF0; //PB0 1 2 输出,低

     DDRB=0x0F;

     rst_H; //rst

     _delay_us(2);

     sclk_L; //sclk

     write_byte_DS(addr); //写地址

     write_byte_DS(data); //写数据

     _delay_us(2);

     rst_L; //rst

    }

    //////////////////////////////// uchar readDS_Data(uchar addr) //针对地址读1302 {

     uchar mid;

     PORTB=0xF0;

     DDRB=0x0F;

     rst_H;

     _delay_us(2);

     sclk_L;

     write_byte_DS(addr); //写地址

     mid=read_byte_DS(); //读数据

     _delay_us(2);

     rst_L;

     return mid;

    }

    /////////////////////////////////

    void set_DS_ini(void) //初始化1302 {

     writeDS_Data(0x8E,0x00); //允许写

     writeDS_Data(0x82,0x00); //0

     writeDS_Data(0x84,0x09); //9

     writeDS_Data(0x86,0x01); //1

     writeDS_Data(0x88,0x07); //7

     writeDS_Data(0x8a,0x04); //4

     writeDS_Data(0x8c,0x10); //10

     writeDS_Data(0x90,0xa0); //不充电

     writeDS_Data(0x80,0x00); //0sec启动

}

    ////////////////////////// //====================================

    //主程序

    //

    //========================== //=======miniClock.c========

    #include <util/delay.h> #include "miniClock.h"

    #include "miniLcd.h"

    #include "DS1302.h"

uchar keyno; //键码

    uchar DateTimeFace_flag=1; //文字-图形界面标记 uchar DateTime[7]; //日历时间数组={0x02,0x08,0x03,0x05,0x07,0x01,0x10};

    uchar ind_h,ind_min,ind_sec; //图形界面中的时分秒针 uchar chik=0; //小鸡标记,按秒依次0 1

Report this document

For any questions or suggestions please email
cust-service@docsford.com