stm32驱动lcd1602秒表显示电路原理图如下:
单片机源程序如下:
- /*
- **************************************************************************************************
- * 深圳新元电子工作室
- * 文件名:stm32驱动LCD1602秒表主程序
- * 描 述:
- * 版本号:V0.0
- * 备 注:
- * 程序员:贾*
- ***************************************************************************************************
- */
- #include "stm32f10x.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_tim.h"
- #include "misc.h"
- #include "stm32f10x_exti.h"
- #include "lcd1602.h"
- #include "pbdata.h"
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void TIM3_Configuration(void);
- void EXTI_Configuration(void);
- void LCD1602Display_Hour(void);
- void LCD1602Display_Minute(void);
- void LCD1602Display_Second(void);
- void LCD1602Display_Millisecond(void);
- volatile u8 hour =0;
- volatile u8 second = 0;
- volatile u8 minute = 0;
- volatile u8 millisecond =0;
- int main(void)
- {
- RCC_Configuration(); //系统时钟初始化
- GPIO_Configuration(); //端口初始化
- TIM3_Configuration();
- NVIC_Configuration();
- EXTI_Configuration();
- InitLcd1602();
- LcdShowStr(0,0,"The stopwatch:");
- LcdShowStr(4,1,": : :");
-
- while(1)
- {
- LCD1602Display_Hour();
- LCD1602Display_Minute();
- LCD1602Display_Second();
- LCD1602Display_Millisecond();
- }
-
- }
- void RCC_Configuration(void)
- {
- SystemInit();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
- //关闭调试 端口重新映射 使用仿真器调试时,不能用此语
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA端口
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 |GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- //所有GPIO为同一类型端口
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出的最大频率为50HZ
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB端口
-
- // KEY
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- }
- void TIM3_Configuration(void)
- {
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
-
- TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
-
- TIM_TimeBaseStruct.TIM_Period = 20;
- TIM_TimeBaseStruct.TIM_Prescaler = 35999;
- TIM_TimeBaseStruct.TIM_ClockDivision = 0;
- TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
-
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
-
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM3,DISABLE);
- }
- void EXTI_Configuration(void)
- {
- EXTI_InitTypeDef EXTI_InitStructure;
-
- EXTI_ClearITPendingBit(EXTI_Line0);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
- EXTI_InitStructure.EXTI_Line = EXTI_Line0;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
-
- EXTI_ClearITPendingBit(EXTI_Line1);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
- EXTI_InitStructure.EXTI_Line = EXTI_Line1;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
-
- EXTI_ClearITPendingBit(EXTI_Line2);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);
- EXTI_InitStructure.EXTI_Line = EXTI_Line2;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
-
- }
- void NVIC_Configuration(void)
- {
-
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // 设置NVIC中断分组2, 2位抢占优先级,2位响应优先级
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // 设置外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级为2
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 响应优先级为2
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能外部中断通道
- NVIC_Init(&NVIC_InitStructure); // 根据NVIC_InitStruct结构体中指定的参数初始化外设NVIC寄存器
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- }
- void LCD1602Display_Second(void)
- {
- u8 str[3];
- str[0] = second/10 +'0';
- str[1] = second%10 + '0';
- str[2] = '\0';
- LcdShowStr(8,1,str);
- }
- void LCD1602Display_Millisecond(void)
- {
- u8 str[3];
- str[0] = millisecond/10 +'0';
- str[1] = millisecond%10 + '0';
- str[2] = '\0';
- LcdShowStr(11,1,str);
- }
- void LCD1602Display_Minute(void)
- {
- u8 str[3];
- str[0] = minute/10 +'0';
- str[1] = minute%10 + '0';
- str[2] = '\0';
- LcdShowStr(5,1,str);
- }
- void LCD1602Display_Hour(void)
- {
- u8 str[3];
- str[0] = hour/10 +'0';
- str[1] = hour%10 + '0';
- str[2] = '\0';
- LcdShowStr(2,1,str);
- }
复制代码
所有资料51hei提供下载:
lcd1602秒表2.rar
(309.01 KB, 下载次数: 291)
|