这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » wenyangzeng ARM DIY进程贴 红外解码

共75条 6/8 |‹ 3 4 5 6 7 8 跳转至
工程师
2012-05-11 20:40:17     打赏
51楼

关键代码
"hw_config.c"
#include "STM32Lib\\stm32f10x.h"
#include "hw_config.h"
#include "STM32Lib\\usblib\\usb_lib.h"
#include "usb_desc.h"
#include "platform_config.h"
#include "usb_pwr.h"

ErrorStatus HSEStartUpStatus;
EXTI_InitTypeDef EXTI_InitStructure;

void Set_System(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  SystemInit();

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  GPIO_AINConfig();

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE);

  /* USB_DISCONNECT used as USB pull-up */
  GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);


  /* Enable Joystick GPIOs clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_JOY_SET1, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_JOY_SET2, ENABLE);

  /* Configure the JoyStick IOs */这里配置IO口的方法又有新意
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIO_UP, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_DOWN;
  GPIO_Init(GPIO_DOWN, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_LEFT;
  GPIO_Init(GPIO_LEFT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_RIGHT;
  GPIO_Init(GPIO_RIGHT, &GPIO_InitStructure);

  /* Enable GPIOB & AFIO clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_KEY | RCC_APB2Periph_AFIO, ENABLE);

  /* Configure the Key pin as input floating */ /* used for remote wakeup */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_KEY;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIO_KEY, &GPIO_InitStructure);
  GPIO_EXTILineConfig(GPIO_KEY_PORTSOURCE, GPIO_KEY_PINSOURCE);  /* Connect EXTI Line9 */
  EXTI_InitStructure.EXTI_Line = GPIO_KEY_EXTI_Line;    /* Configure EXTI Line9 to generate an interrupt on falling edge */
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);      

  /* used for USB resume interrupt */
  EXTI_ClearITPendingBit(EXTI_Line18); 
  EXTI_InitStructure.EXTI_Line = EXTI_Line18;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  EXTI_ClearITPendingBit(GPIO_KEY_EXTI_Line);
}

/*******************************************************************************
* Function Name  : Set_USBClock
* Description    : Configures USB Clock input (48MHz).
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Set_USBClock(void)
{
  /* Select USBCLK source */
  RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  /* Enable USB clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
}

/*******************************************************************************
* Function Name  : GPIO_AINConfig
* Description    : Configures all IOs as AIN to reduce the power consumption.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void GPIO_AINConfig(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable all GPIOs Clock*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALLGPIO, ENABLE);

  /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  GPIO_Init(GPIOE, &GPIO_InitStructure);


  /* Disable all GPIOs Clock*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALLGPIO, DISABLE);
}
/*******************************************************************************
* Function Name  : Enter_LowPowerMode.
* Description    : Power-off system clocks and power while entering suspend mode.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Enter_LowPowerMode(void)
{
  /* Set the device state to suspend */
  bDeviceState = SUSPENDED;

  /* Clear EXTI Line18 pending bit */
  EXTI_ClearITPendingBit(GPIO_KEY_EXTI_Line);

  /* Request to enter STOP mode with regulator in low power mode */
  PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
}

/*******************************************************************************
* Function Name  : Leave_LowPowerMode.
* Description    : Restores system clocks and power while exiting suspend mode.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Leave_LowPowerMode(void)
{
  DEVICE_INFO *pInfo = &Device_Info;

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  /* Enable PLL */
  RCC_PLLCmd(ENABLE);

  /* Wait till PLL is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  {}

  /* Select PLL as system clock source */
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  /* Wait till PLL is used as system clock source */
  while (RCC_GetSYSCLKSource() != 0x08)
  {}

  /* Set the device state to the correct state */
  if (pInfo->Current_Configuration != 0)
  {
    /* Device configured */
    bDeviceState = CONFIGURED;
  }
  else
  {
    bDeviceState = ATTACHED;
  }
}

