当前位置:网站首页>[STM32 learning] (10) stm32f1 general timer realizes pulse counter
[STM32 learning] (10) stm32f1 general timer realizes pulse counter
2022-07-24 09:51:00 【Use small materials】
STM32F1 General timer realizes pulse counter
adopt TIM3 Realize timing , I designed to count every second , Then in the digital tube ( Photoanode ) Displayed on the . Digital not only records pulses , You can also press the key to adjust :
Key 1 Zero clearing
Key 2 Add one
Key 3 Minus one
Key 4 Select pause / To start Two functions
It should be noted that : Key 1- Key 3 The function of must be in the key 4 Only when the pause function is selected , Other conditions fail .
connection :
1.PE0 Pick up LED The lamp , Put it in the interrupt function , Play the prompt of interrupt execution
2. The data ports of the four nixie tubes are connected to PD8- PD15 The film selection ports are connected to PA0-PA3 mouth
3. The four keys are connected to PC0-PC4
The code is as follows :
main.c
/*
Creation time :2019.9.17
founder : King boat
function : Four nixie tubes display circularly 0000 - 9999
connection :
Each digital tube is connected to PD8-PD15
Drive ports of four nixie tubes (com) They are connected to PA0-PA3
Implementation steps :
The difference between common Yang and common Yin of digital tube
*/
#include "stm32f10x.h" // Device header
#include "led.h"
#include "key.h"
#include "delay.h"
#include "time.h"
int main(void)
{
GPIOD_init_wt();
GPIOA_init_wt();
GPIOC_Init_key();
GPIOE_init_led();
TIM3_Init(999,35999); // 0.5s T = (999+1)*(35999+1)/72MHz = 0.5s
while(1)
{
key_scan();
display();
}
}time.h
#include "stdint.h"
void TIM3_Init(uint16_t per,uint16_t psc);time.c
#include "stm32f10x_tim.h"
int temp = 1;
extern int cnt;
void TIM3_Init(uint16_t per,uint16_t psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitStructure.TIM_Period=per; // Auto load values
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; // Division coefficient
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; // Set up count mode
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
NVIC_InitStruct.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);
TIM_Cmd(TIM3,ENABLE);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
temp = (-1)*temp;
if(temp == -1)
{
GPIO_WriteBit(GPIOE, GPIO_Pin_0, Bit_SET);
cnt++;
if(cnt>9999)
{
cnt = 0;
}
}
else
{
GPIO_WriteBit(GPIOE, GPIO_Pin_0, Bit_RESET);
}
}
}
led.h
void GPIOD_init_wt(void);
void GPIOA_init_wt(void);
void GPIOE_init_led(void);
void display(void);
led.c
/*
Function name :void GPIOA_init_wt(void)
No parameters : No arguments
function : Turn on GPIOA mouth
low 8 Bit push-pull output ,50MHz
founder : King boat
Creation time :2019.9.12
Function name :void GPIOA_init_wt(int x)
No parameters : Ginseng
function : Turn on GPIOA mouth
low 8 Bit push-pull output ,50MHz
Modifier : King boat
Job number :0111001
Creation time :2019.9.13
*/
#include "stm32f10x_gpio.h"
#include "delay.h"
unsigned short int table1[]={
0xc000,0xf900,0xa400,0xb000,
0x9900,0x9200,0x8200,0xf800,
0x8000,0x9000,0x8800,0x8300,
0xc600,0xa100,0x8600,0x8e00}; // Gongyang
unsigned short int table2[]={ // 0-F
0x3f00,0x0600,0x5b00,0x4f00,
0x6600,0x6d00,0x7d00,0x0700,
0x7f00,0x6f00,0x7700,0x7c00,
0x3900,0x5e00,0x7900,0x7100}; // Common Yin
int cnt; // Global variables , Count
//extern int flag; // Global variables , Marker bit
void GPIOD_init_wt(void)
{
GPIO_InitTypeDef GPIO_InitStruct; // GPIO Initialize structure
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStruct.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_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStruct);
}
void GPIOA_init_wt(void)
{
GPIO_InitTypeDef GPIO_InitStruct; // GPIO Initialize structure
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //0000 0000
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
}
void GPIOE_init_led(void)
{
GPIO_InitTypeDef GPIO_InitStruct; // GPIO Initialize structure
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //0000 0000
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStruct);
}
/* Show 1234 */
/*void display(void)
{
GPIO_Write(GPIOA,0X000E); // Select the first digital tube 0000 0000 0000 1110
GPIO_Write(GPIOD,table1[1]); // Show 1
delay();
GPIO_Write(GPIOA,0X000D); // Select the first digital tube 0000 0000 0000 1101
GPIO_Write(GPIOD,table1[2]); // Show 1
delay();
GPIO_Write(GPIOA,0X000B); // Select the first digital tube 0000 0000 0000 1011
GPIO_Write(GPIOD,table1[3]); // Show 1
delay();
GPIO_Write(GPIOA,0X0007); // Select the first digital tube 0000 0000 0000 0111
GPIO_Write(GPIOD,table1[4]); // Show 1
delay();
}*/
/*
Digital tube dynamic display - 0000 - 9999
founder : King boat
Method 1
*/
//void display(void)
//{
// int i,j,k,m,n; // Control a hundred thousand for
//
// for(m=0;m<10;m++)
// {
// for(k=0;k<10;k++)
// {
// for(j=0;j<10;j++)
// {
// for(i=0;i<10;i++)
// {
// for(n=0;n<10;n++) // Play the role of delay
// {
// GPIO_Write(GPIOA,0X000E); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[m]); // Show
// delay();
//
// GPIO_Write(GPIOA,0X000D); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[k]); // Show
// delay();
//
// GPIO_Write(GPIOA,0X000B); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[j]); // Show
// delay();
//
// GPIO_Write(GPIOA,0X0007); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[i]); // Show
// delay();
// }
//
// }
// }
// }
// }
//
//}
/*
Digital tube dynamic display - 0000 - 9999
founder : King boat
Method 2
*/
//void display(void)
//{
// int cnt; // Count
// int i; // Loop variable
// int g,s,b,q;
// for(cnt=0;cnt<10000;cnt++)
// {
// g = cnt%10;
// s = cnt%100/10;
// b = cnt/100%10;
// q = cnt/1000;
// for(i=0;i<10;i++)
// {
// GPIO_Write(GPIOA,0X000E); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[q]); // Show
// delay();
//
// GPIO_Write(GPIOA,0X000D); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[b]); // Show
// delay();
//
// GPIO_Write(GPIOA,0X000B); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[s]); // Show
// delay();
//
// GPIO_Write(GPIOA,0X0007); // Select the first digital tube 0000 0000 0000 1110
// GPIO_Write(GPIOD,table1[g]); // Show
// delay();
// }
// }
//}
/*
Digital tube dynamic display - 0000 - 9999
founder : King boat
Method 3
*/
void display(void )
{
int g,s,b,q;
// if(-1 == flag)
// {
//
// }
// else if(1 == flag)
// {
// cnt++;
// }
g = cnt%10;
s = cnt%100/10;
b = cnt/100%10;
q = cnt/1000;
GPIO_Write(GPIOA,0X000E); // Select the first digital tube 0000 0000 0000 1110
GPIO_Write(GPIOD,table1[q]); // Show
delay();
GPIO_Write(GPIOA,0X000D); // Select the first digital tube 0000 0000 0000 1101
GPIO_Write(GPIOD,table1[b]); // Show
delay();
GPIO_Write(GPIOA,0X000B); // Select the first digital tube 0000 0000 0000 1011
GPIO_Write(GPIOD,table1[s]); // Show
delay();
GPIO_Write(GPIOA,0X0007); // Select the first digital tube 0000 0000 0000 0111
GPIO_Write(GPIOD,table1[g]); // Show
delay();
}
key.h
void GPIOC_Init_key(void);
void key_scan(void);key.c
/*
Creation time :2019.9.20
founder : King boat
Modification time :2019.9.23
connection :key1-key4 They are connected to PC0-PC3
step :
1. Turn on the clock
2. Set the working mode
3. Data input
*/
#include "stm32f10x_rcc.h"
#include "delay.h"
int flag=1; // Marker bit
extern int cnt;
void GPIOC_Init_key(void) // PC0-PC2
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU ;
GPIO_Init(GPIOC,&GPIO_InitStruct);
}
void key_scan(void)
{
// key1
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == 0 &&(-1 == flag))
{
delay_wt(); // Eliminate jitter
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == 0 &&(-1 == flag))
{
cnt=0;
}
while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0) == 0); // Judge whether the key is released
}
// key2 Add 1 operation
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1) == 0 &&(-1 == flag))
{
delay_wt(); // Eliminate jitter
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1) == 0 &&(-1 == flag))
{
cnt++;
if(cnt>=10000)
{
cnt=0;
}
}
while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1) == 0); // Judge whether the key is released
}
// key3 reduce 1 operation
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2) == 0 &&(-1 == flag))
{
delay_wt(); // Eliminate jitter
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2) == 0 &&(-1 == flag))
{
cnt--;
if(cnt<0)
{
cnt=9999;
}
}
while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2) == 0); // Judge whether the key is released
}
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) == 0)
{
delay_wt(); // Eliminate jitter
if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) == 0)
{
flag = (-1)*flag;
if(flag == 1)
{
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
//TIM_Cmd(TIM3, ENABLE);
}
else
{
TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE);
//TIM_Cmd(TIM3, DISABLE);
}
}
while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3) == 0); // Judge whether the key is released
}
}
delay.h
void delay(void); // Function declaration
void delay_wt(void);delay.c
void delay(void)
{
int i,j;
for(i=0;i<100;i++)
{
for(j=0;j<400;j++);
}
}
void delay_wt(void)
{
int i,j;
for(i=0;i<100;i++)
{
for(j=0;j<400;j++);
}
}
The effect is as follows :

