下边是这一部分的代码
#include "stm32f10x.h" // Device header
#include "OLED.h"
uint32_t CoOrdinates[11];//stm32接收到的数据
int x1,y1,x2,y2,x3,y3,x4,y4;
void Serival_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //??USART??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //??GPIO??
GPIO_InitTypeDef GPIO_InitStructure;
//RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure; //?????
USART_InitStructure.USART_BaudRate = 115200; //???
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //?????
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //????,???????
USART_InitStructure.USART_Parity = USART_Parity_No; //???
USART_InitStructure.USART_StopBits = USART_StopBits_1; //???,??1????
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //??
USART_Init(USART1, &USART_InitStructure); //???
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //??RXNE????NVIC???
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //??
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE); //??USART
}
void K210_Data(void)//??????K210???
{
x1 = CoOrdinates[2];
y1 = CoOrdinates[3];
x2 = CoOrdinates[4];
y2 = CoOrdinates[5];
x3 = CoOrdinates[6];
y3 = CoOrdinates[7];
x4 = CoOrdinates[8];
y4 = CoOrdinates[9];
}
void Receive_K210_Data(int16_t data)//??K210??????
{
static u8 state = 0;
uint8_t i;
if(state == 0 && data == 0xaa)//?????
{
state = 1;
CoOrdinates[0] = data;
}
else if(state == 1 && data == 0xab)//?????
{
state = 2;
CoOrdinates[1] = data;
}
else if(state == 2)//???????
{
for(i = 2; i < 10; i++)
{
CoOrdinates[i] = data;
}
state=3;
}
else if(state == 3) //???????????,??????
{
if(data == 0xff)
{
state = 0;
CoOrdinates[10] = data;
}
else if(data != 0xff)
{
state = 0;
for(i = 0; i < 11; i++)
{
CoOrdinates[i] = data;
}
}
}
else
{
state = 0;
for(i = 0; i < 11; i++)
{
CoOrdinates[i] = 0x00;
}
}
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
uint8_t com_data;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
USART_ClearFlag(USART1,USART_FLAG_RXNE);
com_data = USART1->DR;
Receive_K210_Data(com_data);
K210_Data();
}
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntExit();
#endif
}
|