/*******************************************************************************
* Function Name  : USB_Interrupts_Config.
* Description    : Configures the USB interrupts.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_Interrupts_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* 2 bit for pre-emption priority, 2 bits for subpriority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  /* Enable the USB interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable the USB Wake-up interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable the Key EXTI line Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI_KEY_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
* Function Name  : USB_Cable_Config.
* Description    : Software Connection/Disconnection of USB Cable.
* Input          : NewState: new state.
* Output         : None.
* Return         : None
*******************************************************************************/
void USB_Cable_Config (FunctionalState NewState)
{
  if (NewState == DISABLE)
  {
    GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  }
  else
  {
    GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  }
}

/*******************************************************************************
* Function Name : JoyState.
* Description   : Decodes the Joystick direction.
* Input         : None.
* Output        : None.
* Return value  : The direction value.
*******************************************************************************/
uint8_t JoyState(void)
{
  /* "right" key is pressed */
  if (!GPIO_ReadInputDataBit(GPIO_RIGHT, GPIO_Pin_RIGHT))
  {
    return RIGHT;
  }
  /* "left" key is pressed */
  if (!GPIO_ReadInputDataBit(GPIO_LEFT, GPIO_Pin_LEFT))
  {
    return LEFT;
  }
  /* "up" key is pressed */
  if (!GPIO_ReadInputDataBit(GPIO_UP, GPIO_Pin_UP))
  {
    return UP;
  }
  /* "down" key is pressed */
  if (!GPIO_ReadInputDataBit(GPIO_DOWN, GPIO_Pin_DOWN))
  {
    return DOWN;
  }
  /* No key is pressed */
  else
  {
    return 0;
  }
}

/*******************************************************************************
* Function Name : Joystick_Send.
* Description   : prepares buffer to be sent containing Joystick event infos.
* Input         : Keys: keys received from terminal.
* Output        : None.
* Return value  : None.
*******************************************************************************/
void Joystick_Send(uint8_t Keys)
{
  uint8_t Mouse_Buffer[4] = {0, 0, 0, 0};
  int8_t 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, GetEPTxAddr(ENDP1), 4);
  /* enable endpoint for transmission */
  SetEPTxValid(ENDP1);
}

/*******************************************************************************
* Function Name  : Get_SerialNum.
* Description    : Create the serial number string descriptor.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void Get_SerialNum(void)
{
  uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

  Device_Serial0 = *(uint32_t*)(0x1FFFF7E8);
  Device_Serial1 = *(uint32_t*)(0x1FFFF7EC);
  Device_Serial2 = *(uint32_t*)(0x1FFFF7F0);

  if (Device_Serial0 != 0)
  {
    Joystick_StringSerial[2] = (uint8_t)(Device_Serial0 & 0x000000FF);
    Joystick_StringSerial[4] = (uint8_t)((Device_Serial0 & 0x0000FF00) >> 8);
    Joystick_StringSerial[6] = (uint8_t)((Device_Serial0 & 0x00FF0000) >> 16);
    Joystick_StringSerial[8] = (uint8_t)((Device_Serial0 & 0xFF000000) >> 24);

    Joystick_StringSerial[10] = (uint8_t)(Device_Serial1 & 0x000000FF);
    Joystick_StringSerial[12] = (uint8_t)((Device_Serial1 & 0x0000FF00) >> 8);
    Joystick_StringSerial[14] = (uint8_t)((Device_Serial1 & 0x00FF0000) >> 16);
    Joystick_StringSerial[16] = (uint8_t)((Device_Serial1 & 0xFF000000) >> 24);

    Joystick_StringSerial[18] = (uint8_t)(Device_Serial2 & 0x000000FF);
    Joystick_StringSerial[20] = (uint8_t)((Device_Serial2 & 0x0000FF00) >> 8);
    Joystick_StringSerial[22] = (uint8_t)((Device_Serial2 & 0x00FF0000) >> 16);
    Joystick_StringSerial[24] = (uint8_t)((Device_Serial2 & 0xFF000000) >> 24);
  }
}

"platform_config.h"
#ifndef __PLATFORM_CONFIG_H
#define __PLATFORM_CONFIG_H

   used to run the example */

  #define USB_DISCONNECT                      GPIOA
  #define USB_DISCONNECT_PIN                  GPIO_Pin_8
 
  #define RCC_APB2Periph_GPIO_DISCONNECT      RCC_APB2Periph_GPIOA
  #define RCC_APB2Periph_GPIO_KEY             RCC_APB2Periph_GPIOB

 #define GPIO_Pin_KEY                        GPIO_Pin_11  //按键口在这里定义
 #define GPIO_Pin_UP                         GPIO_Pin_6
  #define GPIO_Pin_DOWN                       GPIO_Pin_7
  #define GPIO_Pin_LEFT                       GPIO_Pin_13 
  #define GPIO_Pin_RIGHT                      GPIO_Pin_15
 

  #define RCC_APB2Periph_GPIO_JOY_SET1        RCC_APB2Periph_GPIOC
  #define RCC_APB2Periph_GPIO_JOY_SET2        RCC_APB2Periph_GPIOB 

   #define GPIO_RIGHT                          GPIOC
  #define GPIO_LEFT                           GPIOC
  #define GPIO_DOWN                           GPIOA
  #define GPIO_UP                             GPIOC
  #define GPIO_KEY                            GPIOB

  #define GPIO_KEY_PORTSOURCE                 GPIO_PortSourceGPIOB
  #define GPIO_KEY_PINSOURCE                  GPIO_PinSource7
  #define GPIO_KEY_EXTI_Line                  EXTI_Line7

  #define EXTI_KEY_IRQChannel                 EXTI9_5_IRQn

  #define RCC_APB2Periph_ALLGPIO             (RCC_APB2Periph_GPIOA \
                                              | RCC_APB2Periph_GPIOB \
                                              | RCC_APB2Periph_GPIOC \
                                              | RCC_APB2Periph_GPIOD \
                                              | RCC_APB2Periph_GPIOE )

