/---------------------------man.c---------------------------/
#include "main.h"
int main(void)
{
main_init();
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
Delayms(0x5555);
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
while(1)
{
UART1_MAX3485_Puts("11111");
}
}
/--------------------------gpio.c---------------------------/
#include "main.h"
void Hardware_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/--------------------------init.c---------------------------/
#include "main.h"
void main_init(void)
{
RCC_ClocksTypeDef RCC_ClockFreq;
SystemInit();
RCC_GetClocksFreq(&RCC_ClockFreq);
Hardware_GPIO_Init();
Hardware_UART1_MAX3485_Init();
}
/--------------------------uart.c---------------------------/
#include "main.h"
#include "stm32f0xx.h"
#define TX_485 GPIO_SetBits(GPIOA,GPIO_Pin_9);
#define RX_485 GPIO_ResetBits(GPIOA,GPIO_Pin_9);
unsigned char M485_RX_Buff[50] = {0};
unsigned char M485_RX_Length = 0;
unsigned char M485_RX_Buff_Lock = 0;
unsigned char M485_Data_Length = 0;
void Hardware_UART1_MAX3485_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART1_MAX3485_Putc(unsigned char c)
{
USART_SendData(USART3, c);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
void Delayms(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void UART1_MAX3485_Puts(char *str)
{
while(*str)
{
TX_485;
Delayms(0xFF);
USART_SendData(USART1, *str++);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
Delayms(0xFF);
RX_485;
}
}
void M485_Send(unsigned char *P, unsigned short int Length)
{
unsigned short int Send_Number;
TX_485;
Delayms(0xFF);
for(Send_Number=0; Send_Number<Length; Send_Number++)
{
UART1_MAX3485_Putc(*P++);
}
Delayms(0xFF);
RX_485;
}
void Rest_M485_RX(void)
{
unsigned char temp;
M485_RX_Length = 0;
M485_RX_Buff_Lock = 0;
M485_Data_Length = 0;
for(temp=0; temp<50; temp++)
{
M485_RX_Buff[temp] = 0;
}
}
void USART1_IRQHandler(void)
{
if(USART_GetFlagStatus(USART1, USART_IT_RXNE) == SET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
if(M485_RX_Buff_Lock == 0)
{
M485_RX_Buff[M485_RX_Length++] = USART_ReceiveData(USART1);
if(M485_RX_Buff[0] == 0x81)
M485_RX_Buff_Lock = 1;
}
else
USART_ReceiveData(USART1);
}
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) == SET) //½óêÕÂúáË
{
USART_ClearFlag(USART1,USART_FLAG_ORE);
USART_ReceiveData(USART1);
}
一周热门 更多>