因为是初学者,所以不太会调程序,麻烦各位大神指导一下啊。。谢谢谢谢
main.c
#include "main.h"
#ifdef __GNUC__ //用于调用printf()函数
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
extern void uart_init(void); //USART配置函数的声明
static void Delay(__IO uint32_t nCount) //延时函数
{
for (; nCount != 0; nCount--);
}
static void led_init(void) //LED函数(可加可不加):用于检测程序是否正常运行
{
GPIO_InitTypeDef GPIO_InitStructure;
//使能LED所在的GPIOB和GPIOE的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_10 ; //配置LED11 LED15管脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置使用带宽50Mhz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉电阻
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
//初始化 GPIOB和GPIOE
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_15);
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_10);
}
//-------------------------------------------------------------------------------
volatile unsigned char fasong[]={0x68,0xaa,0xaa,0xaa ,0xaa ,0xaa ,0xaa ,0x68 ,0x11 ,0x04 ,0x33 ,0x32 ,0x34 ,0x35 ,0xaf ,0x16};
volatile unsigned int fanum=0;
volatile unsigned int fatx=0;
extern volatile unsigned char jieshuju[26]; //接受缓冲
extern volatile unsigned int jienum; //接受个数
extern volatile unsigned int jiebz; //接受完毕标志
//-------------------------------------------------------------------------------
int main(void)
{
uart_init();
led_init();
UART3Write((uint8_t*)fasong,32); //此函数来自于uart.c文件中
while (1)
{
GPIO_ResetBits(GPIOE, GPIO_Pin_15); //LED12实现亮灭
Delay(0xFFFFFF);
GPIO_SetBits(GPIOE, GPIO_Pin_15);
Delay(0xFFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_10); //LED11实现亮灭
Delay(0xFFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_10);
Delay(0xFFFFFF);
if(jiebz==1)
{
//处理数组jieshou_shuju[0]---[25]
delay(100) ;
//处理完毕清空数组
for(jienum=0; jienum<26; jienum++)
{
jieshuju[jienum]=0;
}
jienum=0;//处理完毕接受个数标志归零
jiebz=0; //处理完毕标志归零
}
}
}
PUTCHAR_PROTOTYPE
{
TX_485; //打开发送控制端(485与232的区别)
delay(10);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET)
{
for(fanum=0;fanum<16;fanum++)
{USART_SendData(USART3, fasong[fanum]); }
}
delay(10); //相应的延时(利于数据完整的接收)
RX_485; //关闭发送控制端(485与232的区别)
return ch;
}
uart.c
#include "uart.h"
void uart_init(void) //USART3的配置
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //GPIOD的时钟使能
/* Enable USART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //USART3的时钟使能
/* Connect USART pins to A9\10 */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); //连接USART3的TX
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); //连接USART3的RX
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //输出TX
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //必须为AF,OUT不行
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化TX口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //输入RX
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //必须为AF
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化RX口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ; //485使能端配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化使能端IO口
//USART3配置
USART_InitStructure.USART_BaudRate = 2400;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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;
USART_Init(USART3, &USART_InitStructure);
//中断配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //优先级与副优先级的分配
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //中断是USART3通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //初始化中断
/* Enable USART */
USART_Cmd(USART3, ENABLE); //使能USART3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //使能USART3接收中断
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
USART_ClearFlag(USART3, USART_FLAG_TC);
}
void delay(unsigned int dl);
void delay(unsigned int dl)
{
unsigned int i,y;
for(i = 0; i < 5000; i++)
{
for(y = 0; y < dl; y++);
}
}
void UART3_SendByte(uint16_t Data)
{
while (!(USART3->SR & USART_FLAG_TXE));
USART_SendData(USART3, Data);
}
void UART3Write(uint8_t* data,uint16_t len)
{
uint16_t i;
TX_485;
for (i=0; i<len; i++)
{
UART3_SendByte(data);
}
delay(10);
RX_485;
}
unsigned char UART3GetByte(void)
{ uint16_t MidData;
RX_485; delay(10);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET) {
MidData = USART_ReceiveData(USART3);
} delay(10);
TX_485;
return MidData;
}
volatile unsigned char jieshuju[26];
volatile unsigned int jienum;
volatile unsigned int jiebz;
void USART3_IRQHandler(uint8_t GetData)
{
//uint8_t BackData;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断产生
{RX_485; //打开接收控制端
delay(10);
for(jienum=0;jienum<27;jienum++)
{
GetData=UART3GetByte();
jieshuju[jienum]=GetData;
}
delay(10);
TX_485;
}
}
#include "stm32f2xx_it.h"
#include "main.h"
#include "uart.h"
void USART3_IRQHandler(uint8_t GetData)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { RX_485; delay(10);
for(jienum=0;jienum<27;jienum++)
{
GetData=UART3GetByte();
jieshuju[jienum]=GetData;
}
if(jieshou_num==26)
{jieshou_bz=1; jieshou_num=0;}
delay(100000);
TX_485;
}
} |
|