0

关于投票
AVR 与 12864 做的时钟
代码:
20:03 2008-5-9#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>


#define uchar unsigned char
#define uint unsigned int
#define xtal 8
#define CS  PA5
#define SID  PA6
#define SCLK PA7

#define Set_CS() DDRA |= (1<<CS);PORTA |= (1<<CS)
#define Set_SID() DDRA |= (1<<SID);PORTA |= (1<<SID)
#define Set_SCLK() DDRA |= (1<<SCLK);PORTA |= (1<<SCLK)

#define Clr_CS() DDRA |= (1<<CS);PORTA &=~(1<<CS)
#define Clr_SID() DDRA |= (1<<SID);PORTA &=~(1<<SID)
#define Clr_SCLK() DDRA |= (1<<SCLK);PORTA &=~(1<<SCLK)

#define  HH  2     //定义  时  为2
#define  MM  1     //定义  分  为1
#define  SS  0     //定义  秒  为0

volatile unsigned char Time_h_m_s[3]={0,0,0};   //时间的 时  分  秒  存贮单元
volatile unsigned char flash_sign=0x00;
//====================================================================
//函数声明
void Delay(uint ms);      //延时子程序
void Serial_W_1byte_to_LCD(uchar RS, uchar W_data);
void Serial_send_cmd(uchar send_cmd);
void Serial_send_data(uchar send_data);
void Write_8bits(uchar W_bits);
void LCD_Init(void);
void Serial_send_string_to_xy(uchar row, uchar col, uchar *p);
void Display_clock1(uchar row, uchar col, uchar *time);

/*=============================================================
功能: TIMER2溢出中断函数
=================================================================*/

SIGNAL(SIG_OUTPUT_COMPARE2)     // _VECTOR(3) 
{  
  static unsigned char half_second; 
  if((++half_second)>=2)
   { 
     if((++Time_h_m_s[SS])>=60)
      {
           Time_h_m_s[SS] = 0;
           if((++Time_h_m_s[MM])>=60)
            {
          Time_h_m_s[MM] = 0;
          if((++Time_h_m_s[HH])>=24)
            Time_h_m_s[HH] = 0;
            }        
      }
         half_second = 0x00;     
    }        
  flash_sign &= 0x01;    
  flash_sign ^= 0x01;            //置位闪动标志位(该位为0时闪动)



 /*******************************************************************
函 数 名:Port_init
入口参数:无
出口参数:无
建立日期:2008年05月09日
修改日期:
函数作用:MCU端口初始化函数
说    明:
********************************************************************/
 void Port_init(void)
 {
    DDRC = ~(1<<DDC6);
    PORTC = 0x00;        
    
   ASSR |= (1<<AS2);             //允许Timer2用TOSC1脚的时钟信号
   TIMSK |= (1<<OCIE2);             //允许定时器2比较匹配中断允许
   TCCR2 |= (1<<WGM21)|(1<<CS22)|(1<<CS21);  //CTC模式 ,256分频
   OCR2 = 63;
 }
/*******************************************************************
函 数 名:Serial_send_string
入口参数: *p_send_data
出口参数:无
建立日期:2008年05月09日
修改日期:
函数作用:发送字符串子函数
说    明:
********************************************************************/
void Serial_send_string(uchar *p_send_data)
{
  uchar *p_temp;
  p_temp = p_send_data;
  while(*p_temp != 0)
    {
          Serial_send_data(*p_temp++);
        }
}

//===================================================================*/
/********************************************************************
函 数 名:Serial_send_string_to_xy
入口参数:row,col,*p
出口参数:无
建立日期:2008年04月13日
修改日期:
函数作用:
说    明:
********************************************************************/
void Serial_send_string_to_xy(uchar row, uchar col, uchar *p) 

   switch(row)
    {
          case 0:Serial_send_cmd(0x80+col);
                  break;
          case 1:Serial_send_cmd(0x90+col);
                  break;
          case 2:Serial_send_cmd(0x88+col);
                  break;
          case 3:Serial_send_cmd(0x98+col);
                  break;
          default: break;
    } 
 while(*p != 0)
 {
  Serial_send_data(*p++); //写数据到RAM
 }
}

