Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_tim.h"
- #include "misc.h"
- int a=0;
- GPIO_InitTypeDef GPIO_InitStructure;
- void Segment(int);
- void Wysw(int,int,int,int,int,int,int);
- void TIM2_IRQHandler()
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- a=0;
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5)){
- a=a+1; }
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6)){
- a=a+2; }
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)){
- a=a+4; }
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)){
- a=a+8; }
- if(a>4) Segment(1);
- else Segnment(0);
- }
- void Segment (int i){
- switch(i){
- case 0:
- Wysw(0,0,0,1,1,1,0);
- break;
- case 1:
- Wysw(0,1,1,0,1,1,1);
- break;
- }
- }
- void Wysw(int a, int b, int c, int d, int e, int f, int g){
- if(a) GPIO_ResetBits(GPIOB, GPIO_Pin_1);
- else GPIO_SetBits(GPIOB, GPIO_Pin_1);
- if(b) GPIO_ResetBits(GPIOB, GPIO_Pin_2);
- else GPIO_SetBits(GPIOB, GPIO_Pin_2);
- if(c) GPIO_ResetBits(GPIOB, GPIO_Pin_3);
- else GPIO_SetBits(GPIOB, GPIO_Pin_3);
- if(d) GPIO_ResetBits(GPIOB, GPIO_Pin_4);
- else GPIO_SetBits(GPIOB, GPIO_Pin_4);
- if(e) GPIO_ResetBits(GPIOB, GPIO_Pin_5);
- else GPIO_SetBits(GPIOB, GPIO_Pin_5);
- if(f) GPIO_ResetBits(GPIOB, GPIO_Pin_6);
- else GPIO_SetBits(GPIOB, GPIO_Pin_6);
- if(g) GPIO_ResetBits(GPIOB, GPIO_Pin_7);
- else GPIO_SetBits(GPIOB, GPIO_Pin_7);
- }
- int main(void)
- {
- GPIO_InitTypeDef gpio;
- GPIO_InitTypeDef wa, wb, wc, wd, we, wf, wg;
- GPIO_InitTypeDef c0, c1, c2, c3;
- TIM_TimeBaseInitTypeDef tim;
- NVIC_InitTypeDef nvic;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- GPIO_StructInit(&gpio);
- gpio.GPIO_Pin = GPIO_Pin_5;
- gpio.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOA, &gpio);
- GPIO_StructInit(&wa);
- wa.GPIO_Pin = GPIO_Pin_1;
- wa.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOB, &wa);
- GPIO_StructInit(&wb);
- wb.GPIO_Pin = GPIO_Pin_2;
- wb.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOB, &wb);
- GPIO_StructInit(&wc);
- wc.GPIO_Pin = GPIO_Pin_3;
- wc.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOB, &wc);
- GPIO_StructInit(&wd);
- wd.GPIO_Pin = GPIO_Pin_4;
- wd.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOB, &wd);
- GPIO_StructInit(&we);
- we.GPIO_Pin = GPIO_Pin_5;
- we.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOB, &we);
- GPIO_StructInit(&wf);
- wf.GPIO_Pin = GPIO_Pin_6;
- wf.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOB, &wf);
- GPIO_StructInit(&wg);
- wg.GPIO_Pin = GPIO_Pin_7;
- wg.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_Init(GPIOB, &wg);
- GPIO_StructInit(&c0);
- c0.GPIO_Pin = GPIO_Pin_5;
- c0.GPIO_Mode = GPIO_Mode_IN;
- c0.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(GPIOC, &c0);
- GPIO_StructInit(&c1);
- c1.GPIO_Pin = GPIO_Pin_6;
- c1.GPIO_Mode = GPIO_Mode_IN;
- c1.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(GPIOC, &c1);
- GPIO_StructInit(&c2);
- c2.GPIO_Pin = GPIO_Pin_7;
- c2.GPIO_Mode = GPIO_Mode_IN;
- c2.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(GPIOC, &c2);
- GPIO_StructInit(&c3);
- c3.GPIO_Pin = GPIO_Pin_8;
- c3.GPIO_Mode = GPIO_Mode_IN;
- c3.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(GPIOC, &c3);
- TIM_TimeBaseStructInit(&tim);
- tim.TIM_CounterMode = TIM_CounterMode_Up;
- tim.TIM_Prescaler = 16000 - 1;
- tim.TIM_Period = 2 - 1;
- TIM_TimeBaseInit(TIM2, &tim);
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- TIM_Cmd(TIM2, ENABLE);
- nvic.NVIC_IRQChannel = TIM2_IRQn;
- nvic.NVIC_IRQChannelPreemptionPriority = 0;
- nvic.NVIC_IRQChannelSubPriority = 0;
- nvic.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&nvic);
- while (1) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement