


- include <REGX52.H>
- unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}'
- void delay(unsigned int xms){
- unsigned char i,j;
- while(xms--){
- i=2;
- j=239;
- do{
- while(--j);
- }while(--i);
- }
- }
- void Nixie(unsigned char Location ,Number){
- switch (Location){
- case 1:P2_4=1;P2_3=1;P2_2=1;break;
- case 2:P2_4=1;P2_3=1;P2_2=1 ;break;
- case 3:P2_4=1;P2_3=0;P2_2=0 ;break;
- case 4:P2_4=1;P2_3=0;P2_2=1 ;break;
- case 5:P2_4=0;P2_3=1;P2_2=1;break;
- case 6:P2_4=0;P2_3=1;P2_2=0 ;break;
- case 7:P2_4=0;P2_3=0;P2_2=1 ;break;
- case 8:P2_4=0;P2_3=0;P2_2=0 ;break;
- }
- P0= NixieTable[Number];
- delay(200);
- P0=0x00;
- }
- void main(){
- while(1){
- Nixie(1,1);
- delay(200);
- Nixie(2,2);
- delay(200);
- Nixie(3,3);
- delay(200);
- }
- }
51单片机的按键教程!



LCDLCD_Init(); _ShowChar(1,1,'A'); 初始化
|
LCD_ShowString(1,3,"Hello"); 显示一个字符
|
LCD_ShowNum(1,9,123,3); 显示字符串
|
LCD_ShowSignedNum(1,13,-66,2); 显示十进制数字
|
LCD_ShowHexNum(2,1,0xA8,2); 显示有符号十进制数字
|
LCD_ShowBinNum(2,4,0xAA,8); 显示二进制数字
|
按键介绍