/********************************************************************/
int main(void)
{
 PORTA = 0XFF;   //
 DDRA = 0XFF;   //PA口全部设为输出模式
 Port_init();
 Clr_CS();
 Clr_SID();
 Clr_SCLK(); 
 LCD_Init();
 Delay(10);
 LCD_Init();     //初始化两边是关键,否则液晶上电重起将不能显示
 sei();  //开总中断 
 while(1)
 { 
  asm("nop");
  asm("nop");  
  Serial_send_cmd(0x30);  //基本指令集,,绘图显示OFF
  //Serial_send_cmd(0x01);  //清除显示    
  Serial_send_string_to_xy(0, 0, "现在时间:");
  Display_clock1(1, 3,Time_h_m_s);
 }
    
}
/*******************************************************************
函 数 名:LCD_Init
入口参数:无
出口参数:无
建立日期:2008年05月09日
修改日期:
函数作用:12864液晶初始化函数
说    明:
********************************************************************/
void LCD_Init(void)
{
  uchar cmd;
 Delay(50); 
  cmd=0x30;   //功能设置 8位数据,基本指令
 Serial_send_cmd(cmd);
 Delay(20);
 cmd=0x0C;   //显示状态 ON,游标OFF,反白OFF
 Serial_send_cmd(cmd); //写指令
 Delay(20);
 cmd=0x01;   //清除显示
 Serial_send_cmd(cmd); //写指令
 Delay(20);
 cmd=0x02;   //地址归位
 Serial_send_cmd(cmd); //写指令
 Delay(20);
 cmd=0x80;   //设置DDRAM地址
 Serial_send_cmd(cmd); //写指令
 Delay(20);   //延时
}
/*******************************************************************
函 数 名:Serial_send_cmd
入口参数:cmd
出口参数:无
建立日期:2008年4月13日
修改日期:
函数作用:写一个字节指令的到12864液晶,
说    明:
********************************************************************/
void Serial_send_cmd(uchar send_cmd)
{
  Serial_W_1byte_to_LCD(0, send_cmd);
}
/*******************************************************************
函 数 名:Serial_send_data
入口参数:cmd
出口参数:无
建立日期:2008年4月13日
修改日期:
函数作用:写一个字节数据到12864液晶,
说    明:
********************************************************************/
void Serial_send_data(uchar send_data)
{
  Serial_W_1byte_to_LCD(1, send_data);
}



/*******************************************************************
函 数 名:Serial_Serial_W_1byte_to_LCD_to_LCD
入口参数:RW、RS、W_data
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:写一个字节的数据到12864液晶,包括指令和数据
说    明:RW=1,从液晶读数据到MCU;RW=0,写一个数据到液晶;
   (一般RW都设为0,即只向液晶写数据,不读数据)
          RS=1,写入的是数据;RS=0,写入的是指令;
    一般模式:RW=0,RS=1;写数据
       RW=0,RS=0;写指令
********************************************************************/
void Serial_W_1byte_to_LCD(uchar RS, uchar W_data)
{
 uchar H_data,L_data,S_ID = 0xf8;  //11111 RWRS 0
 
 if(RS == 0)
  {
    S_ID &= ~0x02;
  }
 else     //if(RS==1)
  {
    S_ID |= 0X02;
  }
 H_data = W_data;
 H_data &= 0xf0;   //屏蔽低4位的数据
 L_data = W_data;  //xxxx0000格式
 L_data <<= 4;     //xxxx0000格式
 L_data &= 0xf0;   //屏蔽高4位的数据
    
 Set_CS();
 asm("nop");
 asm("nop");
 Write_8bits(S_ID);   //发送S_ID
 asm("nop");
 Write_8bits(H_data); //发送H_data
 asm("nop");
 Write_8bits(L_data); //发送L_data
 asm("nop");
 Clr_CS(); 
}
/********************************************************************
函 数 名:Write_8bits
入口参数:W_bits
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:负责串行输出8个bit位
说    明:
********************************************************************/
void Write_8bits(uchar W_bits)
{
 uchar i,Temp_data;
 Temp_data = W_bits;
 for(i=0; i<8; i++)
 {  
   if((Temp_data&0x80)==0)
    {
      Clr_SID();
    }
   else
    {
     Set_SID();
    } 
   asm("nop");
   asm("nop");   
   Set_SCLK();
   asm("nop");
   asm("nop");   
   Clr_SCLK();
   asm("nop");
   asm("nop");  
   Temp_data <<= 1;  
 }
 Clr_SID();
}
/********************************************************************
函 数 名:Delay
入口参数:ms
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:毫秒级的延时程序,当晶振为12Mhz时,xtal=12;
说    明:
********************************************************************/
void Delay(uint ms) 

    uint i; 
    while(ms--)    
   { 
     for(i=1;i<(uint)(xtal*143-2);i++) 
         ; 
   }   
}

