USART 2IRQHandlervoid haluartinitvoid haluartwriteunsigned char data unsigned char

  • Slides: 10
Download presentation

串口 • USART 2_IRQHandler(void) • hal_uart_init(void) • hal_uart_write(unsigned char *data, unsigned char len) •

串口 • USART 2_IRQHandler(void) • hal_uart_init(void) • hal_uart_write(unsigned char *data, unsigned char len) • UART_send_byte(uint 8_t byte)

hal_uart_init(void) • RCC_APB 2 Periph. Clock. Cmd( RCC_APB 2 Periph_GPIOA|RCC_APB 2 Periph_AFIO, ENABLE) •

hal_uart_init(void) • RCC_APB 2 Periph. Clock. Cmd( RCC_APB 2 Periph_GPIOA|RCC_APB 2 Periph_AFIO, ENABLE) • GPIO_Init. Structure. GPIO_Pin = GPIO_Pin_2; • GPIO_Init. Structure. GPIO_Mode = GPIO_Mode_AF_PP; • GPIO_Init. Structure. GPIO_Speed = GPIO_Speed_50 MHz; • GPIO_Init(GPIOA, &GPIO_Init. Structure); • GPIO_Init. Structure. GPIO_Pin = GPIO_Pin_3; • GPIO_Init. Structure. GPIO_Mode = GPIO_Mode_IN_FLOATING; • GPIO_Init. Structure. GPIO_Speed = GPIO_Speed_50 MHz; • GPIO_Init(GPIOA, &GPIO_Init. Structure); • RCC_APB 1 Periph. Clock. Cmd(RCC_APB 1 Periph_USART 2, ENABLE );

 • USART_De. Init(USART 2); • USART_Init. Structure. USART_Baud. Rate = 115200; • USART_Init.

• USART_De. Init(USART 2); • USART_Init. Structure. USART_Baud. Rate = 115200; • USART_Init. Structure. USART_Word. Length = USART_Word. Length_8 b; • USART_Init. Structure. USART_Parity = USART_Parity_No; • USART_Init. Structure. USART_Hardware. Flow. Control = • USART_Hardware. Flow. Control_None; • USART_Init. Structure. USART_Mode = USART_Mode_Rx | USART_Mode_Tx; • USART_Init(USART 2, &USART_Init. Structure); • USART_ITConfig(USART 2, USART_IT_RXNE, ENABLE); • USART_Cmd(USART 2, ENABLE);

hal_uart_write() • while(len != 0) • { • while(!((USART 1 ->SR)&(1<<7))); • USART 2

hal_uart_write() • while(len != 0) • { • while(!((USART 1 ->SR)&(1<<7))); • USART 2 ->DR= *data; • data++; • len--; • } • • return len;

UART_send_byte() • while(!((USART 2 ->SR)&(1<<7))); • USART 2 ->DR=byte;

UART_send_byte() • while(!((USART 2 ->SR)&(1<<7))); • USART 2 ->DR=byte;

数模转换函数 • Adc_Init(void) • int Get_Adc(unsigned char ch) • Get_Adc_Average(unsigned char ch, unsigned char

数模转换函数 • Adc_Init(void) • int Get_Adc(unsigned char ch) • Get_Adc_Average(unsigned char ch, unsigned char times)

Get_Adc() • ADC_Regular. Channel. Config(ADC 1, ch, 1, ADC_Sample. Time_239 Cycles 5 ); •

Get_Adc() • ADC_Regular. Channel. Config(ADC 1, ch, 1, ADC_Sample. Time_239 Cycles 5 ); • ADC_Software. Start. Conv. Cmd(ADC 1, ENABLE); • while(!ADC_Get. Flag. Status(ADC 1, ADC_FLAG_EOC )); • return ADC_Get. Conversion. Value(ADC 1);

Get_Adc_Average() • u 32 temp_val=0; • u 8 t; • for(t=0; t<times; t++) •

Get_Adc_Average() • u 32 temp_val=0; • u 8 t; • for(t=0; t<times; t++) • { temp_val+=Get_Adc(ch); • • } • return temp_val/times;

主函数 • int main(void) • { • unsigned int adc_data; • hal_uart_init(); • Adc_Init();

主函数 • int main(void) • { • unsigned int adc_data; • hal_uart_init(); • Adc_Init(); • printf("stm 32 f 103 usart successdrn"); • while(1) • { • adc_data = Get_Adc_Average(ADC_Channel_15, 10); • printf("adc_data: %drn", adc_data); • Delay(2000000); //2 s • • } }