#endif /* __PLATFORM_CONFIG_H */


工程师
2012-05-11 20:43:01     打赏
52楼

"hw_config.h"
#ifndef __HW_CONFIG_H
#define __HW_CONFIG_H
#include "STM32Lib\\usblib\\usb_type.h"

#define DOWN            1
#define LEFT            2
#define RIGHT           3
#define UP              4
#define CURSOR_STEP     10  //移动速度
extern void Set_System(void);
extern void Set_USBClock(void);
extern void GPIO_AINConfig(void);
extern void Enter_LowPowerMode(void);
extern void Leave_LowPowerMode(void);
extern void USB_Interrupts_Config(void);
extern void USB_Cable_Config (FunctionalState NewState);
extern void Joystick_Send(uint8_t Keys);
extern uint8_t JoyState(void);
extern void Get_SerialNum(void);

#endif 
“main.c"
#include "STM32Lib\\stm32f10x.h"
#include "STM32Lib\\usblib\\usb_lib.h"
#include "hw_config.h"
#include "usb_pwr.h"

void Delay(__IO uint32_t nCount);

int main(void)
{
  Set_System();

  USB_Interrupts_Config();

  Set_USBClock();

  USB_Init();

  while (1)
  {
    Delay(10000);
    if ((JoyState() != 0) & (bDeviceState == CONFIGURED))
    {
      Joystick_Send(JoyState());
    }
  }
}

void Delay(__IO uint32_t nCount)
{
  for (; nCount != 0;nCount--);
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {}
}
#endif


工程师
2012-05-13 09:24:32     打赏
53楼
楼上过奖了,我只不过是个“抄袭大王”。大家共同交流进步。

工程师
2012-05-13 09:29:39     打赏
54楼

 ARM DIY进程12:USB虚拟串口 
  
      将USB口虚拟成一个串口连接PC机,连接成功后在Windows XP的设备管理器的端口配置里可以看见多出了一个“STMcroelectronics Virtual COM Port(COM10)",见图1。将其属性中的”端口设置“的波特率修改为115200.其他选项不变。见图2.将板上的COM1也连接到PC机的COM1,这时打开2个超级终端,一个打开COM10,一个打开COM1。通信协议都设置相同,现在2个端口通过PC机可以通信了。图3是发送字符串的画面,图4是发送文本文件的画面。这些串行通信的发送和接收都是采用中断方式来进行的。
     USB虚拟设备需要多个USB相关C文件支持,从图5可以看出虚拟串口的程序比虚拟鼠标的程序多出了一个ENDP.C。这是大家在编写程序时应该注意到的。



                                                   图1



                                                   图2


                                                  图3


