STM32的485最簡(jiǎn)單例程_第1頁(yè)
STM32的485最簡(jiǎn)單例程_第2頁(yè)
STM32的485最簡(jiǎn)單例程_第3頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、485 最基本的半雙工通信配置采用 STM32F103ZET6串口 3 連接 485 芯片通信口, 485 芯片的 A,B 通過(guò) 485 轉(zhuǎn)串口模塊與電腦相連,完成在串口軟件上輸入輸出功能。串口 3 ,配置函數(shù):void USART3_Config(void)GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_

2、AFIO,ENABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);/ USART3_TX -> PB10 , USART3_RX-> PB11GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);GPIO_Init

3、Structure.GPIO_Pin = GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);USART_InitStructure.USART_BaudRate = 115200; / 1200;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitSt

4、ructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No; /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)

5、;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);#ifdef PDU_USA

6、RT3_DMAUSART_ITConfig(USART3, USART_IT_IDLE, ENABLE);#else/ Enable the USART Receive interrupt: this interrupt is generated when the/ USART3 receive data register is not empty/USART_ClearITPendingBit(USART3, USART_IT_TC);USART_ClearFlag(USART3,USART_FLAG_TC);USART_ITConfig(USART3, USART_IT_RXNE, ENA

7、BLE);USART_ITConfig(USART3, USART_IT_TC, ENABLE);/USART_ITConfig(USART3, USART_IT_TXE, ENABLE);USART_ClearFlag(USART3,USART_FLAG_TC);#endifUSART_Cmd(USART3, ENABLE);USART_ClearFlag(USART3,USART_FLAG_TC);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE |RCC_APB2Periph_AFIO,ENABLE);GPIO_InitStructure.GPIO_

8、Pin = GPIO_Pin_15;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOE, &GPIO_InitStructure);RS_485_TX_ENABLE();PUTCHAR_PROTOTYPE/* Place your implementation of fputc here */* e.g. write a character to the USART */USART_SendData(USART3,

9、 (uint8_t) ch);/USART_SendData(USART1,(uint8_t)ch);/* Loop until the end of transmission */while (USART_GetFlagStatus(USART1, USART_FLAG_TC) = RESET)while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) = RESET)return ch;中斷函數(shù):void USART3_IRQHandler(void)if(USART_GetITStatus(USART3, USART_IT_IDLE) != RE

10、SET) USART_ClearITPendingBit(USART3, USART_IT_IDLE);if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE)!=RESET) USART_ClearFlag(USART3,USART_FLAG_RXNE);TIM3_Init(1000, 7200);BLUE_ON();if(cnt>=32)cnt=0;elsedatacnt=USART_ReceiveData(USART3);cnt+;if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) | (U

11、SART_GetITStatus(USART3, USART_IT_TXE) != RESET) )USART_ClearITPendingBit(USART3, USART_IT_TC);USART_ClearITPendingBit(USART3, USART_IT_TXE);/USART_SendData(USART3, 0XFFFF);void TIM3_IRQHandler(void)if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) TIM_ClearITPendingBit(TIM3, TIM_IT_Update);TIM3_De

12、Init();BLUE_OFF();/printf("rn %x",cnt);cnt=0;static void NVIC_TIM3Configuration(void)NVIC_InitTypeDef NVIC_InitStructure;/* Set the Vector Table base address at 0x08000000 */NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);/* Enable the TIM3 gloabal Interrupt */NVIC_InitStructure.NVIC_IRQCh

13、annel = TIM3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);定時(shí)器:void TIM3_Init(uint16_t tcon, uint16_t psc)TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;/

14、 TIM3 clock enableRCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);/ reset timerTIM_DeInit(TIM3);/ Time base configuration/ value to reloadTIM_TimeBaseStructure.TIM_Period = (tcon - 1);/ pre -scaler value against system clockTIM_TimeBaseStructure.TIM_Prescaler = (psc - 1);/ scaler value against s

15、ystem clockTIM_TimeBaseStructure.TIM_ClockDivision = 0;/ counting modeTIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; / init timerTIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);/ Clear TIM3 update pending flagTIM_ClearITPendingBit(TIM3, TIM_IT_Update);/ TIM IT enable of over -countin

16、gTIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);/ config timer's interruption function NVIC_TIM3Configuration();/ Enable TIM3 counter TIM_Cmd(TIM3, ENABLE);void TIM3_DeInit(void)/ Disable TIM3 counter TIM_Cmd(TIM3, DISABLE);/ Reset timerTIM_DeInit(TIM3);/ Disable TIM3 clockRCC_APB1PeriphClockCmd(RCC_

17、APB1Periph_TIM3, DISABLE);/tim3_stat = 0x00;主函數(shù):#ifdef _GNF_#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

18、, FILE *f)#endif /* _GNUC_ */#endif /* _GNF_ */void Delay(uint32_t tick)while(tick -);/* Private function prototypes- */extern unsigned char datasize;extern volatile unsigned char cnt;/* Function Name: main* Description: Main program* Input: None* Output: None* Return: None* Attention: None*/int main(void)/*USART configuration*/USART3_Config();RED_ON();BLUE_ON();GREEN_ON();Delay(100000);BLUE_OFF();

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論