PORT_Init (void)
{
XBR0 = 0x01; // Enable UART0
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x10; // Set TX pin to push-pull ???
P2MDOUT |= 0x08; // enable LED as a push-pull output
P2MDOUT |= 0x04; // ÉèÖÃLED1£¨¼ì²â£©¿ØÖƹܽÅΪÍÆÍìÉÏÀ­
P2MDOUT |= 0x02; // ÉèÖÃLED2£¨×Լ죩¿ØÖƹܽÅΪÍÆÍìÉÏÀ­
P1MDIN &= 0x7F; // set P1.7 as an analog input, for ADC0
P1SKIP |= 0x80; // skip P1.7 pin
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at 100uS
// intervals. Timer 2 overflow automatically triggers ADC0 conversion.
//
//-----------------------------------------------------------------------------
void Timer2_Init (void)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK as timebase, 16-bit
// auto-reload
CKCON = 0x30; // select SYSCLK for timer 2 source
TMR2RL = - (SYSCLK / 10000); // init reload value for 10uS ???
TMR2 = 0xffff; // set to reload immediately
TR2 = 0; // stop Timer2
}
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configures ADC0 to make single-ended analog measurements on pin P1.7
//
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x03; // ADC0 disabled, normal tracking,
// conversion triggered on TMR2 overflow
// right-justify result
REF0CN = 0x13; // Enable on-chip VREF = 2.2v and buffer
/*
P1MDIN &= 0xFB; // set P1.2 as an analog input, VREF input
P1SKIP |= 0x04; // skip P1.2 pin
REF0CN = 0x02; // ÍⲿVREF×÷Ϊ²Î¿¼µçѹ
*/
ADC0MX = 0x0f; // Set P1.7 as positive input
// ADC0MX = 0x18; // ζȴ«¸ÐÆ÷ÊäÈë
ADC0CF = ((SYSCLK/3000000)-1)<<3; // set SAR clock to 3MHz
ADC0CF |= 0x00; // only 1 times ADC
EIE1 |= 0x08; // enable ADC0 conversion complete interrupt.
AD0EN = 1; // enable ADC0
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits£¬¹Ø±Õ½ÓÊÕ¼°·¢ËÍÖжÏ
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x08; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else if (SYSCLK/BAUDRATE/2/256 < 48) {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
} else {
while (1); // Error. Unsupported baud rate
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
//
// This ISR averages 2048 samples then prints the result to the terminal. The
// ISR is called after each ADC conversion which is triggered by Timer2.
//
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 10
{
AD0INT = 0; // clear ADC0 conv. complete flag
// if(ADC0==0x0fff) return; // È¥³ýÒì³£µã
accumulator += ADC0;
measurements--;
if(measurements == 0)
{
measurements = 2048;
result = accumulator / 2048;
accumulator = 0;
// The 12-bit ADC value is averaged across 2048 measurements.
// The measured voltage applied to P1.7 is then:
//
// Vref (mV)
// measurement (mV) = --------------- * result (bits)
// (2^12)-1 (bits)
myAD0.mV = result * 2200 / 4095; // ¼ÆËãƽ¾ùÖµ »ù×¼µçѹ2.2V
// myAD0.mV = ADC0 * 2200 / 4095;
// myAD0.mV = result;
// vTemp = (100*(myAD0.mV-900))/295.0; // ת»»³É»ªÊÏζÈ
// vTemp = (5*vTemp-160)/9.0; // ת»»³ÉÉãÊÏζÈ
// myAD0.mV = (ulong)vTemp;
//SendCOM0Byte(myAD0.ch[0]); // ·¢Ë͵½´®¿Ú0
//SendCOM0Byte(myAD0.ch[1]);
SendCOM0Byte(myTemp.ch[0]); // ·¢ÉúζÈÊý¾Ý0
SendCOM0Byte(myTemp.ch[1]); // ·¢ËÍζÈÊý¾Ý1
SendCOM0Byte(myAD0.ch[2]);
SendCOM0Byte(myAD0.ch[3]);
// printf("P1.1 voltage: %ld mV\n",mV);
}
}
//-----------------------------------------------------------------------------
// SendCOM0Byte : Send COM0 a Byte Servive Routines
//-----------------------------------------------------------------------------
//
// Return value : None
// Parameter : byte to be send -- ch
//-----------------------------------------------------------------------------
void SendCOM0Byte(uchar ch)
{
// ES0 = 0; // ¹Ø±Õ´®¿ÚÖжÏ
TI0 = 0; // ÇåÁã´®¿Ú·¢ËÍÍê³É±êÖ¾
SBUF0 = ch; // ·¢ËÍ´®¿ÚÊý¾Ý
while(TI0==0); // µÈ´ý´®¿Ú·¢ËÍÍê³É
TI0 = 0; // ÇåÁã´®¿Ú·¢ËÍÍê³É±êÖ¾
// ES0 = 0; // ÔÊÐí´®¿ÚÖжÏ
}
//-----------------------------------------------------------------------------
// ÑÓʱ×Ó³ÌÐò
//-----------------------------------------------------------------------------
//
// ·µ»ØÖµ£ºÎÞ
// ²Î Êý£ºus ÎÞ·ûºÅ×Ö½Ú
//-----------------------------------------------------------------------------
void TempDelay (uchar us)
{
while(us--);
}
//-----------------------------------------------------------------------------
// ÑÓʱ³ÌÐò
//-----------------------------------------------------------------------------
//
// |