最近心血来潮,打算用stm32f103rbt6核心板配合MY2480-16P这一个语音模块做个小闹钟,可以实现三组闹钟的设置、懒人模式设置、工作日模式设置等功能,目前尚未完工,现将已经完成的部分代码分享。
定时器TIM3实现实时时钟,配置文件timer.c如下:
#include "timer.h"
clock localTime;//时钟结构体
int localmonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};//每个月的最大日期,没区分平年和闰年
/*
************************************************************
* Timer3_Init
************************************************************
*/
void Timer3_Init(TIM_TypeDef * TIMx, unsigned short arr, unsigned short psc)
{
TIM_TimeBaseInitTypeDef timerInitStruct3;
NVIC_InitTypeDef nvicInitStruct3;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
nvicInitStruct3.NVIC_IRQChannel = TIM3_IRQn;
timerInitStruct3.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStruct3.TIM_Period = arr;
timerInitStruct3.TIM_Prescaler = psc;
TIM_TimeBaseInit(TIM3, &timerInitStruct3);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
nvicInitStruct3.NVIC_IRQChannelCmd = ENABLE;
nvicInitStruct3.NVIC_IRQChannelPreemptionPriority = 2;
nvicInitStruct3.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&nvicInitStruct3);
TIM_Cmd(TIM3, ENABLE);
}
/*
************************************************************
* TIM3_IRQHandler
************************************************************
*/
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
localTime.sec += 1;
if(60 == localTime.sec)
{
localTime.sec = 0;
localTime.min += 1;
if(60 == localTime.min)
{
localTime.min = 0;
localTime.hour += 1;
if(24 == localTime.hour)
{
localTime.hour = 0;
localTime.day += 1;
localTime.dateTemp +=1;
if(localTime.dateTemp>=7){localTime.dateTemp=0;}
NetTime.daysInMonth = localmonth[localTime.month-1];
if(localTime.day > NetTime.daysInMonth )
{
localTime.day = 1;
localTime.month += 1;
if(localTime.month > 12)
{
localTime.month = 1;
localTime.year += 1;
}
}
}
}
}
}
配置文件timer.h如下:
#ifndef _timer_h_
#define _timer_h_
#include "stm32f10x.h"
typedef struct
{
unsigned char sec;
unsigned char min;
unsigned char hour;
unsigned char day;
unsigned char date[20];
unsigned char month;
int year;
unsigned char dateTemp; // 0->Sun 1->Mon 2->Tue 3->Wed 4->Thur 5->Fri. 6->Sat.
}clock;
#endif
0.96寸spi oled的驱动oled.c
#include "oled.h"
#include "stm32f10x.h"
#include "stdlib.h"
#include "delay.h"
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
#if OLED_MODE==1
void OLED_WR_Byte(u8 dat,u8 cmd)
{
DATAOUT(dat);
if(cmd)
OLED_DC_Set();
else
OLED_DC_Clr();
OLED_CS_Clr();
OLED_WR_Clr();
OLED_WR_Set();
OLED_CS_Set();
OLED_DC_Set();
}
#else
void OLED_WR_Byte(u8 dat,u8 cmd)
{
u8 i;
if(cmd)
OLED_DC_Set();
else
OLED_DC_Clr();
OLED_CS_Clr();
for(i=0;i<8;i++)
{
OLED_SCLK_Clr();
if(dat&0x80)
OLED_SDIN_Set();
else
OLED_SDIN_Clr();
OLED_SCLK_Set();
dat<<=1;
}
OLED_CS_Set();
OLED_DC_Set();
}
#endif
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
}
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCÃüÁî
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDCÃüÁî
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD);
OLED_WR_Byte (0x00,OLED_CMD);
OLED_WR_Byte (0x10,OLED_CMD);
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
}
}
//x:0~127
//y:0~63
void OLED_ShowChar(u8 x,u8 y,u8 chr)
{
unsigned char c=0,i=0;
c=chr-' ';
if(x>Max_Column-1){x=0;y=y+2;}
if(SIZE ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
else {
OLED_Set_Pos(x,y+1);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8[c][i],OLED_DATA);
}
}
u32 oled_pow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size/2)*t,y,' ');
continue;
}else enshow=1;
}
OLED_ShowChar(x+(size/2)*t,y,temp+'0');
}
}
void OLED_ShowString(u8 x,u8 y,u8 *chr)
{
unsigned char j=0;
while (chr[j]!='\0')
{ OLED_ShowChar(x,y,chr[j]);
x+=8;
if(x>120){x=0;y+=2;}
j++;
}
}
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_4);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1);
OLED_RST_Set();
DelayMs(100);
OLED_RST_Clr();
DelayMs(200);
OLED_RST_Set();
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping
OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction
OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
OLED_WR_Byte(0x00,OLED_CMD);//-not offset
OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
OLED_Clear();
OLED_Set_Pos(0,0);
}
配置文件oled.h
#define __OLED_H
#include "stm32f10x.h"
#include "stdlib.h"
#define OLED_MODE 0
#define SIZE 16
#define XLevelL 0x00
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#define X_WIDTH 128
#define Y_WIDTH 64
//-----------------OLED引脚---------------
#define OLED_SCLK_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_5)//CLK
#define OLED_SCLK_Set() GPIO_SetBits(GPIOA,GPIO_Pin_5)
#define OLED_SDIN_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_7)//DIN
#define OLED_SDIN_Set() GPIO_SetBits(GPIOA,GPIO_Pin_7)
#define OLED_RST_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_0)//RES
#define OLED_RST_Set() GPIO_SetBits(GPIOB,GPIO_Pin_0)
#define OLED_DC_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_1)//DC
#define OLED_DC_Set() GPIO_SetBits(GPIOB,GPIO_Pin_1)
#define OLED_CS_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_4)//CS
#define OLED_CS_Set() GPIO_SetBits(GPIOA,GPIO_Pin_4)
#define OLED_CMD 0
#define OLED_DATA 1
void OLED_WR_Byte(u8 dat,u8 cmd);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Init(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);
void OLED_ShowChar(u8 x,u8 y,u8 chr);
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size);
void OLED_ShowString(u8 x,u8 y, u8 *p);
void OLED_Set_Pos(unsigned char x, unsigned char y);
常用的延时函数delay.c、delay.h
/***********delay.c************/
#include "stm32f10x.h"
#include "delay.h"
unsigned char UsCount = 0;
unsigned short MsCount = 0;
void Delay_Init(void)
{
SysTick->CTRL &= ~(1 << 2);
UsCount = 9;
MsCount = UsCount * 1000;
}
void DelayUs(unsigned short us)
{
unsigned int ctrlResult = 0;
us &= 0x00FFFFFF;
SysTick->LOAD = us * UsCount;
SysTick->VAL = 0;
SysTick->CTRL = 1;
do
{
ctrlResult = SysTick->CTRL;
}
while((ctrlResult & 0x01) && !(ctrlResult & (1 << 16)));
SysTick->CTRL = 0;
SysTick->VAL = 0;
}
void DelayXms(unsigned short ms)
{
unsigned int ctrlResult = 0;
if(ms == 0)
return;
ms &= 0x00FFFFFF;
SysTick->LOAD = ms * MsCount;
SysTick->VAL = 0;
SysTick->CTRL = 1;
do
{
ctrlResult = SysTick->CTRL;
}
while((ctrlResult & 0x01) && !(ctrlResult & (1 << 16)));
SysTick->CTRL = 0;
SysTick->VAL = 0;
}
void DelayMs(unsigned short ms)
{
unsigned char repeat = 0;
unsigned short remain = 0;
repeat = ms / 500;
remain = ms % 500;
while(repeat)
{
DelayXms(500);
repeat--;
}
if(remain)
DelayXms(remain);
}
/**********delay.h*************/
#ifndef _DELAY_H_
#define _DELAY_H_
void Delay_Init(void);
void DelayUs(unsigned short us);
void DelayXms(unsigned short ms);
void DelayMs(unsigned short ms);
#endif
测试代码main.c
//单片机头文件
#include "stm32f10x.h"
//硬件驱动
#include
#include "oled.h"
//C库
#include
#include
#include
extern clock localTime;
int main(void)
{
u32 hourHigh,hourLow,minHigh,minLow,secHigh,secLow,monthhigh,monthlow,dayhigh,daylow;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Delay_Init();//systick初始化
OLED_Init();
OLED_Clear();
Timer3_Init(TIM3,6666, 7199);//8000的晶振下,接近1s一次中断
while(1)
{
//固定字符显示
OLED_ShowChar(41,0,'/');
OLED_ShowChar(44,2,':');
OLED_ShowChar(68,2,':');
//时间计算与显示
hourHigh = (localTime.hour)/10;
hourLow = (localTime.hour)%10;
minHigh = localTime.min/10;
minLow = localTime.min%10;
secHigh = localTime.sec/10;
secLow = localTime.sec%10;
monthhigh=(localTime.month)/10;
monthlow=(localTime.month)%10;
dayhigh=(localTime.day)/10;
daylow=(localTime.day)%10;
OLED_ShowNum(24,0,monthhigh,1,16);
OLED_ShowNum(33,0,monthlow,1,16);
OLED_ShowNum(49,0,dayhigh,1,16);
OLED_ShowNum(57,0,daylow,1,16);
OLED_ShowNum(28,2,hourHigh,1,8);
OLED_ShowNum(36,2,hourLow,1,8);
OLED_ShowNum(53,2,minHigh,1,8);
OLED_ShowNum(60,2,minLow,1,8);
OLED_ShowNum(76,2,secHigh,1,8);
OLED_ShowNum(84,2,secLow,1,8);
}
}