/********************************************************************
函 数 名:Display_clock1
入口参数:row,col,*time
出口参数:无
建立日期:2008年05月09日
修改日期:
函数作用:
说    明:
********************************************************************/
void Display_clock1(uchar row, uchar col, uchar *time) 

  unsigned char  c_data[16];
  switch(row)
    {
          case 0:Serial_send_cmd(0x80+col);
                  break;
          case 1:Serial_send_cmd(0x90+col);
                  break;
          case 2:Serial_send_cmd(0x88+col);
                  break;
          case 3:Serial_send_cmd(0x98+col);
                  break;
          default: break;
    }
        
    utoa(time[HH]/10,c_data,10);
    Serial_send_string(c_data);
        utoa(time[HH]%10,c_data,10);
    Serial_send_string(c_data);
        
        if(flash_sign==0x00)
         Serial_send_string(":");
        else
     Serial_send_string(" ");    
        
        utoa(time[MM]/10,c_data,10);
    Serial_send_string(c_data);
        utoa(time[MM]%10,c_data,10);
    Serial_send_string(c_data);
        
        if(flash_sign==0x00)
         Serial_send_string(":");
        else
     Serial_send_string(" ");  
        
        utoa(time[SS]/10,c_data,10);
    Serial_send_string(c_data);
        utoa(time[SS]%10,c_data,10);
    Serial_send_string(c_data);
}
系统分类: 单片机
用户分类: 学习AVR
标签: 无标签
来源: 无分类
发表评论 阅读全文(159) | 回复(0)

0

关于投票
电子类相关网站

 

http://www.cyfronika.com.pl/BDM/willem.html
http://www.cyfronika.com.pl                                             
国外好网站
http://www.william.com.cn/atmel/index.asp#                              ATMEL
中文推广
http://dzxx.cn-dz.com 
http://dzxx.mlyhs.com
http://bes.8u8.com/pcb/k.htm                                 
热转印制板的详细过程                              
http://www.teachersong.com/index1.htm                         
宋容个人教学单片机
http://www.etuni.com/index.asp                               
下载,论坛,特好!
http://www.pcbtech.net/                                       
中国PCB技术网
http://allgames.gamesh.com/emu/host/ediy/dpj/mcuy10a.htm
http://sdkh.51.net/dz/atm89.htm
http://www.mcudiy.com/index.htm                     AVR
开发!
*****
http://www.szhr.com.cn/mainframe.html                         
深圳市人才市场招聘
http://shada.ocnc.net/laf/                                    tc
教程                              
http://www.89s51.com/article/ArticleShow.asp?ArticleID=26     keil c
教程
http://lightingchina.com/
http://www.wenrun.com/

http://lightingchina.com/member/vipunit_cp.asp?unitid=9993    LED
http://www.mcu21cn.com/default.asp                           
单片机
http://www.jinjubao.com/                                     
聚宝盆
http://www.fjmcu.com/
http://www.mcustudy.com/cpld/ispexpert.pdf                    cpld
http://www.21icbbs.com/club/bbs/ShowAnnounce.asp?v=&ID=663163        AVR
http://www.teachersong.com/                            pic
单片机
http://elec.51.net/cpzz.htm                           
遥控  
http://www.21icsearch.com/pmcu/dpjjx/ndpjjx.htm 
http://www.mcustudio.com/dpjjx/cteach/learnC.htm       
学单片机了
http://www.jlste.com.cn                               
自考
http://hjwang.xiloo.com/index2.html                   
电话控制                                                    
http://hlbds.xiloo.com/index.htm                       
电子制作***
http://www.c51bbs.com/c51src.htm                       C51
咨询网
http://hjwang.xiloo.com/ic1/main_ic3.htm               
实用文章
http://www.amr-system.com.cn/default.htm               
电力线载波
http://202.117.27.64/ziliao/www.daqchina.net
http://www.netdzb.com/zhongyao/zywz.asp               
电子报
http://www.yxdz.com.cn/docc/yxdz9.htm                 
防盗
http://www.szintech.com/gpstech/tech2.htm              GPS
jdtwpublic@sina.com
http://www.58ic.com/emccomm.asp                        IC
介绍
http://wuming.3322.net/index1.htm                      Protel
入门
http://ecad.51.net/                                    Protel
入门
http://www.mcustudy.com                                   
单片机

