相关代码
void ADCTEMP_Configuration(void)
{
/* 允许ADC */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
/* ADC1 */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 1, ADC_SampleTime_239Cycles5);
ADC_TempSensorVrefintCmd(ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
//DMA的配置
void DMA_Configuration(void)
{ NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)( &(ADC1->DR)); DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADCCov; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 16;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //半字
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void DMAReConfig(void)
{
DMA_DeInit(DMA1_Channel1);
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
int main(void)
{
SystemInit();
RCC_GetClocksFreq(&RCC_ClockFreq);
GPIO_Configuration();
ADCTEMP_Configuration();
DMA_Configuration();
TestLCDS();
while (1)
{
if(ADC_Ok==1)
{
ADC_Ok=0;
ADCResult=DigitFilter(ADCCov,16);
DMAReConfig();
ADCResult= ADCResult*0.814;
a= ADCResult/1000;
b= ADCResult%1000/100;
c= ADCResult%100/10;
d= ADCResult%10;
byte_disp(5,8,a+'0');
byte_disp(5,9,'.');
byte_disp(5,10,b+'0');
byte_disp(5,11,c+'0');
byte_disp(5,12,d+'0');
}
/*
ADCResult=(1.42 - ADCResult*3.3/4096)*1000/4.35 + 250
a = ADCResult/1000;
b = (ADCResult - a*1000)/100;
c = (ADCResult - a*1000 - b*100)/10;
d = ADCResult - a*1000 - b*100 - c*10;
*/
}
}
ARM DIY进程5:RTC
曾经玩过HT1380实时时钟,觉得玩STM32的这个RTC应该也没有什么大不了的。岂料真的进入了。才知道并非易事。STM32的RTC只是个简单的秒中断定时器,它的年月日时分秒要存储在备份寄存器中。使用时要开放stm32f10x_bkp.h、stm32f10x_pwr.h和stm32f10x_rtc.h。还要使能内部的RCC_APB1Periph_PWR 和RCC_APB1Periph_BKP,还要开放RTC中断。年月日时分秒,闰年等等都要自己计算。晕!
如果你想要在DIY开放板上COPY范例代码来直接套用,由于硬件方面牵涉到LCD显示电路引脚的不同和显示代码的差异,真的不像运行跑马灯那样简单了。费了九牛二虎之力,总算让RTC时钟跑起来了。图1中显示结果是相隔1秒拍的2张照片。你的脑子肯定比我聪明,希望你的RTC做的比我的好。
图1 RTC时钟动了
部分代码(看了头真的有点晕)
//主函数
#include "stm32f10x.h"
#include "user.h"
#include "LCD.C"
#include "RTC.C"
int main(void)
{
SystemInit();
RCC_GetClocksFreq(&RCC_ClockFreq);
GPIO_Configuration();
TestLCDS();
RTC_Configuration();
Clock_Adjustment();
while (1)
{
byte_disp(5,0,' ');
byte_disp(5,1,' ');
byte_disp(5,6,((RealTime.hour)/10)+0x30);
byte_disp(5,7,((RealTime.hour)%10)+0x30);
byte_disp(5,8,':');
byte_disp(5,9,((RealTime.minute)/10)+0x30);
byte_disp(5,10,((RealTime.minute)%10)+0x30);
byte_disp(5,11,':');
byte_disp(5,12,((RealTime.second)/10)+0x30);
byte_disp(5,13,((RealTime.second)%10)+0x30);
byte_disp(4,0,' ');
byte_disp(4,1,' ');
byte_disp(4,5,'2');
byte_disp(4,6,'0');
byte_disp(4,7,((RealTime.year)/10)+0x30);
byte_disp(4,8,((RealTime.year)%10)+0x30);
byte_disp(4,9,'/');
byte_disp(4,10,((RealTime.month)/10)+0x30);
byte_disp(4,11,((RealTime.month)%10)+0x30);
byte_disp(4,12,'/');
byte_disp(4,13,((RealTime.date)/10)+0x30);
byte_disp(4,14,((RealTime.date)%10)+0x30);
}
}
//RTC函数
#include "stm32f10x.h"
void RTC2Time(void);
void TimeToRTC(T_STRUCT* time);
void Clock_Adjustment(void) ;
void RTC_IRQHandler(void);
void GetTime(T_STRUCT* time);
static u8 RTC_Blank=0;
void RTC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(32776);
RTC_WaitForLastTask();
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
RTC_Blank = 1;
}
else
{
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC2Time();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
}
RCC_ClearFlag();
}
//----------------------------
extern const u8 Month2Day_Tab[12];
void RTC2Time(void)
{
u32 count;
u8 tmp,change = 0;
RealTime.year = BKP_ReadBackupRegister(BKP_DR2);
RealTime.month = BKP_ReadBackupRegister(BKP_DR3);
RealTime.date = BKP_ReadBackupRegister(BKP_DR4);
RealTime.day = BKP_ReadBackupRegister(BKP_DR5);
RTC_ITConfig(RTC_IT_SEC, DISABLE);
count = RTC_GetCounter();
while(count >= 0x0001517f)
{ change = 1;
count -= 0x0001517f;
if((++RealTime.day) >= 8)
RealTime.day = 1;
if(RealTime.month == 2)
{
if(RealTime.year%4)
tmp = 28;
else
tmp = 29;
}
else
{
tmp = Month2Day_Tab[RealTime.month-1];
}
if((++RealTime.date)>tmp)
{
RealTime.date = 1;
if((++RealTime.month)>12)
{
RealTime.month = 1;
if((++RealTime.year) >= 100)
{
RealTime.year = 0;
}
}
}
}
RealTime.hour = count/3600;
RealTime.minute = (count%3600)/60;
RealTime.second = (count%3600)%60;
if(change)
{
RTC_SetCounter(count);
BKP_WriteBackupRegister(BKP_DR5,RealTime.day);
BKP_WriteBackupRegister(BKP_DR4,RealTime.date);
BKP_WriteBackupRegister(BKP_DR3,RealTime.month);
BKP_WriteBackupRegister(BKP_DR2,RealTime.year);
}
RTC_ITConfig(RTC_IT_SEC, ENABLE);
}
//------------------------
void TestRtc(void)
{
if(RTC_Blank)
Clock_Adjustment();
}
//---------------------------
void TimeToRTC(T_STRUCT* time)
{
u32 count;
RTC_ITConfig(RTC_IT_SEC, DISABLE);
RTC_WaitForLastTask();
RealTime.year = time->year;
RealTime.month = time->month;
RealTime.date = time->date;
RealTime.hour = time->hour;
RealTime.minute = time->minute;
RealTime.second = time->second;
RealTime.day = time->day;
BKP_WriteBackupRegister(BKP_DR5,RealTime.day);
BKP_WriteBackupRegister(BKP_DR4,RealTime.date);
BKP_WriteBackupRegister(BKP_DR3,RealTime.month);
BKP_WriteBackupRegister(BKP_DR2,RealTime.year);
count = RealTime.hour*3600+RealTime.minute*60+RealTime.second;
RTC_WaitForLastTask();
RTC_SetCounter(count);
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
}
//--------------------------------
void Clock_Adjustment(void)
{
T_STRUCT time;
u8 d[14];
d[0]=1;
d[1]=2;
d[2]=0;
d[3]=4;
d[4]=1;
d[5]=6;
d[6]=1;
d[7]=1;
d[8]=5;
d[9]=0;
d[10]=0;
d[11]=0;
d[12]=1;
time.year = d[0]*10+d[1];
time.month = d[2]*10+d[3];
time.date = d[4]*10+d[5];
time.hour = d[6]*10+d[7];
time.minute = d[8]*10+d[9];
time.second = d[10]*10+d[11];
time.day = d[12];
TimeToRTC(&time);
}
//--------------------------
extern T_STRUCT RealTime;
void GetTime(T_STRUCT* time)
{
RTC_ITConfig(RTC_IT_SEC, DISABLE);
RTC_WaitForLastTask();
time->year = RealTime.year;
time->month = RealTime.month;
time->date = RealTime.date;
time->hour = RealTime.hour;
time->minute = RealTime.minute;
time->second = RealTime.second;
time->day = RealTime.day;
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
}
//----------------------
//中断函数
//----------------------
T_STRUCT RealTime;
void RTC_IRQHandler(void)
{
u8 tmp;
if(RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
RTC_WaitForLastTask();
RTC_ClearITPendingBit(RTC_IT_SEC);
if((++RealTime.second)>59)
{
RealTime.second=0;
if((++RealTime.minute)>59)
{
RealTime.minute=0;
if((++RealTime.hour)>23)
{
RealTime.hour=0;
if((++RealTime.day)>=8)
RealTime.day=1;
BKP_WriteBackupRegister(BKP_DR5,RealTime.day);
if(RealTime.month==2)
{
if(RealTime.year%4)
tmp=28;
else
tmp=29;
}
else
{
tmp=Month2Day_Tab[RealTime.month-1];
}
if((++RealTime.date)>tmp)
{
RealTime.date = 1;
if((++RealTime.month)>12)
{
RealTime.month=1;
if((++RealTime.year)>99)
{
RealTime.year=0;
}
BKP_WriteBackupRegister(BKP_DR2,RealTime.year);
}
BKP_WriteBackupRegister(BKP_DR3,RealTime.month);
}
BKP_WriteBackupRegister(BKP_DR4,RealTime.date);
}
}
}
RTC_WaitForLastTask();
if(RTC_GetCounter() >= 0x0001517f)
RTC_SetCounter(0x0);
}
}
//-----------
//中断初始化
//-----------
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
ARM DIY进程6:Usart
串口通讯的练习,将ARM开发板的COM1与PC机的COM1连接,通讯协议设置为8位数据位,1位停止位,无奇偶校验,9600波特率。开发板上要显示的信息用fprint()发送到PC机。PC机用串口调试软件SSCOM,PC机成为一个显示终端。调试中的变量可以以十进制、十六进制、字符式在PC机上显示。
记得将options-Target-Use MicroLIB选项打勾。
图1
上图是运行结果。以后就可以用这个调试环境来跟踪变量了。
代码
#include "stm32f10x.h"
int main(void)
{
u32 a;
ChipHalInit();
ChipOutHalInit();
a=0x23;
printf("变量a用无符号十进制形式显示:%u\r\n",a);
printf("变量a用十六进制形式显示:%x\r\n",a);
printf("变量a用字符形式显示:%c\r\n",a);
while(1);
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|
RCC_APB2Periph_AFIO |
RCC_APB2Periph_USART1 ,
ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
int fputc(int ch)
{
USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return ch;
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
vscode+cmake搭建雅特力AT32L021开发环境被打赏30分 | |
【换取逻辑分析仪】自制底板并驱动ArduinoNanoRP2040ConnectLCD扩展板被打赏47分 | |
【分享评测,赢取加热台】RISC-V GCC 内嵌汇编使用被打赏38分 | |
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 |