设计中的一些关键函数及程序代码如下: (1)温湿度检测程序 温湿度检测的程序代码为:
- i=0;
- POINT_COLOR=RED;
- BACK_COLOR=WHITE; //BLACK
- DHT11_Init();
- while(SET == gpio_input_bit_get(GPIOA,GPIO_PIN_0)) // 确认
- {
- DHT11_Read_Data(&temp,&humi);
- LCD_ShowCharh(100,100,'T',1);
- LCD_ShowCharh(100,90,'e',1);
- LCD_ShowCharh(100,80,'m',1);
- LCD_ShowCharh(100,70,'p',1);
- LCD_ShowCharh(100,60,':',1);
- LCD_ShowCharh(100,50,temp/10+'0',0);
- LCD_ShowCharh(100,40,temp%10+'0',0);
- LCD_ShowCharh(120,100,'H',1);
- LCD_ShowCharh(120,90,'u',1);
- LCD_ShowCharh(120,80,'m',1);
- LCD_ShowCharh(120,70,'i',1);
- LCD_ShowCharh(120,60,':',1);
- LCD_ShowCharh(120,50,humi/10+'0',0);
- LCD_ShowCharh(120,40,humi%10+'0',0);
- delay_1ms(200);
- }
复制代码
(2)波形曲线绘制 曲线绘制的程序代码为:
- adc_config();
- adc_resolution_config(ADC1,ADC_RESOLUTION_12B);
- adc_oversample_mode_config(ADC1,ADC_OVERSAMPLING_ALL_CONVERT,ADC_OVERSAMPLING_SHIFT_NONE,ADC_OVERSAMPLING_SHIFT_NONE);
- adc_oversample_mode_disable(ADC1);
- while(SET == gpio_input_bit_get(GPIOA,GPIO_PIN_0))
- {
- LCD_Clear(WHITE);
- sp=95;
- POINT_COLOR=RED;
- LCD_DrawLine(10, 304, 230, 304);
- LCD_DrawLine(125, 10, 125, 304);
- POINT_COLOR=BLUE;
- for ( i = 0 ; i < 64; i++)
- {
- adc_enable(ADC1);
- adc_software_trigger_enable(ADC1,ADC_REGULAR_CHANNEL);
- while(RESET == adc_flag_get(ADC1,ADC_FLAG_EOC));
- sj=ADC_RDATA(ADC1);
- sj=sj/10;
- LCD_DrawLine(320-sp, 300-4*(i-1), 320-sj, 300-4*(i));
- sp=sj;
- delay_1ms(20);
- }
- }
复制代码
(3)图像显示 图标显示函数为:
- void show_tbh(unsigned int x,unsigned int y,unsigned int n) //200*200
- {
- unsigned int i,j,k;
- unsigned int da;
- k=0;
- for(i=0;i<50;i++)
- {
- for(j=0;j<50;j++)
- {
- if(n==0) da=gImage_tb[k*2];
- if(n==1) da=gImage_pwm[k*2];
- if(n==2) da=gImage_rtc[k*2];
- if(n==3) da=gImage_adc[k*2];
- if(n==4) da=gImage_tft[k*2];
- if(n==5) da=gImage_sz[k*2];
- if(n==6) da=gImage_kz[k*2];
- if(n==7) da=gImage_dht[k*2];
- da<<=8;
- if(n==0) da|=gImage_tb[k*2+1];
- if(n==1) da|=gImage_pwm[k*2+1];
- if(n==2) da|=gImage_rtc[k*2+1];
- if(n==3) da|=gImage_adc[k*2+1];
- if(n==4) da|=gImage_tft[k*2+1];
- if(n==5) da|=gImage_sz[k*2+1];
- if(n==6) da|=gImage_kz[k*2+1];
- if(n==7) da|=gImage_dht[k*2+1];
- POINT_COLOR=da;
- LCD_DrawPoint(x+i,y+j);
- k++;
- }
- }
- }
复制代码
图像显示函数为:
- void show_pic(const unsigned char *p)
- {
- int i,j;
- unsigned char picH,picL;
- Address_set(0,0,LCD_W-1,LCD_H-1);
- for(i=0;i<240;i++)
- {
- for(j=0;j<320;j++)
- {
- picH=*p++;
- picL=*p++;
- LCD_WR_DATA8(picH);
- LCD_WR_DATA8(picL);
- }
- }
- }
复制代码
(4)MP3播放程序 通过串口来控制MP3播放模块的指令是存放在数值中,其定义如下: uint8_t aTxBuffer[] = " **** UART_TwoBoards_ComPolling **** "; uint8_t cmd[] = {0X7E, 0xFF, 0x06, 0X0D, 00, 00, 00, 0xFE, 0xEE, 0XEF}; //play uint8_t cmd1[] = {0X7E, 0xFF, 0x06, 0X01, 00, 00, 00, 0xFE, 0xFA, 0XEF}; //next uint8_t cmd2[] = {0X7E, 0xFF, 0x06, 0X02, 00, 00, 00, 0xFE, 0xF9, 0XEF}; //previous uint8_t cmd3[] = {0X7E, 0xFF, 0x06, 0X03, 00, 00, 01, 0xFE, 0xF7, 0XEF}; //*play index uint8_t cmd4[] = {0X7E, 0xFF, 0x06, 0X0E, 00, 00, 00, 0xFE, 0xED, 0XEF}; //pause uint8_t cmd5[] = {0X7E, 0xFF, 0x06, 0X16, 00, 00, 00, 0xFE, 0xE5, 0XEF}; //stop uint8_t cmd6[] = {0X7E, 0xFF, 0x06, 0X06, 00, 00, 10, 0xFE, 0xE5, 0XEF}; //*set Volume 串行通讯的初始化函数为:
- void u***_to_uart_init(void)
- {
- // enable GPIO clock
- rcu_periph_clock_enable(RCU_GPIOD);
- // enable USART clock
- rcu_periph_clock_enable(RCU_USART1);
- // connect port to USARTx_Tx
- gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_5);
- // connect port to USARTx_Rx
- gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_6);
- // configure USART Tx as alternate function push-pull
- gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_5);
- gpio_output_options_set(GPIOD,GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_5);
- // configure USART Rx as alternate function push-pull
- gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
- gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
- // USART configure
- usart_deinit(USART1);
- usart_baudrate_set(USART1, 9600U);
- usart_receive_config(USART1, USART_RECEIVE_ENABLE);
- usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
- usart_word_length_set(USART1, USART_WL_8BIT);
- usart_stop_bit_set(USART1, USART_STB_1BIT);
- usart_parity_config(USART1, USART_PM_NONE);
- usart_enable(USART1);
- }
复制代码
发送播放指令的函数为:
- void playn(uint16_t index) //play index
- {
- int i;
- uint8_t checksum = 0;
- cmd3[5] = (uint8_t)(index >> 8);
- cmd3[6] = (uint8_t)(index);
- for (i=2; i<8; i++)
- {
- checksum += cmd3[i];
- }
- cmd3[8] = (uint8_t)~checksum;
- }
复制代码
发送音量指令的函数为:
- void setVolume(uint8_t index) //setVolume index
- {
- uint8_t checksum = 0;
- int i;
- cmd6[6] = (uint8_t)(index);
- for (i=2; i<8; i++)
- {
- checksum += cmd6[i];
- }
- cmd6[8] = (uint8_t)~checksum;
- }
复制代码
|