边栏推荐
- JS locate Daquan to get the brother, parent and child elements of the node, including robot instances
- Cloud primordial (12) | introduction to kubernetes foundation of kubernetes chapter
- 【笔记】什么是内核/用户空间 从CPU如何运行程序讲起
- Recursion - if the function calls itself internally, then the function is a recursive function & the effect is the same as that of the loop & the push condition return should be added, otherwise stack
- Spark Learning: implement compact table command
- Wenxin big model raises a new "sail", and the tide of industrial application has arrived
- [note] what is kernel / user space? Let's start with how the CPU runs the program
- [don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?
- System a uses window.open to call system B, and system B carries data back to system a after processing the business
- [C language] implementation of three versions of address book small project (including source code)
猜你喜欢

2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices

Spark Learning: Spark implementation of distcp

What if path is deleted by mistake when configuring system environment variables?

Spark Learning: implement compact table command
![[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)](/img/3b/981bd564a5855a317ccdd4800871ce.png)
[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)

note: expected ‘void * (***)(void ***)’ but argument is of type ‘void (*)(void *)’

Little dolphin "transformed" into a new intelligent scheduling engine, which can be explained in simple terms in the practical development and application of DDS

When the hot tea is out of stock, what does the new tea drink rely on to continue its life?
![[don't bother to strengthen learning] video notes (III) 2. SARS learning realizes maze walking](/img/a8/0d3bd3cc2b3e1d43e201e5dfe4b729.png)
[don't bother to strengthen learning] video notes (III) 2. SARS learning realizes maze walking

Opencv learning Day5
随机推荐
Arduino drive Lora module master node
获取所有股票历史行情数据
缓冲区的概念真的理解么?带你揭开缓冲区的面纱~
Dorissql syntax Usage Summary
This article takes you to understand the dynamic memory allocation of C language
Raspberry Pie: /bin/sh: 1: bison: not found
Basic knowledge of PHP - complete collection of PHP functions
Openstack network neutron knowledge point "openstack"
PHP Basics - PHP magic method
[Luogu p5410] [template] extend KMP (Z function) (string)
How to improve office efficiency through online collaborative documents
JS 84*148=b6a8 how many decimal places can you make both sides equal
Synchronized scope "concurrent programming"
Centos7 install mysql8.0
详解LinkedList
C#/VB. Net: convert word or EXCEL documents to text
PHP Basics - PHP types
PHP Basics - session control - Session
[don't bother to strengthen learning] video notes (III) 2. SARS learning realizes maze walking
Cloud primordial (12) | introduction to kubernetes foundation of kubernetes chapter