简体   繁体   中英

STM32 receive input from geiger counter

I am trying to send output signal from device on the photo to STM32F100rbt6b. I connected UIN pin from geiger counter to PA10 pin on STM32F100rbt6b. But it seems that it is working wrong because every time when geiger counter click LCD display does not increment number. I think that it is related with wrong Pin10 mode. Which is right? With GPIO_Mode_IN_FLOATING it is increment number too fast.

#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"

#define LCD_PORT GPIOC
#define LCD_RCC_GPIO RCC_AHB1Periph_GPIOC
#define LCD_E_Pin GPIO_Pin_12
#define LCD_RS_Pin GPIO_Pin_10

void delay(unsigned int s);
void lcd_init_gpio();
void lcd_write_data(int data);
void lcd_init();
void lcd_write_str(char*str);
void lcd_write_cmd(u16 cmd);
void lcd_set_cursor(int line,int pos);
void lcd_write_dec_xxx(uint16_t data);
void lcd_write_dec_xxx(uint16_t data);
void lcd_write_dec_xx(uint8_t data);
void lcd_write_dec_x(uint8_t data);
int counter = 0;

int main(void)
{
      lcd_init();
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

        GPIO_InitTypeDef initInput;
        initInput.GPIO_Mode = GPIO_Mode_AIN;
        initInput.GPIO_Speed = GPIO_Speed_2MHz;
        initInput.GPIO_Pin = GPIO_Pin_10;
        GPIO_Init(GPIOA, &initInput);
      lcd_set_cursor(0,1);

    while(1){

                    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10))
                    {
                        counter=counter+1;
                        lcd_set_cursor(0,3);
                        lcd_write_dec_xxx(((uint8_t)counter));
                        delay(1000000);
                    }           

    }
}

void delay(unsigned int s){
    while(--s > 0) {
        __NOP();
    }
}

const uint8_t lcd_2x16_decode[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

void lcd_write_dec_xxxx(uint16_t data){
    lcd_write_data(lcd_2x16_decode[(data / 1000) & 0x0F]);
    lcd_write_data(lcd_2x16_decode[((data % 1000) / 100) & 0x0F]);
    lcd_write_data(lcd_2x16_decode[((data % 1000) % 100) / 10 & 0x0F]);
    lcd_write_data(lcd_2x16_decode[((data % 1000) % 100) % 10 & 0x0F]);
}

void lcd_write_dec_xxx(uint16_t data){
    lcd_write_data(lcd_2x16_decode[(data / 100) & 0x0F]);
    lcd_write_data(lcd_2x16_decode[((data % 100) / 10) & 0x0F]);
    lcd_write_data(lcd_2x16_decode[((data % 100) % 10) & 0x0F]);
}

void lcd_write_dec_xx(uint8_t data){
    lcd_write_data(lcd_2x16_decode[((data % 100) / 10) & 0x0F]);
    lcd_write_data(lcd_2x16_decode[((data % 100) % 10) & 0x0F]);
}

void lcd_write_dec_x(uint8_t data) {
    lcd_write_data(lcd_2x16_decode[data]);
}
void lcd_init_gpio() {
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
    GPIO_InitTypeDef init;
    init.GPIO_Mode = GPIO_Mode_Out_PP;
    init.GPIO_Speed=GPIO_Speed_50MHz;
    init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10  | GPIO_Pin_12;
    GPIO_Init(GPIOC, &init);
}
void lcd_write_data(int data) {
    GPIO_SetBits(LCD_PORT,data | LCD_E_Pin);
    delay(0xFFFF);
    GPIO_ResetBits(LCD_PORT,LCD_E_Pin | data);
}
void lcd_init() {
    lcd_init_gpio();
    int del = 99999;
    GPIO_ResetBits(LCD_PORT, LCD_RS_Pin);
    delay(del);
    lcd_write_data(0x30);
    delay(del);
    lcd_write_data(0x30);
    delay(del);
    lcd_write_data(0x30);
    delay(del);
    lcd_write_data(0x38);
    delay(del);
    lcd_write_data(0x0f);
    delay(del);
    lcd_write_data(0x01);
    delay(del);
    lcd_write_data(0x06);
    delay(del);
    lcd_write_data(0x02);
    delay(del);
    GPIO_SetBits(LCD_PORT,LCD_RS_Pin);
}

void lcd_write_str(char*str) {
    do {
        lcd_write_data(*str);
    }while(*++str);
}
void lcd_write_cmd(u16 cmd) {
    GPIO_ResetBits(LCD_PORT,LCD_RS_Pin);
    lcd_write_data(cmd);
    GPIO_SetBits(LCD_PORT,LCD_RS_Pin);
}
void lcd_set_cursor(int line,int pos) {
    pos |=0x80;
    if (line == 1) {
        pos += 0x40;
    }
    lcd_write_cmd(pos);
}

在此处输入图片说明

You have initialized your PA10 pin as an analogue input. If you want to make use of an analogue input with an LCD screen, you need an ADC set up on this pin.

If you want to set up your PA10 pin as an ordinary high/low digital input you would do something like this:

/* Setup PA10 input */
void PA10_Config(void)
{
  /* Local variables */
  GPIO_InitTypeDef   GPIO_InitStructure;

  /* Configure PA10 as input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

But looking at your application, you probably don't want that. What you're most likely looking for is an analogue input with an ADC, that converts an analogue voltage on that pin to a number your micro can interpret.

If you wanted PA10 to be an analogue input with an ADC, unfortunately this is not possible, according to Table 4. Low & medium-density STM32F100xx pin definitions section of the STM32F100x6 datasheet, ADC is not listed as one of the alternate functions (AFs) of this pin:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM