更正代码1

#include
#include
typedef unsigned int uint;//宏定义 写uint就直接代替了unsigned int 
typedef unsigned char uchar;
uchar temp ;
sbit beep=P2^3;void Delay100ms()		//@11.0592MHz
{unsigned char i, j, k;_nop_();_nop_();i = 5;j = 52;k = 195;do{do{while (--k);} while (--j);} while (--i);
}void main(){temp=0xfe;P2=temp;beep=0;//蜂鸣器响 当他等于1的时候就停了while(1){temp=_crol_(temp,1);//将temp循环左移1位Delay100ms()  ;P2=temp;}}

数码管
先选择第几个 P2的234端口对应12345678 的数码管 选择后再赋值即可

#include
#include
typedef unsigned int uint;//宏定义 写uint就直接代替了unsigned int 
typedef unsigned char uchar;
sbit a1=P2^2;
sbit a2=P2^3;
sbit a3=P2^4;
uint show[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};void choose(uint n,uint m){switch(n) {case 1: a1=0;a2=0;a3=0; break;case 2: a1=1;a2=0;a3=0; break;case 3:	a1=0;a2=1;a3=0;	break;case 4: a1=1;a2=1;a3=0; break;case 5: a1=0;a2=0;a3=1; break;case 6: a1=1;a2=0;a3=1; break;case 7: a1=0;a2=1;a3=1; break;	case 8: a1=1;a2=1;a3=1; break;}P0=show[m];}
void main(){choose(2,8);}

循环显示


#include
#include
typedef unsigned int uint;//宏定义 写uint就直接代替了unsigned int 
typedef unsigned char uchar;
sbit a1=P2^2;
sbit a2=P2^3;
sbit a3=P2^4;
uint show[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};void Delay100ms()		//@11.0592MHz
{unsigned char i, j, k;_nop_();_nop_();i = 5;j = 52;k = 195;do{do{while (--k);} while (--j);} while (--i);
}void choose(uint n,uint m){switch(n) {case 1: a1=0;a2=0;a3=0; break;case 2: a1=1;a2=0;a3=0; break;case 3:	a1=0;a2=1;a3=0;	break;case 4: a1=1;a2=1;a3=0; break;case 5: a1=0;a2=0;a3=1; break;case 6: a1=1;a2=0;a3=1; break;case 7: a1=0;a2=1;a3=1; break;	case 8: a1=1;a2=1;a3=1; break;}P0=show[m];Delay100ms();P0=0x00;//上面两句用来防止数字串位,先延时一下再全部清零
}
void main(){
uint i=1;
while(i<=8)	 {
choose(i,2)	 ;Delay100ms();i++;}}

模块化编程
将常用的函数放入.c文件 然后创建相应的.h文件
.h文件内容

#ifndef _public_H  
#define _public_H //如果没有定义_public_H就定义这个#include "reg52.h"typedef unsigned int u16;	//对系统默认数据类型进行重定义
typedef unsigned char u8;
typedef unsigned long u32;void delay_10us(u16 ten_us);//将.c中的函数名称直接赋值过来就可以
void delay_ms(u16 ms);#endif

=================================================
=======LCD1602的调试
1.首先将对应的LCD1602的c文件和对应的H文件导入项目中
2.将需要用到的延时函数对应的c和H文件也导入项目中
3.调用相关函数即可

#include "public.h"
#include "lcd1602.h"
void main()
{	lcd1602_init();//LCD1602初始化lcd1602_show_string(2,0,"Hello World!");//第一行显示	第一个参数表示在第几列开始显示lcd1602_show_string(0,1,"0123456789");//第二行显示while(1){}	
}

lcd1602.c

#include "lcd1602.h"/*******************************************************************************
* 函 数 名       : lcd1602_write_cmd
* 函数功能		 : LCD1602写命令
* 输    入       : cmd:指令
* 输    出    	 : 无
*******************************************************************************/
#if (LCD1602_4OR8_DATA_INTERFACE==0)//8位LCD
void lcd1602_write_cmd(u8 cmd)
{LCD1602_RS=0;//选择命令LCD1602_RW=0;//选择写LCD1602_E=0;LCD1602_DATAPORT=cmd;//准备命令delay_ms(1);LCD1602_E=1;//使能脚E先上升沿写入delay_ms(1);LCD1602_E=0;//使能脚E后负跳变完成写入	
}
#else	//4位LCD
void lcd1602_write_cmd(u8 cmd)
{LCD1602_RS=0;//选择命令LCD1602_RW=0;//选择写LCD1602_E=0;LCD1602_DATAPORT=cmd;//准备命令delay_ms(1);LCD1602_E=1;//使能脚E先上升沿写入delay_ms(1);LCD1602_E=0;//使能脚E后负跳变完成写入LCD1602_DATAPORT=cmd<<4;//准备命令delay_ms(1);LCD1602_E=1;//使能脚E先上升沿写入delay_ms(1);LCD1602_E=0;//使能脚E后负跳变完成写入	
}
#endif/*******************************************************************************
* 函 数 名       : lcd1602_write_data
* 函数功能		 : LCD1602写数据
* 输    入       : dat:数据
* 输    出    	 : 无
*******************************************************************************/
#if (LCD1602_4OR8_DATA_INTERFACE==0)//8位LCD
void lcd1602_write_data(u8 dat) 
{LCD1602_RS=1;//选择数据LCD1602_RW=0;//选择写LCD1602_E=0;LCD1602_DATAPORT=dat;//准备数据delay_ms(1);LCD1602_E=1;//使能脚E先上升沿写入delay_ms(1);LCD1602_E=0;//使能脚E后负跳变完成写入		
}
#else
void lcd1602_write_data(u8 dat) 
{LCD1602_RS=1;//选择数据LCD1602_RW=0;//选择写LCD1602_E=0;LCD1602_DATAPORT=dat;//准备数据delay_ms(1);LCD1602_E=1;//使能脚E先上升沿写入delay_ms(1);LCD1602_E=0;//使能脚E后负跳变完成写入LCD1602_DATAPORT=dat<<4;//准备数据delay_ms(1);LCD1602_E=1;//使能脚E先上升沿写入delay_ms(1);LCD1602_E=0;//使能脚E后负跳变完成写入		
}
#endif/*******************************************************************************
* 函 数 名       : lcd1602_init
* 函数功能		 : LCD1602初始化
* 输    入       : 无
* 输    出    	 : 无
*******************************************************************************/
#if (LCD1602_4OR8_DATA_INTERFACE==0)//8位LCD
void lcd1602_init(void)
{lcd1602_write_cmd(0x38);//数据总线8位,显示2行,5*7点阵/字符lcd1602_write_cmd(0x0c);//显示功能开,无光标,光标闪烁lcd1602_write_cmd(0x06);//写入新数据后光标右移,显示屏不移动lcd1602_write_cmd(0x01);//清屏	
}
#else
void lcd1602_init(void)
{lcd1602_write_cmd(0x28);//数据总线4位,显示2行,5*7点阵/字符lcd1602_write_cmd(0x0c);//显示功能开,无光标,光标闪烁lcd1602_write_cmd(0x06);//写入新数据后光标右移,显示屏不移动lcd1602_write_cmd(0x01);//清屏	
}
#endif/*******************************************************************************
* 函 数 名       : lcd1602_clear
* 函数功能		 : LCD1602清屏
* 输    入       : 无
* 输    出    	 : 无
*******************************************************************************/
void lcd1602_clear(void)
{lcd1602_write_cmd(0x01);	
}/*******************************************************************************
* 函 数 名       : lcd1602_show_string
* 函数功能		 : LCD1602显示字符
* 输    入       : x,y:显示坐标,x=0~15,y=0~1;str:显示字符串
* 输    出    	 : 无
*******************************************************************************/
void lcd1602_show_string(u8 x,u8 y,u8 *str)
{u8 i=0;if(y>1||x>15)return;//行列参数不对则强制退出if(y<1)	//第1行显示{	while(*str!='\0')//字符串是以'\0'结尾,只要前面有内容就显示{if(i<16-x)//如果字符长度超过第一行显示范围,则在第二行继续显示{lcd1602_write_cmd(0x80+i+x);//第一行显示地址设置	}else{lcd1602_write_cmd(0x40+0x80+i+x-16);//第二行显示地址设置	}lcd1602_write_data(*str);//显示内容str++;//指针递增i++;	}	}else	//第2行显示{while(*str!='\0'){if(i<16-x) //如果字符长度超过第二行显示范围,则在第一行继续显示{lcd1602_write_cmd(0x80+0x40+i+x);	}else{lcd1602_write_cmd(0x80+i+x-16);	}lcd1602_write_data(*str);str++;i++;	}	}				
}

public.c文件

#include "public.h"/*******************************************************************************
* 函 数 名       : delay_10us
* 函数功能		 : 延时函数,ten_us=1时,大约延时10us
* 输    入       : ten_us
* 输    出    	 : 无
*******************************************************************************/
void delay_10us(u16 ten_us)
{while(ten_us--);	
}/*******************************************************************************
* 函 数 名       : delay_ms
* 函数功能		 : ms延时函数,ms=1时,大约延时1ms
* 输    入       : ms:ms延时时间
* 输    出    	 : 无
*******************************************************************************/
void delay_ms(u16 ms)
{u16 i,j;for(i=ms;i>0;i--)for(j=110;j>0;j--);
}

=================================================


矩阵键盘
矩阵键盘的好处可以减少io口的占用
采用逐行或者逐列的扫描,就可以读出任何位置按键的状态

数码管的扫描是一种输出扫描
矩阵键盘是输入扫描,读取第1行/列->再读取第2行/列 然后快速循环这个过程,最终实现所有按键同时检测

数码管扫描方式和矩阵键盘的都是为了节省i/o口

四行对应的io口 其中一个为0就是扫描该行,其他就是1,再确定列 如果1234列中其中对应一个为0,就能准确确定该按钮的位置了

单片机IO口是一种弱上拉的,输出0比输出1要强


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部