8951串口中断
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) { coun
8951串口中 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)