工程师
2012-05-13 09:32:19     打赏
55楼


                                       图4


                                              图5

工程师
2012-05-13 09:41:21     打赏
56楼

//------------------------
//主函数
//------------------------
int main(void)
{
  Set_System();
  Set_USBClock();
  USB_Interrupts_Config();
  USB_Init();

 
  while(1)  {
    switch (usart_2_usb_process)
    {
      case USART_RECEIVE:
        break;
      case START_USART2USB:
        usart_2_usb_send();
        break;
      case WAIT_USART2USB_END:
        usart_2_usb_waitend();
        break;
      default:
        break;
    }
  }
}

//--------------------
//hw_config.c
//--------------------
#include "stm32f10x_it.h"

#include "stm32lib\\usblib\\usb_lib.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "hw_config.h"
#include "platform_config.h"
#include "usb_pwr.h"
#include "stm32f10x_tim.h"

USART_InitTypeDef USART_InitStructure;

unsigned char buffer_in[BUFFER_SIZE];
unsigned char buffer_out[BUFFER_SIZE];
unsigned char *volatile pbuffer_in_usart;
unsigned char *volatile pbuffer_in_usb;
unsigned char *pbuffer_out_usart;
unsigned int TimeBase;
volatile unsigned short usart_2_usb_count;
volatile bool usart_2_usb_complete;
extern LINE_CODING linecoding;

void Set_System(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Setup the microcontroller system. Initialize the Embedded Flash Interface, 
     initialize the PLL and update the SystemFrequency variable. */
  SystemInit();
 
  /* Enable GPIOA, GPIOD and USART1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1  , ENABLE);
 
  /* Enable TIM2 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
   
  /* Configure USART1 Rx (PA.10) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  /* PA Configure: USB DISCONNECT(PA8) as output */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
  
 
  pbuffer_in_usart = buffer_in;
  pbuffer_in_usb = buffer_in;
 
  pbuffer_out_usart = buffer_out;
 
}

void Set_USBClock(void)
{
  /* USBCLK = PLLCLK / 1.5 */
  RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
  /* Enable USB clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
}

void Enter_LowPowerMode(void)
{
  /* Set the device state to suspend */
  bDeviceState = SUSPENDED;
}

void Leave_LowPowerMode(void)
{
  DEVICE_INFO *pInfo = &Device_Info;

  /* Set the device state to the correct state */
  if (pInfo->Current_Configuration != 0)
  {
    /* Device configured */
    bDeviceState = CONFIGURED;
  }
  else
  {
    bDeviceState = ATTACHED;
  }
}

void USB_Interrupts_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable USART1 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

 

void USB_Cable_Config (FunctionalState NewState)
{
  if (NewState == DISABLE)
  {
    GPIO_ResetBits(GPIOA, GPIO_Pin_8);
  }
  else
  {
    GPIO_SetBits(GPIOA, GPIO_Pin_8);
  }
}

void USART_Config_Default(void)
{
  /* USART1 default configuration */
  USART_InitStructure.USART_BaudRate = 115200;//波特率
  USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_Even;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  /* Configure the USART1 */
  USART_Init(USART1, &USART_InitStructure);

  /* Enable the USART Receive interrupt */
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}

