新聞中心

STM32 DS1302

作者: 時間:2012-09-10 來源:網(wǎng)絡(luò) 收藏

#ifndef ___H

本文引用地址:http://2s4d.com/article/148439.htm

#define ___H

#include stm32f10x.h

extern u8 d[7]; //依次為年,周,月,日,時,分,秒

extern u8 disp[14];//依次為年[0]【1】;周[2]【3】;月[4]【5】

//***********************日[6]【7】;時[8]【9】;分[10]【11】;秒[12]【13】

void ds1302d_convert(void); //如果用低disp[];必須在read_time();后調(diào)用此函數(shù);

void ds1302set_time(void); //對時函數(shù)

void ds1302read_time(void); //讀取時間

#endif /* __F10x_IT_H */

——C程序

時 間:11—5-4

標 注:用到了 PA_3—5,PA3_5位控制為依次sclk---GPIOa3,io----GPIOa4,res----GPIOa5

已在函數(shù)中將他們以寄存器形式配置,注再出再次配置PA時

不應(yīng)在配置這些口;

在主函數(shù)中需將PA使能。

頭函數(shù):#includeds1302.h

函 數(shù):在ds1302.h中有列舉;

返回值:無;

/*************************

// ds1302,定時,

**************************/

//#include

#includestm32f10x.h

#includeds1302.h

/***************************/

//sbit sclk=P1^6; GPIOa3

//sbit io=P3^5; GPIOa4

//sbit res=P1^7; GPIOa5

/***************************/

u8 write_add[7]={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;i8;i++)

{

GPIOA->ODR=0xffef; //GPIOA->ODR=04;

GPIOA->ODR=0xfff7; //GPIOA->ODR=03; //sclk=0;

if(dat0x01)

GPIOA->ODR|=14; //io=dat0x01;

dat=dat>>1;

GPIOA->ODR|=13; //sclk=1;

}

}

/***********************/

void write_ds1302_byte(u8 add,u8 dat)

{

GPIOA->ODR=0xffdf;//GPIOA->ODR=05; //res=0;????????????

delayms(1); //_nop_();

GPIOA->ODR=0xfff7;//GPIOA->ODR=03; //sclk=0;

delayms(1); //_nop_();

GPIOA->ODR|=15; //GPIOA->ODR|=15; //res=1;

delayms(1); //_nop_();

writeds1302_dat(add);

writeds1302_dat(dat);

GPIOA->ODR=0xffdf;//GPIOA->ODR=05; //res=0;

delayms(1); //_nop_();

}

/***************************/

u8 read_ds1302_byte(u8 dat)

{

u8 i,temp;

GPIOA->ODR=0xffdf;//GPIOA->ODR=05; //res=0;

delayms(1); //_nop_();

GPIOA->ODR=0xfff7;//GPIOA->ODR=03;//sclk=0;

delayms(1); //_nop_();

GPIOA->ODR|=15; //res=1;

delayms(1); //_nop_();

for(i=0;i8;i++)

{

GPIOA->ODR=0xffef;//GPIOA->ODR=0;

GPIOA->ODR=0xfff7;//GPIOA->ODR=03; //sclk=0;

if(dat0x01)

GPIOA->ODR|=14; //io=dat0x01;

dat=dat>>1;

GPIOA->ODR|=13; //sclk=1;

}

for(i=0;i8;i++)

{

temp=temp>>1;

GPIOA->ODR=0xfff7;//GPIOA->ODR=03; //sclk=0;

if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4))

temp=temp|0x80;

GPIOA->ODR|=13; //sclk=1;

}

GPIOA->ODR=0xffdf;//GPIOA->ODR=05; //res=0;

delayms(1); //_nop_();

return temp;

}

//**************************

void dat_pros()

{

u8 i,temp;

for(i=0;i7;i++)

{

temp=settime[i]/10;

settime[i]=settime[i]%10+temp*16; //*************trans to BCD

}

}

//******************************

void ds1302set_time()

{

u8 i;

GPIOA->CRL=0XFF000FFF;

GPIOA->CRL|=0X00333000; //PA3-4推挽輸出

GPIOA->ODR|=73; //PA3-4置高電平

dat_pros();

write_ds1302_byte(0x8e,0x00); //************del wr protect

for(i=0;i7;i++)

{

write_ds1302_byte(write_add[i],settime[i]);

}

write_ds1302_byte(0x8e,0x80);//**************add wr protect

}

//*****************************

void ds1302d_convert()

{

disp[0]=d[0]/10;

disp[1]=d[0]%10;

disp[2]=d[1]/10;

disp[3]=d[1]%10;

disp[4]=d[2]/10;

disp[5]=d[2]%10;

disp[6]=d[3]/10;

disp[7]=d[3]%10;

disp[8]=d[4]/10;

disp[9]=d[4]%10;

disp[10]=d[5]/10;

disp[11]=d[5]%10;

disp[12]=d[6]/10;

disp[13]=d[6]%10;

}

void ds1302read_time()

{

u8 i;

for(i=0;i7;i++)

{

settime[i]=read_ds1302_byte(read_add[i]);

}

d[0]=settime[0]/16*10+settime[0]%16;

d[1]=settime[1]/16*10+settime[1]%16;

d[2]=settime[2]/16*10+settime[2]%16;

d[3]=settime[3]/16*10+settime[3]%16;

d[4]=settime[4]/16*10+settime[4]%16;

d[5]=settime[5]/16*10+settime[5]%16;

d[6]=settime[6]/16*10+settime[6]%16;

//d_convert();

}

應(yīng)用舉例

main.c

#include stm32f10x.h

#include1602.h

#includeds1302.h

EXTI_InitTypeDef EXTI_InitStructure;

ErrorStatus HSEStartUpStatus;

/* Private function prototypes -----------------------------------------------*/

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;

/* System Clocks Configuration */

RCC_Configuration();

/* NVIC configuration */

NVIC_Configuration();

/* Configure the GPIO ports */

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]);//顯示秒

}

}

void RCC_Configuration(void)

{

RCC_DeInit();

/* Enable HSE */

RCC_HSEConfig(RCC_HSE_ON);

/* Wait till HSE is ready */

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)

{

/* Enable Prefetch Buffer */

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Flash 2 wait state */

FLASH_SetLatency(FLASH_Latency_2);

/* HCLK = SYSCLK */

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1);

/* PCLK1 = HCLK/2 */

RCC_PCLK1Config(RCC_HCLK_Div2);

/* PLLCLK = 8MHz * 9 = 72 MHz */

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */

RCC_PLLCmd(ENABLE);

/* Wait till PLL is ready */

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

{

}

/* Select PLL as system clock source */

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait till PLL is used as system clock source */

while(RCC_GetSYSCLKSource() != 0x08)

{

}

}

/* Enable Key Button GPIO Port, GPIO_LED and AFIO clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB

| RCC_APB2Periph_AFIO, ENABLE);

}

/*******************************************************************************

* Function Name : GPIO_Configuration

* Description : Configures the different GPIO ports.

* Input : None

* Output : None

* Return : None

*******************************************************************************/

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

/* Set the Vector Table base location at 0x20000000 */

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

#else /* VECT_TAB_FLASH */

/* Set the Vector Table base location at 0x08000000 */

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

#endif

}



關(guān)鍵詞: DS1302 STM32

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