uartchm

#ifndef __USART_H
#define __USART_H
#include “stdio.h”
#include “sys.h”
//
//±¾³ÌÐòÖ»¹©Ñ§Ï°Ê¹Óã¬Î´¾­×÷ÕßÐí¿É£¬²»µÃÓÃÓÚÆäËüÈκÎÓÃ;
//ALIENTEK STM32¿ª·¢°å
//´®¿Ú1³õʼ»¯
//ÕýµãÔ­×Ó@ALIENTEK
//¼¼ÊõÂÛ̳:www.openedv.com
//ÐÞ¸ÄÈÕÆÚ:2012/8/18
//°æ±¾£ºV1.5
//°æÈ¨ËùÓУ¬µÁ°æ±Ø¾¿¡£
//Copyright© ¹ãÖÝÊÐÐÇÒíµç×ӿƼ¼ÓÐÏÞ¹«Ë¾ 2009-2019
//All rights reserved
//********************************************************************************
//V1.3ÐÞ¸Ä˵Ã÷
//Ö§³ÖÊÊÓ¦²»Í¬ÆµÂÊϵĴ®¿Ú²¨ÌØÂÊÉèÖÃ.
//¼ÓÈëÁ˶ÔprintfµÄÖ§³Ö
//Ôö¼ÓÁË´®¿Ú½ÓÊÕÃüÁÄÜ.
//ÐÞÕýÁËprintfµÚÒ»¸ö×Ö·û¶ªÊ§µÄbug
//V1.4ÐÞ¸Ä˵Ã÷
//1,Ð޸Ĵ®¿Ú³õʼ»¯IOµÄbug
//2,ÐÞ¸ÄÁËUSART_RX_STA,ʹµÃ´®¿Ú×î´ó½ÓÊÕ×Ö½ÚÊýΪ2µÄ14´Î·½
//3,Ôö¼ÓÁËUSART_REC_LEN,ÓÃÓÚ¶¨Òå´®¿Ú×î´óÔÊÐí½ÓÊÕµÄ×Ö½ÚÊý(²»´óÓÚ2µÄ14´Î·½)
//4,ÐÞ¸ÄÁËEN_USART1_RXµÄʹÄÜ·½Ê½
//V1.5ÐÞ¸Ä˵Ã÷
//1,Ôö¼ÓÁ˶ÔUCOSIIµÄÖ§³Ö
#define USART_REC_LEN 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200
#define EN_USART1_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ

#define RecSta1 1
#define RecSta2 2
#define RecSta3 3
#define RecSta4 4
#define RecSta5 5
#define KBUart1 6
#define KBVersion 7

extern int Uart1_TxNum;
extern int Uart1_RxNum;
extern int Uart1_TxLen;
extern int Uart1_RxSta;
extern u8 Uart1_Buffer[16];

extern u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû
extern u16 USART_RX_STA; //½ÓÊÕ״̬±ê¼Ç

extern int ReceiveFlag;
extern int LedChannel;
extern int LedState;
extern int ReceiveState;
extern int UartSendTime;
extern int KBUartPIDAck;
extern int KBUartVIDAck;

//Èç¹ûÏë´®¿ÚÖжϽÓÊÕ£¬Ç벻ҪעÊÍÒÔϺ궨Òå
void uart_init(u32 bound);
void Uart1_Send_Byte(u8 Data);
void Uart1_Send_PVID(int Order);
void UartTimeDelay(int ms);

#endif

#include “sys.h”
#include “usart.h”
#include “stm32f10x_usart.h”
#include “LedControl.h”
//
//Èç¹ûʹÓÃucos,Ôò°üÀ¨ÏÂÃæµÄÍ·Îļþ¼´¿É.
#if SYSTEM_SUPPORT_OS
#include “includes.h” //ucos ʹÓÃ
#endif

//
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý
struct __FILE
{
int handle;

};

FILE __stdout;
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ
void _sys_exit(int x)
{
x = x;
}
//ÖØ¶¨Òåfputcº¯Êý
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
USART1->DR = (u8) ch;
return ch;
}
#endif

/ʹÓÃmicroLibµÄ·½·¨/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}	

return ch;

}
int GetKey (void) {

while (!(USART1->SR & USART_FLAG_RXNE));

return ((int)(USART1->DR & 0x1FF));

}
*/

//#if EN_USART1_RX //Èç¹ûʹÄÜÁ˽ÓÊÕ
//´®¿Ú1ÖжϷþÎñ³ÌÐò
//×¢Òâ,¶ÁÈ¡USARTx->SRÄܱÜÃâĪÃûÆäÃîµÄ´íÎó
u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
//½ÓÊÕ״̬
//bit15£¬ ½ÓÊÕÍê³É±êÖ¾
//bit14£¬ ½ÓÊÕµ½0x0d
//bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿
u16 USART_RX_STA=0; //½ÓÊÕ״̬±ê¼Ç

