一、模块
USART2+RTC
二、实现功能
在AccessPort软件中输入C,返回电压;
输入T,返回RTC时间。
三、代码
1.初始化函数
tx.c
#include "tx.h"
void Adc1_Init(void){
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
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_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_adc(void){
u16 temp;
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==0);
temp=ADC_GetConversionValue(ADC1);
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
return temp;
}
void Usart2_Init(void){
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_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;
/* Configure USARTy */
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void Send_string(u8 *str){
u8 i=0;
while(str[i]!=0){
USART_SendData(USART2,str[i]);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==0);
i++;
}
}
void RTC_Init(u8 HH,u8 MM,u8 SS){
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the RTC Interrupt */
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);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(40000-1);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
RTC_SetCounter(3600*HH+60*MM+SS);
RTC_WaitForLastTask();
}
tx.h
/*
程序说明: CT117E嵌入式竞赛板LCD驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT117E嵌入式竞赛板
日 期: 2011-8-9
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TX_H
#define __TX_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
void Adc1_Init(void);
u16 Get_adc(void);
void Usart2_Init(void);
void Send_string(u8 *str);
void RTC_Init(u8 HH,u8 MM,u8 SS);
#endif /* __TX_H */
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
2.中断函数
stm32f10x_it.c中
extern u8 RXD_flag;
extern u8 RXD_buf[20];
u16 RXD_count=0;
void USART2_IRQHandler(void)
{
u16 temp;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
temp = USART_ReceiveData(USART2);
if(temp=='\n'){
RXD_flag=1;
RXD_count=0;
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
else
RXD_buf[RXD_count++]=temp;
}
}
extern u16 time;
extern u8 hour,min,sec;
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
RTC_ClearITPendingBit(RTC_FLAG_SEC);
time=RTC_GetCounter();
if(time==23*3600+59*60+59)
RTC_SetCounter(0);
hour=time/3600;
min=time%3600/60;
sec=time%3600%60;
}
}
3.主函数
main.c
#include "stm32f10x.h"
#include "lcd.h"
#include "tx.h"
#include "stdio.h"
#include "i2c.h"
u32 TimingDelay = 0;
u8 str[20];
float adc_value;
u8 temp;
u8 i;
u8 RXD_flag=0;
u8 RXD_buf[20];
u16 time;
u8 hour,min,sec;
void Usart2_action(void);
void Delay_Ms(u32 nTime);
//Main Body
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
Delay_Ms(200);
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
Adc1_Init();
Usart2_Init();
RTC_Init(12,50,50);
while(1){
KEY_Driver();
adc_value=Get_adc()*3.3/4096;
sprintf((char*)str," ADC : %.2fV ",adc_value);
LCD_DisplayStringLine(Line2,str);
sprintf((char*)str," Time:%d-%d-%d",hour,min,sec);
LCD_DisplayStringLine(Line6,str);
if(RXD_flag){
RXD_flag=0;
Usart2_action();
}
}
}
void Usart2_action(void){
if(RXD_buf[0]=='C'){
sprintf((char*)str,"ADC : %.2fV\r\n",adc_value);
Send_string(str);
}
else if(RXD_buf[0]=='T'){
sprintf((char*)str,"Time:%.2d-%.2d-%.2d\r\n",hour,min,sec);
Send_string(str);
}
for(i=0;i<20;i++)
RXD_buf[i]=0;
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}