bool USART_Config(void)
{

  /* set the Stop bit*/
  switch (linecoding.format)
  {
    case 0:
      USART_InitStructure.USART_StopBits = USART_StopBits_1;
      break;
    case 1:
      USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
      break;
    case 2:
      USART_InitStructure.USART_StopBits = USART_StopBits_2;
      break;
    default :
    {
      USART_Config_Default();
      return (FALSE);
    }
  }

  /* set the parity bit*/
  switch (linecoding.paritytype)
  {
    case 0:
      USART_InitStructure.USART_Parity = USART_Parity_No;
      break;
    case 1:
      USART_InitStructure.USART_Parity = USART_Parity_Even;
      break;
    case 2:
      USART_InitStructure.USART_Parity = USART_Parity_Odd;
      break;
    default :
    {
      USART_Config_Default();
      return (FALSE);
    }
  }

  /*set the data type : only 8bits and 9bits is supported */
  switch (linecoding.datatype)
  {
    case 0x07:
      /* With this configuration a parity (Even or Odd) should be set */
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      break;
    case 0x08:
      if (USART_InitStructure.USART_Parity == USART_Parity_No)
      {
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      }
      else
      {
        USART_InitStructure.USART_WordLength = USART_WordLength_9b;
      }
     
      break;
    default :
    {
      USART_Config_Default();
      return (FALSE);
    }
  }

  if (linecoding.bitrate < 115200)
    TimeBase = TIMER_ARR_10MS;
  else if (linecoding.bitrate >= 256000)
    TimeBase = TIMER_ARR_1MS;
  else
    TimeBase = TIMER_ARR_5MS;
  Timer_Init(TimeBase);
 
  USART_InitStructure.USART_BaudRate = linecoding.bitrate;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);
  return (TRUE);
}

*******************************************************************************/
void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
{
  uint32_t i;

  for (i = 0; i < Nb_bytes; i++)
  {
    USART_SendData(USART1, *(data_buffer + i));
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  }
}

void USART_2_USB_Send_Data(void)
{
  if (linecoding.datatype == 7)
  {
    *pbuffer_in_usart++ = USART_ReceiveData(USART1) & 0x7F;
  }
  else if (linecoding.datatype == 8)
  {
    *pbuffer_in_usart++ = USART_ReceiveData(USART1);
  }
 
  if (pbuffer_in_usart == &buffer_in[BUFFER_SIZE])
    pbuffer_in_usart = buffer_in;
}

void Get_SerialNum(void)
{
  uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

  Device_Serial0 = *(__IO uint32_t*)(0x1FFFF7E8);
  Device_Serial1 = *(__IO uint32_t*)(0x1FFFF7EC);
  Device_Serial2 = *(__IO uint32_t*)(0x1FFFF7F0);

  if (Device_Serial0 != 0)
  {
    Virtual_Com_Port_StringSerial[2] = (uint8_t)(Device_Serial0 & 0x000000FF);
    Virtual_Com_Port_StringSerial[4] = (uint8_t)((Device_Serial0 & 0x0000FF00) >> 8);
    Virtual_Com_Port_StringSerial[6] = (uint8_t)((Device_Serial0 & 0x00FF0000) >> 16);
    Virtual_Com_Port_StringSerial[8] = (uint8_t)((Device_Serial0 & 0xFF000000) >> 24);

    Virtual_Com_Port_StringSerial[10] = (uint8_t)(Device_Serial1 & 0x000000FF);
    Virtual_Com_Port_StringSerial[12] = (uint8_t)((Device_Serial1 & 0x0000FF00) >> 8);
    Virtual_Com_Port_StringSerial[14] = (uint8_t)((Device_Serial1 & 0x00FF0000) >> 16);
    Virtual_Com_Port_StringSerial[16] = (uint8_t)((Device_Serial1 & 0xFF000000) >> 24);

    Virtual_Com_Port_StringSerial[18] = (uint8_t)(Device_Serial2 & 0x000000FF);
    Virtual_Com_Port_StringSerial[20] = (uint8_t)((Device_Serial2 & 0x0000FF00) >> 8);
    Virtual_Com_Port_StringSerial[22] = (uint8_t)((Device_Serial2 & 0x00FF0000) >> 16);
    Virtual_Com_Port_StringSerial[24] = (uint8_t)((Device_Serial2 & 0xFF000000) >> 24);
  }
}


void Timer_Init(uint16_t Time_ARR)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure; 
 
  TIM_DeInit(TIM2);         /* deinitiate */
 
  TIM_TimeBaseStructure.TIM_Period = Time_ARR;   /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 23;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;   /* Timing Mode :Channel1 */
  TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM2, ENABLE); 

  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);   /* TIM IT enable */

  TIM_Cmd(TIM2, ENABLE);  /* TIM3 enable counter */
}