void uart_init(u32 bound){
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//ʹÄÜUSART1£¬GPIOAʱÖÓ

//USART1_TX   GPIOA.9

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9

//USART1_RX GPIOA.10³õʼ»¯
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10

//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷

//USART ³õʼ»¯ÉèÖÃ

USART_InitStructure.USART_BaudRate = bound;//´®¿Ú²¨ÌØÂÊ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎÞÆæÅ¼Ð£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//ÊÕ·¢Ä£Ê½

USART_Init(USART1, &USART_InitStructure); //³õʼ»¯´®¿Ú1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆô´®¿Ú½ÓÊÜÖжÏ
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1
// USART_ITConfig(USART1, USART_IT_TC, ENABLE);//Tramsimssion Completeºó£¬²Å²úÉúÖжϣ¬¿ªTCÖжϱØÐë·ÅÓÚ´Ë£¬·ñÔò»¹ÊǻᶪʧµÚÒ»×Ö½Ú
}

void Uart1_Send_PVID(int Order)
{
u8 SendData[4];
int i;
SendData[0] = 0xFE;
SendData[1] = 0xEF;
switch(Order)
{
case KBUart1: SendData[0] = 0xFE;
SendData[1] = 0xEF;
SendData[2] = 0x7F;
SendData[3] = 0x68;
for(i= 0;i < 4;i++)
{
Uart1_Send_Byte(SendData[i]);
UartTimeDelay(2);
}
break;

	case KBVersion:           
                                    SendData[0] = 0x02;
                                    SendData[1] = 0x27; 
									SendData[2] = 0xB1;
									SendData[3] = 0x02;
									for(i= 0;i < 4;i++)
									{
										Uart1_Send_Byte(SendData[i]);
										UartTimeDelay(2);
									}
									break;
 }

}

void Uart1_Send_Byte(u8 Data)
{

// USART_ClearFlag(USART1, USART_FLAG_TC); //·¢ËÍÍê³É±ê־룬·¢ËÍÍêµÚÒ»¸ö×Ö½Úºó²Å½øÈëÖжϣ¬ÐèÒªÏÈÇå³ý£¬·ñÔò¿ÉÄܻᶪʧµÚÒ»¸ö×Ö½ÚÊý¾Ý

USART_SendData(USART1,(u8)Data);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);   

// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
// if( USART_GetITStatus(USART1, USART_IT_TXE) == SET )
// {
// if( Data == ‘\0’ )
// {
// USART_ClearFlag(USART1, USART_FLAG_TC);
// }
// else
// USART_SendData(USART1,(u8)Data);
// }
// USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

// while( Data != ‘\0’ )
// {
// while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
// USART_SendData(USART1,(u8)Data);
// }

}

