/*============================================================
LPC21XX 串口功能演示
==============================================================
本例程的CCLK=60M.
晶振采用12M
倍频系数为:5
分频系数为:2
以上设置在Startup.s中设置
*/
#include <LPC213X.H>
#include <Config.H>
#define UART_BAUD(baud) (unsigned int)((FOSC*PLL_M) / (baud * 16))
void Init_Uart0(unsigned int Baud)
{
/* initialize the serial interface */
PINSEL0 = 0x00000005; /* Enable RxD0 and TxD0 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLM=(unsigned char)(Baud>>8);
U0DLL = (unsigned char)Baud;
U0LCR = 0x03; /* DLAB = 0 */
}
void delay (unsigned int i) { /* Delay function */
unsigned int n;
while(i>1)
{
for(n=65535;n>1;n--);
i--;
}
}
void Sent_Byte(unsigned char data)
{
U0THR = data; // 发送数据
while( (U0LSR&0x40)==0 ); // 等待数据发送完毕
}
void Sent_Str(unsigned char const *str)
{ while(1)
{ if( *str == '\0' ) break;
Sent_Byte(*str++); // 发送数据
}
}
//10K的热敏电阻0-50度所对应的码表值
const unsigned int Temp_code[]=
{
// 0-25
2843,2718,2599,2486,2379,2277,2180,2087,1999,1916,1836,1760,1688,1619,1553,1490,1430,1373,1319,1266,1217,1169,1124,1081,1039,1000,
// 26--50
962,925,891,857,826,795,766,738,711,686,661,638,615,593,573,553,534,515,497,480,464,449,433,419,405,
// 51-75
// 392,379,366,355,343,332,321,311,301,292,283,274,265,257,249,242,234,227,220,214,208,201,195,190,184,
// 76--100
// 179,174,169,164,159,155,150,146,142,138,134,131,127,124,120,117,114,111,108,105,102,100,97,94,92
};
/***********************************************************************
//查电阻值所对应的温度是多少
***********************************************************************/
unsigned char check_code(unsigned int k)
{
unsigned char i;
if(k<405)return(51); //大于50度以上都显示为51度
for(i=0;k<Temp_code[i];i++);
return (i-1);
}
/***********************************************************************
//R=30K或20K
//RT=10K
***********************************************************************/
unsigned int Totemp(unsigned int AD)
{
unsigned int Itemp;
unsigned int Tk;
Tk=1023-AD;
Itemp=(unsigned int)(((unsigned long)1000*AD)/Tk); //扩大100倍
return(Itemp);
}
unsigned char Get_Temp(unsigned int i)
{
return (check_code(Totemp(i))); //转换温度
}
void main(void)
{ unsigned char temp;
unsigned int adata;
AD0CR = 0x00200402; /* Setup A/D: 10-bit AIN0 @ 3MHz */
PINSEL1 = 0x01000000; /* enable DAC */
Init_Uart0(UART_BAUD(115200));
for(;;)
{ AD0CR |=0x01000000; /*START*/
Sent_Str("www.armdiy.com \n") ;
while(!(AD0DR&0x80000000));
adata=(AD0DR>>6)&0x000003ff;
temp=Get_Temp(adata);
Sent_Byte(temp/10+0x30);
Sent_Byte(temp%10+0x30);
Sent_Str("\n");
delay(200);
}
}