main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
int main(void)
{
u8 len;
u16 times=0;
HAL_Init();
Stm32_Clock_Init(336,8,2,7);
delay_init(168);
uart_init(9600);
LED_Init();
KEY_Init();
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;
printf("
Äú·¢Ë͵ÄÏûϢΪ:
");
HAL_UART_Transmit(&UART2_Handler,(uint8_t*)USART_RX_BUF,len,1000);
while(__HAL_UART_GET_FLAG(&UART2_Handler,UART_FLAG_TC)!=SET);
printf("
");//²åÈë»»ÐÐ
USART_RX_STA=0;
}else
{
times++;
if(times%5000==0)
{
printf("
ALIENTEK °¢²¨ÂÞSTM32F429¿ª·¢°å ´®¿ÚʵÑé
");
printf("ÕýµãÔ­×Ó@ALIENTEK
");
}
if(times%200==0)printf("ÇëÊäÈëÊý¾Ý,ÒԻسµ¼ü½áÊø
");
if(times%30==0)LED0=!LED0;//ÉÁ˸LED,ÌáʾϵͳÕýÔÚÔËÐÐ.
delay_ms(10);
}
}
}
usart.c
#include "usart.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//Èç¹ûʹÓÃos,Ôò°üÀ¨ÏÂÃæµÄÍ·Îļþ¼´¿É.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //os ʹÓÃ
#endif
//////////////////////////////////////////////////////////////////////////////////
//±¾³ÌÐòÖ»¹©Ñ§Ï°Ê¹Óã¬Î´¾­×÷ÕßÐí¿É£¬²»µÃÓÃÓÚÆäËüÈκÎÓÃ;
//ALIENTEK STM32F429¿ª·¢°å
//´®¿Ú2³õʼ»¯
//ÕýµãÔ­×Ó@ALIENTEK
//¼¼ÊõÂÛ̳:
www.openedv.com
//ÐÞ¸ÄÈÕÆÚ:2015/9/7
//°æ±¾£ºV1.5
//°æȨËùÓУ¬µÁ°æ±Ø¾¿¡£
//Copyright(C) ¹ãÖÝÊÐÐÇÒíµç×ӿƼ¼ÓÐÏÞ¹«Ë¾ 2009-2019
//All rights reserved
//********************************************************************************
//V1.0ÐÞ¸Ä˵Ã÷
//////////////////////////////////////////////////////////////////////////////////
//¼ÓÈëÒÔÏ´úÂë,Ö§³Öprintfº¯Êý,¶ø²»ÐèҪѡÔñuse MicroLIB
//#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#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((USART2->SR&0X40)==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
USART2->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART2_RX
u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.
//½ÓÊÕ״̬
//bit15£¬ ½ÓÊÕÍê³É±êÖ¾
//bit14£¬ ½ÓÊÕµ½0x0d
//bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿
u16 USART_RX_STA=0; //½ÓÊÕ״̬±ê¼Ç
u8 aRxBuffer[RXBUFFERSIZE];//HAL¿âʹÓõĴ®¿Ú½ÓÊÕ»º³å
UART_HandleTypeDef UART2_Handler; //UART¾ä±ú
//³õʼ»¯IO ´®¿Ú2
//bound:²¨ÌØÂÊ
void uart_init(u32 bound)
{
//UART ³õʼ»¯ÉèÖÃ
UART2_Handler.Instance=USART2; //USART2
UART2_Handler.Init.BaudRate=bound; //²¨ÌØÂÊ
UART2_Handler.Init.WordLength=UART_WORDLENGTH_8B; //×Ö³¤Îª8λÊý¾Ý¸ñʽ
UART2_Handler.Init.StopBits=UART_STOPBITS_2; //Ò»¸öֹͣλ
UART2_Handler.Init.Parity=UART_PARITY_NONE; //ÎÞÆæżУÑéλ
UART2_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //ÎÞÓ²¼þÁ÷¿Ø
UART2_Handler.Init.Mode=UART_MODE_TX_RX; //ÊÕ·¢Ä£Ê½
HAL_UART_Init(&UART2_Handler); //HAL_UART_Init()»áʹÄÜUART2
HAL_UART_Receive_IT(&UART2_Handler, (u8 *)aRxBuffer, RXBUFFERSIZE);//¸Ãº¯Êý»á¿ªÆô½ÓÊÕÖжϣº±ê־λUART_IT_RXNE£¬²¢ÇÒÉèÖýÓÊÕ»º³åÒÔ¼°½ÓÊÕ»º³å½ÓÊÕ×î´óÊý¾ÝÁ¿
}
//UARTµ×²ã³õʼ»¯£¬Ê±ÖÓʹÄÜ£¬Òý½ÅÅäÖã¬ÖжÏÅäÖÃ
//´Ëº¯Êý»á±»HAL_UART_Init()µ÷ÓÃ
//huart:´®¿Ú¾ä±ú
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
//GPIO¶Ë¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_Initure;
if(huart->Instance==USART2)//Èç¹ûÊÇ´®¿Ú2£¬½øÐд®¿Ú2 MSP³õʼ»¯
{
__HAL_RCC_GPIOA_CLK_ENABLE(); //ʹÄÜGPIOAʱÖÓ
__HAL_RCC_USART2_CLK_ENABLE(); //ʹÄÜUSART2ʱÖÓ
GPIO_Initure.Pin=GPIO_PIN_2; //PA2
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Initure.Pull=GPIO_PULLUP; //ÉÏÀ­
GPIO_Initure.Speed=GPIO_SPEED_FAST; //¸ßËÙ
GPIO_Initure.Alternate=GPIO_AF7_USART2; //¸´ÓÃΪUSART2
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //³õʼ»¯
A9
GPIO_Initure.Pin=GPIO_PIN_3; //PA10
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //³õʼ»¯
A3
#if EN_USART2_RX
HAL_NVIC_EnableIRQ(USART2_IRQn); //ʹÄÜUSART1ÖжÏͨµÀ
HAL_NVIC_SetPriority(USART2_IRQn,3,3); //ÇÀÕ¼ÓÅÏȼ¶3£¬×ÓÓÅÏȼ¶3
#endif
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance==USART2)//Èç¹ûÊÇ´®¿Ú2
{
if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
{
if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
{
if(aRxBuffer[0]!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØпªÊ¼
else USART_RX_STA|=0x8000; //½ÓÊÕÍê³ÉÁË
}
else //»¹Ã»ÊÕµ½0X0D
{
if(aRxBuffer[0]==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=aRxBuffer[0] ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½ÓÊÕÊý¾Ý´íÎó,ÖØпªÊ¼½ÓÊÕ
}
}
}
}
}
//´®¿Ú2ÖжϷþÎñ³ÌÐò
void USART2_IRQHandler(void)
{
u32 timeout=0;
#if SYSTEM_SUPPORT_OS //ʹÓÃOS
OSIntEnter();
#endif
HAL_UART_IRQHandler(&UART2_Handler); //µ÷ÓÃHAL¿âÖжϴ¦Àí¹«Óú¯Êý
timeout=0;
while (HAL_UART_GetState(&UART2_Handler) != HAL_UART_STATE_READY)//µÈ´ý¾ÍÐ÷
{
timeout++;////³¬Ê±´¦Àí
if(timeout>HAL_MAX_DELAY) break;
}
timeout=0;
while(HAL_UART_Receive_IT(&UART2_Handler, (u8 *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)//Ò»´Î´¦ÀíÍê³ÉÖ®ºó£¬ÖØпªÆôÖжϲ¢ÉèÖÃRxXferCountΪ1
{
timeout++; //³¬Ê±´¦Àí
if(timeout>HAL_MAX_DELAY) break;
}
#if SYSTEM_SUPPORT_OS //ʹÓÃOS
OSIntExit();
#endif
}
#endif
/*ÏÂÃæ´úÂëÎÒÃÇÖ±½Ó°ÑÖжϿØÖÆÂß¼­Ð´ÔÚÖжϷþÎñº¯ÊýÄÚ²¿¡£
˵Ã÷£º²ÉÓÃHAL¿â´¦ÀíÂß¼­£¬Ð§Âʲ»¸ß¡£*/
/*
//´®¿Ú2ÖжϷþÎñ³ÌÐò
void USART2_IRQHandler(void)
{
u8 Res;
#if SYSTEM_SUPPORT_OS //ʹÓÃOS
OSIntEnter();
#endif
if((__HAL_UART_GET_FLAG(&UART1_Handler,UART_FLAG_RXNE)!=RESET)) //½ÓÊÕÖжÏ(½ÓÊÕµ½µÄÊý¾Ý±ØÐëÊÇ0x0d 0x0a½áβ)
{
HAL_UART_Receive(&UART2_Handler,&Res,1,1000);
if((USART_RX_STA&0x8000)==0)//½ÓÊÕδÍê³É
{
if(USART_RX_STA&0x4000)//½ÓÊÕµ½ÁË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½ÓÊÕ´íÎó,ÖØпªÊ¼
else USART_RX_STA|=0x8000; //½ÓÊÕÍê³ÉÁË
}
else //»¹Ã»ÊÕµ½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;//½ÓÊÕÊý¾Ý´íÎó,ÖØпªÊ¼½ÓÊÕ
}
}
}
}
HAL_UART_IRQHandler(&UART2_Handler);
#if SYSTEM_SUPPORT_OS //ʹÓÃOS
OSIntExit();
#endif
}
#endif
*/
usart.h
#ifndef _USART_H
#define _USART_H
#include "sys.h"
#include "stdio.h"
//////////////////////////////////////////////////////////////////////////////////
//±¾³ÌÐòÖ»¹©Ñ§Ï°Ê¹Óã¬Î´¾­×÷ÕßÐí¿É£¬²»µÃÓÃÓÚÆäËüÈκÎÓÃ;
//ALIENTEK STM32F429¿ª·¢°å
//´®¿Ú2³õʼ»¯
//ÕýµãÔ­×Ó@ALIENTEK
//¼¼ÊõÂÛ̳:
www.openedv.csom
//ÐÞ¸ÄÈÕÆÚ:2015/6/23
//°æ±¾£ºV1.0
//°æȨËùÓУ¬µÁ°æ±Ø¾¿¡£
//Copyright(C) ÕýµãÔ­×Ó 2009-2019
//All rights reserved
//********************************************************************************
//V1.0ÐÞ¸Ä˵Ã÷
//////////////////////////////////////////////////////////////////////////////////
#define USART_REC_LEN 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200
#define EN_USART2_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú2½ÓÊÕ
extern u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû
extern u16 USART_RX_STA; //½ÓÊÕ״̬±ê¼Ç
extern UART_HandleTypeDef UART2_Handler; //UART¾ä±ú
#define RXBUFFERSIZE 1 //»º´æ´óС
extern u8 aRxBuffer[RXBUFFERSIZE];//HAL¿âUSART½ÓÊÕBuffer
void uart_init(u32 bound);
#endif
一周热门 更多>