/*----金牛开发板_STM32107VCT6----*/
/*灯脚:LED2=PD2; LED3=PD3; LED4=PD4; LED5=PD7; */
/*按键:Button3=PC13; */
#include"map.h" //寄存器地址及结构包含
#include"stm32f10x_vector.c" //中断向量表及中断函数
#include"ILI93xx.c" //LCD
void RCC_DeInit(void)
{
RCC->APB1RSTR=0x0; //APB1复位
RCC->APB2RSTR=0x0; //APB2复位
RCC->AHBENR=0x00000014; //Flash与SRAM开启,其它关闭
RCC->APB1ENR=0x0; //外部时钟关闭
RCC->APB2ENR=0x0; //外部时钟关闭
RCC->CR |= 0x01; //使能内部晶振
RCC->CFGR &= 0xF8FF0000; //复位:SW[1:0],HPRE[3:0],PPRE1[2:0],PPRE2[2:0],ADCPRE[1:0],MCO[2:0]
RCC->CR &= 0xFEF6FFFF; //复位:HSEON,CSSON,PLLON
RCC->CR &= 0xFFFBFFFF; //复位:HSEBYP
RCC->CFGR &= 0xFF80FFFF; //复位:PLLSRC,PLLXTPRE,PLLMUL[3:0],USBPRE
RCC->CIR=0x0; //关闭所有中断
#ifdef VECT_TAB_RAM //该关键字在开发环境设置中索引
SCB->ExceptionTableOffset = 0X20000000; //RAM启动
#else
SCB->ExceptionTableOffset = 0X8000000; //FLASH启动
#endif
}
void RCC_Configuration()
{
u16 i=0;
const u16 N=65535;
RCC->CR |= 1<<0; //HSI启振为时钟源
for (i=0; !(0x2 & RCC->CR) && i<=N; i++); //等待HSI启振稳定
//RCC->CR |= 0x00010000; //HSE启振为时钟源
//for (i=0; !(0x00020000 & RCC->CR) && i<=N; i++); //等待HSE启振稳定
if (i<=N)
{
FLASH->ACR |= 0x10; //Enable Prefetch Buffer
FLASH->ACR |= 0x10; //Sets the Latency value
RCC->CFGR |= 0x0; //HCLK=SYSCLK
RCC->CFGR |= 0x0; //PCLK2=HCLK
RCC->CFGR |= 0x400; //PCLK1=HCLK/2
RCC->CFGR |= 0x001C0000; //PLLCLK=HSI*9=72MHz
RCC->CR |= 0x01000000; //PLL启振
for (i=0; !(0x02000000 & RCC->CR) && i<=N; i++); //等待PLL稳定
RCC->CFGR |= 0x2; //PLL选为系统时钟源
i=0;
while (!((RCC->CFGR & 0xC)>>2 == 0x2) && i<=N) i++; //等待SYSCLK稳定
}
}
void delay()
{
u32 i=0;
for (i=0; i<0X8FFF; i++);
}
void RTC_Configuration()
{
NVIC->Enable[0]=1<<3; //中断使能
// if (BKP->DR1!=0x5050)
{
RCC->APB1ENR |= 1<<28; //电源使能
RCC->APB1ENR |= 1<<27; //备份使能
PWR->CR |= 1<<8; //取消备份区写保护
RCC->BDCR |= 1<<16; //备分区域外软复位
RCC->BDCR &=~(1<<16); //备份区域软复位结束
RCC->BDCR |= 1<<0; //开启LSE
while (!(RCC->BDCR&0x02)); //等待LSE就绪
RCC->BDCR |= 1<<8; //LSE作为RTC时钟
RCC->BDCR |= 1<<15; //RTC使能
RTC->CRH =0x7; //闹钟中断
RTC->CRL = 1<<4; //进入配置
RTC->PRLH=0x0;
RTC->PRLL=32767; //预分频
RTC->CNTH=0;
RTC->CNTL=100;
RTC->ALRH=0;
RTC->ALRL=105;
RTC->CRL &=~1<<4; //退出配置
while (!(RTC->CRL & (1<<5))); //写完成
while (!(RTC->CRL & (1<<3))); //等待同步
// BKP->DR1=0x5050;
}
/* else
{
RTC->CRH |=0x1;
while (!(RTC->CRL & (1<<5))); //写完成
while (!(RTC->CRL & (1<<3))); //等待同步
}*/
}
void RTC_IRQHandler(void)
{
if (RTC->CRL & 0x2) //每一秒条件
{
RTC->CRL = 1<<4; //进入配置
RTC->PRLH=0x0;
RTC->CNTL=100;
RTC->CRL &=~1<<4; //退出配置
LCD_Clear(BLUE);
LCD_ShowString(30,110,"miao-S"); //显示字符
}
else if (RTC->CRL & 0x1) //闹钟条件
LCD_ShowString(30,110,"clock");
RTC->CRL &= 0x0FF8; //清除以上三个标志(秒,闹钟,溢出)
while (!(RTC->CRL &(1<<5))); //等待RTC完成
LCD_ShowNum(30,90,RTC->CNTL,4,16);
GPIOD->BSRR=1<<2|1<<3|1<<4|1<<7; //灯亮
delay();
GPIOD->BRR=1<<2|1<<3|1<<4|1<<7; //灯灭
}
void LED_Configuration()
{ //灯
RCC->APB2ENR=1<<5; //使能APB2时钟
GPIOD->CRL=0X30033300; //配置输出速度50MHz
GPIOD->ODR=0x0;
}
void main()
{
RCC_DeInit(); //初始化RCC
RCC_Configuration();
RTC_Configuration();
LED_Configuration();
LCD_Init();
LCD_Clear(BLUE);
POINT_COLOR=RED;
LCD_ShowString(30,50,"RTC");
LCD_ShowString(30,70,"2011-1-18 Write");
POINT_COLOR=BLUE;
while (1);
}
/*程序说明:每程序每1秒钟,中断函数执行灯闪一次,并在LCD显示第几秒。达到某一闹钟条件值时LCD显示闹钟*/
QQ:280704150 有问题大家和我交流!谢谢大家的关注!
IAR4.42工程文件:
007 RTC_实时时钟.rar
(64.06 KB)
|