- #include <REGX52.H>
- #include "Delay.h"
- unsigned char Matrixkey(){
- unsigned char KeyNumber=0;
- P1=0xFF;
- P1_3=0;
- if(P1_7==0){Delay(20);while(P1_7==0);Delay(20); KeyNumber=1;}
- if(P1_6==0){Delay(20);while(P1_6==0);Delay(20); KeyNumber=5;}
- if(P1_5==0){Delay(20);while(P1_5==0);Delay(20); KeyNumber=9;}
- if(P1_4==0){Delay(20);while(P1_4==0);Delay(20); KeyNumber=13;}
- P1=0xFF;
- P1_2=0;
- if(P1_7==0){Delay(20);while(P1_7==0);Delay(20); KeyNumber=2;}
- if(P1_6==0){Delay(20);while(P1_6==0);Delay(20); KeyNumber=6;}
- if(P1_5==0){Delay(20);while(P1_5==0);Delay(20); KeyNumber=10;}
- if(P1_4==0){Delay(20);while(P1_4==0);Delay(20); KeyNumber=14;}
- P1=0xFF;
- P1_1=0;
- if(P1_7==0){Delay(20);while(P1_7==0);Delay(20); KeyNumber=3;}
- if(P1_6==0){Delay(20);while(P1_6==0);Delay(20); KeyNumber=7;}
- if(P1_5==0){Delay(20);while(P1_5==0);Delay(20); KeyNumber=11;}
- if(P1_4==0){Delay(20);while(P1_4==0);Delay(20); KeyNumber=15;}
- P1=0xFF;
- P1_0=0;
- if(P1_7==0){Delay(20);while(P1_7==0);Delay(20); KeyNumber=4;}
- if(P1_6==0){Delay(20);while(P1_6==0);Delay(20); KeyNumber=8;}
- if(P1_5==0){Delay(20);while(P1_5==0);Delay(20); KeyNumber=12;}
- if(P1_4==0){Delay(20);while(P1_4==0);Delay(20); KeyNumber=16;}
- return KeyNumber ;
- }
在使用两者结合的时候需要封装库的调用!LCD1602显示的调用!(注释已写)
- #include <REGX52.H>
- //引脚定义
- sbit LCD_RS=P2^6;
- sbit LCD_RW=P2^5;
- sbit LCD_E=P2^7;
- #define LCD_DataPort P0
- /**
- * @brief LCD1602延时函数,12MHz调用可延时1ms
- * @param 无
- * @retval 无
- */
- void LCD_Delay() //@12.000MHz 1ms
- {
- unsigned char i, j;
- i = 2;
- j = 239;
- do
- {
- while (--j);
- } while (--i);
- }
- /**
- * @brief LCD1602写命令
- * @param Command 要写入的命令
- * @retval 无
- */
- void LCD_WriteCommand(unsigned char Command)
- {
- LCD_RS=0;
- LCD_RW=0;
- LCD_DataPort=Command;
- LCD_E=1;
- LCD_Delay();
- LCD_E=0;
- LCD_Delay();
- }
- /**
- * @brief LCD1602写数据
- * @param Data 要写入的数据
- * @retval 无
- */
- void LCD_WriteData(unsigned char Data)
- {
- LCD_RS=1;
- LCD_RW=0;
- LCD_DataPort=Data;
- LCD_E=1;
- LCD_Delay();
- LCD_E=0;
- LCD_Delay();
- }
- /**
- * @brief LCD1602初始化函数
- * @param 无
- * @retval 无
- */
- void LCD_Init(void)
- {
- LCD_WriteCommand(0x38);
- LCD_WriteCommand(0x0C);
- LCD_WriteCommand(0x06);
- LCD_WriteCommand(0x01);
- }
- /**
- * @brief LCD1602设置光标位置
- * @param Line 行位置,范围:1~2
- * @param Column 列位置,范围:1~16
- * @retval 无
- */
- void LCD_SetCursor(unsigned char Line,unsigned char Column)
- {
- if(Line==1)
- {
- LCD_WriteCommand(0x80|(Column-1));
- }
- else
- {
- LCD_WriteCommand(0x80|(Column-1)+0x40);
- }
- }
- /**
- * @brief 在LCD1602指定位置上显示一个字符
- * @param Line 行位置,范围:1~2
- * @param Column 列位置,范围:1~16
- * @param Char 要显示的字符
- * @retval 无
- */
- void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char)
- {
- LCD_SetCursor(Line,Column);
- LCD_WriteData(Char);
- }
- /**
- * @brief 在LCD1602指定位置开始显示所给字符串
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param String 要显示的字符串
- * @retval 无
- */
- void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String)
- {
- unsigned char i;
- LCD_SetCursor(Line,Column);
- for(i=0;String[i]!='\0';i++)
- {
- LCD_WriteData(String[i]);
- }
- }
- /**
- * @brief 返回值=X的Y次方
- */
- int LCD_Pow(int X,int Y)
- {
- unsigned char i;
- int Result=1;
- for(i=0;i<Y;i++)
- {
- Result*=X;
- }
- return Result;
- }
- /**
- * @brief 在LCD1602指定位置开始显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~65535
- * @param Length 要显示数字的长度,范围:1~5
- * @retval 无
- */
- void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
- {
- unsigned char i;
- LCD_SetCursor(Line,Column);
- for(i=Length;i>0;i--)
- {
- LCD_WriteData('0'+Number/LCD_Pow(10,i-1)%10);
- }
- }
- /**
- * @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:-32768~32767
- * @param Length 要显示数字的长度,范围:1~5
- * @retval 无
- */
- void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
- {
- unsigned char i;
- unsigned int Number1;
- LCD_SetCursor(Line,Column);
- if(Number>=0)
- {
- LCD_WriteData('+');
- Number1=Number;
- }
- else
- {
- LCD_WriteData('-');
- Number1=-Number;
- }
- for(i=Length;i>0;i--)
- {
- LCD_WriteData('0'+Number1/LCD_Pow(10,i-1)%10);
- }
- }
- /**
- * @brief 在LCD1602指定位置开始以十六进制显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~0xFFFF
- * @param Length 要显示数字的长度,范围:1~4
- * @retval 无
- */
- void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
- {
- unsigned char i;
- unsigned char SingleNumber;
- LCD_SetCursor(Line,Column);
- for(i=Length;i>0;i--)
- {
- SingleNumber=Number/LCD_Pow(16,i-1)%16;
- if(SingleNumber<10)
- {
- LCD_WriteData('0'+SingleNumber);
- }
- else
- {
- LCD_WriteData('A'+SingleNumber-10);
- }
- }
- }
- /**
- * @brief 在LCD1602指定位置开始以二进制显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
- * @param Length 要显示数字的长度,范围:1~16
- * @retval 无
- */
- void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
- {
- unsigned char i;
- LCD_SetCursor(Line,Column);
- for(i=Length;i>0;i--)
- {
- LCD_WriteData('0'+Number/LCD_Pow(2,i-1)%2);
- }
- }
- #ifndef __LCD1602_H__
- #define __LCD1602_H__
- void LCD_Init(void);
- void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char);
- void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String);
- void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
- void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
- void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
- void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
- #endif
延迟函数的写法!!- #include <REGX52.H>
- void Delay(unsigned int xms)
- {
- unsigned char i, j;
- while(xms--)
- {
- i = 2;
- j = 239;
- do
- {
- while (--j);
- } while (--i);
- }
- }
LED介绍
LED介绍
评论(0)