我在用STM32F1做一个串口通讯的程序,串口接触摸屏,利用触摸屏上的按钮发送控制信号给32,而32发送给触摸屏的信号是(利用超声波测量的距离值).触摸屏能一直更新距离,但是触摸屏控制32的信号只能控制一次。请问如何解决?(要是我把32发送给触摸屏的程序//掉的好,控制信号能一直传输)
//
#include <stdio.h>
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "beep.h"
#include "nrf24l01.h"
#include "beep.h"
#include "USART.h"
#include "time.h"
extern u8 TIM4CH4_CAPTURE_STA; //êäèë2¶»ñ×′ì¬
extern u16 TIM4CH4_CAPTURE_VAL; //êäèë2¶»ñÖμ
extern void My_NRF(void);
extern void My_TJC(void);
extern void My_HC(void);
u16 t;
u16 len;
u8 tem_TJC[16];
u8 tmp_buf[33];
u32 temp=0;
u32 jl;
int main(void)
{
LED_Init();//LED3õê¼»ˉ
BEEP_Init();//·äÃùÆ÷3õê¼»ˉ
KEY_Init();//°′¼ü3õê¼»ˉ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
uart_init(9600); //′®¿ú3õê¼»ˉÎa9600
delay_init();
NRF24L01_Init(); //3õê¼»ˉNRF24L01
TIM4_Cap_Init(0XFFFF,72-1); //òÔ1MhzμÄÆμÂê¼Æ
while(NRF24L01_Check()) //¼ì2éNRF24L01êÇ·ñÔúλ.
{
// LED0=0;
// delay_ms(1000);
// LED0=1;
delay_ms(1000);
}
NRF24L01_RX_Mode(); //RXÄ£ê½
while(1)
{
My_NRF();
My_TJC();
My_HC();
delay_ms(20);
}
}
void My_TJC()
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;//μÃμ½′Ë′νóêÕμ½μÄêy¾Y3¤¶è
for(t=0;t<len;t++)
{
tem_TJC[t]=USART_RX_BUF[t];
USART_SendData(USART1, USART_RX_BUF[t]);//Ïò′®¿ú1·¢Ëíêy¾Y
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//μè′y·¢Ëí½áê
}
switch(tem_TJC[0])
{
case 'A'
ED0=0;break;
case 'B'
ED0=1;break;
case 'C'
ED1=0;break;
case 'D'
ED1=1;break;
case 'E':BEEP=0;break;
case 'F':BEEP=1;break;
case 'G':BEEP=0;break;
case 'H':BEEP=1;break;
}
USART_RX_STA=0;
delay_ms(10);
}
// else
// {
// My_HC();
// }
}
void My_NRF()
{
if(NRF24L01_RxPacket(tmp_buf)==0)//ò»μ©½óêÕμ½DÅÏ¢,ÔòÏÔê¾3öà′.
{
switch(tmp_buf[1])
{
case '1'
ED0=~LED0;break;
case '2'
ED1=~LED1;break;
case '3':{LED1=~LED1;LED0=~LED0;}break;
case '0':BEEP=~BEEP;break;
case '4'
ED0=~LED0;break;
case '5'
ED1=~LED1;break;
case '6':{LED1=~LED1;LED0=~LED0;}break;
case '7':BEEP=~BEEP;break;
}
tmp_buf[32]=0;//¼óèë×Ö·û′®½áêø·û
}else delay_us(100);
}
void My_HC(void)
{
tri=1;
delay_us(20);
tri=0;
delay_us(20);
if(TIM4CH4_CAPTURE_STA&0X80)//3é1|2¶»ñμ½áËò»′ÎéÏéyÑØ
{
temp=TIM4CH4_CAPTURE_STA&0X3F;
temp*=65536;//òç3öê±¼ä×üoí
temp+=TIM4CH4_CAPTURE_VAL;//μÃμ½×üμĸßμçƽ걼
jl=temp*170/10000;
printf("t3.txt="%d"",jl);
stop();
TIM4CH4_CAPTURE_STA=0;//¿aÆôÏÂò»′Î2¶»ñ
delay_ms(10);
}
}
//usart程序
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); //ê1Äü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; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.9
//USART1_RX GPIOA.103õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.10
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//′®¿ú2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú1
}
void stop(void) //×Ö½ú·¢Ëíoˉêy
{
u8 i;
printf("9");
for(i=0;i<3;i++)
{
USART_SendData(USART1,0xff); //·¢Ëíò»¸ö×Ö½ú
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=SET);//μè′y·¢Ëí½áêø
}
}
void USART1_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART1); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else {USART_RX_STA|=0x8000;}//½óêÕíê3éáË
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
}
}
}
}
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntExit();
#endif
}
#endif
一周热门 更多>