#include "stm32f10x.h"
u8 a[]="Current time is 00:00:00\r\n";
u8 count_10ms;
u8 hour,min,sec;
void delay(uint32_t n)
{
uint32_t i;
for(i=n;i>0;i--);
}
void GPIO_Configration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void USART2_Configration(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits =USART_StopBits_1;
USART_InitStructure.USART_Parity =USART_Parity_No;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void U2_PutChar(u8 ch)
{
USART_GetFlagStatus(USART2, USART_FLAG_TC);
USART_SendData(USART2, ch);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET)
{}
}
void U2_Printf(u8 *pBuff)
{
while(*pBuff!='\0')
{
U2_PutChar(*pBuff++);
}
}
//定时器移植
void NVIC_Configration(void)
{
NVIC_InitTypeDef aaa;
aaa.NVIC_IRQChannel = TIM3_IRQn;
aaa.NVIC_IRQChannelPreemptionPriority = 0;
aaa.NVIC_IRQChannelSubPriority = 2;
aaa.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&aaa);
}
void TIMER3_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler=71;
TIM_TimeBaseStructure.TIM_Period=10000-1;
TIM_TimeBaseStructure. TIM_CounterMode= TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3, ENABLE); //开启时钟
}
int main()
{
GPIO_Configration();
USART2_Configration();
NVIC_Configration();
TIMER3_Init();
while(1)
{
a[16]=hour/10+0x30;
a[17]=hour%10+0x30;
a[19]=min/10+0x30;
a[20]=min%10+0x30;
a[22]=sec/10+0x30;
a[23]=sec%10+0x30;
U2_Printf(a);
delay(1000000);
}
}
|