博文

AVR ADC Analog to Digital Converter

图片
What is an ADC? An ADC, or Analog to Digital Converter, allows one to convert an analog voltage to a digital value that can be used by a microcontroller. There are many sources of analog signals that one might like to measure. There are analog sensors available that measure temperature, light intensity, distance, position, and force, just to name a few. Introduction The AVR ADC The AVR ADC allows the AVR microcontroller to convert analog voltages to digital values with few to no external parts. The author wrote this tutorial with the ATMega128 in mind, though other AVRs use similar hardware. The ADC built into the ATMega128 is capable of 10 bit resolution. The ATMega128 microcontroller has 8 ADC channels, allowing up to 8 analog sources to be attached to the microcontroller. The 8 ADC channels are connected to the internal DAC through a device called a multiplexer. The multiplexer connects the 8 ADC channels (the 8 pins of Port F on the ATMega128) to the internal ADC. One channel...

AVR Tutorial - Input / Output

图片
Overview You cannot imagine to use microcontroller without using any of its i/o pins. Finally its all about : taking input , processing it and generating output ! Thus i/o registers and their correct settings is indispensable part while learning to program any uC. We will learn how to use AVR ports and actually ‘code’ for writing/reading data to/from port pins. It is slightly confusing for beginners, however once you understand it, you will certainly appreciate the way it is designed. NOTE : I will frequently refer to ‘configuring pin’ or simply ‘pin’. Remember, a port has multiple pins. Thus in order to change setting for one port, you have to change setting for all port pins of that port. To change setting for one single pin of the port, you have to change a particular bit in associated register. Got that ? If not read this para again. Registers AVR is 8 bit microcontroller. All its ports are 8 bit wide. Every port has 3 registers associated with it each one with 8 bits...

计数问题

题目:输入一个整数 n,求从1到n这n个整数的十进制表示中1出现的次数。 例如输入 12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。 分析:这是一道广为流传的 google面试题。用最直观的方法求解并不是很难,但遗憾的是效率不是很高;而要得出一个效率较高的算法,需要比较强的分析能力,并不是件很容易的事情。当然,google的面试题中简单的也没有几道。 一、直觉法    首先我们来看最直观的方法,分别求得 1到n中每个整数中1出现的次数,          然后把它们加起来就行了。         然而, google 的面试不是这么容易 让你过关的。     因为这种方法的复杂度是 O( n ) ,对较大的整数,运算时间太长 二、 新方法     我们通过分析数字的规律,找出加快计算速度的方法。 0 - 9 之间    所有数字中 出现的 1 为   1 个 0 –99       十位的1 有 10个                去掉十位后, 有10组 0~9                所以 共有   10 + 10 个 0 ~ 999   之间              百位的1 有 100 ...

C modifier

when variable has those modifier: n/a variable outside function variable inside function default default as global, could combine with extern default as auto, local scope auto can not use outside function local scope register can not use outside function local scope but faster access static internal linkage, global to translation unit, can not combine with extern persistent vairable to function static can also be used for function, when a function use static it means it can not be used outside its translation unit, such as declared in another file.

ROM and RAM

RAM Random Access Memory or RAM is a form of data storage that can be accessed randomly at any time, in any order and from any physical location., allowing quick access and manipulation. RAM allows the computer to read data quickly to run applications. It allows reading and writing. It is non-volatile i.e. its contents are retained even when the device is powered off. Static RAM vs. Dynamic RAM Static RAM has a pair of transistors forcing each other on and off, so there are electric fields turning on channels to conduct and turn off the opposite transistor. This is a self-reinforcing state, so  it can go on forever. In a dynamic RAM there is just a little bit of charge or not on the gate of a transistor.  This charge will leak away in milliseconds, as there is nothing actively maintaining it all the time.   So dynamic RAM need to have a refresh jolt every few milliseconds , where a pulse causes the read transistor to reinforce the charge level on its gate. Dyna...

保护眼睛的颜色设置

第一步, 在桌面上点击鼠标右键,依次点击“属性”、“外观”、“高级”按钮。   第二步, 在打开的“高级”对话框中,在“项目”下拉列表里选择“窗口”。   第三步, 再打开右边对应的“颜色”列表,选择其中的“其他”一项。在打开的对话框里, 把“色调”的参数设置为85,把“饱和度”参数设置为90,把“亮度”参数设置为205。点击“添加到自定义颜色”按钮,再点击“确定”退出设置。   第四步(如果在看那些很刺眼的网页的情况下),打开IE浏览器,选择“工具”栏中的“internet选项”,点击“辅助功能”按钮,在“不使用网页中指定的颜色”前打钩。然后次点“确定”。

Floyd的cycle-detection算法解析(原创)

图片
要解决的问题是: 如何确定一个链表中时候存在环,如果存在的话,环的起点在哪里? 借用这张经典的解析图 Floyd 的算法又叫做龟兔赛跑算法,那么我们就用龟兔赛跑来解释。 我们假设乌龟和兔子的速度是1:2,然后 假设乌龟的兔子在赛道的某一点相遇了 。(相当于他们直线跑入一个环形赛道,然后在赛道的某一点相遇了)那么因为他们跑得时间是一样的,即他们跑得路程1个是i,另一个是2i。借用上图的表示,我们有如下等式: 1) i = m + p*n + k 2) 2i = m + q*n + k 这里的p和q分别是兔子和乌龟在环里跑得圈数。q>p 解上面的等式去掉i,我们就得到下面这个重要的等式: m + k = (q-2p) * n  (等式一) 因此,如果我们能够证明至少有一种k, p, q的值可以使得这个等式成立,我们就证明了这样的m和n的是存在的。 (如果环存在,即上面等式成立,m和n的值是确定不可变的,只有k,p,q是可变值。) 进而也就证明了乌龟和兔子的相遇是成立的。 这里我们只要使 k = m*n-m; q-2p = 2m; 也就是说存在k,q,p的确定的值的组合(因为他们都可以用m和n表示),使得上式成立。 下面我们来解决第二个问题,即环的起点在哪里。 让我们再加一只乌龟进来,叫做乌龟二号。 乌龟二号和乌龟一号有同样的速度。当乌龟二号在链表起点时,乌龟一号和兔子相遇在环内的K处。 现在,当两只乌龟一起走m步时,即乌龟二号到达了环的起点,这时候 乌龟一号走的总路程 = m + p*n + k + m 根据等式一计算m+k得出: 乌龟一号走的总路程 = (q-2p) * n + p*n + m 即 乌龟一号走的总路程 = (q-p)*n + m 所以当乌龟二号在环起点的时候,乌龟一号走过了(q-n)圈个环,再加上m的路。所以乌龟一和乌龟二第一次相遇的时候的点就是环的起点。 参考 http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare