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

共107条 8/11 |‹ 6 7 8 9 10 11 跳转至
工程师
2012-05-11 20:43:01     打赏
71楼

"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 00:48:51     打赏
72楼
楼主的鼠标真棒啊~~
向楼主学习

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

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

 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     打赏
75楼


                                       图4


                                              图5

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

//------------------------
//主函数
//------------------------
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     打赏
77楼
中断函数
//------------------------------------------------
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 12:02:57     打赏
78楼
学习了~~

楼主好强大啊~~

菜鸟
2012-05-13 20:07:37     打赏
79楼
学习了,做个记号

助工
2012-05-13 22:09:45     打赏
80楼
楼主怎么不用彩屏啊?

共107条 8/11 |‹ 6 7 8 9 10 11 跳转至

回复

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