stc15f2k60s2计算器代码
时间: 2023-05-12 21:01:34 浏览: 213
STC15F2K60S2单片机例程集合
5星 · 资源好评率100%
STC15F2K60S2是一款高性能、低功耗的单片机,适用于各种计算器应用。下面是一个简单的计算器代码:
#include <reg52.h>
sbit Key1 = P1^0; //按键1
sbit Key2 = P1^1; //按键2
sbit Key3 = P1^2; //按键3
sbit Key4 = P1^3; //按键4
sbit Key5 = P1^4; //按键5
sbit Key6 = P1^5; //按键6
sbit Key7 = P1^6; //按键7
sbit Key8 = P1^7; //按键8
sbit LCD_RS = P2^0; //LCD_RS
sbit LCD_RW = P2^1; //LCD_RW
sbit LCD_EN = P2^2; //LCD_EN
sbit LCD_D4 = P2^4; //LCD_D4
sbit LCD_D5 = P2^5; //LCD_D5
sbit LCD_D6 = P2^6; //LCD_D6
sbit LCD_D7 = P2^7; //LCD_D7
void delay_ms(unsigned int time)
{
unsigned int i, j;
for (i=0; i<time; i++)
for (j=0; j<118; j++);
}
void LCD_Config()
{
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
LCD_D4 = 0;
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
delay_ms(15);
LCD_EN = 1;
delay_ms(5);
LCD_EN = 0;
delay_ms(5);
LCD_EN = 1;
delay_ms(5);
LCD_EN = 0;
delay_ms(5);
LCD_EN = 1;
delay_ms(5);
LCD_EN = 0;
delay_ms(5);
LCD_D5 = 1;
delay_ms(5);
LCD_EN = 1;
delay_ms(5);
LCD_EN = 0;
delay_ms(5);
LCD_D5 = 0;
delay_ms(5);
LCD_D4 = 1;
LCD_EN = 1;
delay_ms(5);
LCD_EN = 0;
delay_ms(5);
LCD_D4 = 0;
delay_ms(5);
}
void LCD_WriteCmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
LCD_D4 = cmd >> 4;
LCD_D5 = cmd & 0x0F;
LCD_EN = 1;
delay_ms(5);
LCD_EN = 0;
delay_ms(5);
}
void LCD_WriteData(unsigned char dat)
{
LCD_RS = 1;
LCD_EN = 0;
LCD_RW = 0;
LCD_D4 = dat >> 4;
LCD_D5 = dat & 0x0F;
LCD_EN = 1;
delay_ms(5);
LCD_EN = 0;
delay_ms(5);
}
void LCD_ShowString(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char i;
if (y == 0)
LCD_WriteCmd(0x80 + x);
else
LCD_WriteCmd(0xC0 + x);
for (i=0; str[i]!=0; i++)
LCD_WriteData(str[i]);
}
void main()
{
P1 = 0xFF; //初始化P1为输入模式
LCD_Config(); //初始化LCD
while(1)
{
if (Key1 == 0) //如果按键1按下
{
LCD_ShowString(0, 0, "1 pressed"); //在LCD显示“1 pressed”
while (Key1 == 0); //等待按键1释放
}
if (Key2 == 0) //如果按键2按下
{
LCD_ShowString(0, 0, "2 pressed"); //在LCD显示“2 pressed”
while (Key2 == 0); //等待按键2释放
}
if (Key3 == 0)
{
LCD_ShowString(0, 0, "3 pressed");
while (Key3 == 0);
}
if (Key4 == 0)
{
LCD_ShowString(0, 0, "4 pressed");
while (Key4 == 0);
}
if (Key5 == 0)
{
LCD_ShowString(0, 0, "5 pressed");
while (Key5 == 0);
}
if (Key6 == 0)
{
LCD_ShowString(0, 0, "6 pressed");
while (Key6 == 0);
}
if (Key7 == 0)
{
LCD_ShowString(0, 0, "7 pressed");
while (Key7 == 0);
}
if (Key8 == 0)
{
LCD_ShowString(0, 0, "8 pressed");
while (Key8 == 0);
}
}
}
阅读全文