http://www.xiao-qi.com/cpld/cpld.php3                  CPLD
http://www.atnet1000.com/                             
自动化

www.datasheetlocator.com                              
世界IC资料

http://topinc.126.com                                  IC
资料及制作***

http://www.netdzb.com/bbs/listbbs.asp?boardid=8&page=27 
电子BBS***

http://a51.nease.net/index.htm                         
单片机***

http://www.chinadz.com/index/                         
资料联盟

httt://gddq.126.com

http://www.jimmyjun.8u8.com/dianzizhizou/j.htm         
语音识别

http://dada.tongtu.net/                               
大大电子

http://www.jlste.com.cn/bbs/detailTable.asp?id=9208
http://www.ioor.net/hseda/main.htm                     ******
http://www.mcu51.com/download/digitpdf/40xx/关于4000系列资料
http://www.mcu51.com/download/digitpdf/45xx/关于4500系列资料
http://www.mcu51.com/download/digitpdf/74xx/关于7400系列资料
http://www.mcu51.com/download/RS485note.pdf关于RS485的资料
http://www.mcu51.com/download/binhex.com工具
http://www.mcu51.com/download/hexbin.exe工具
http://www.mcu51.com/download/c51eval.zip关于C51的资料
http://www.mcu51.com/download/digitpdf/CPU/关于CPU的资料
http://www.maxim-ic.com.cn/Max1800-2690/MAX220-M.PDF资料
http://www.maxim-ic.com.cn达拉斯公司主站
http://etuni.com/downdata.asp?id=832&C51Tip.pdf关于C51的资料
http://www.mcu-tech.com/download/c51%20user1.pdf关于C51的资料
http://www.laogu.com/download/c51ctl.zip关于C51的资料
http://etuni.com/downsoft.asp?id=118&KeilC51v612.zipC51中文资料
http://www.whwave.com.cn/wsjiaocai.htm教材、资料
http://www.whwave.com.cn/WWIC.htm部分IC资料
http://www.whwave.com.cn/FM24CXX.htm关于24系列资料
http://www.hongyan-e.com/web/dzbic.htm部分IC资料
http://www.cetinet.com/downloadtemp/chuankouzsV2.1.zip免费串口调试器
http://www.atmel.com/dyn/products/param_table.asp?family_id=604&OrderBy=part_no&Direction=ASC关于ATEML全系列资料


1. 中国电子网(http://www.21ic.com/
2.
中国电子资料网(http://www.chinadz.com/
3.
广州电子技术网(http://gzdzw.51.net/
4.
中国电子行业信息网(http://www.ceic.gov.cn/
5.
电子制作天地(http://www.dzdiy.com/
6.
电子爱好者(http://www.etuni.com
7.
电子工程师(http://www.eebyte.com
8.
电子空间资讯(http://go.163.com/~rihuri
9.
老古开发网(http://www.laogu.com/
10.
单片机产品设计中心(http://www.syhbgs.com/
11. 51
单片机世界(http://wwwmcu51.com/
12.
平凡的单片机(http://teach51.yeah.net/
13.
周立功单片机世界(http://www.zigmcu.com/
14.
嵌入开发网(http://www.embed.com.cn/
15. Microchip
公司中文网站(http://microchip.com.cn
16. Atmel
公司英文网站(http://www.atmel.com/
17.
茂林电子继承电路专页(http:/www.maolin.com.ccircuit/main.htm
18.
中国集成电路网(http://www.chinaicip.com/
19. IC
交易网(http://ic.net.cn/
20. IC
商务网(http://www.buy-ic.com/
21.
集成网(http://ic-net.com.cn/
22.
电子元器件世界(http://www.ic-world.net/
23.
元器件在线(http://www.56789.com/
24.
中华IC网(http://www.sinoic.com/
25.
盛名零件网(http://www.icminer.com/
26.
网络半导体数据库(http://www.bgs.nu/sdw/
27.
中国PCB技术网(http://pcbtech.net/
28. PCB
信息网(http://www.pcbinfo.net/
29. EDA2000(
http://www.myeda.org/cn/)
30. EDA
中国论坛(http://www.edachina.com/
31.
可编程逻辑器件(http://www.pld.com.cn/
32.
宇成设计(http://ecdesign.my163.com/
33.
吴鸣工作站(http://wuming.3322.net/
34. PCB
世界(http://www.pcbworld.net/

系统分类: PCB
用户分类: PCB
标签: 无标签
来源: 转贴
发表评论 阅读全文(180) | 回复(0)
总共 , 当前 /