volatile USB_USART_STATE usart_2_usb_process;
void usart_2_usb_send(void)
{
  unsigned char *ptemp = NULL;
 
  if (pbuffer_in_usb == &buffer_in[BUFFER_SIZE])
    pbuffer_in_usb = buffer_in;
 
  if(pbuffer_in_usb == pbuffer_in_usart) {
    usart_2_usb_process = WAIT_USART2USB_END;        // no data needed to send from USB
    usart_2_usb_complete = TRUE;
    return;
  }
  else if(pbuffer_in_usb > pbuffer_in_usart) {  // if the pbuffer_in_usart is rollback
    usart_2_usb_count = &buffer_in[BUFFER_SIZE] - pbuffer_in_usb;
  }
  else {
    usart_2_usb_count = pbuffer_in_usart - pbuffer_in_usb;
  }
 
  if (usart_2_usb_count > VIRTUAL_COM_PORT_DATA_SIZE){
    ptemp = pbuffer_in_usb;
    UserToPMABufferCopy(ptemp, ENDP1_TXADDR, VIRTUAL_COM_PORT_DATA_SIZE);
    SetEPTxCount(ENDP1, VIRTUAL_COM_PORT_DATA_SIZE);
    usart_2_usb_count -= VIRTUAL_COM_PORT_DATA_SIZE;
    pbuffer_in_usb += VIRTUAL_COM_PORT_DATA_SIZE;
  }
  else {
    ptemp = pbuffer_in_usb;
    UserToPMABufferCopy(ptemp, ENDP1_TXADDR, usart_2_usb_count);
    SetEPTxCount(ENDP1, usart_2_usb_count);
    pbuffer_in_usb += usart_2_usb_count;
    usart_2_usb_count = 0;   
  }
 
  usart_2_usb_process = WAIT_USART2USB_END;
  SetEPTxValid(ENDP1);
}

#define CR1_CEN_Set                 ((uint16_t)0x0001)
void usart_2_usb_waitend(void)
{
  if (usart_2_usb_complete == TRUE){
    usart_2_usb_process = USART_RECEIVE;    
    TIM2->CR1 |= CR1_CEN_Set;       // enable timer
    usart_2_usb_complete = FALSE;
  }
  else {
  }
}


extern __IO uint32_t count_out;
void usb_2_usart_send_data(void)
{
  if (count_out == 0) {
    USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    SetEPRxValid(ENDP3);
  }
  else {
    USART1->DR = *pbuffer_out_usart & (uint16_t)0x01FF;
    pbuffer_out_usart++;
    count_out--;
  }
}


工程师
2012-05-13 09:43:47     打赏
57楼
中断函数
//------------------------------------------------
void USART1_IRQHandler(void)
{
  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    /* Send the received data to the PC Host*/
    USART_2_USB_Send_Data();
  }
 
  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {  
    usb_2_usart_send_data();
  }
}
//------------------------------------------------------
#define CR1_CEN_Reset           ((uint16_t)0x03FE)
#define EGR_UG_Set              ((u16)0x0001)
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
  {
    TIM2->CR1 &= CR1_CEN_Reset;                           // disable timer
    TIM2->SR = (u16)~TIM_FLAG_CC1;                        // clear timer overflow flag
    usart_2_usb_process = START_USART2USB;
  }
}
//----------------------------------------------------
void USB_LP_CAN1_RX0_IRQHandler(void)
{
  USB_Istr();
}

工程师
2012-05-13 22:28:50     打赏
58楼
我从一开始为了体验3V的IO驱动5VLCD,写完代码就一直用下去,况且PCB板设计位置使得彩屏一装,底下很多跳线都动不了。干脆就不用算了。反正不影响程序的调试运行。

工程师
2012-05-13 22:31:09     打赏
59楼
我从一开始为了体验3V的IO驱动5VLCD,写完代码就一直用下去,况且PCB板设计位置使得彩屏一装,底下很多跳线都动不了。干脆就不用算了。反正不影响程序的调试运行。

工程师
2012-05-15 16:27:02     打赏
60楼
ARM DIY进程13:读CUP ID

       下图中小程序读取CPU唯一码,发送到COM1显示。你也试看看。


共75条 6/8 |‹ 3 4 5 6 7 8 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]