|
|
发表于 2021-1-9 17:27:11
|
显示全部楼层
//STC15W408AS 11.0592MHz QQ:1149268555
#include<reg52.h>
#include<intrins.h>
#define U8 unsigned char
#define U16 unsigned int
#define U32 unsigned long
sfr P1M0 = 0x92;
sfr P1M1 = 0x91;
sfr P2M0 = 0x96;
sfr P2M1 = 0x95;
sfr P3M0 = 0xb2;
sfr P3M1 = 0xb1;
sfr P5 = 0xC8;
sfr ADC_CONTR = 0xBC;
sfr ADC_RES = 0xBD;
sfr ADC_LOW2 = 0xBE;
sfr P1ASF = 0x9D;
sfr AUXR = 0x8e;
sfr T2H = 0xD6;
sfr T2L = 0xD7;
sbit led = P3^2;
sbit OUT1 = P1^2;
sbit OUT2 = P1^3;
sbit OUT3 = P1^4;
sbit OUT4 = P1^5;
bit flagTxd =0;
U8 cntRxd=0;
U8 flagRxd=0;
U8 pdata bufRxd[64];
U8 pdata Data[64];
U8 num;
U8 ch=2;
U8 ms;
void Delay05ms()//@11.0592MHz 0.5
{
unsigned char i, j;
i = 6;
j = 100;
do
{
while (--j);
} while (--i);
}
void Delay(U16 t)
{do
{ Delay05ms();
} while (--t);
}
void Delay4us()//@11.0592MHz
{
unsigned char i;
i = 14;
while (--i);
}
void delays(U8 i)
{
while(i--)
Delay4us();
}
U8 GetADC(U8 ch)
{
ADC_CONTR=0x80|0x00|ch|0x08;
_nop_();
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR&0x10));
ADC_CONTR&=~0x10;
return ADC_RES;
}
void InitADC()
{
P1ASF=0x02;
ADC_RES=0;
ADC_CONTR=0x80|0x00;
Delay(100);
}
void UartInit(void)
{
SCON = 0x50;
AUXR |= 0x01;
AUXR |= 0x04;
T2L = 0xe8;
T2H = 0xFf;
AUXR |= 0x10;
ES = 1;
EA = 1;
}
void Timer0Init(void)//2ms @11.0592MHz
{
IP=0x10;
ET0=1;
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x66;
TH0 = 0x7e;
TF0 = 0;
TR0 = 1;
}
void UartWrite(U8 *buf, U8 len)
{
while (len--)
{
flagTxd = 0;
SBUF = *buf++;
while (!flagTxd);
}
Delay(1);
}
main()
{
InitADC();
Timer0Init();
UartInit();
while(1)
{
Data[0]=GetADC(1);
if(flagRxd==1)
{
Delay05ms();
led=~led;
Data[1]=bufRxd[1];
Data[2]=bufRxd[2];
Data[3]=bufRxd[3];
UartWrite(Data,1);
flagRxd=0;
cntRxd=0;
}
}
}
void Time0()interrupt 1
{
ms++;
if(ms==12)ms=0;
switch(ms)
{
case 0:OUT1=1,Delay05ms(),delays(255-Data[0]),OUT1=0;break;
case 1:OUT2=1,Delay05ms(),delays(Data[0]),OUT2=0;break;
case 2:OUT3=1,Delay05ms(),delays(Data[0]),OUT3=0;break;
case 3:OUT4=1,Delay05ms(),delays(Data[1]),OUT4=0;break;
}
}
void InterruptUART() interrupt 4
{
if (RI)
{
RI = 0;
flagRxd=1;
if (cntRxd < sizeof(bufRxd))
{
bufRxd[cntRxd++] = SBUF;
}
}
if (TI)
{
TI = 0;
flagTxd = 1;
}
}
|
| |