|
ppt內(nèi)容預(yù)覽:
0.jpg (47.21 KB, 下載次數(shù): 33)
下載附件
2019-12-31 13:07 上傳
51hei.png (173.44 KB, 下載次數(shù): 30)
下載附件
2019-12-31 13:07 上傳
51hei.png (50.04 KB, 下載次數(shù): 26)
下載附件
2019-12-31 13:08 上傳
51hei.png (19.88 KB, 下載次數(shù): 22)
下載附件
2019-12-31 13:08 上傳
51hei.png (146.15 KB, 下載次數(shù): 22)
下載附件
2019-12-31 13:09 上傳
STM32F4單片機(jī)源程序如下:
- #include "stm32f4xx.h"
- //#include "led.h"
- //#include "delay.h"
- void delay1(int n);
- void delay_init(void);
- void delay_ms(u16 nms);
- void LED_Init(void);
- void sysclock_init(void);
- void uart_init(void);
- void USART1_IRQHandler(void);
- // void Adc_Init(void);
- // u16 Get_Adc(u8 ch) ;
- // u16 Get_Adc_Average(u8 ch,u8 times);
-
-
-
- int main(void)
- {
-
- // u16 adcx;
- // float temp;
- //delay_init(168);
- sysclock_init();
- LED_Init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//???????????2
- uart_init(); //?????????115200
- // Adc_Init(); //???ADC
- while(1){
- GPIO_SetBits(GPIOC,GPIO_Pin_2);
- GPIO_ResetBits(GPIOF,GPIO_Pin_7);
- // delay_ms(5000000);
- delay1(5000000);
- GPIO_ResetBits(GPIOC,GPIO_Pin_2);
- GPIO_SetBits(GPIOF,GPIO_Pin_7);
-
- delay1(5000000);
-
-
- // USART_SendData(USART1,0x55); //???1????
- // while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//??????
-
- }
- }
- void sysclock_init(void)
- {
- unsigned int HSEStartUpStatus;
-
- /*???RCC ?????????*/
- RCC_DeInit(); /*????????( HSE)*/
- RCC_HSEConfig(RCC_HSE_ON); //RCC_HSE_ON ?SE ????(ON)/*??HSE ??*/
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS) //SUCCESS :HSE ???????
- { /*??AHB ??( HCLK)*/
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //RCC_SYSCLK_Div1 ?AHB??= ????
- /* ????AHB ??( PCLK2 )*/
- RCC_PCLK2Config(RCC_HCLK_Div1); //RCC_HCLK_Div1 ?APB2??=HCLK
- /*????AHB ??( PCLK1 )*/
- RCC_PCLK1Config(RCC_HCLK_Div2); //RCC_HCLK_Div2 ? APB1??=HCLK/2
- /*??FLASH ??????????*/
- FLASH_SetLatency(FLASH_Latency_2); //FLASH_Latency_2 2 ????
- /*??FLASH ????????*/
- //FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // ??????? /*??PLL ????????*/
- //RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- // PLL ?????= HSE ????; RCC_PLLMul_9 ?PLL ????x 9 /*??PLL */
- RCC_PLLCmd(ENABLE);
- /*?????RCC ???(PLL ?????)????*/
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- { }
- /*??????( SYSCLK )*/
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- //RCC_SYSCLKSource_PLLCLK ???PLL ?????? /* PLL ????????????*/
- while(RCC_GetSYSCLKSource() != 0x08); //0x08 :PLL ??????
-
- }
- }
- void delay1(int n)
- {
- while(n--);
- }
- void delay_ms(u16 nms)
- { int i;
- for(i=0;i<nms;i++);
-
- }
- //??nus
- //nus?????us?.
- //??:nus??,????798915us
- void LED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
- //F9
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- GPIO_SetBits(GPIOC,GPIO_Pin_2);
-
- //F10
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
-
- GPIO_Init(GPIOF,&GPIO_InitStructure);
- GPIO_SetBits(GPIOF,GPIO_Pin_7);
- }
- void uart_init(void){
- //GPIO????
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //??GPIOA??
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//??USART1??
-
- //??1????????
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9???USART1
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10???USART1
-
- //USART1????
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9?GPIOA10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//????
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //??50MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //??????
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //??
- GPIO_Init(GPIOA,&GPIO_InitStructure); //???PA9,PA10
- //USART1 ?????
- USART_InitStructure.USART_BaudRate = 115200;//?????
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???8?????
- 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); //?????1
-
- USART_Cmd(USART1, ENABLE); //????1
-
- //USART_ClearFlag(USART1, USART_FLAG_TC);
-
- //#if EN_USART1_RX
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//??????
- //Usart1 NVIC ??
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//??1????
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//?????3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //????3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ????
- NVIC_Init(&NVIC_InitStructure); //??????????VIC????
- //#endif
-
- }
- void USART1_IRQHandler(void) //??1??????
- {
- u8 Res;
- u8 tempx=2;
- Res=3;
- tempx+=Res;
- Res=USART_ReceiveData(USART1);
- tempx+=Res;
-
- USART_SendData(USART1,Res); //???1????
- while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//??????
- //#if SYSTEM_SUPPORT_OS //??SYSTEM_SUPPORT_OS??,?????OS.
- // OSIntEnter();
- //#endif
- // if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //????(?????????0x0d 0x0a??)
- // {
- // Res =USART_ReceiveData(USART1);//(USART1->DR); //????????
- //
- // if((USART_RX_STA&0x8000)==0)//?????
- // {
- // if(USART_RX_STA&0x4000)//????0x0d
- // {
- // if(Res!=0x0a)USART_RX_STA=0;//????,????
- // else USART_RX_STA|=0x8000; //?????
- // }
- // else //????0X0D
- // {
- // if(Res==0x0d)USART_RX_STA|=0x4000;
- // else
- // {
- // USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
- // USART_RX_STA++;
- // if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//??????,??????
- // }
- // }
- // }
- // }
- //#if SYSTEM_SUPPORT_OS //??SYSTEM_SUPPORT_OS??,?????OS.
- // OSIntExit();
- //#endif
- }
復(fù)制代碼
所有資料51hei提供下載:
串口通訊.7z
(7.08 MB, 下載次數(shù): 34)
2019-12-31 13:10 上傳
點(diǎn)擊文件名下載附件
資料 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|