#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "buzzer.h"
#include "usart.h"
#include "rtc.h"
#include "NVIC.h"
#include "adc.h"
#include "oled.h"
extern vu16 ADC_DMA_IN4; //声明外部变量
extern u8 INT_MARK;//中断标志?
extern u32 ui;//用于遥控变量
extern u8 q;
u16 d=0;
u8 w;
void key(){
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){ //读按键接口的电平
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1)); //蜂鸣器接口输出高电平1
delay_ms(20); //延时去抖动
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){ //读按键接口的电平
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(q++));
while(!GPIO_ReadInputDataBit(KEYPORT,KEY1)); //等待按键松开
if(q==3){q=0;}
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0)); //蜂鸣器接口输出高电平1
} }
}
void key2(){
if(GPIO_ReadInputDataBit(KEYPORT,KEY2)){ //读按键接口的电平
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1)); //蜂鸣器接口输出高电平1
if(GPIO_ReadInputDataBit(KEYPORT,KEY2)){ //读按键接口的电平
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(q++));
while(GPIO_ReadInputDataBit(KEYPORT,KEY2)); //等待按键松开
if(q==3){q=0;}
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0)); //蜂鸣器接口输出高电平1
} }
}
void key3(){
if(GPIO_ReadInputDataBit(KEYPORT,KEY3)){ //读按键接口的电平
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1)); //蜂鸣器接口输出高电平1
if(GPIO_ReadInputDataBit(KEYPORT,KEY3)){ //读按键接口的电平
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(q++));
while(GPIO_ReadInputDataBit(KEYPORT,KEY3)); //等待按键松开
if(q==3){q=0;}
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0)); //蜂鸣器接口输出高电平1
} }
}
void key4(){
if(GPIO_ReadInputDataBit(KEYPORT,KEY4)){ //读按键接口的电平
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1)); //蜂鸣器接口输出高电平1
if(GPIO_ReadInputDataBit(KEYPORT,KEY4)){ //读按键接口的电平
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(q++));
while(GPIO_ReadInputDataBit(KEYPORT,KEY4)); //等待按键松开
if(q==3){q=0;}
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0)); //蜂鸣器接口输出高电平1
} }
}
void key5(){
if(GPIO_ReadInputDataBit(KEYPORT,KEY5)){ //读按键接口的电平
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(1)); //蜂鸣器接口输出高电平1
if(GPIO_ReadInputDataBit(KEYPORT,KEY5)){ //读按键接口的电平
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(q++));
while(GPIO_ReadInputDataBit(KEYPORT,KEY5)); //等待按键松开
if(q==3){q=0;}
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0)); //蜂鸣器接口输出高电平1
} }
}
int main (void){//主程序
u8 bya;
// w=0;
INT_MARK=0;//中断标志位
ui=0;
q=0;
RCC_Configuration(); //系统时钟初始化
RTC_Config(); //实时时钟初始化
LED_Init();//LED初始化
KEY_Init();//按键初始化
BUZZER_Init();//蜂鸣器初始化
USART1_Init(115200); //串口初始化,参数中写波特率
// USART1_Init(9600); //串口初始化,参数中写波特率
USART1_RX_STA=0xC000; //初始值设为有回车的状态,即显示一次欢迎词
// KEY_INT_INIT();
TIMENVIC_INT_INIT ();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); //使能电源PWR时钟
// RTC_Set(2021,8,8,20,59,00);
// RTC_Alarm_Set(2020,12,13,00,00,10);
// MIDI_PLAY();
ADC_Configuration(); //ADC初始化设置
OLED_Init(); //初始化OLED
OLED_Clear();
while(1){
GPIO_WriteBit(BUZZERPORT,BUZZER,(BitAction)(0)); //蜂鸣器接口输出高电平1
d=(ADC_DMA_IN4-120)/71.428571428571*100;
// if(ADC_DMA_IN4<880){
// GPIO_WriteBit(LEDPORT,LED2,(BitAction)(0));GPIO_WriteBit(LEDPORT,LED3,(BitAction)(0));
// GPIO_WriteBit(LEDPORT1,LED4,(BitAction)(0));
// }
if(ui==0xa1){q=1;printf("打开路由器 \r\n");ui=0;}
if(ui==0xa2){q=2;printf("打开灯光 \r\n");ui=0;}
if(ui==0xa0){q=0;printf("关闭所有 \r\n");ui=0;}else{ui=0;}
// OLED_ShowNum(48,6,rsec,3,16);//显示ASCII字符
// OLED_ShowNum(103,6,rmin,3,16);//显示ASCII字符的码值
// PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);//进入停机模式
// RCC_Configuration(); //系统时钟初始化(停机唤醒后会改用HSI时钟,需要重新对时钟初始化)
// delay_ms(500);
if(q==1){q=2;}
if(q==0){GPIO_WriteBit(LEDPORT,LED2,(BitAction)(0));OLED_ShowCHinese(54,0,17);GPIO_WriteBit(LEDPORT,LED3,(BitAction)(0));OLED_ShowCHinese(54,2,17);}
if(q==1){GPIO_WriteBit(LEDPORT,LED2,(BitAction)(1));OLED_ShowCHinese(54,0,16);GPIO_WriteBit(LEDPORT,LED3,(BitAction)(0));OLED_ShowCHinese(54,2,17);}
if(q==2){GPIO_WriteBit(LEDPORT,LED2,(BitAction)(1));OLED_ShowCHinese(54,0,16);GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1));OLED_ShowCHinese(54,2,16);}
key();key2();key3();key4();key5();
if(USART1_RX_STA&0xC000){ //如果标志位是0xC000表示收到数据串完成,可以处理。
if((USART1_RX_STA&0x3FFF)==0){ //单独的回车键再显示一次欢迎词
if(RTC_Get()==0){ //读出时间值,同时判断返回值是不是0,非0时读取的值是错误的。
printf(" 洋桃开发板STM32实时时钟测试程序 \r\n");
printf(" 现在实时时间:%d-%d-%d %d:%d:%d ",ryear,rmon,rday,rhour,rmin,rsec);//显示日期时间
if(rweek==0)printf("星期日 \r\n");//rweek值为0时表示星期日
if(rweek==1)printf("星期一 \r\n");
if(rweek==2)printf("星期二 \r\n");
if(rweek==3)printf("星期三 \r\n");
if(rweek==4)printf("星期四 \r\n");
if(rweek==5)printf("星期五 \r\n");
if(rweek==6)printf("星期六 \r\n");
printf(" 单按回车键更新时间。输入字母C初始化时钟 \r\n");
printf(" 请输入设置时间,格式20170806120000,按回车键确定! \r\n");
}else{
printf("读取失败!\r\n");
}
}else if((USART1_RX_STA&0x3FFF)==1){ //判断数据是不是2个
if(USART1_RX_BUF[0]=='c' || USART1_RX_BUF[0]=='C'){
RTC_First_Config(); //键盘输入c或C,初始化时钟
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);//配置完成后,向后备寄存器中写特殊字符0xA5A5
printf("初始化成功! \r\n");//显示初始化成功
}else{
printf("指令错误! \r\n"); //显示指令错误!
}
}
else if((USART1_RX_STA&0x3FFF)==14){ //判断数据是不是14个
//将超级终端发过来的数据换算并写入RTC
ryear = (USART1_RX_BUF[0]-0x30)*1000+(USART1_RX_BUF[1]-0x30)*100+(USART1_RX_BUF[2]-0x30)*10+USART1_RX_BUF[3]-0x30;
rmon = (USART1_RX_BUF[4]-0x30)*10+USART1_RX_BUF[5]-0x30;//串口发来的是字符,减0x30后才能得到十进制0~9的数据
rday = (USART1_RX_BUF[6]-0x30)*10+USART1_RX_BUF[7]-0x30;
rhour = (USART1_RX_BUF[8]-0x30)*10+USART1_RX_BUF[9]-0x30;
rmin = (USART1_RX_BUF[10]-0x30)*10+USART1_RX_BUF[11]-0x30;
rsec = (USART1_RX_BUF[12]-0x30)*10+USART1_RX_BUF[13]-0x30;
bya=RTC_Set(ryear,rmon,rday,rhour,rmin,rsec); //将数据写入RTC计算器的程序
if(bya==0)printf("写入成功! \r\n");//显示写入成功
else printf("写入失败! \r\n"); //显示写入失败
}else{ //如果以上都不是,即是错误的指令。
printf("指令错误! \r\n"); //如果不是以上正确的操作,显示指令错误!
}
USART1_RX_STA=0; //将串口数据标志位清0
}
if(RTC_Get()==0){ //读出时间值,同时判断返回值是不是0,非0时读取的值是错误的。
// OLED_ShowString(20,4,ryear/rmon/rday,16);
OLED_ShowCHinese(0,0,10); //路
OLED_ShowCHinese(18,0,11);//由
OLED_ShowCHinese(36,0,12);//器
// OLED_ShowCHinese(54,0,16);//开
OLED_ShowCHinese(0,2,13); //照
OLED_ShowCHinese(18,2,14);//明
OLED_ShowCHinese(36,2,15);//灯
// OLED_ShowCHinese(54,2,17);//关
// OLED_ShowNum(80,0,d,6,16); //10.95v=900 13v=1050 82.19178082
OLED_ShowNum(82,0,(d/100),2,16);
OLED_ShowString(98,0,".",16);
OLED_ShowNum(100,0,(d%100),2,16);
OLED_ShowString(120,0,"v",16);
OLED_ShowNum(0,4,ryear,5,16);
OLED_ShowString(40,4,"/",16);
OLED_ShowNum(47,4,rmon,3,16);
OLED_ShowString(73,4,"/",16);
OLED_ShowNum(80,4,rday,3,16);
OLED_ShowNum(0,6,rhour,3,16);
OLED_ShowString(23,6,":",16);
OLED_ShowNum(28,6,rmin,3,16);
OLED_ShowString(51,6,":",16);
OLED_ShowNum(56,6,rsec,3,16);
OLED_ShowCHinese(102,6,18);//周
// OLED_ShowString(95,6,"NO:",16);
if(rweek!=0){w=rweek;}//rweek值为0时表示星期日
if(rweek==0){w=7;}//rweek值为0时表示星期日
OLED_ShowNum(120,6,w,1,16);
// GPIO_WriteBit(LEDPORT,LED2,(BitAction)(rmin%2)); //LED2接口
if(ADC_DMA_IN4>890){ if(rhour==20){if(rmin==30){if(rsec==1){q=2;}}}}
if(rhour==0){if(rmin==30){if(rsec==1){q=0;}}}
if(rhour==2){if(rmin==00){if(rsec==1){q=0;}}}
if(rhour==8){if(rmin==30){if(rsec==1){q=0;}}}
if(rhour==0){if(rmin==29){if(rsec==1){
if(GPIO_ReadInputDataBit(LEDPORT,LED3)){
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(0));delay_ms(500);
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1));delay_ms(500);
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(0));delay_ms(500);
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1));delay_ms(500);
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(0));delay_ms(500);
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1));
}
}
}
}
if(ADC_DMA_IN4<890){GPIO_WriteBit(LEDPORT1,LED4,(BitAction)(rsec%2));}
if(ADC_DMA_IN4>900){GPIO_WriteBit(LEDPORT1,LED4,(BitAction)(1));}
key();key2();key3();key4();key5();
}
// GPIO_WriteBit(LEDPORT1,LED4,(BitAction)(0));
}
}
|