版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
附錄Main主程序代碼:#include"led.h"#include"delay.h"#include"sys.h"#include"usart.h"#include"key.h"#include"dht11.h"#include"oled.h"intmain(void){ u8i=0; delay_init(); //延時(shí)函數(shù)初始化 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置NVIC中斷分組2:2位響應(yīng)優(yōu)先級 uart_init(115200); //串口初始化115200 LED_Init(); //LED端口初始化 KEY_Init(); //數(shù)據(jù)選擇器初始化 OLED_Init(); //初始化OLED OLED_Clear();//清屏 while(DHT11_Init()) //DHT11初始化 { delay_ms(200); i++; if(i>=50)//初始化失敗,跳出 break; } while(1) { usart_data(); //串口掃描 DHT11_Read_Data(&temperature,&humidity);//讀取溫度和濕度 OLED_Page();//液晶更新 delay_ms(100); } }led.c程序代碼:#include"led.h"#include"delay.h"http://LEDIO初始化voidLED_Init(void){GPIO_InitTypeDefGPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//使能PB,PE端口時(shí)鐘GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13; //LED0-->PB.5端口配置GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽輸出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //IO口速度為50MHzGPIO_Init(GPIOC,&GPIO_InitStructure); //根據(jù)設(shè)定參數(shù)初始化GPIOB.5 GPIO_ResetBits(GPIOC,GPIO_Pin_13); //PB.5輸出高}voidled_overturn(){ LED=1; delay_ms(100); LED=0;}key.c程序代碼:#include"stm32f10x.h"#include"key.h"#include"sys.h"#include"delay.h"#include"led.h" //初始化函數(shù)voidKEY_Init(void)//IO初始化{GPIO_InitTypeDefGPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能PB端口時(shí)鐘GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽輸出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //IO口速度為50MHzGPIO_Init(GPIOB,&GPIO_InitStructure); //根據(jù)設(shè)定參數(shù)初始化 GPIO_SetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);9//輸出高}voidMODE_Clean() //關(guān)閉選擇器{ S0=1; S1=1; S2=1; E=1;}voidMODE_Y0() //打開通道0{ S0=0; S1=0; S2=0; E=0;}voidMODE_Y1()//打開通道1{ S0=1; S1=0; S2=0; E=0;}voidMODE_Y2()//打開通道2{ S0=0; S1=1; S2=0; E=0;}voidMODE_Y3()//打開通道3{ S0=1; S1=1; S2=0; E=0;}voidMODE_Y4()//打開通道4{ S0=0; S1=0; S2=1; E=0;}dht11程序代碼:#include"dht11.h"#include"delay.h"u8temperature=0; u8humidity=0;//復(fù)位DHT11voidDHT11_Rst(void) { DHT11_IO_OUT(); //SETOUTPUT DHT11_DQ_OUT=0; //拉低DQ delay_ms(20); //拉低至少18ms DHT11_DQ_OUT=1; //DQ=1 delay_us(30); //主機(jī)拉高20~40us}//等待DHT11的回應(yīng)//返回1:未檢測到DHT11的存在//返回0:存在u8DHT11_Check(void) { u8retry=0; DHT11_IO_IN();//SETINPUT while(DHT11_DQ_IN&&retry<100)//DHT11會拉低40~80us { retry++; delay_us(1); }; if(retry>=100)return1; elseretry=0;while(!DHT11_DQ_IN&&retry<100)//DHT11拉低后會再次拉高40~80us { retry++; delay_us(1); }; if(retry>=100)return1; return0;}//從DHT11讀取一個(gè)位//返回:1/0u8DHT11_Read_Bit(void) { u8retry=0; while(DHT11_DQ_IN&&retry<100)//等待變?yōu)榈碗娖?{ retry++; delay_us(1); } retry=0; while(!DHT11_DQ_IN&&retry<100)//等待變高電平 { retry++; delay_us(1); } delay_us(40);//等待40us if(DHT11_DQ_IN)return1; elsereturn0; }//從DHT11讀取一個(gè)字節(jié)//返回值:讀到的數(shù)據(jù)u8DHT11_Read_Byte(void){u8i,dat;dat=0; for(i=0;i<8;i++) { dat<<=1; dat|=DHT11_Read_Bit();} returndat;}//從DHT11讀取一次數(shù)據(jù)//temp:溫度值(范圍:0~50°)//humi:濕度值(20%~90%)//返回值:0正常;1讀取失敗u8DHT11_Read_Data(u8*temp,u8*humi){ u8buf[5]; u8i; DHT11_Rst(); if(DHT11_Check()==0) { for(i=0;i<5;i++)//讀取40位數(shù)據(jù) { buf[i]=DHT11_Read_Byte(); } if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]) { *humi=buf[0]; *temp=buf[2]; } }elsereturn1; return0; }//初始化DHT11的IO口DQ同時(shí)檢測DHT11的存在//返回1:不存在//返回0:存在 u8DHT11_Init(void){ GPIO_InitTypeDefGPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能PB端口時(shí)鐘 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0; //PB1端口配置 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽輸出 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化IO口GPIO_SetBits(GPIOB,GPIO_Pin_1); //PB1輸出高 DHT11_Rst();//復(fù)位DHT11 returnDHT11_Check();//等待DHT11的回應(yīng)}oled程序代碼:#include"oled.h"#include"stdlib.h"#include"oledfont.h" #include"delay.h"#include"string.h"#include"stdio.h"#include"dht11.h"#include"key.h"voidIIC_Start(){ OLED_SCLK_Set(); OLED_SDIN_Set(); OLED_SDIN_Clr(); OLED_SCLK_Clr();}voidIIC_Stop(){OLED_SCLK_Set();OLED_SCLK_Clr(); OLED_SDIN_Clr(); OLED_SDIN_Set(); }voidIIC_Wait_Ack(void){ OLED_SCLK_Set(); OLED_SCLK_Clr();}voidWrite_IIC_Byte(unsignedcharIIC_Byte){ unsignedchari; unsignedcharm,da; da=IIC_Byte; OLED_SCLK_Clr(); for(i=0;i<8;i++) { m=da; m=m&0x80; if(m==0x80) {OLED_SDIN_Set();} elseOLED_SDIN_Clr(); da=da<<1; OLED_SCLK_Set(); OLED_SCLK_Clr(); }}voidWrite_IIC_Command(unsignedcharIIC_Command){IIC_Start();Write_IIC_Byte(0x78);//Slaveaddress,SA0=0 IIC_Wait_Ack(); Write_IIC_Byte(0x00); //writecommand IIC_Wait_Ack(); Write_IIC_Byte(IIC_Command); IIC_Wait_Ack(); IIC_Stop();}voidWrite_IIC_Data(unsignedcharIIC_Data){IIC_Start();Write_IIC_Byte(0x78); //D/C#=0;R/W#=0 IIC_Wait_Ack(); Write_IIC_Byte(0x40); //writedata IIC_Wait_Ack(); Write_IIC_Byte(IIC_Data); IIC_Wait_Ack(); IIC_Stop();}voidOLED_WR_Byte(unsigneddat,unsignedcmd){ if(cmd) {Write_IIC_Data(dat); } else{Write_IIC_Command(dat); }}voidfill_picture(unsignedcharfill_Data){ unsignedcharm,n; for(m=0;m<8;m++) { OLED_WR_Byte(0xb0+m,0); //page1 OLED_WR_Byte(0x00,0); //lowcolumnstartaddress OLED_WR_Byte(0x10,0); //highcolumnstartaddress for(n=0;n<128;n++) { OLED_WR_Byte(fill_Data,1); } }}voidDelay_50ms(unsignedintDel_50ms){ unsignedintm; for(;Del_50ms>0;Del_50ms--) for(m=6245;m>0;m--);}voidDelay_1ms(unsignedintDel_1ms){ unsignedcharj; while(Del_1ms--) { for(j=0;j<123;j++); }}//坐標(biāo)設(shè)置 voidOLED_Set_Pos(unsignedcharx,unsignedchary){ OLED_WR_Byte(0xb0+y,OLED_CMD); OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); OLED_WR_Byte((x&0x0f),OLED_CMD);} //開啟OLED顯示voidOLED_Display_On(void){ OLED_WR_Byte(0X8D,OLED_CMD);//SETDCDC命令 OLED_WR_Byte(0X14,OLED_CMD);//DCDCON OLED_WR_Byte(0XAF,OLED_CMD);//DISPLAYON}//關(guān)閉OLED顯示voidOLED_Display_Off(void){ OLED_WR_Byte(0X8D,OLED_CMD);//SETDCDC命令 OLED_WR_Byte(0X10,OLED_CMD);//DCDCOFF OLED_WR_Byte(0XAE,OLED_CMD);//DISPLAYOFF} voidOLED_Clear(void){ u8i,n; for(i=0;i<8;i++) { OLED_WR_Byte(0xb0+i,OLED_CMD);//設(shè)置頁地址(0~7) OLED_WR_Byte(0x00,OLED_CMD);//設(shè)置顯示位置-列低地址 OLED_WR_Byte(0x10,OLED_CMD);//設(shè)置顯示位置-列高地址 for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA); }//更新顯示}voidOLED_On(void){ u8i,n; for(i=0;i<8;i++) { OLED_WR_Byte(0xb0+i,OLED_CMD);//設(shè)置頁地址(0~7) OLED_WR_Byte(0x00,OLED_CMD);//設(shè)置顯示位置-列低地址 OLED_WR_Byte(0x10,OLED_CMD);//設(shè)置顯示位置-列高地址 for(n=0;n<128;n++)OLED_WR_Byte(1,OLED_DATA); }}voidOLED_ShowChar(u8x,u8y,u8chr,u8Char_Size){ unsignedcharc=0,i=0; c=chr-'';//得到偏移后的值 if(x>Max_Column-1){x=0;y=y+2;} if(Char_Size==16) { OLED_Set_Pos(x,y); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i],OLED_DATA); OLED_Set_Pos(x,y+1); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA); } else{ OLED_Set_Pos(x,y); for(i=0;i<6;i++) OLED_WR_Byte(F6x8[c][i],OLED_DATA); }}u32oled_pow(u8m,u8n){ u32result=1; while(n--)result*=m; returnresult;} //顯示2個(gè)字符//x,y:起點(diǎn)坐標(biāo) //len:數(shù)字的位數(shù)//size:字體大小//mode:模式0,填充模式;1,疊加模式//num:數(shù)值(0~4292967295);voidOLED_ShowNum(u8x,u8y,u32num,u8len,u8size2){ u8t,temp; u8enshow=0; for(t=0;t<len;t++) { temp=(num/oled_pow(10,len-t-1))%10; if(enshow==0&&t<(len-1)) { if(temp==0) { OLED_ShowChar(x+(size2/2)*t,y,'',size2); continue; }elseenshow=1; } OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2); }}//顯示一個(gè)字符號串voidOLED_ShowString(u8x,u8y,u8*chr,u8Char_Size){ unsignedcharj=0; while(chr[j]!='\0') { OLED_ShowChar(x,y,chr[j],Char_Size); x+=8; if(x>120){x=0;y+=2;} j++; }}voidOLED_ShowCHinese(u8x,u8y,u8no){ u8t,adder=0; OLED_Set_Pos(x,y); for(t=0;t<16;t++) { OLED_WR_Byte(Hzk[2*no][t],OLED_DATA); adder+=1;} OLED_Set_Pos(x,y+1); for(t=0;t<16;t++) { OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA); adder+=1;} }voidOLED_DrawBMP(unsignedcharx0,unsignedchary0,unsignedcharx1,unsignedchary1,unsignedcharBMP[]){ unsignedintj=0;unsignedcharx,y;if(y1%8==0)y=y1/8;elsey=y1/8+1; for(y=y0;y<y1;y++) { OLED_Set_Pos(x0,y);for(x=x0;x<x1;x++) { OLED_WR_Byte(BMP[j++],OLED_DATA); } }}//初始化SSD1306 voidOLED_Init(void){ GPIO_InitTypeDefGPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_12|GPIO_Pin_11); delay_ms(200);OLED_WR_Byte(0xAE,OLED_CMD); OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0x10,OLED_CMD); OLED_WR_Byte(0x40,OLED_CMD); OLED_WR_Byte(0xB0,OLED_CMD); OLED_WR_Byte(0x81,OLED_CMD); OLED_WR_Byte(0xFF,OLED_CMD); OLED_WR_Byte(0xA1,OLED_CMD); OLED_WR_Byte(0xA6,OLED_CMD); OLED_WR_Byte(0xA8,OLED_CMD); OLED_WR_Byte(0x3F,OLED_CMD); OLED_WR_Byte(0xC8,OLED_CMD);OLED_WR_Byte(0xD3,OLED_CMD); OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0xD5,OLED_CMD); OLED_WR_Byte(0x80,OLED_CMD); OLED_WR_Byte(0xD8,OLED_CMD); OLED_WR_Byte(0x05,OLED_CMD); OLED_WR_Byte(0xD9,OLED_CMD); OLED_WR_Byte(0xF1,OLED_CMD); OLED_WR_Byte(0xDA,OLED_CMD); OLED_WR_Byte(0x12,OLED_CMD); OLED_WR_Byte(0xDB,OLED_CMD); OLED_WR_Byte(0x30,OLED_CMD); OLED_WR_Byte(0x8D,OLED_CMD); OLED_WR_Byte(0x14,OLED_CMD); OLED_WR_Byte(0xAF,OLED_CMD);}u8lcd_show_buf[64]={0};voidOLED_Page(){ OLED_ShowCHinese(9,4,6);//溫 OLED_ShowCHinese(27,4,5);//度 sprintf((char*)lcd_show_buf,":%d",temperature); OLED_ShowString(45,4,lcd_show_buf,16); OLED_ShowCHinese(81,4,5);//度 OLED_ShowCHinese(9,6,7);//濕 OLED_ShowCHinese(27,6,5);//度 sprintf((char*)lcd_show_buf,":%d%%",humidity); OLED_ShowString(45,6,lcd_show_buf,16); }usart.c程序代碼:#include"sys.h"#include"usart.h"#include"string.h"#include"led.h"#include"key.h"#include"delay.h"#ifSYSTEM_SUPPORT_OS#include"includes.h"#endif#if1#pragmaimport(__use_no_semihosting)struct__FILE{ inthandle;};FILE__stdout;void_sys_exit(intx){ x=x;}//重定義fputc函數(shù)intfputc(intch,FILE*f){ while((USART1->SR&0X40)==0)//循環(huán)發(fā)送,直到發(fā)送完畢USART1->DR=(u8)ch; returnch;}#endif#ifEN_USART1_RXu8USART_RX_BUF[USART_REC_LEN];//接收狀態(tài)//bit15£? 接收完成標(biāo)志//bit14£? 接收到0x0d//bit13~0接收到的有效字節(jié)數(shù)目u16USART_RX_STA=0;//接收狀態(tài)標(biāo)記 voiduart_init(u32bound){ //GPIO端口設(shè)置 GPIO_InitTypeDefGPIO_InitStructure; USART_InitTypeDefUSART_InitStructure; NVIC_InitTypeDefNVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE); //使能USART1,GPIOA時(shí)鐘 //USART1_TXGPIOA.9 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//PA.9 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //復(fù)用推挽輸出 GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化GPIOA.9 //USART1_RX GPIOA.10初始化 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//PA10 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空輸入 GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化GPIOA.10 //Usart1NVIC配置 NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); //USART初始化設(shè)置 USART_InitStructure.USART_BaudRate=bound; 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);USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);USART_Cmd(USART1,ENABLE);}voidUSART1_IRQHandler(void)//串口1中斷服務(wù)程序{ u8Res;#ifSYSTEM_SUPPORT_OS OSIntEnter();#endif if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET) { Res=USART_ReceiveData(USART1); if((USART_RX_STA&0x8000)==0) { if(USART_RX_STA&0x4000) { if(Res!=0x0a)USART_RX_STA=0; elseUSART_RX_STA|=0x8000; } else { 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; } } } }#ifSYSTEM_SUPPORT_OS OSIntExit(); #endif} u16len;u16t;voidusart_data(){if(USART_RX_STA&0x8000)//接收到了完整數(shù)據(jù) { len=USART_RX_STA&0x3FFF; //得到此次接收的數(shù)據(jù)長度 if
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 信息技術(shù)(信創(chuàng)版)(微課版)課件全套 徐麗 項(xiàng)目1-6 計(jì)算機(jī)基礎(chǔ) - 其他常用軟件的應(yīng)用-1
- 十八項(xiàng)醫(yī)療核心制度解讀
- 2026年劇本殺運(yùn)營公司員工晉升與調(diào)崗管理制度
- 2026年及未來5年中國金融軟件行業(yè)市場競爭格局及投資前景展望報(bào)告
- 2025年社區(qū)智慧健康管理服務(wù)平臺技術(shù)創(chuàng)新與市場前景研究報(bào)告
- 體檢科各檢查室制度
- 產(chǎn)科護(hù)理與跨學(xué)科合作
- 人事四項(xiàng)制度
- 機(jī)動車檢測站培訓(xùn)內(nèi)容課件
- 中國科學(xué)院空間應(yīng)用工程與技術(shù)中心2025年校園招聘備考題庫及1套完整答案詳解
- 醫(yī)療器械胰島素泵市場可行性分析報(bào)告
- 地鐵施工現(xiàn)場防臺風(fēng)措施
- 種植業(yè)合作社賬務(wù)處理
- 【麗江玉龍旅游薪酬制度的創(chuàng)新研究6100字】
- 公司兩權(quán)分離管理制度
- 車輛叉車日常檢查記錄表
- 廣東高校畢業(yè)生“三支一扶”計(jì)劃招募考試真題2024
- 膠帶機(jī)硫化工藝.課件
- 種雞免疫工作總結(jié)
- 河南省商丘市柘城縣2024-2025學(xué)年八年級上學(xué)期期末數(shù)學(xué)試題(含答案)
- 河南省信陽市2024-2025學(xué)年高二上學(xué)期1月期末英語試題(含答案無聽力原文及音頻)
評論
0/150
提交評論