/**
************************************************** ****************************
* @file CYE-STM32F4-C V1.0/main.c
*
@Author CYESS Howard Lin <
howard.lin@cyess.com>& Green Yeh
* @version V1.0.0
* @date 2015/01/31 main
* @brief Main program body
*/
#include "main.h"
#define MESSAGE1 "CYESS UART Example"
#define MESSAGE2 " Testing on "
#define MESSAGE3 " UART1"
//------------------------------------------------ ----------//
#define SCLK_H GPIOA->BSRRL = GPIO_Pin_0
#define SCLK_L GPIOA->BSRRH = GPIO_Pin_0
#define DSIO GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)
#define CE_H GPIOA->BSRRL = GPIO_Pin_2
#define CE_L GPIOA->BSRRH = GPIO_Pin_2
//------------------------------------------------ -----------//
void Ds1302ReadTime(void);
void Ds1302Init(void);
__IO uint8_t USARTFLAG=0;
__IO uint32_t TimingDelay = 0;
USART_InitTypeDef USART_InitStructure;
uint8_t READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uint8_t WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
//sec min Hour data month Day Year
uint8_t TIME[7] = {0x30, 0x34, 0x03, 0x22, 0x12, 0x06, 0x18};
//-----------------DS1302 Function----------------------------- //
void GPIO_Config(void);
void Ds1302Write(uint8_t addr, uint8_t dat);
void GPIO_Config_DSIO_READ(void);
uint8_t Ds1302Read(uint8_t addr, uint8_t loop);
//------------------------------------------------ ----//
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength);
__IO TestStatus TransferStatus1 = FAILED;
/* Private function prototypes --------------------------------------------- --*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
int main(void)
{
SystemCoreClockUpdate();
if (SysTick_Config(SystemCoreClock /1000)){
while (1);
}
GPIO_Config();
Ds1302Init();
while(1)
{
Ds1302ReadTime();
printf("TIM ARRAY 20%0.2x-%0.2x-%0.2x-%0.2x %0.2x:%0.2x:%0.2x
",TIME[6],TIME[5],TIME[4] ,TIME[3],TIME[2],TIME[1],TIME[0]);
Delay(950);
if(TIME[2]==20&&TIME[1]==30)
{
printf("Alarm Running
");
}
}
}
//---------------------DS1302-------------------------- ----//
void Ds1302Write(uint8_t addr, uint8_t dat)
{
uint8_t n;
Delay(1);
GPIO_WriteBit(GPIOA,GPIO_Pin_2 ,0); //CE=0
Delay(1);
SCLK_L;//珂蔚SCLK離腴萇﹝
Delay(1);
GPIO_WriteBit(GPIOA,GPIO_Pin_2 ,1); //綴蔚RST(CE)離詢萇﹝
Delay(10);
addr = addr & 0xFE;
for (n = 0; n < 8; n++)
{
if (addr & 0x01)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_1 ,1);
}
else
{
GPIO_WriteBit(GPIOA,GPIO_Pin_1,0);
}
SCLK_H; //產生時鐘
Delay(1);
SCLK_L;
addr = addr >> 1;
}
for (n=0; n<8; n++)//寫入要讀取的address;
{
if(dat&0x01)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_1 ,1); //DSIO
}
else
{
GPIO_WriteBit(GPIOA,GPIO_Pin_1 ,0);//DSIO
}
SCLK_H; //產生時鐘
Delay(1);
SCLK_L;
dat = dat >> 1;
}
GPIO_WriteBit(GPIOA,GPIO_Pin_1 ,0);//DSIO SET 0;
}
//=========DS2302 Read Function=================//
uint8_t Ds1302Read(uint8_t addr,uint8_t loop)
{
uint8_t n,dat,dat1;
GPIO_WriteBit(GPIOA,GPIO_Pin_2 ,0);
Delay(1);
GPIO_WriteBit(GPIOA,GPIO_Pin_0 ,0);// SCLK 低電位
Delay(1);
GPIO_WriteBit(GPIOA,GPIO_Pin_2 ,1);//Chip enable//
Delay(1);
//printf("READ_RTC_ADDR: %0.2x
",addr);
for (n=0; n<8; n++)//寫入要讀取的address;
{
GPIO_WriteBit(GPIOA,GPIO_Pin_1 ,addr & 0x01);
addr >>= 1;
SCLK_H;
Delay(1);
SCLK_L;
Delay(1);
}
Delay(1);
GPIO_Config_DSIO_READ();
// start to reading.......//
for(n=0;n<8;n++)
{
dat1=GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);
Delay(1);
for(n=0;n<8;n++)
{
dat>>=1;
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)==1)dat|=0x80;
SCLK_H;
Delay(1);
SCLK_L;
Delay(1);
}
}
GPIO_Config();
Delay(1);
GPIO_WriteBit(GPIOA,GPIO_Pin_2 ,0);
Delay(1); //以下是DS1302復位穩定時間,.必須。
SCLK_H;
Delay(1);
GPIO_WriteBit(GPIOA,GPIO_Pin_1,0); //DSIO=0
Delay(1);
GPIO_WriteBit(GPIOA,GPIO_Pin_1,1); //DSIO=1
Delay(1);
TIME[loop]=dat;
return dat;
}
void Ds1302Init()
{
uint8_t n;
Ds1302Write(0x8E,0X00);
for (n=0; n<7; n++)//
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80);
}
//======================================//
void Ds1302ReadTime()
{
uint8_t n;
for (n=0; n<7; n++)//抈?7嗅劘據?瞣?券懠????
{
TIME[n]=Ds1302Read(READ_RTC_ADDR[n],n);
}
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA ,ENABLE);
//PORTB 0 is SCLK
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PORTB 1 is DSIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//RST
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void GPIO_Config_DSIO_READ(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA ,ENABLE);
//PORTB 1 is DSIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_DS;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*1ms delay for Delay(1)*/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/*Handle the time decrecing*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00){
TimingDelay--;
}
}
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength)
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}
pBuffer1++;
pBuffer2++;
}
return PASSED;
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* eg write a character to the USART */
USART_SendData(EVAL_COM1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}
return ch;
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1){}
}
#endif
一周热门 更多>