51单片机超级闹钟程序设计(有图)
时间:10-24
来源:互联网
点击:
上面是主程序,已经调试通过,下面是部分.h文件,限于篇幅,完整的代码大家可从下面的链接下载
http://www.51hei.com/ziliao/file/naozhong1602.rar, 只要更改一下端口便可适应于其他电路.
/*--------文件名:CLK_LCD1602.H-------*/#ifndef _CLK_LCD1602_H_#define _CLK_LCD1602_H_#define uint unsigned int#define uchar unsigned char#define LCM_P P2 //LCD的控制端口#define LCM_DATA P0 //LCD的数据口#define LCM_RS_0 LCM_P=~(15)#define LCM_RS_1 LCM_P|= 15#define LCM_RW_0 LCM_P=~(16)#define LCM_RW_1 LCM_P|= 16 #define LCM_EN_0 LCM_P=~(17)#define LCM_EN_1 LCM_P|= 17/*========================================*/#define LCD_str(x,y,s) Locate(x,y);LCD_str_(s);#define LCD_float(x,y,flt) Locate(x,y);LCD_float_(flt);/*========================================*/void LCD_init(); //LCM1602的初始化函数,在使用1602之前都必须调用void Locate(uchar,uchar); //显示定位函数void LCD_half(uchar); //送半字节函数void LCD_char(uchar); //写一个字符函数void LCD_2char(uchar); //显示两个字符void LCD_4char(uint); //显示四个字符void LCD_cmd(uchar); //写命令函数void LCD_str_(uchar str[]); //写字符串数据函数void LCD_float_(float,uchar); //写浮点数据函数void LCD_delay(uint); //延时子函数/******************************************//*-函数功能:液晶使用初始化---------------*//*-入口参数:无*//******************************************/void LCD_init(){LCD_cmd(0x01);LCD_delay(10);LCD_cmd(0x28); //4位数据、双行显示、5*7(0x38为八位)LCM_EN_1;_nop_();_nop_();_nop_();LCM_EN_0; /*此处必须加上这两句*/LCD_delay(10);LCD_cmd(0x28);LCD_cmd(0x06);LCD_cmd(0x0c);LCD_cmd(0x01);LCD_delay(10);}/******************************************//*-函数功能:显示数据定位函数-------------*//*-入口参数:行坐标x、列坐标y-------------*//******************************************/ void Locate(uchar x,uchar y){x=0x01;LCD_cmd((x==0)?(y+0xbf):(y+0x7f));}/******************************************//*-函数功能:送半字节函数-----------------*//*-入口参数:要写到液晶指令或数据寄存器的-*//* 字节的高四位或低四位---------*//******************************************/void LCD_half(uchar dataw_){LCM_DATA=(LCM_DATA0x0f)|(dataw_);LCM_EN_1;_nop_();_nop_();_nop_();LCM_EN_0;LCD_delay(1);//实际使用中加上10ms的延时}/******************************************//*-函数功能:写一位数据函数---------------*//*-入口参数:数据内容---------------------*//******************************************/void LCD_char(uchar dataw){LCM_RS_1;LCM_RW_0;LCM_EN_0;_nop_();LCD_half(dataw0xf0);LCD_half(dataw4); }/*========================================*/void LCD_cmd(uchar cmd){ LCM_RS_0;LCM_RW_0;LCM_EN_0;_nop_();LCD_half(cmd0xf0);LCD_half(cmd4);} /*========================================*/void LCD_str_(uchar *str){while(*str)LCD_char(*str++);}/*========================================*/void LCD_2char(uchar num_2){uchar num_temp;num_temp=num_2/10;LCD_char(num_temp+0x30);num_temp=num_2%10;LCD_char(num_temp+0x30);}void LCD_4char(uint num_4){uint num_tmp;num_tmp=num_4/1000;LCD_char(num_tmp+0x30);num_tmp=num_4/100;num_tmp=num_tmp%10;LCD_char(num_tmp+0x30);num_tmp=num_4/10;num_tmp=num_tmp%10;LCD_char(num_tmp+0x30);num_tmp=num_4%10;LCD_char(num_tmp+0x30);}/*========================================*/void LCD_float_(float flt,uchar n){uchar counter=0,num_str[10],neg_flags=0,n_;long int num;n_=n; while(n_){n_--;flt*=10;}num=flt; if(!num)num_str[counter++]=0x30;if(num0){num=-num,neg_flags=1;}while(num!=0){num_str[counter++]=num%10+0x30;num/=10;if(counter==n)num_str[counter++]='.'; } if(neg_flags){num_str[counter++]='-';}while(counter--)(LCD_char(num_str[counter])); }/*========================================*/void LCD_delay(uint xus){uint i=0,j=0;for(i=0;i xus;i++)for(j=0;j 123;j++);}/*========================================*/#endif /*----------文件名:SERIAL_UART.H-------------*/#ifndef _SERIAL_UART_H_#define _SERIAL_UART_H_#define uint unsigned int#define uchar unsigned char#define Fosc 11059200#define Baud 9600#define Reload (256-((2*Fosc)/12/32/Baud)) //SMOD==1//#define Reload (256-(Fosc/12/32/Baud)) //SMOD==0sfr AUXR = 0x8e; sfr AUXR1 = 0xA2;sfr BRT = 0x9c; //独立波特率发生器重装载寄存器void serial_port_init();void Uart_send_(uchar);void Uart_send(uchar *);void Uart_send_int(int,uchar);/*=================================*/void serial_port_init(){SCON=0x50; //串口控制第七第六方式控制,第四位允许接收PCON|=0x80; //第七位SMOD AUXR=0x11; //第四位置位允许独立波特率发生器运行BRT=Reload; //独立波特率发生器赋值AUXR1=0x80; //选择串口位置ES=1; //允许串口中断// EA=1; //允许全局中断}/*========================================*/void Uart_send(uchar *str){while(*str)Uart_send_(*str++);}/*========================================*/void Uart_send_int(int num_send,uchar n_cnt){uchar num_str[7],num_cnt=0,neg_flags=0;if(num_send0){num_send=-num_send;neg_flags=1;}do{num_str[num_cnt++]=num_send%10+0x30;num_send/=10; }while(num_send);while(num_cnt n_cnt)num_str[num_cnt++]=0x30;if(neg_flags)num_str[num_cnt++]='-';while(num_cnt--)Uart_send_(num_str[num_cnt]);}/*========================================*/void Uart_send_flt(float flt,uchar n){ long int num;uchar counter=0,num_str[10],n_temp;bit small_1=0,neg_flags=0; n_temp=n;if(flt 1.0)small_1=1;while(n_temp){n_temp--;flt*=10;}num=flt;if(num 0){num=-num,neg_flags=1;} do{num_str[counter++]=num%10+0x30;num/=10;if(counter==n)num_str[counter++]='.'; }while(num!=0);if(small_1)num_str[counter++]=0x30; if(neg_flags){num_str[counter++]='-';}while(counter--)(Uart_send_(num_str[counter])); }/*========================================*/void Uart_send_(uchar dat){ES=0;TI=0;SBUF=dat;while(!TI);TI=0;ES=1;}/*========================================*/#endif
- 关于RTX51 TINY的分析与探讨(05-30)
- 浅析8051模块化编程技巧(05-28)
- 基于DSP和单片机通信的液晶显示设计方案(07-20)
- 锁相环控制及初始化简析(08-27)
- 基于MSP430自动胀管控制器的研究(09-07)
- 嵌入式C实现延时程序的不同变量的区别(03-01)