初始化串口中止:
void InitCOM_Interrupt(void)
{
#if 1
SCON = 0x50; //8bit
TMOD = 0x20; //timer1 mode 2
PCON = 0x00; //SMOD=1, double baud
TL1 = 0xFD;
TH1 = 0xFD; //Baud = 9600, 11.0592MHz
//IE |= 0x90; //Serail break enable
TR1 = 1; //timer1 start
ES = 1;
EA = 1; //disable interrupt
#else
SCON = = 0x50;
TH2 = 0XFF;
TL2 = 0xFD //baud = 115200, 11.0592MHz
RCAP2H = 0xFF;
RCAP2L = 0xFD; //
TCLK = 1;
RCLK = 1;
C-T2 = 0;
TR2 = 1;
#endif
}
中止函数
void UART_Interrupt(void) interrupt 4
{
char temp;
COMWrite(“Interrupt\n”);
if(RI)
{
RI=0;
temp=SBUF;
COMPutc(temp);
}
}
悉数贴上
/******************************************
FCT applicaiton
Flex-B15-TSD
First:20110916
Modify: 20111217
Remark: modify the COM data format. Get the data with \n.
******************************************/
#include
#include
#include
#include
#include
#define LOW 0
#define HIGH 1
#define ACTIVE 0x00
#define IDLE 0x01
sfr P1_ADC_EN = 0x97;
sfr ADC_CONTR = 0xC5;
sfr ADC_DATA = 0xC6;
void InitPort()
{
//P0 = 0x00;
//P3 = 0X00;
P2 = 0xFF;//Out controller P2=0x00
P1 = 0XFF;//input
}
void Delay(unsigned char cnt)
{
while(cnt > 0)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
cnt–;
}
}
void Delay_Loop(unsigned int loop)
{
unsigned int i = 0;
for(i=0; i
Delay(0xA0);
}
/****************************
ADC function: voltage = 256*Vin/Vcc
****************************/
unsigned char ADConvert(unsigned char channel)
{
ADC_DATA = 0;
P1_ADC_EN = (0x01 << channel);
ADC_CONTR = channel;
Delay(1);
ADC_CONTR |= 0x08; //set ADC_START =1,
while(!(ADC_CONTR &0x10)); //wait until ADC_FLAG =1
ADC_CONTR &= 0xF7;
return ADC_DATA; //data = 256*Vin/Vcc (Vcc is the vlitage of MCU)
}
void InitCOM(void)
{
#if 1
SCON = 0x50; //8bit
TMOD = 0x20; //timer1 mode 2
PCON = 0x00; //SMOD=1, double baud
TL1 = 0xFD;
TH1 = 0xFD; //Baud = 9600, 11.0592MHz
//IE |= 0x90; //Serail break enable
TR1 = 1; //timer1 start
ES = 0;
EA = 0; //disable interrupt
#else
SCON = = 0x50;
TH2 = 0XFF;
TL2 = 0xFD //baud = 115200, 11.0592MHz
RCAP2H = 0xFF;
RCAP2L = 0xFD; //
TCLK = 1;
RCLK = 1;
C-T2 = 0;
TR2 = 1;
#endif
}
void InitCOM_Interrupt(void)
{
#if 1
SCON = 0x50; //8bit
TMOD = 0x20; //timer1 mode 2
PCON = 0x00; //SMOD=1, double baud
TL1 = 0xFD;
TH1 = 0xFD; //Baud = 9600, 11.0592MHz
//IE |= 0x90; //Serail break enable
TR1 = 1; //timer1 start
ES = 1;
EA = 1; //disable interrupt
#else
SCON = = 0x50;
TH2 = 0XFF;
TL2 = 0xFD //baud = 115200, 11.0592MHz
RCAP2H = 0xFF;
RCAP2L = 0xFD; //
TCLK = 1;
RCLK = 1;
C-T2 = 0;
TR2 = 1;
#endif
}
/*****************************
Send data
*****************************/
void COMPutc(char cdata)
{
SBUF = cdata;
while(!TI);
TI = 0;
}
void COMWrite(char* str)
{
while(*str)
COMPutc(*str++);
}
void COMWriteNum(int num, char* str)
{
char d[32];
sprintf(d, “%s:%d\n”, str, num);
COMWrite(d);
}
/********************************
Read Data
********************************/
char COMGetc(void)
{
char temp;
while(!RI);
temp = SBUF;
RI = 0;
return temp;
}
void COMRead(char* str, unsigned char length)
{
unsigned char i = 0;
for(i = 0; i < length; i++)
{
str[i] = COMGetc();
}
}
void COMReadEnter(char* str)
{
char temp;
do
{
//Delay(1);
//COMWrite(“\ndata\n”);
temp = COMGetc();
*str=temp;
//COMPutc(temp);
str++;
}while(temp!=\n && temp!=\r);
*str = \0;
}
bit COMReadTimeout(char* str, unsigned char length, unsigned int timeout)
{
unsigned char i = 0;
unsigned int count = 0;
for(i = 0; i < length; i++)
{
count = 0;
while(RI == 0)
{
count++;
Delay(2);
if (count > timeout) return 1;
}
str[i] = SBUF;
}
return 0;
}
bit COMReadEnterTimeout(char* str, unsigned int timeout)
{
unsigned int count;
char temp;
do
{
count =0;
while(!RI)
{
count++;
_nop_();
_nop_();
if(count>timeout) return 1;
}
temp = SBUF;
*str = temp;
str++;
RI=0;
}while(temp!=\n && temp!=\r);
*str=\0;
return 0;
}
void UART_Interrupt(void) interrupt 4
{
char temp;
COMWrite(“Interrupt\n”);
if(RI)
{
RI=0;
temp=SBUF;
COMPutc(temp);
}
}
void main()
{
InitPort();
InitCOM_Interrupt();
while(1)
{
}//end of(while(1))
}//end of main
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/jieda/274484.html