void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò
{
int Data;

// int Uart1_Tx=0;
// int Uart1_Rx=0;
// int Uart1_Sta=0;
// u8 Uart1_Buffer[4]; //ÓÉÓÚ´®¿Úÿ´ÎÖ»½ÓÊÕ1¸ö×Ö½Ú£¬Ã¿À´Ò»¸ö×Ö½Ú½øÒ»´ÎÖжϣ¬ËùÒÔÒªÏë¶à´Î½ÓÊÕ£¬buffer¼°Ïà¹Ø±äÁ¿¶¨Òå Ðë·Åµ½ÖжϺ¯ÊýÍâÃæ

// printf("%s\r\n",“enter int”);

// if (USART_GetFlagStatus(USART1, USART_FLAG_PE) != RESET)
// {
// USART_ReceiveData(USART1);
// USART_ClearFlag(USART1, USART_FLAG_PE);
// }
//
// if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
// {
// USART_ReceiveData(USART1);
// USART_ClearFlag(USART1, USART_FLAG_ORE);
// }
// if (USART_GetFlagStatus(USART1, USART_FLAG_FE) != RESET)
// {
// USART_ReceiveData(USART1);
// USART_ClearFlag(USART1, USART_FLAG_FE);
// }

 if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET) 
 { 

// USART_ClearITPendingBit(USART1,USART_IT_RXNE);
Uart1_Buffer[Uart1_RxNum] = USART_ReceiveData(USART1);
// printf("%x\r\n", Uart1_Buffer[Uart1_RxNum]);
Uart1_RxNum++;
Uart1_RxNum &= 0x0F; //±£³ÖUart1_Rx¼ÆÊýÖµ£¬Êµ¼Ê×î´óÖµ²»³¬¹ý16£¬Êý×ébuffer×î´óÐòºÅ16-1=15
}

  if( Uart1_Buffer[Uart1_RxNum-1]==0xEF)               //ÓÉÓÚ´®¿Úû½ÓÊÕ1¸ö×Ö½Ú½øÒ»´ÎÖжϣ¬ËùÒÔµÚÒ»´Î½ÓÊÕµ½Ö¡Í·¼´¿ÉÉ趨TxµÄÆðʼÐòºÅ
  {
        Uart1_TxNum = Uart1_RxNum-1;                      //TxÆðʼÐòºÅÒÔ½ÓÊÕµ½Ö¡Í·Ê±Rx-1ֵΪ׼£¬²»Ò»¶¨¾ÍÊÇ0,¶øÇÒÖ»½ÓÊÕµ½Ö¡Í·²Å¸³Öµ
  }
 
  if( Uart1_Buffer[Uart1_TxNum]==0xEF&&Uart1_Buffer[Uart1_RxNum-1]==0xFE) //¼ì²âµ½Ö¡Í·ÏÂÓмì²âµ½Ö¡Î²£¬Ôò³¤¶È¿ÉµÃ£ºÎª²îÖµ
  {
      Uart1_TxLen=  Uart1_RxNum-1-Uart1_TxNum;
      Uart1_RxSta=1;                                          //¼ì²âµ½Ö¡Î²ºó½«±ê־λÖÃ1,ÒÔ½øÈë·¢ËÍ
  }
  
  if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) == SET) 
  { 
       USART_ClearFlag(USART1,USART_FLAG_ORE); 
       USART_ReceiveData(USART1); 
  }
  if(Uart1_RxSta) 
    {         
         for(Uart1_TxNum= Uart1_RxNum-1-Uart1_TxLen;Uart1_TxNum < Uart1_RxNum;Uart1_TxNum++)
          {
               Uart1_Send_Byte(Uart1_Buffer[Uart1_TxNum]);         
          }
          Uart1_TxNum=0;
          Uart1_RxNum=0;
          Uart1_RxSta=0;
    }

//***************************** ͨ¹ýcaseÓï¾ä(ÀàËÆ×´Ì¬»ú£¬Ðë×¢Òâ²»ÄÜ©µôÒÔÍâ״̬£¬¿ÉÄÜÐèÒª¸´Î»)£¬Ò»¸öÒ»¸ö×Ö½Ú½ÓÊÕÅжϣ¬×îºóÔÙÒ»Æð·¢ËÍÊÇOKµÄ

// if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½ÓÊÕÖжÏ
// {

USART_ClearFlag(USART1, USART_FLAG_RXNE);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
printf("%s\r\n",“Enter_receive”);
//
// Data =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
// switch(ReceiveState)
// {
Data =USART_ReceiveData(USART1); //¶ÁÈ¡½ÓÊÕµ½µÄÊý¾Ý
// case RecSta1:
// if(Data == 0xFE)
// {
// ReceiveState = RecSta2;
Uart1_Send_Byte(0xEF);
printf("%s\r\n",“EF”);
// }
// else
// ReceiveState = RecSta1;
// break;
// case RecSta2:
// if(Data == 0xEF)
// {
// ReceiveState = RecSta3;
Uart1_Send_Byte(0xFE);
printf("%s\r\n",“FE”);
// }
// else
// ReceiveState = RecSta1;
// break;
// case RecSta3:
// if(Data == 0x7F)
// {
printf("%s\r\n",“7F”);
Uart1_Send_Byte(0x7F);
// ReceiveState = RecSta5;
// }
// else
// {
// LedChannel = Data;
// ReceiveState = RecSta4;
// }
// break;
// case RecSta4:
// ReceiveFlag = 1;
// LedState = Data;
// ReceiveState = RecSta1;
//
// break;
// case RecSta5:
// if(Data == 0x8F)
// {
KBUartPIDAck=1;
printf("%s\r\n",“8F”);
Uart1_Send_PVID(KBUart1);
// Uart1_Send_Byte(0xFE);
// Uart1_Send_Byte(0xEF);
// Uart1_Send_Byte(0x7F);
// Uart1_Send_Byte(0x68);
//
// }
// else if(Data == 0xF8)
// {
KBUartVIDAck=1;
printf("%s\r\n",“F8”);
Uart1_Send_PVID(KBVersion);
// Uart1_Send_Byte(0x02);
// Uart1_Send_Byte(0x27);
// Uart1_Send_Byte(0xB1);
// Uart1_Send_Byte(0x02);
// }
//
// ReceiveState = RecSta1; //·¢ËÍÍê4×Ö½ÚÒԺ󣬱ØÐë»Øµ½sta1,·ñÔòºóÐøÎÞ·¨½øÈë½ÓÊÕ
// break;
//
// default:
// ReceiveState = RecSta1;
// break;
// }

