简体   繁体   English

STM32 接收来自盖革计数器的输入

[英]STM32 receive input from geiger counter

I am trying to send output signal from device on the photo to STM32F100rbt6b.我正在尝试将照片上设备的输出信号发送到 STM32F100rbt6b。 I connected UIN pin from geiger counter to PA10 pin on STM32F100rbt6b.我将盖革计数器的 UIN 引脚连接到 STM32F100rbt6b 上的 PA10 引脚。 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.我认为这与错误的Pin10模式有关。 Which is right?哪个是对的? With GPIO_Mode_IN_FLOATING it is increment number too fast.使用GPIO_Mode_IN_FLOATING它是增量数字太快。

#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.您已将 PA10 引脚初始化为模拟输入。 If you want to make use of an analogue input with an LCD screen, you need an ADC set up on this pin.如果您想使用带 LCD 屏幕的模拟输入,则需要在此引脚上设置 ADC。

If you want to set up your PA10 pin as an ordinary high/low digital input you would do something like this:如果您想将 PA10 引脚设置为普通的高/低数字输入,您可以执行以下操作:

/* 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.您最有可能寻找的是带有 ADC 的模拟输入,它将该引脚上的模拟电压转换为您的微型计算机可以解释的数字。

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:如果您希望 PA10 成为带 ADC 的模拟输入,很遗憾,根据Table 4. Low & medium-density STM32F100xx pin definitions ,这是不可能的。STM32F100x6 数据Table 4. Low & medium-density STM32F100xx pin definitions部分,ADC 未列为替代功能之一(AFs ) 此引脚:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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