單片機MODBUD RTU從站C語言程序
單片機源程序如下:
- /**
- ******************************************************************************
- * @文件
- * @作者
- * @版本
- * @日期
- * @概要
- ******************************************************************************
- * @注意事項
- *
- *
- *
- ******************************************************************************
- */
- /* 頭文件 --------------------------------------------------------------*/
- #include <stdio.h>
- #include "stm32f10x.h"
- #include "mb.h"
- /* 私有數據類型 --------------------------------------------------------------*/
- /* 私有定義 --------------------------------------------------------------*/
- /* 私有宏定義 --------------------------------------------------------------*/
- #ifdef __GNUC__
- /* 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__ */
- /* 私有變量 --------------------------------------------------------------*/
- /* 私有函數聲明 --------------------------------------------------------------*/
- void LED_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- }
- /**
- * @brief Configure the nested vectored interrupt controller.
- * @param None
- * @retval : None
- */
- #if 0
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Enable the TIM2 gloabal Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* Enable the TIM2 gloabal Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- #endif
- #define USE_UART1_INT
- #define USE_TIM2_INT
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- #if 1
- #ifdef USE_UART1_INT
- /* Enable the USART1 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_UART2_INT
- /* Enable the USART2 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_UART3_INT
- /* Enable the USART3 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_UART4_INT
- /* Enable the USART4 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_UART5_INT
- /* Enable the USART5 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_EXTI0_INT
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_EXTI1_INT
- NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_EXTI2_INT
- NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_EXTI3_INT
- NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_EXTI4_INT
- NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_EXTI9_5_INT
- NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_EXTI15_10_INT
- NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_TIM1_CC_INT
- NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_TIM2_INT
- /* Enable the TIM2,4~7 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_TIM4_INT
- NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_TIM5_INT
- NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_TIM6_INT
- NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_TIM7_INT
- NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_PWM_IN_INT
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_RTC_INT
- /* Enable the RTC Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #ifdef USE_RTCALARM_INT
- NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #endif
- #if 1 //#ifdef USE_CAN1_INT
- //CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0消息掛號中斷允許.
- #if 1 /* For compiling F105 TO F103 */
- NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;//CAN1_RX0_IRQn;
- #endif
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- #if 0 //#ifdef USE_CAN2_INT
- NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- }
- #define Open10x_DAC_CLK RCC_APB2Periph_GPIOB
- #define Open10x_CLK_PORT GPIOB
- #define Open10x_CLK_PIN GPIO_Pin_2
- #define Open10x_SDI_PORT GPIOB
- #define Open10x_SDI_PIN GPIO_Pin_3
- #define Open10x_CLR_PORT GPIOB
- #define Open10x_CLR_PIN GPIO_Pin_0
- #define Open10x_LD_PORT GPIOB
- #define Open10x_LD_PIN GPIO_Pin_1
- #define Open10x_CS_PORT GPIOB
- #define Open10x_CS_PIN GPIO_Pin_4
- #define Open10x_LED_CLK RCC_APB2Periph_GPIOC
- #define Open10x_LED0_PORT GPIOC
- #define Open10x_LED0_PIN GPIO_Pin_14
- #define Open10x_LED1_PORT GPIOC
- #define Open10x_LED1_PIN GPIO_Pin_15
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //啟動時鐘安全系統CSS
- RCC_ClockSecuritySystemCmd(ENABLE);
- /* Enable GPIO clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
- GPIO_InitStructure.GPIO_Pin = Open10x_CLK_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
- //GPIO_ResetBits(GPIOB, GPIO_Pin_7);
- GPIO_InitStructure.GPIO_Pin = Open10x_SDI_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
- GPIO_ResetBits(Open10x_CLK_PORT, Open10x_SDI_PIN);
- GPIO_SetBits(Open10x_CLK_PORT, Open10x_SDI_PIN);
-
- GPIO_InitStructure.GPIO_Pin = Open10x_CLR_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
- GPIO_SetBits(Open10x_CLK_PORT, Open10x_CLR_PIN);
-
- GPIO_InitStructure.GPIO_Pin = Open10x_LD_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(Open10x_CLK_PORT, &GPIO_InitStructure);
- //GPIO_ResetBits(GPIOB, GPIO_Pin_7);
-
- GPIO_InitStructure.GPIO_Pin = Open10x_CS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(Open10x_CS_PORT, &GPIO_InitStructure);
-
- GPIO_ResetBits(Open10x_CS_PORT, Open10x_CS_PIN);
-
- GPIO_InitStructure.GPIO_Pin = Open10x_LED0_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(Open10x_LED0_PORT, &GPIO_InitStructure);
- GPIO_SetBits(Open10x_LED0_PORT, Open10x_LED0_PIN);
-
- GPIO_InitStructure.GPIO_Pin = Open10x_LED1_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(Open10x_LED0_PORT, &GPIO_InitStructure);
- GPIO_SetBits(Open10x_LED0_PORT, Open10x_LED1_PIN);
- }
- /**
- * @功能
- * @參數
- * @返回值
- */
- int main(void)
- {
- //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_Configuration();
- GPIO_Configuration();
- //LED_Config();
- dac7611_Init();
- dac7611Conv(0);
- /*模式 從機地址 端口 波特率 校驗位*/
- eMBInit( MB_RTU, 0x64, 0, 9600, MB_PAR_NONE );
-
- /* Enable the Modbus Protocol Stack. */
- eMBEnable( );
-
- for( ;; )
- {
- ( void )eMBPoll( );
- /* Here we simply count the number of poll cycles. */
- //usRegInputBuf[0]++;
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
STM32_MODBUS.rar
(647.51 KB, 下載次數: 27)
2017-12-29 22:29 上傳
點擊文件名下載附件
|