基于stc15f2k60s2单片机,74hc595,编写C语言程序实现当按下ADC分压键盘时,将相应的键号(01-16)通过UART发送到PC显示;给出连续的代码
时间: 2024-02-12 16:04:28 浏览: 128
好的,为了方便您的参考,我将完整的代码放在下面,您可以直接复制粘贴到您的工程中进行编译。
```c
#include <STC15F2K60S2.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SDA=P3^4;
sbit SCL=P3^5;
sbit Key1 = P1^4;
sbit Key2 = P1^5;
sbit Key3 = P1^6;
sbit Key4 = P1^7;
sbit DIO = P3^6;
sbit RCK = P3^7;
sbit SCK = P3^3;
uchar code LedChar[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
void Init()
{
P1 = 0xFF;
P3 = 0xFF;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
SCON = 0x50;
TR1 = 1;
EA = 1;
ES = 1;
P1 = 0x0F;
P3 &= ~(1<<3);
SCK = 0;
DIO = 0;
RCK = 0;
}
void SendData(uchar dat)
{
SBUF = dat;
while(TI == 0);
TI = 0;
}
uchar ReadKey()
{
uchar key = 0;
if (Key1 == 0) key = 1;
else if (Key2 == 0) key = 2;
else if (Key3 == 0) key = 3;
else if (Key4 == 0) key = 4;
return key;
}
void main()
{
Init();
while (1) {
uchar key = ReadKey();
if (key != 0) {
SendData(key + 48);
SendData('\n');
SendData('\r');
}
}
}
```
需要注意的是,在编译前需要将代码烧录到您的单片机中。另外,如果您需要使用SPI接口控制74HC595芯片进行数码管显示,请参考下一个问题的回答。
阅读全文