STM32F107串口通信RS232 modbus(去注释方便打印)

2019-04-09 11:33

Main

#include \

typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure;

//#define countof(a) (sizeof(a) / sizeof(*(a))) u32 CCR1_Val=1000;

USART_InitTypeDef USART_InitStructure;

//u8 NbrOfDataToTransfer1 ; //u8 NbrOfDataToRead1;

volatile TestStatus TransferStatus1 = FAILED; ErrorStatus HSEStartUpStatus;

void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void Uart_config(void);

void TIM2_config(void);

extern void Com0_Communication(void);

TestStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);

int main(void) {

#ifdef DEBUG debug(); #endif

/* System Clocks Configuration */ RCC_Configuration();

/* NVIC configuration */ NVIC_Configuration();

/* Configure the GPIO ports */ GPIO_Configuration();

Uart_config();

TIM2_config(); while (1)

{

Com0_Communication(); } }

void RCC_Configuration(void) {

/* RCC system reset(for debug purpose) */ RCC_DeInit();

/* Enable HSE */

RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS) {

/* Enable Prefetch Buffer */

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Flash 2 wait state */

FLASH_SetLatency(FLASH_Latency_2);

/* HCLK = SYSCLK */

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */

RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 MHz */

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */ RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }

/* Select PLL as system clock source */

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { }

}

/* TIM2 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

/* Enable USART1, GPIOA, GPIOA and AFIO clocks */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA RCC_APB2Periph_AFIO, ENABLE); }

void GPIO_Configuration(void) {

GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); }

void NVIC_Configuration(void) {

NVIC_InitTypeDef NVIC_InitStructure;

#ifdef VECT_TAB_RAM

/* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

| #endif

/* Configure the NVIC Preemption Priority Bits配置优先级组 */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

/* Enable the USART1 Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the TIM2 gloabal Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }

/* USART1 configuration ------------------------------------------------------*/ /* USART configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit

- No parity

- Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */

void Uart_config(void) {

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; /* Configure USART1 */

USART_Init(USART1, &USART_InitStructure); /* Enable USART1 Receive and Transmit interrupts */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); /* Enable the USART1 */

USART_Cmd(USART1, ENABLE); }

=

void TIM2_config(void)

{

/* Time base configuration[初始化时间基单元] */ TIM_TimeBaseStructure.TIM_Period = 0xffff;//周期值

TIM_TimeBaseStructure.TIM_Prescaler = 0;//时钟的预分频数=0x0000

TIM_TimeBaseStructure.TIM_ClockDivision = 0;//TIM_ClockDivision 配置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* Prescaler configuration [配置TIM的预分频数]*/

TIM_PrescalerConfig(TIM2, 36, TIM_PSCReloadMode_Immediate);//TIM2,36分频,预分频数立即载入

/* Output Compare Timing Mode configuration: Channel1 [配置输出比较模式:通道1]*/ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;//TIM输出比较计时模式 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出允许 TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//脉冲CCR1_Val

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//极性HIGH

TIM_OC1Init(TIM2, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);//关闭TIM2在CCR1寄存器上的预载入寄存器

/* TIM IT enable [使能TIM2比较中断]*/ TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE);

}

#ifdef DEBUG

void assert_failed(u8* file, u32 line)

{

/* User can add his own implementation to report the file name and line number, ex: printf(\

/* Infinite loop */ while (1) { } }

#endif


STM32F107串口通信RS232 modbus(去注释方便打印).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:答案--心理健康竞赛题库(预赛)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: