⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vcomdemo.c

📁 lpc2368 usb cdc的测试程序
💻 C
字号:
/*----------------------------------------------------------------------------
 *      Name:    vcomdemo.c
 *      Purpose: USB virtual COM port Demo
 *      Version: V1.00
 *----------------------------------------------------------------------------
 *      This file is part of the uVision/ARM development tools.
 *      This software may only be used under the terms of a valid, current,
 *      end user licence from KEIL for a compatible version of KEIL software
 *      development tools. Nothing else gives you the right to use it.
 *
 *      Copyright (c) 2005-2007 Keil Software.
 *---------------------------------------------------------------------------*/

#include <LPC23xx.H>                                  /* LPC23xx definitions */

#include "type.h"

#include "usb.h"
#include "usbcfg.h"
#include "usbhw.h"
#include "usbcore.h"
#include "cdc.h"
#include "cdcuser.h"
#include "serial.h"
#include "lcd.h"
#include "vcomdemo.h"


#define NO_UART_CABLE		0


buf_t txBuf;
char txData[SER_BUF_SIZE];

buf_t rxBuf;
char rxData[SER_BUF_SIZE];


/*----------------------------------------------------------------------------
 Initialises the VCOM port.
 Call this function before using VCOM_putchar or VCOM_getchar
 *---------------------------------------------------------------------------*/
void VCOM_Init(void) {

  buf_Init(&txBuf, txData);
  buf_Init(&rxBuf, rxData);

  CDC_SerialState = CDC_GetSerialState();
}


/*----------------------------------------------------------------------------
  Writes one character to VCOM port
 *---------------------------------------------------------------------------*/
void VCOM_PutChar(void) {
  char ch;

  if (buf_Get(&rxBuf, &ch) == TRUE) {
#if NO_UART_CABLE
    FIO2SET = ( ch & LEDMSK);
    FIO2CLR = (~ch & LEDMSK);
#else
    ser_PutChar((int)ch);
#endif
  }
}


/*----------------------------------------------------------------------------
  Reads one character from VCOM port
 *---------------------------------------------------------------------------*/
void VCOM_GetChar(void) {
/*
#if NO_UART_CABLE
  #define UNBOUNCE_CNT   5
  static int keyCount = 0, keyPressed = 0;

  if (keyPressed) {
    if (!((FIO2PIN & S2) == 0 )) {             // Check if S2 is not pressed 
      if (keyCount < UNBOUNCE_CNT) keyCount++;
      else {
        keyPressed = 0;
        keyCount = 0;    
      }
    }
  }
  else {
    if (((FIO2PIN & S2) == 0 ))  {				// Check if S2 is pressed 
      if (keyCount < UNBOUNCE_CNT) keyCount++;
      else {
        USB_WriteEP(CDC_DEP_IN, "Int0 pressed\r\n", 14);
        keyPressed = 1;
        keyCount = 0;
      }
    }
  }

#else
*/
  int ch;

  if (ser_AvailChar() == TRUE) {
    ch = ser_GetChar();

    if (buf_Free(&txBuf) > 0) {
//__TEST__
//      buf_Put(&txBuf, (char)ch);
BYTE b = ch;
USB_WriteEP (CDC_DEP_IN, &b, 1); 
//__TEST__

    }
  }
//#endif
}


/*----------------------------------------------------------------------------
  checks the serial state and initiates notification
 *---------------------------------------------------------------------------*/
void VCOM_CheckSerialState (void) {
  WORD serialState;

  serialState = CDC_GetSerialState();
  if (serialState != CDC_SerialState) {
     CDC_SerialState = serialState;
     CDC_NotificationIn();                 /* send SERIAL_STATE notification */
  }
}


/*----------------------------------------------------------------------------
  Main Program
 *---------------------------------------------------------------------------*/
int main (void) {

  PINSEL10 = 0;                             /* Disable ETM interface */
  FIO2DIR  = LEDMSK;                        /* LED's defined as Outputs */
  
  PINSEL9  =0x00000000  ;
  FIO4DIR  =0x10000000      ; 
 // LCD_init();                               /* Initialize LCD display module */
 // LCD_cur_off();
 // LCD_cls();
 // LCD_puts("MCB2300 USB VCOM");
 // LCD_puts("  www.keil.com  ");

  ser_Init();                               /* Serial I/O Initialization */
  VCOM_Init();                              /* VCOM Initialization */

  USB_Init();                               /* USB Initialization */
  USB_Connect(TRUE);                        /* USB Connect */

  while (!USB_Configuration) ;              /* wait until USB is configured */

  while (1) {                               /* Loop forever */
    VCOM_GetChar();
    VCOM_PutChar();
    VCOM_CheckSerialState();

	
    FIO4SET  =0x10000000      ;

  } // end while											   
} // end main ()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -