uart.c
- /*---------------------------------------------------------------------*/
- /* --- STC MCU Limited ------------------------------------------------*/
- /* --- STC 1T Series MCU Demo Programme -------------------------------*/
- /* --- Mobile: (86)13922805190 ----------------------------------------*/
- /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
- /* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
- /* --- Web: www.STCAI.com ---------------------------------------------*/
- /* --- BBS: www.STCAIMCU.com -----------------------------------------*/
- /* --- QQ: 800003751 -------------------------------------------------*/
- /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
- /*---------------------------------------------------------------------*/
-
- #include "stc.h"
- #include "uart.h"
- #include "usb.h"
- #include "util.h"
- #include "usb_req_class.h"
-
- BOOL UartBusy;
-
- void uart_init()
- {
- P_SW1 |= 0x40;
- P3M0 = 0x00; // P3.0,P3.1设置为准双向模式
- P3M1 = 0x00;
-
- AUXR |= 0x14; // 定时器2时钟1T模式,开始计时
- IE2 |= 1; // 允许中断
- SCON = 0x50; // 8位数据,可变波特率
- AUXR |= 0x01; // 串口1选择定时器2为波特率发生器
- AUXR |= 0x04; // 定时器时钟1T模式
- T2L = BR(115200);
- T2H = BR(115200) >> 8;
- AUXR |= 0x10; // 定时器2开始计时
-
- LineCoding.dwDTERate = REV4(115200);
- LineCoding.bCharFormat = 0;
- LineCoding.bParityType = 0;
- LineCoding.bDataBits = 8;
- }
-
- void uart1_isr() interrupt 4
- {
- if (TI) // 检测串口1发送中断
- {
- TI = 0; // 清除串口1发送中断请求位
- UartBusy = 0;
- }
- if (RI) // 检测串口1接收中断
- {
- RI = 0; // 清除串口1接收中断请求位
- TxBuffer[TxWptr++] = S2BUF;
- }
- }
-
- void uart_set_parity(BYTE parity)
- {
- switch (parity)
- {
- case NONE_PARITY:
- SCON = 0x50;
- break;
- case ODD_PARITY:
- case EVEN_PARITY:
- case MARK_PARITY:
- SCON = 0xda;
- break;
- case SPACE_PARITY:
- SCON = 0xd2;
- break;
- }
- }
-
- void uart_set_baud(DWORD baud)
- {
- WORD temp;
-
- temp = (WORD)BR(baud);
- T2L = temp;
- T2H = temp >> 8;
- }
-
- void uart_polling()
- {
- BYTE dat;
- BYTE cnt;
-
- if (DeviceState != DEVSTATE_CONFIGURED)
- return;
-
- if (!UsbInBusy && (TxRptr != TxWptr))
- {
- IE2 &= ~0x80; // EUSB = 0;
- UsbInBusy = 1;
- usb_write_reg(INDEX, 1);
- cnt = 0;
- while (TxRptr != TxWptr)
- {
- usb_write_reg(FIFO1, TxBuffer[TxRptr++]);
- cnt++;
- if (cnt == EP1IN_SIZE)
- break;
- }
- usb_write_reg(INCSR1, INIPRDY);
- IE2 |= 0x80; // EUSB = 1;
- }
-
- if (!UartBusy && (RxRptr != RxWptr))
- {
- dat = RxBuffer[RxRptr++];
- UartBusy = 1;
- switch (LineCoding.bParityType)
- {
- case NONE_PARITY:
- case SPACE_PARITY:
- SCON &= ~0x08; // STB8 = 0;
- break;
- case ODD_PARITY:
- ACC = dat;
- if (P)
- SCON &= ~0x08; // STB8 = !P;
- else
- SCON |= 0x08;
- break;
- case EVEN_PARITY:
- ACC = dat;
- if (P)
- SCON |= 0x08; // STB8 = P;
- else
- SCON &= ~0x08;
- break;
- case MARK_PARITY:
- SCON |= 0x08; // STB8 = 1;
- break;
- }
- SBUF = dat;
-
- while (UartBusy)
- ;
- }
-
- if (UsbOutBusy)
- {
- IE2 &= ~0x80; // EUSB = 0;
- if (RxWptr - RxRptr < 256 - EP1OUT_SIZE)
- {
- UsbOutBusy = 0;
- usb_write_reg(INDEX, 1);
- usb_write_reg(OUTCSR1, 0);
- }
- IE2 |= 0x80; // EUSB = 1;
- }
- }
复制代码
|