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

📄 main.c

📁 STR912的usb例程
💻 C
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 2/6/2006
* Description        : Mouse Demo main program body
********************************************************************************
* History:
* 2/6/2006 : V0.1
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include "91x_lib.h"
#include "USB_lib.h"
#include "USB_conf.h"
#include "USB_prop.h"
#include "USB_pwr.h"
#include "USB_mem.h"

#include "LCD.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* ujoystick direction */
#define DOWN	1
#define LEFT	2
#define RIGHT	3
#define UP      4

#define CURSOR_STEP     10
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
const char disp_ln1[] = "STR91x DEMO";
const char disp_ln2[] = "USB Mouse";

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : JoyState
* Description   : Decodes the Joystick direction
* Output        : None
* Return value  : the direction value
*******************************************************************************/
u8 JoyState(void)
{
  u8 port;
  port =((GPIO_Read(GPIO9)&0x20)<<1)|(GPIO_Read(GPIO9)&0x80)|(GPIO_Read(GPIO6)&0x10)|(GPIO_Read(GPIO6)&0x20);
  if ((port&0xF0)==0xB0) return(UP);
  if ((port&0xF0)==0x70) return(DOWN);
  if ((port&0xF0)==0xE0) return(LEFT);
  if ((port&0xF0)==0xD0) return(RIGHT);
  else return(0);
}

/*******************************************************************************
* Function Name : Mouse_Send
* Description   : prepares buffer to be sent containing mouse event infos
* Input         : Keys: keys received from terminal
* Output        : None
* Return value  : None
*******************************************************************************/
void Mouse_Send(BYTE Keys)
{
  BYTE Mouse_Buffer[4] = {0,0,0,0};
  char X = 0, Y = 0;

  switch (Keys)
  {
	case LEFT:
	  X -= CURSOR_STEP;
	  break;
	case RIGHT:

	  X += CURSOR_STEP;
	  break;
	case UP:
	  Y -= CURSOR_STEP;
	  break;
	case DOWN:
	  Y += CURSOR_STEP;
	  break;
	default:
	  return;
  }

  /* prepare buffer to send */
  Mouse_Buffer[1] = X;
  Mouse_Buffer[2] = Y;
  /*copy mouse position info in ENDP1 Tx Packet Memory Area*/
  UserToPMABufferCopy(&Mouse_Buffer[0],ENDP1_TXADDR,4);
  /* enable endpoint for transmission */
  SetEPTxValid(ENDP1);

} /* Mouse_Send */


int main()
{
  u32 i ;

  #ifdef DEBUG
  debug();
  #endif


  SCU_MCLKSourceConfig(SCU_MCLK_OSC);
  /*configure PLL factors for 48MHz*/
  SCU_PLLFactorsConfig(0xC0,0x19,0x3);
  /*Enable PLL*/
  SCU_PLLCmd(ENABLE);
  while(!(SCU->SYSSTATUS&SCU_FLAG_LOCK));
  /*PLL as MCLK clock*/
  SCU_MCLKSourceConfig(SCU_MCLK_PLL);
  /*Enable VIC clock*/
  SCU_AHBPeriphClockConfig(__VIC,ENABLE);
  SCU_AHBPeriphReset(__VIC,DISABLE);
  /*USB clock = MCLK= 48MHz*/
  SCU_USBCLKConfig(SCU_USBCLK_MCLK);
  /*Enable USB clock*/
  SCU_AHBPeriphClockConfig(__USB,ENABLE);
  SCU_AHBPeriphReset(__USB,DISABLE);

  SetCNTR(0);
  SetISTR(0);
  SCU_AHBPeriphClockConfig(__USB48M,ENABLE);
  SCU_APBPeriphClockConfig(__GPIO9,ENABLE);
  SCU_APBPeriphReset(__GPIO9,DISABLE);
  SCU_APBPeriphClockConfig(__GPIO6,ENABLE);
  SCU_APBPeriphReset(__GPIO6,DISABLE);
  SCU_APBPeriphClockConfig(__GPIO2,ENABLE);
  SCU_APBPeriphReset(__GPIO2,DISABLE);
  SCU_APBPeriphClockConfig(__GPIO8,ENABLE);
  SCU_APBPeriphReset(__GPIO8,DISABLE);
  
  GPIO_DeInit(GPIO2);
  GPIO_DeInit(GPIO6);
  GPIO_DeInit(GPIO8);
  
  

  /*Configure GPIO9*/
  GPIO_DeInit(GPIO9);
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_Init (GPIO9, &GPIO_InitStructure);

  /*Configure GPIO6*/
//  GPIO_DeInit(GPIO6);
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_Init (GPIO6, &GPIO_InitStructure);
  
  /*Configure P6.7,pin 93*/
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
  GPIO_Init (GPIO6, &GPIO_InitStructure);


  /*LCD Display -----------------------------------------------------------------*/ 
  LCD_Init();
  LCD_Clr();
  
  LCD_SetCursor(0x02);
  for(i=0;disp_ln1[i]!='\0';i++)
  	LCD_DISP_CHAR(disp_ln1[i]);
  	
  LCD_SetCursor(0x42);
  for(i=0;disp_ln2[i]!='\0';i++)
  	LCD_DISP_CHAR(disp_ln2[i]);


  /* Enable and configure the priority of the USB_LP IRQ Channel*/
  VIC_DeInit();
  VIC_Config(USBLP_ITLine, VIC_IRQ, 0);
  VIC_ITCmd(USBLP_ITLine, ENABLE);

//  GPIO_WriteBit(GPIO6,GPIO_Pin_7,1);

  /* USB  initialization */
  GPIO_WriteBit(GPIO6,GPIO_Pin_7,0);
  wInterrupt_Mask = IMR_MSK;
  USB_Init();



  while(1)
  {
  	for(i=0;i<10000;i++);
  	if (JoyState()!=0)
  	Mouse_Send(JoyState());
  }

}
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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