简体   繁体   English

检查引脚上的电压并打开LED

[英]Check voltage on pin and turn on an LED

I'm starting my way into PICs microcontrollers, and I want to start with simple things. 我正在开始使用PIC微控制器,我想从简单的事情开始。 I already tried reading a book, but it was more a C book than a microcontrollers book. 我已经尝试过阅读一本书,但它比起微控制器的书更像是一本C书。 I learned about ADCs, timers, memory banks, etc., but I didn't get some practical examples. 我了解了ADC,计时器,存储库等,但是没有得到一些实际的例子。

What are the basics steps to check for some voltage on a pin or turn on an LED ? 检查引脚上的某个电压或打开LED的基本步骤是什么? I mean, I'm not requesting for all the code but a guide on how to do these basic tasks. 我的意思是,我并不需要所有代码,而是有关如何执行这些基本任务的指南。 Also, I know that the code depends on the microcontroller, so I have a 16F628A. 另外,我知道代码取决于微控制器,所以我有16F628A。

Controlling an LED like this is a great way to introduce yourself to microcontrollers and electronics. 像这样控制LED是向您介绍微控制器和电子设备的好方法。 You'll learn how to read data sheets, how to perform low-level system configuration, and how to build some electronic circuits. 您将学习如何读取数据手册,如何执行低级系统配置以及如何构建一些电子电路。 But as you've probably seen already, even the simplest tasks require a bit of bit-twiddling and trial-and-error at first. 但是,正如您可能已经看到的那样,即使最简单的任务,一开始也需要一些琐碎的操作和反复试验。 The good news is that, once you understand the basics, you can apply the same techniques to entire classes of microcontollers, and you'll develop your own library of functions that will make it increasingly easier to build new projects. 好消息是,一旦您了解了基础知识,就可以将相同的技术应用于整个微控制器类,并且将开发自己的函数库,这将使构建新项目变得越来越容易。


The 16F628A has memory-mapped I/O , which means its internal registers control the behavior of its special-function pins. 16F628A具有内存映射的I / O ,这意味着其内部寄存器控制其特殊功能引脚的行为。

According to the data sheet (PDF), the 28A has two analog comparators . 根据数据表 (PDF),28A具有两个模拟比较器 Each comparator is connected to three pins on the 28A. 每个比较器都连接到28A上的三个引脚。 Two pins are used for input: they're connected to the voltages you want to compare. 两个引脚用于输入:它们连接到要比较的电压。 The third pin is used for output: the comparator indicates which input voltage is higher by setting the voltage on the output pin high or low. 第三个引脚用于输出:比较器通过将输出引脚上的电压设置为高电平或低电平来指示哪个输入电压较高。

The 28A also has a voltage reference that's connected to another pin. 28A还具有连接到另一个引脚的参考电压。 This can generate a known voltage, and you can connect it to one of the comparator inputs. 这可以产生一个已知电压,您可以将其连接到比较器输入之一。 Connect your test voltage to the other comparator input, and use the output pin to drive the LED. 将测试电压连接到比较器的另一个输入,然后使用输出引脚驱动LED。 Then the LED will turn on or off when your test voltage is higher than the reference. 然后,当您的测试电压高于参考电压时,LED就会亮起或熄灭。 (The 28A is actually configurable: you can choose which condition will light the LED.) (28A实际上是可配置的:您可以选择哪种条件将点亮LED。)

The data sheet includes some assembly code that shows you how to configure the comparator by setting the appropriate bits in its control register. 数据手册包括一些汇编代码,向您展示了如何通过设置比较器控制寄存器中的相应位来配置比较器。

You'll also need to find out whether the 28A can drive an LED directly. 您还需要找出28A是否可以直接驱动LED。 Some devices contain the appropriate current-limiting circuitry internally; 某些设备内部包含适当的限流电路。 others require you to provide it yourself. 其他人则需要您自己提供。

You may have some luck with a web search for "16F628A application notes" (for manufacturer's suggestions) or just "16F628A application" (for apps created by users). 您可能会在网上搜索“ 16F628A应用程序说明”(对于制造商的建议)或仅在“ 16F628A应用程序”(对于用户创建的应用程序)进行搜索。 This looks interesting, and the author offers to email his software to you. 看起来很有趣,作者提供了将其软件发送给您的电子邮件。 Maybe he'll offer some tips as well. 也许他也会提供一些提示。

Good luck! 祝好运!

Actual code will depend slightly on the compiler you're using, and greatly on whether you want to use some wrapper library (I don't recommend it, as often the code to call the wrapper is longer than just setting the right registers). 实际的代码将在某种程度上取决于您使用的编译器,并在很大程度上取决于您是否要使用某些包装器库(我不建议您这样做,因为调用包装器的代码通常比设置正确的寄存器要长)。

In general, what you are wanting to do is called GPIO (general purpose input and output), and you need to do the following: 通常,您要执行的操作称为GPIO(通用输入和输出),您需要执行以下操作:

  1. Make sure the GPIO peripheral clock is enabled. 确保启用了GPIO外设时钟。 For PICs, there isn't a separate clock for GPIOs, so nothing to do here. 对于PIC,没有用于GPIO的单独时钟,因此在此无需执行任何操作。
  2. Configure the pins for their GPIO function (most pins have multiple purposes, and can be connected to SPI controllers, analog-to-digital-converters, etc.) 配置引脚以使其具有GPIO功能(大多数引脚具有多种用途,可以连接至SPI控制器,模数转换器等)。
  3. Write the initial value for output pins. 写入输出引脚的初始值。
  4. Configure the pin direction. 配置引脚方向。 (input or output) (输入或输出)
  5. Configure pull-up and pull-down registers, or lack thereof. 配置上拉和下拉寄存器,或缺少上拉和下拉寄存器。
  6. Configure interrupt-on-change. 配置电平变化中断。 (if you want to use it) (如果您想使用它)
  7. Install the interrupt handler. 安装中断处理程序。 (if you are using it) PICs place interrupt handlers at specific locations in memory, so nothing needed here. (如果使用的话)PIC将中断处理程序放置在内存中的特定位置,因此这里不需要。
  8. Read and write the GPIO pins as your application requires. 根据您的应用要求读写GPIO引脚。

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

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