DS1302——C程序
时 间:11—5-4
标 注:用到了 PA_6,PA6--DQ
已在函数中将他们以寄存器形式配置,注再出再次配置PA时
不应在配置这些口;
在主函数中需将PA使能。
头函数:#include"ds18b20.h"
函 数:在ds18b20.h中有列举;
返回值:无;
#ifndef __DS18B20_H
#define __DS18B20_H
#include "stm32f10x.h"
u8 ds18b20(void); //read ds18b20
u8 read_ds18b20(void);//read ds18b20 a byte
void write_ds18b20(u8 val);//write ds18b20 a byte
u8 ds18b20_res(void); //res function return 0 sucess
#endif
//#include<intrins.h>
#include"stm32f10x.h"
#include"ds1302.h"
//sbit sclk=P1^6; GPIOa3
//sbit io=P3^5; &nb7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};//读地址
u8 read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};//写地址
u8 settime[7]={11,4,2,23,13,54,11};//年,周,月,日,时,分,秒
u8 d[7];
u8 disp[14];
void delayms(unsigned short time)//nms
{
unsigned short i, j;
for(; time > 0; time--){
for(j = 0; j < 10; j++){
for(i = 0; i < 1000; i++);
}
}
}
void writeds1302_dat(u8 dat)
{
u8 i;
for(i=0;i<8;i++)
{
GPIOA->ODR&=0xffef; //GPIOA->ODR&=0<<4;
GPIOA->ODR&=0xfff7; //GPIOA->ODR&=0<<3; //sclk=0;
if(dat&0x01)
GPIOA->ODR|=1<<4; //io=dat&0x01;
dat=dat>>1;
GPIOA->ODR|=1<<3; //sclk=1;
}
}
void write_ds1302_byte(u8 add,u8 dat)
{
GPIOA->ODR&=0xffdf;//GPIOA->ODR&=0<<5; //res=0;????????????
delayms(1); //_nop_();
GPIOA->ODR&=0xfff7;//GPIOA->ODR&=0<<3; //sclk=0;
delayms(1); //_nop_();
GPIOA->ODR|=1<<5; //GPIOA->ODR|=1<<5; //res=1;
delayms(1); //_nop_();
writeds1302_dat(add);
writeds1302_dat(dat);
GPIOA->ODR&=0xffdf;//GPIOA->ODR&=0<<5; //res=0;
delayms(1); //_nop_();
}
u8 read_ds1302_byte(u8 dat)
{
u8 i,temp;
GPIOA->ODR&=0xffdf;//GPIOA->ODR&=0<<5; //res=0;
delayms(1); //_nop_();
GPIOA->ODR&=0xfff7;//GPIOA->ODR&=0<<3;//sclk=0;
delayms(1); //_nop_();
GPIOA->ODR|=1<<5; //res=1;
delayms(1); //_nop_();
for(i=0;i<8;i++)
{
GPIOA->ODR&=0xffef;//GPIOA->ODR&=0;
GPIOA->ODR&=0xfff7;//GPIOA->ODR&=0<<3; //sclk=0;
if(dat&0x01)
GPIOA->ODR|=1<<4; //io=dat&0x01;
dat=dat>>1;
GPIOA->ODR|=1<<3; //sclk=1;
}
for(i=0;i<8;i++)
{
temp=temp>>1;
GPIOA->ODR&=0xfff7;//GPIOA->ODR&=0<<3; //sclk=0;
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4))
temp=temp|0x80;
GPIOA->ODR|=1<<3; //sclk=1;
}
GPIOA->ODR&=0xffdf;//GPIOA->ODR&=0<<5; //res=0;
delayms(1); //_nop_();
return temp;
}
/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
u8 table[]="2010-4-30";
u8 table1[3]="hjw";
u8 table2[2]="00";
void Delay(unsigned short time)//nms
{
unsigned short i, j;
for(; time > 0; time--){
for(j = 0; j < 10; j++){
for(i = 0; i < 1000; i++);
}
}
}
int main(void)
{
u8 i;
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
lcd1602_init();
ds1302set_time();
while (1)
{
ds1302read_time();
ds1302d_convert();
write1602_Achar(1,2,disp[8]); //显示 时
write1602_Achar(1,3,disp[9]);
write1602_Achar(1,4,0x0a);
write1602_Achar(1,5,disp[10]);//显示 分
write1602_Achar(1,6,disp[11]);
write1602_Achar(1,7,0x0a);
write1602_Achar(1,8,disp[12]); //秒
write1602_Achar(1,9,disp[13]);
i=ds18b20();
write1602_Achar(1,12,i/10); //温度十位
write1602_Achar(1,13,i);//个位
write1602_Achar(1,14,0XDF-0X30); //显示 ·
write1602_Achar(1,15,0X63-0X30);//显示 C 即为温度 ‘C 符号
}
}
void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB
| RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
//NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
文章评论(0条评论)
登录后参与讨论