新手上路
积分 30
金钱 30
注册时间 2019-3-12
在线时间 5 小时
1 金钱
#include "STC12C5628AD.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L
#define BAUD 4800
sbit bit9 = P2^2;
bit busy;
bit dat;
sbit led1=P2^2;
sbit led2=P2^3;
sbit led3=P3^0;
sbit KEY1 = P1^4;
sbit KEY2 = P1^5;
sbit KEY3 = P2^7;
sbit KEY4 = P3^7;
sbit KEY5 = P2^5;
sbit KEY6 = P2^4;
sbit KEY7 = P1^6;
sbit KEY8 = P1^0;
sbit KEY9 = P1^7;
sbit KEY10 = P1^1;
sbit KEY11 = P2^0;
sbit KEY12 = P1^2;
sbit KEY13 = P2^1;
sbit KEY14 = P1^3;
sbit KEY15 = P3^5;
sbit KEY16 = P3^4;
sbit KEY17 = P3^3;
sbit KEY18 = P2^6;
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT EVEN_PARITY //Testing even parity
void DelayMs(unsigned int xms);
void Uartinit(void);
//void Delay200ms();
void Delay20ms();
void Delay50ms();
void SendData( BYTE dat);
void DelayMs(unsigned int xms) //消抖延时程序
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void Uartinit(void) //4800bps@11.0592MHz //串口初始化
{
#ifdef MODE1T
AUXR = 0x80;
#endif
// TMOD = 0x01;
// TL0 = T1MS;
// TH0 = T1MS >> 8;
// TR0 = 1;
// ET0 = 1;
EA = 1;
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50;
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xDA;
#elif (SPACE_PARITY)
SCON = 0xD2;
#endif
TMOD = 0x20;
TH1 = TL1 = -(FOSC/12/32/BAUD);
TR1 = 1;
ES = 1;
EA =1;
}
void Uart_Isr() interrupt 4
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
P0 = SBUF; //P0 show UART data
bit9 = RB8; //P2.2 show parity bit
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}
BYTE key_scan() //独立按键扫描
{
if( KEY1==0)
{
DelayMs(10);
if(KEY1==0)
{
led2=~led2;
// return 1;
}
while(!KEY1);
}
if( KEY2==0)
{
DelayMs(10);
if(KEY2==0)
{
led2=0;
Delay20ms();
led2=1;
return 2;
}
}
if( KEY3==0)
{
DelayMs(10);
if(KEY3==0)
{
led2=0;
Delay20ms();
led2=1;
return 3;
}
}
if( KEY4==0)
{
DelayMs(10);
if(KEY4==0)
{
led2=0;
Delay20ms();
led2=1;
return 4;
}
}
if( KEY5==0)
{
DelayMs(10);
if(KEY5==0)
{
led2=0;
Delay20ms();
led2=1;
return 5;
}
}
if( KEY6==0)
{
DelayMs(10);
if(KEY6==0)
{
led2=0;
Delay20ms();
led2=1;
return 6;
}
}
if( KEY7==0)
{
DelayMs(10);
if(KEY7==0)
{
led2=0;
Delay20ms();
led2=1;
return 7;
}
}
if( KEY8==0)
{
DelayMs(10);
if(KEY8==0)
{
led2=0;
Delay20ms();
led2=1;
return 8;
}
}
if( KEY9==0)
{
DelayMs(10);
if(KEY9==0)
{
led2=0;
Delay20ms();
led2=1;
return 9;
}
}
if( KEY10==0)
{
DelayMs(10);
if(KEY10==0)
{
led2=0;
Delay20ms();
led2=1;
return 10;
}
}
if( KEY11==0)
{
DelayMs(10);
if(KEY11==0)
{
led2=0;
Delay20ms();
led2=1;
return 11;
}
}
if( KEY12==0)
{
DelayMs(10);
if(KEY12==0)
{
led2=0;
Delay20ms();
led2=1;
return 12;
}
}
if( KEY13==0)
{
DelayMs(10);
if(KEY13==0)
{
led2=0;
Delay20ms();
led2=1;
return 13;
}
}
if( KEY14==0)
{
DelayMs(10);
if(KEY14==0)
{
led2=0;
Delay20ms();
led2=1;
return 14;
}
}
if( KEY14==0)
{
DelayMs(10);
if(KEY14==0)
{
led2=0;
Delay20ms();
led2=1;
return 14;
}
}
if( KEY15==0)
{
DelayMs(10);
if(KEY15==0)
{
led2=0;
Delay20ms();
led2=1;
return 15;
}
}
if( KEY16==0)
{
DelayMs(10);
if(KEY16==0)
{
led2=0;
Delay20ms();
led2=1;
return 16;
}
}
if( KEY17==0)
{
DelayMs(10);
if(KEY17==0)
{
led2=0;
Delay20ms();
led2=1;
return 17;
}
}
if( KEY18==0)
{
DelayMs(10);
if(KEY18==0)
{
led2=0;
Delay20ms();
led2=1;
return 18;
}
}
return 0;
}
void main()
{
unsigned int keycode;
KEY1 = 1;
KEY2 = 1;
KEY3 = 1;
KEY4 = 1;
KEY5 = 1;
KEY6 = 1;
KEY7 = 1;
KEY8 = 1;
KEY9 = 1;
KEY10 = 1;
KEY11 = 1;
KEY12 = 1;
KEY13 = 1;
KEY14 = 1;
KEY15 = 1;
KEY16 = 1;
KEY17 = 1;
KEY18 = 1;
P1M1=0xFF;
P1M0=0xFF;
P2M1=0xFF;
P2M0=0xFF;
keycode=key_scan;
Uartinit();
Delay50ms();
key_scan();
DelayMs(10);
while(1)
{
// led1=0;
// Delay50ms();
// led1=1;
// Delay50ms();
// key_scan();
SendData( 0xAA);
DelayMs(3);
SendData( 0xAB);
DelayMs(3);
// DelayMs(10);
/* switch(keycode)
{
case 1:
SendData(0xA5);
SendData(0x01);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0xAB);
break;
case 2:
SendData(0xA5);
SendData(0x02);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0xA8);
break;
case 3:
SendData(0xA5);
SendData(0x04);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0xAE);
break;
case 4:
SendData(0xA5);
SendData(0x08);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0xA2);
break;
case 5:
SendData(0xA5);
SendData(0x10);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0xBA);
break;
case 6:
SendData(0xA5);
SendData(0x20);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0x8A);
break;
case 7:
SendData(0xA5);
SendData(0x40);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0xEA);
break;
case 8:
SendData(0xA5);
SendData(0x80);
SendData(0x00);
SendData(0x00);
SendData(0x0F);
SendData(0x2A);
break;
case 9:
SendData(0xA5);
SendData(0x00);
SendData(0x01);
SendData(0x00);
SendData(0x0F);
SendData(0xAB);
break;
case 10:
SendData(0xA5);
SendData(0x00);
SendData(0x02);
SendData(0x00);
SendData(0x0F);
SendData(0xAB);
break;
case 11:
SendData(0xA5);
SendData(0x00);
SendData(0x04);
SendData(0x00);
SendData(0x0F);
SendData(0xAE);
break;
case 12:
SendData(0xA5);
SendData(0x00);
SendData(0x08);
SendData(0x00);
SendData(0x0F);
SendData(0xA2);
break;
case 13:
SendData(0xA5);
SendData(0x00);
SendData(0x10);
SendData(0x00);
SendData(0x0F);
SendData(0xBA);
break;
case 14:
SendData(0xA5);
SendData(0x00);
SendData(0x20);
SendData(0x00);
SendData(0x0F);
SendData(0x8A);
break;
case 15:
SendData(0xA5);
SendData(0x00);
SendData(0x40);
SendData(0x00);
SendData(0x0F);
SendData(0xEA);
break;
case 16:
SendData(0xA5);
SendData(0x00);
SendData(0x80);
SendData(0x00);
SendData(0x0F);
SendData(0x2A);
break;
case 17:
SendData(0xA5);
SendData(0x00);
SendData(0x00);
SendData(0x01);
SendData(0x0F);
SendData(0xAB);
break;
case 18:
SendData(0xA5);
SendData(0x00);
SendData(0x00);
SendData(0x02);
SendData(0x0F);
SendData(0xA8);
break;
default: break;
}*/
}
}
void Delay20ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 1;
j = 216;
k = 35;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay50ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 3;
j = 26;
k = 223;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay200ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 9;
j = 104;
k = 139;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
我来回答