// }

      LedConfigure.X86_ControlLed();	//X86µãµÆ

// if(KBUartPIDAck1)
// {
// KBUartPIDAck=0;
printf("%s\r\n",“EFFE7F68”);
Uart1_Send_PVID(KBUart1);
// Uart1_Send_Byte(0xEF);
// Uart1_Send_Byte(0xFE);
// Uart1_Send_Byte(0x7F);
// Uart1_Send_Byte(0x68);
// }
// if(KBUartVIDAck
1)
// {
// KBUartVIDAck=0;
printf("%s\r\n",“0223B100”);
Uart1_Send_PVID(KBVersion);
// Uart1_Send_Byte(0x02);
// Uart1_Send_Byte(0x25);
// Uart1_Send_Byte(0xB1);
// Uart1_Send_Byte(0x01);
// }

}

void UartTimeDelay(int ms)
{
UartSendTime = 0;
while(UartSendTime < ms);
}

//#endif

/******************** © COPYRIGHT 2017 PRETTA ********************

  • File Name : main.c
  • Author : Messi
  • Version : V0.0.1
  • Date : 011/11/2017
  • Description : USB Keyboard main file

Includes ------------------------------------------------------------------*/
#include “my_usb.h”
#include “gpio.h”
#include “keyboard.h”
#include “PowerSequence.h”
#include “stdio.h”
#include “TgcConfig.h”
#include “TimerConfig.h”
#include “sys.h”
#include “KnobConfig.h”
#include “LedControl.h”
#include “usart.h”
#include “stm32f10x_usart.h”

int LastSendData = 0;
int SendEna = 0;
int LastSendTimeCnt = 0;

int MultiKeyBuffer[16];
int OldScanCode[CombKeyNum];
int CurScanCode[CombKeyNum];
int TimeCnt = 0;
int TgcTimeCnt = 0;
int Tgc1sCnt = 0;
int KnobTimeCnt = 0;
int KeyboardTimeCnt = 0;
int UsbSendTimeCnt = 0;
int LedControlCnt = 0;
int KeyState = Idel;
int ShiftData = 0;
int UartSendTime = 0;

int KnobState1 = KnobWait;
int KnobState2 = KnobWait;
int KnobState3 = KnobWait;
int KnobState4 = KnobWait;
int KnobState5 = KnobWait;
int KnobState6 = KnobWait;
int KnobState7 = KnobWait;
int KnobState8 = KnobWait;

int THI_State = 0;
int Freeze_State = 0;
int B_State = 0;
int M_State = 0;
int CFM_State = 0;
int PW_State = 0;
int CW_State = 0;

int THI_Led = 0;
int Freeze_Led = 0;
int B_Led = 0 ;
int M_Led = 0;
int CFM_Led = 0;
int PW_Led = 0;
int CW_Led = 0;

int ReceiveFlag = 0;
int LedChannel = 0;
int LedState = 0;
int ReceiveState = RecSta1;
int KBUartPIDAck = 0;
int KBUartVIDAck = 0;

int Uart1_TxNum=0;
int Uart1_RxNum=0;
int Uart1_TxLen=0;
int Uart1_RxSta=0;
u8 Uart1_Buffer[16];

uint8_t TgcOldData_0 = 0;
uint8_t TgcOldData_1 = 0;
uint8_t TgcOldData_2 = 0;
uint8_t TgcOldData_3 = 0;
uint8_t TgcOldData_4 = 0;
uint8_t TgcOldData_5 = 0;
uint8_t TgcOldData_6 = 0;
uint8_t TgcOldData_7 = 0;

int main(void)
{
UsbInitial(); //USB³õʼ»¯
TgcConfigure.TgcAdcInitial(); //TGC³õʼ»¯
GpioInitial(); //¼üÅÌGPIO³õʼ»¯
TimerConfigure.TimerInitial(); //¶¨Ê±Æ÷³õʼ»¯
LedConfigure.LedInitial();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
// LedConfigure.LedInitial(); //X86µãµÆ
while(1)
{

	KeyboardDrive.KeySendToUsb();		//¼ì²â°´¼ü²¢Í¨¹ýUSB·¢ËÍ
	TgcConfigure.TgcSendUsb();			//TGC¼ì²â²¢Í¨¹ýUSB·¢ËÍ
	KnobConfigure.KnobSendUsb();		//ÐýÅ¥¼ì²â²¢Í¨¹ýUSB·¢ËÍ
    uart_init(115200);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值