DS1302驅動和數碼管驅動程序如附件!
單片機源程序如下:
- #include "ds18b20.h"
- #include "display.h"
- #include "delay.h"
- void Ds18B20_Output()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE); //使能PG時鐘
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11; //PG11口
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //設定I/O口推挽輸出
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOG,&GPIO_InitStructure);
- }
- void Ds18B20_Input()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE); //使能PG時鐘
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11; //PG11口
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空輸入
- GPIO_Init(GPIOG,&GPIO_InitStructure);
- }
- void Ds18b20_Reset(void)
- {
- Ds18B20_Output(); //將PG11配置為推挽輸出
- DQ_Write_1; //拉高總線
- delayus(1);
- DQ_Write_0; //拉低總線
- delayus(480); //延時,拉低總線480~960us
- DQ_Write_1; //釋放總線
- Ds18B20_Input(); //DQ改為輸入模式
- delayus(40); //延時約60us
- while((DQ_ReadBit)); //等待從機DS18B20應答
- while(!(DQ_ReadBit)); //等待應答信號結束,釋放總線
- }
- void Ds18b20_Write(u8 dat)
- {
- u8 m0;
- Ds18B20_Output(); //將PG11配置為推挽輸出
- for(m0=0;m0<8;m0++)
- {
- DQ_Write_0; //拉低總線
- delayus(10); //延時10us,最大不超過15us
- if(dat&0x01)
- DQ_Write_1;
- else
- DQ_Write_0;
- delayus(30); //延時40us
- DQ_Write_1; //釋放總線
- delayus(1); //兩個寫之間,間隔至少1us
- dat>>=1; //右移1位,
- }
- }
- u8 Ds18b20_Read(void)
- {
- u8 m0,temp0;
- for(m0=0;m0<8;m0++)
- {
- temp0>>=1; //數據右移一位
- Ds18B20_Output(); //將PG11配置為推挽輸出
- DQ_Write_0; //拉低總線,啟動
- DQ_Write_1; //釋放總線
- if(DQ_ReadBit==1)
- temp0|=0x80;
- delayus(40);
- }
- return temp0;
- }
- void temperature(void)
- {
- u16 temp1,temp2;
- Ds18b20_Reset();
- Ds18b20_Write(0xcc); //跳過ROM
- Ds18b20_Write(0x44); //溫度轉換
- Ds18b20_Reset();
- Ds18b20_Write(0xcc); //跳過ROM
- Ds18b20_Write(0xbe); //讀取RAM
-
- temp1=Ds18b20_Read(); //讀取低8位
- temp2=Ds18b20_Read(); //讀取高8位
- Ds18b20_Reset(); //復位,表示讀取結束
- display(((temp2<<8)|temp1)*0.0625);
- }
復制代碼
注意缺少main函數等,介意的就不要下載了:
HARDWARE.rar
(2.22 KB, 下載次數: 30)
2020-11-24 11:19 上傳
點擊文件名下載附件
|