STM32實(shí)用性超強(qiáng)的usart例程_第1頁(yè)
STM32實(shí)用性超強(qiáng)的usart例程_第2頁(yè)
STM32實(shí)用性超強(qiáng)的usart例程_第3頁(yè)
STM32實(shí)用性超強(qiáng)的usart例程_第4頁(yè)
STM32實(shí)用性超強(qiáng)的usart例程_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡(jiǎn)介

超級(jí)實(shí)用的例程實(shí)現(xiàn)在應(yīng)用中的串口收發(fā)例程,可應(yīng)用在實(shí)際項(xiàng)目中,技術(shù)關(guān)鍵點(diǎn)在于采用定時(shí)器判斷接收是否完成,及所接受數(shù)據(jù)個(gè)數(shù),并將數(shù)據(jù)存儲(chǔ)在緩沖區(qū),待后續(xù)處理與應(yīng)用。串口配置voidUSART1_Config(void){GPIO_InitTypeDefGPIO_InitStructure;USART_InitTypeDefUSART_InitStructure;NVIC_InitTypeDefNVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);//GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);//USART1_TX->PB6,USART1_RX->PB7GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;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_Init(USART1,&USART_InitStructure);NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);USART_ClearFlag(USART1,USART_FLAG_TC);/*EnabletheUSARTReceiveinterrupt:thisinterruptisgeneratedwhentheUSART1receivedataregisterisnotempty*///USART_ClearITPendingBit(USART1,USART_IT_TC);USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);USART_ITConfig(USART1,USART_IT_TC,ENABLE);//USART_ITConfig(USART1,USART_IT_TXE,ENABLE);USART_ClearFlag(USART1,USART_FLAG_TC);USART_Cmd(USART1,ENABLE);}staticvoidNVIC_TIM3Configuration(void){NVIC_InitTypeDefNVIC_InitStructure;/*SettheVectorTablebaseaddressat0x08000000*///NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0000);/*EnabletheTIM3gloabalInterrupt*/NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);}定時(shí)器配置@°_EyAuTIM3」nit@1;AU Aa0ATIM3zIEy°_Ey£-AtAe%^Ey^^0D^I0?,l@2TEyTP***@屮》00口Tp****/voidTIM3_Init(uint16_ttcon,uint16_tpsc){TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;//TIM3clockenableRCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//resettimerTIM_DeInit(TIM3);//Timebaseconfiguration//valuetoreloadTIM_TimeBaseStructure.TIM_Period=(tcon-1);//pre-scalervalueagainstsystemclockTIM_TimeBaseStructure.TIM_Prescaler=(psc-1);//scalervalueagainstsystemclockTIM_TimeBaseStructure.TIM_ClockDivision=0;//countingmodeTIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//inittimerTIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);//ClearTIM3updatependingflagTIM_ClearITPendingBit(TIM3,TIM_IT_Update);//TIMITenableofover-countingTIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//configtimer'sinterruptionfunctionNVIC_TIM3Configuration();//EnableTIM3counterTIM_Cmd(TIM3,ENABLE);}voidTIM3_DeInit(void){//DisableTIM3counterTIM_Cmd(TIM3,DISABLE);//ResettimerTIM_DeInit(TIM3);//DisableTIM3clockRCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,DISABLE);//tim3_stat=0x00;PUTCHAR_PROTOTYPE{/*Placeyourimplementationoffputchere*//*e.g.writeacharactertotheUSART*/USART_SendData(USART1,(uint8_t)ch);//USART_SendData(USART1,(uint8_t)ch);/*Loopuntiltheendoftransmission*///while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET){}returnch;}串口中斷接收voidUSART1_IRQHandler(void){//if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//{//// //CleartheUSART1Receiveinterrupt// USART_ClearITPendingBit(USART1,USART_IT_RXNE);//}//USART_SendData(USART1,0X02);if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET){USART_ClearFlag(USART1,USART_FLAG_RXNE);// printf("\n\rt^E%^OEOEy%Y");TIM3_Init(200,7200);if(cnt>=size){cnt=0;// printf("\n\r^6Ed^^^AEy%Y3-1yAE32BYTE");}else{data[cnt]=USART_ReceiveData(USART1);cnt++;GPIO_WriteBit(GPIOB,GPIO_Pin_6,1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_6));}}〃VEI?°弔圧NO£-Ec1OODEV%YDaO@VEI%IVEIEV%Y£-Ec1CiA?OD£-%l°NVEIOD勺l10±Oif(USART_GetITStatus(USART1,USART_IT_TC)!=RESET){USART_ClearITPendingBit(USART1,USART_IT_TC);USART_ClearITPendingBit(USART

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論