MMA7260的STC12C5410AD單片機(jī)程序
#include "STC12C5410AD.H"
sbit MMA7260sleep=P1^0; //MMA7260休眠與否,0-休眠;1-正常工作
//---------------------------------------
//名稱: 延時(shí)約1毫秒函數(shù)
//-----------------------------------------
void delay1ms()
{
word k;
for(k=0;k<12000;k++);
}
void delayms(word ms)
{
word k,j;
for(j=0;j
for(k=0;k<12000;k++);
}
//---------------------------------------
//名稱: 串口數(shù)據(jù)發(fā)送函數(shù)
//-----------------------------------------
void uart_putchar(byte ch)
{
TI=0;
if (ch == )
{
SBUF= 0x0d; //outputCR
while(!TI);
return;
}
SBUF=ch;
while(!TI);
}
//---------------------------------------
//名稱: 串口數(shù)據(jù)接收函數(shù)
//-----------------------------------------
byte uart_getchar(void)
{
if(RI)
{
RI=0;
return SBUF; //有數(shù)據(jù)接收到,返回1
}
else
{
return 0; //無數(shù)據(jù)接收到,返回0
}
}
void putstr(char ch[])
{
byte ptr=0;
while(ch[ptr])
{
uart_putchar((byte)ch[ptr++]);
}
}
//---------------------------------------
//名稱: 模數(shù)轉(zhuǎn)換函數(shù)(8位)
//-----------------------------------------
byte ReadAD(byte ch)
{
ADC_DATA = 0; //清A/D轉(zhuǎn)換結(jié)果寄存器
ADC_CONTR = 0xF8|ch; //0000,1000ADCS = 1,啟動(dòng)轉(zhuǎn)換
delay1ms();
do{;}
while((ADC_CONTR&0x10)==0); //0001,0000等待A/D轉(zhuǎn)換結(jié)束
ADC_CONTR = ADC_CONTR&0xE7; //1110,0111清ADC_FLAG位,停止A/D轉(zhuǎn)換
return ADC_DATA;
}
/***************************************************
把0--255的數(shù)值轉(zhuǎn)化為3位字符串格式
****************************************************/
void Byte2Str3(char zifu[],byte val,byte StartPtr)
{
char characters[11]="0123456789";
byte tv=0;
tv=val/100;
zifu[StartPtr++] = characters[tv];
tv=(val%100)/10;
zifu[StartPtr++] = characters[tv];
tv=val%10;
zifu[StartPtr] = characters[tv];
//zifu[3] =