搜索
bottom↓
回复: 86

mcu-based LC meter

[复制链接]

出0入0汤圆

发表于 2009-10-8 07:48:58 | 显示全部楼层 |阅读模式
I have been thinking about this for a while. there are lots of lc meter designs floating on the web, typically based on a PIC or AVR, with a LCD display.

I am thinking about building one of them. Unfortunately, pretty much all of them are programmed in assembly. making them quite difficult to port from different platforms.

I am thinking about a similar design, using however, just C.

the design should have the following features:

1) can measure inductance and capacitance;
2) can self calibrate;
3) should be immune to parasitic inductance / capacitance;
4) minimum parts;
5) if possible, it can be used as a counter or a frequency meter.
6) the software should be portable to other platforms.
7) open source. If you can make money off of it, more power to you.

this thread is simply a design process, and I will start from the very basic building blocks. so expect it to take a long time to complete.

if you want, your help is more than welcome.

出0入0汤圆

发表于 2009-10-8 07:50:35 | 显示全部楼层

出0入0汤圆

 楼主| 发表于 2009-10-8 08:11:10 | 显示全部楼层
the first order of business is to pick a mcu.

I am going to go with a 16f684. because I have quite a few of them, and because I can simulate them in proteus (7.4sp3), and I can program them in picc-lite.

I don't know, however, in the end if it is going to have enough memory for what we want to do. But I will deal with that later.

出0入0汤圆

 楼主| 发表于 2009-10-8 08:12:35 | 显示全部楼层
the first thing is to get a lcd to work. I have taken a lcd demo from hi-tech and modified it so you can arbitrarily arrange the pin-out.

here it is.


(原文件名:mcu-based lc meter - 1.PNG)

出0入0汤圆

 楼主| 发表于 2009-10-8 08:14:11 | 显示全部楼层
here is the source code.

点击此处下载 ourdev_489266.rar(文件大小:3K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-8 08:18:14 | 显示全部楼层
the next thing to do is to move LCD_RS from RA0 to RC0, and LCD_EN from RA1 to RC1.

we need to free up RA0 and RA1 because we need to use Comparator #1 which takes RA0, RA1 and RA2.

出0入0汤圆

 楼主| 发表于 2009-10-9 07:58:07 | 显示全部楼层
I just moved the two lines (LCD_RS and LCD_EN to RC0 and RC1, respectively).


(原文件名:mcu-based LC meter - 2.PNG)


also introduced a new function, LCD_display() that shows the content of vRAM[2][16]. future update to the LCD will be written to vRAM first and then displayed with LCD_display().

the next job is to write a function that can convert a floating variable to string for display.

出0入0汤圆

发表于 2009-10-9 08:20:40 | 显示全部楼层
顶!!!

出0入0汤圆

 楼主| 发表于 2009-10-10 05:52:29 | 显示全部楼层
the simplest way to convert a floating number to a string is to use the sprintf() function. Unfortunately, it adds a lot of overhead (about 1k for pic).

here is a quick example.


(原文件名:mcu-based LC meter - 3.PNG)

however, here is a big problem with this: since we are using floating math which takes 1k of code space, we are dangerously close to the rom space limit of the chip (2k). As a matter of fact, the code as is takes 7DAh vs. 8FFh of total available space.

So we will have to write our own ftoa() function.

the code base is here: 点击此处下载 ourdev_489932.rar(文件大小:4K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-10 05:53:36 | 显示全部楼层
the bottom row, "[Lft]" and "[Rgt]", represents two menu items to be activated by two soft keys, to be implemented later.

出0入0汤圆

 楼主| 发表于 2009-10-10 18:15:50 | 显示全部楼层
first revision to a new ftoa function that does not rely on sprintf().


(原文件名:mcu-based LC meter - 4.PNG)



two problems with the new my_ftoa() function:

1) it doesn't seem to be very accurate: a numeric figure of 1230457.789 gets displayed as 1230407.71.
2) it doesn't control the length of the resulting string.

we will fix that in the next iteration.

the code now takes just over 1k, a much better improvement over the previous version that relied on sprintf().

出0入0汤圆

 楼主| 发表于 2009-10-10 18:16:30 | 显示全部楼层
also, we have now three softkeys on two hardware keys: left, right, and both.

出0入0汤圆

 楼主| 发表于 2009-10-11 03:33:36 | 显示全部楼层
a better ftoa() function. it produces the conversion in the desired length, produces smaller code and is more accurate (and faster too).


(原文件名:mcu-based LC meter - 5.PNG)

the fx is actually 12345678.90. so the new ftoa() function is accurate upto the 8th digit.

出0入0汤圆

 楼主| 发表于 2009-10-12 05:00:23 | 显示全部楼层
the first thing to do in a lc meter is to measure frequency. so our first job is to design a frequency meter.

so here it is.

we used two interrupts: RA2/INT which triggers whenever RA2 goes from low to high (on the rising edge), and timer0 which triggers after a pre-set period of time (in this case, we set timer0 to interrupt 120x, with each interrupt being 8.192ms long).

the software also does averaging to help smooth out the display but as is it is disabled.

we fed our meter with a 10khz signal on RA2, as you can see in the display.


(原文件名:mcu-based LC meter - 6.PNG)


two issues:
1) our reading is off by about 50%. it should read close to 10khz but instead it reads 4.6khz.
2) it still has leading errors, rather than leading blanks. we will need to revisit our ftoa() function.

more work to do.

出0入0汤圆

 楼主| 发表于 2009-10-12 05:02:03 | 显示全部楼层
here is the existing code base.

点击此处下载 ourdev_490584.rar(文件大小:10K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-12 05:47:43 | 显示全部楼层
here is a revision to the last one. because of stupidity on my part, I didn't set some bits in the OPTION register.

how it reads much better: 10khz source measured as 10.6khz.

I also implemented a flip on RA4 to help me make sure that the timer0 interrupts every 8.192 seconds.


(原文件名:mcu-based LC meter - 7.PNG)

出0入0汤圆

发表于 2009-10-12 06:03:47 | 显示全部楼层
支持

出0入0汤圆

 楼主| 发表于 2009-10-12 06:15:19 | 显示全部楼层
well, a stupid mistake on my part, in one of the "define" statement ("#define timer0_limit = 120"), :)

here is the revised code. it understates the frequency by about 3%, due in part to the branching in the ISR.

点击此处下载 ourdev_490587.rar(文件大小:10K) (原文件名:16F684 - MyLCDDemo.rar)

"支持"

thanks. it is a struggle for me, :).

出0入0汤圆

发表于 2009-10-12 06:48:09 | 显示全部楼层
暂未装PICC编译器,请附HEX文件.

出0入0汤圆

 楼主| 发表于 2009-10-12 07:01:54 | 显示全部楼层
one problem with the design is the use of RA2/INT as interrupt trigger: the software / hardware isn't fast enough at high frequencies (in this case, the highest frequency the meter can measure is between 50k and 100khz).

another approach is to configure timer0 as a counter and use timer1 to determine the duration.

will take some time to reconfigure the mcu that way.

出0入0汤圆

 楼主| 发表于 2009-10-12 07:04:34 | 显示全部楼层
"暂未装PICC编译器,请附HEX文件."

this one should work.

picc also generate cof files, making debugging at source file levels in proteus a dream come true.

点击此处下载 ourdev_490589.rar(文件大小:13K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-12 07:07:07 | 显示全部楼层
the last version has a variable called calib_f that in the future will allow for user calibration.

出0入0汤圆

发表于 2009-10-12 07:45:11 | 显示全部楼层
one problem with the design is the use of RA2/INT as interrupt trigger: the software / hardware isn't fast enough at high frequencies (in this case, the highest frequency the meter can measure is between 50k and 100khz).

===========================================================================================================================================
供参考


点击此处下载 ourdev_490593.pdf(文件大小:129K) (原文件名:AN592.pdf)

出0入0汤圆

发表于 2009-10-12 08:24:29 | 显示全部楼层
我早就写了一个c 的了。可参考。

出0入0汤圆

发表于 2009-10-12 08:29:20 | 显示全部楼层
frequency meter可以看看这个,感觉不错,即有信号处理电路,也可以测量到50M,近期准备仿制一个,就是PIC单片机不熟,不知道能不能用AVR或增强51代替

frequency meter (原文件名:pfc50shc.gif)

出0入0汤圆

发表于 2009-10-12 09:43:18 | 显示全部楼层
非常支持!

出0入0汤圆

 楼主| 发表于 2009-10-12 19:24:13 | 显示全部楼层
an592 is really the grandfather of all mcu-based frequency / lc meters. it is quite simple to implement. it uses timer0 as a counter, and stops the timer after a pre-determined period of time (aka precision delay the note was talking about).

it has the advantage of simplicity. But also the disadvantage of in-accuracy caused by the isr in the delay loop.

because of the use of the 8-bit prescaler in timer0, it has to use another pin to shift out the value in the prescaler.

most recent lc meters have moved to a two-timer approach: one timer function like a counter and another for precision timing.

I am not sure which is more accurate but we are going to find it out by end of this exercise.

出0入0汤圆

 楼主| 发表于 2009-10-12 22:12:51 | 显示全部楼层
here is a slightly different approach: it uses timer1 (a 16bit timer) to count the pulse train on c1cki (ra5). Prescaler is set to 1:4. With an input of 10khz signal, the program counted 3048 pulses (should really be 3000 pulses) in 300ms.

the approach here is quite similar to the one used in an592 - except that I didn't gate timer1's pulse train with hardware - the chip (16F684 has that capability but it will take one extra pin).

the advantage is the simplicity (thus the accuracy) of the ISR routine. But the delay routine wouldn't be as accurate.


(原文件名:mcu-based LC meter - 8.PNG)

点击此处下载 ourdev_490908.rar(文件大小:19K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-12 22:20:04 | 显示全部楼层
someone asked that I explain the basic structure of the frequency meter.

Essentially, the pulse train is counted, in this case, by timer1, through a prescaler first, and then through TMR1L (the lowest 8-bit) and then TMR1H (the highest 8-bit) and then by a software counter, unsigned long f_count.

in this particular set-up, the prescaler is set to 1:4x. so every four pulses will trigger TMR1L to increment by one. once TMR1L rolls from 0xff to 0x00, it triggers TMR1H to increment by 1. when TMR1H:TMR1L rolls from 0xffff to 0x0000, it triggers the TMR1IF flat and the ISR() routine. in the ISR routine, we increment f_count by one.

so the end result is that the count's lowest 8 bits are in TMR1L, the 2nd highest 8 bits are in TMR1H, and the highest 32 bits are in f_count. That's the results returned by count_measurement();

the inaccuracy obviously is the pulses in the prescaler (up to 8 pulses possible, and upto 4 pulses in our particular implementation). this can be a problem for very short duration or very slow pulses.

出0入0汤圆

 楼主| 发表于 2009-10-12 23:28:46 | 显示全部楼层
did a few revisions to the measurement routine, and this is a more accurate one. As you can see, it goes to at least 500khz, and potentially higher, with no material worsening of the accuracy - error rate, unadjusted, is about 1.6%.


(原文件名:mcu-based LC meter - 9 - 100k.PNG)


(原文件名:mcu-based LC meter - 9 - 500k.PNG)

点击此处下载 ourdev_490931.rar(文件大小:19K) (原文件名:16F684 - MyLCDDemo.rar)


the timer1 prescaler can be set by the user and the program will automatically adjust the output based on the prescaler used.

three tasks:

1) rewrite the ftoa() routine;
2) add menu items;
3) allow user calibration.

出0入0汤圆

发表于 2009-10-13 00:09:13 | 显示全部楼层
support you

and one question
Performance Indicators ?

出0入0汤圆

发表于 2009-10-13 03:27:30 | 显示全部楼层
为了验证PIC本身的测频上限,曾做过PIC的50MHz频率计试验,我用了很多NOP调整闸门时间来确保精度,硬件实验通过并比仿真显示的精度还高.就是凑闸门的准确时间费点工夫.实践证明PIC不加外分频电路做50MHz频率计是可能的,AVR则不能.
(原文件名:50MHz.png)

出0入0汤圆

 楼主| 发表于 2009-10-13 04:31:20 | 显示全部楼层
we are getting closer, slightly, :).

here is the revised code base. major changes from the last revision:

1) added the conversion from the counter to frequency.
2) revised the display of menu bar.

.hex fileourdev_490971.rar(文件大小:3K) (原文件名:16F684-MyLCDDemo.rar)
source filesourdev_490972.rar(文件大小:19K) (原文件名:16F684 - MyLCDDemo.rar)

(原文件名:mcu-based LC meter - 10.PNG)

出0入0汤圆

 楼主| 发表于 2009-10-13 04:37:39 | 显示全部楼层
"我用了很多NOP调整闸门时间来确保精度,硬件实验通过并比仿真显示的精度还高.就是凑闸门的准确时间费点工夫."

it depends.  the issue here is that servicing the interrupt will take time, away from the delay loop.

how many times you service the interrupt, however, depends on the pulse train and cannot be accurately predicted before hand. a fast pulse train will necessarily generate more interrupts and needs to be serviced more frequently than a slow pulse train.

so the "optimal" length of the delay loop for one pulse train may not be optimal for another pulse train.

"实践证明PIC不加外分频电路做50MHz频率计是可能的,"

you can use the prescaler to do it. the issue with using IOC or INT, as I did before, is that they don't have a highspeed buffer, as the cki pins (t1cki or t0cki) do.

出0入0汤圆

 楼主| 发表于 2009-10-13 04:39:20 | 显示全部楼层
"Performance Indicators ?"

you mean how accurate it is? it will likely depends on how well you can calibrate the meter, and how accurately the software is. I think getting to 1% isn't difficult. More accuracy is possible but needs better calibration and better software capabilities.

出0入0汤圆

 楼主| 发表于 2009-10-13 04:46:03 | 显示全部楼层
fixed the ftoa() routine.

change

"        for (i=0; (i<ftoa_str_len-1) | (d>0); i++) {"

to

"        for (i=0; (i<ftoa_str_len-1) & (d>0); i++) {"

stupid mistake and careless programming.


(原文件名:mcu-based LC meter - 11 - 500hz.PNG)

出0入0汤圆

 楼主| 发表于 2009-10-13 07:14:27 | 显示全部楼层
we are roughly there, :).

here is the code base that allows user calibration.

here is how it works:

1) at power up, the meter goes straight into a frequency meter mode, measuring the pulse train on RA5, and displays its results on the LCD. the measurement is based on calib_f, which in the program is deliberately set 900, with 985.20 being the correct number. because of this, the frequency meter will have a substantial error, under-estimating the frequency of the pulse train.

2) the main menu provides three choices, on the left is the L/C meter mode (not implemented right now), on the right is the calibration mode (fully implemented), and if you push both left and right keys (by pressing the double throw switch SW1), you will enter the setup mode (not implemented right now).

3) once in the calibration mode, you will be able to change the value of calib_f, up or down by 1% (user selectable), until the meter reads the correct reading (for a pulse train of a known frequency). At that point, you push both the right and left button to exit the calibration mode (by pressing SW1).


main menu (原文件名:mcu-based LC meter - 12 - main menu.PNG)


sub menu - calibration (原文件名:mcu-based LC meter - 12 - calibration submenu.PNG)

source file + .hex fileourdev_490979.rar(文件大小:21K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-13 07:22:00 | 显示全部楼层
a few things:

1) on calib_f: in production, calib_f should be declared an eeprom variable so that any changes made to calib_f in the calibration menu will be automatically saved.

2) R1/R2 can be potentially eliminated by enabling weak pull-up on porta. I didn't have time for that right now.

3) l/c meter mode: depending on how we want to utilize RA0..2 in the future, we can implement the switching from frequency meter mode to l/c meter mode via software or via a switch. I haven't thought much about it.

we have about 35% of the program space left, and we can save more by pushing some messages into eeprom. I am pretty sure that we will be able to keep it under the 2k space for this chip.

出0入0汤圆

 楼主| 发表于 2009-10-13 07:22:24 | 显示全部楼层
please let me know if you run into any bugs. Thanks!

出0入0汤圆

发表于 2009-10-13 12:26:25 | 显示全部楼层
well done

出0入0汤圆

 楼主| 发表于 2009-10-14 08:33:07 | 显示全部楼层
to measure L or C, we will need an oscillator whose output frequency depends on L and C.

that is the L/C tank circuitry you will see now.


(原文件名:mcu-based LC meter - 13 - f1.PNG)


here, L1 (=L) is the inductor, and C3(=C) is the capacitor. The output frequency, f1 = 1/(2*pi*sqrt(L*C)), where pi=3.1415926.

SW2 and C4/L2 simulate the calibration capacitor (Ccal), or inductor (Lx) or capacitor (Cx) to be tested.

the process goes like this:

1)  you run the oscillator without Cx or Lx in the circuit - SW2 is in the center. you get a frequency, call it f1:

f1=1/(2*pi*sqrt(L*C));

2) you then run the oscillator with Ccal in the circuit - SW2 is on the left. you get another frequency reading, call it f2:

f2=1/(2*pi*sqrt(L*(C+Ccal)));

obviously, f1/f2=sqrt(1+Ccal/C).

or C=Ccal/((f1/f2)^2-1).

from that, you get L.

that is, as long as you know Ccal, you can find out C, thus L.

let's see the theory at work. the following is the same simulation run (uncaliberated frequency meter), after SW2 is pushed to the left (so Ccal=C4=1u).


(原文件名:mcu-based LC meter - 13 - f2.PNG)

thus, we have f1=14525.76hz; f2=10193.76hz; Ccal=1uf. so C=1uf/((14525.76/10193.76)^2-1)=1uf/1.030527706=0.970376627, or an error of 3%, with a meter that is uncalibrated!

L=1/((2*pi*f1)^2*C)=123.715u. or a 10% error.

not great but not bad either.

here is the source files + .hex file.

点击此处下载 ourdev_491353.rar(文件大小:27K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-14 08:37:54 | 显示全部楼层
once you know L, C, (thus f1), you then measure f2 with either a Cx or Lx in parallel with your tank.

if f2 > f1, you know you have an inductor Lx in parallel with your L/C tank, and you can always calculates its equivalent inductance Le = 1/(2*pi*f2*sqrt(C))^2, and you know 1/Le=1/L+1/Lx. since you know Le, and L, you can solve for Lx easily.

similarly, if f2<f1, you know you have a capacitor Cx in parallel with the tank. you can calculate the equivalent capacitance Ce=1/(2*pi*f2*sqrt(L))^2, and you know Ce=C+Cx, so Cx = Ce-C.

that, basically, is the principle of a L/C meter.

that's why we have to build a frequency meter first, :).

出0入0汤圆

 楼主| 发表于 2009-10-14 08:40:02 | 显示全部楼层
we will have to first build a (L/C meter) calibration function. since we don't know what calibration capacitor, Ccal, our user will be using so we will build a function that allows the user to input the known value of a capacitor used in the calibration.

from there, we can build up the necessary math to finish the calculation.

出0入0汤圆

发表于 2009-10-14 11:55:02 | 显示全部楼层
LC METER MARK

出0入0汤圆

发表于 2009-10-14 13:20:40 | 显示全部楼层
millwood0 是外国人?英文很好啊!顶

出0入0汤圆

 楼主| 发表于 2009-10-17 19:31:49 | 显示全部楼层
I have 47h words left in the program space (out of 800h).

But I got most of the routines done. just need to have a good and logic layout for the meter. so I may still come in under 800h words.

stay tuned.

出0入0汤圆

 楼主| 发表于 2009-10-18 05:16:56 | 显示全部楼层
good news: the meter works now!

the basic set-up is for a L=110uh, C=3484.41pf tank to oscillate at 250k (= 1/(2*pi*sqrt(110u*3484.41ph)).

if the lank is oscillating at 500khz, the lc meter automatically detects that an inductor has been added to the tank, and measured the total inductance at 27.50uh = (1/((2*pi*500khz)^2*3484.41ph). so the added inductance must be 1/(1/27.50uh - 1/110uh) = 36.66uh.

our meter measures it at 36.66uh!


(原文件名:mcu-based LC meter - 14 - Lx.PNG)

similarly, when the lc meter sees 125khz, it knows that a capacitor (Cx) has been added to the l/c tank, and calculates the capacitor to be 11053.76pf (vs 11253.21pf).


(原文件名:mcu-based LC meter - 14 - Cx.PNG)

here is the code + .hex file. the code is wired to go straight into lc meter mode right now, for testing purposes.

点击此处下载 ourdev_492877.rar(文件大小:28K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-18 05:19:11 | 显示全部楼层
the bad news?

I am running out of space for data and code. the program as is is 767h long (vs. 800h total) for code, and 77h vs. 80h for data.

My calibration portion for Ccal is almost done but will likely need more than 33h words.

so I have to figure out how to save some space. or going to a bigger chip.

出0入0汤圆

 楼主| 发表于 2009-10-18 05:33:12 | 显示全部楼层
here is a better set of simulation and code: in the code posted above, I used an inaccurate calib_f parameter. here is the corrected one.

by changing the clock frequency, you can simulate how the software reacts to different situation.

when the clock runs at 500khz, the software correctly detects that an inductor of 36.67uh is at the terminal. the correct value is 36.66uh -> an error of 0.01u / 36.66uh=0.03%.


(原文件名:mcu-based LC meter - 15 - Lx.PNG)

when the clock runs at 125khz, the software correctly detects that a capacitor of 11059.84pf is at the test termianals. the correct value is 11053.21pf, or an error rate of 0.06%.


(原文件名:mcu-based LC meter - 15 - Cx.PNG)

the code attached below is complete for the frequency meter portion (both frequency measurement + frequency calibration) but not complete for the lc meter calibration.

enjoy.

点击此处下载 ourdev_492908.rar(文件大小:28K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

 楼主| 发表于 2009-10-18 07:55:45 | 显示全部楼层
I fixed a few minor bugs and tried to shrink the program down so I could include a calibration piece for Ccal. No luck. so this will be the last version for PIC16F684. After this, I will move the program to PIC16F886, for more space.

here is a fully function frequency meter, with a working calibration piece, plus a working l/c meter (with automatic inductance / capacitance detection), but an incomplete calibration piece. SW3 simulate the L/C tank when different L or C is attached to the tank and the l/c meter will automatically detect that and shows the right value for the L or C.

you can play with it and be amazed at the meter's accuracy.


(原文件名:mcu-based LC meter - 16 - LCx.PNG)


here is the code, including source code and .hex file.

点击此处下载 ourdev_492911.rar(文件大小:28K) (原文件名:16F684 - MyLCDDemo.rar)

出0入0汤圆

发表于 2009-10-18 08:17:39 | 显示全部楼层
thanks a lot

出0入0汤圆

 楼主| 发表于 2009-10-18 08:19:08 | 显示全部楼层
here is how the program runs on a 16F886. with minor changes - all on port settings and using the right registers. all was done inside 10 minutes.


(原文件名:mcu-based LC meter - 17 - LCx.PNG)

the source files + .hex file

点击此处下载 ourdev_492917.rar(文件大小:30K) (原文件名:16F886 LCF Meter.rar)

出0入0汤圆

 楼主| 发表于 2009-10-18 08:22:45 | 显示全部楼层
in terms of space, the program space is 26% used, and data space 33% used. so we have a long way to go to exhaust the resources here.

the lesson I learned here is that if you are writing a reasonably complicated program in C, it pays to do it on a larger chip, a chip with more memory because the c compiler is inevitably less efficient than an assembler. in that regard, the new extended mid-range (EMR) chips (16F88x and 16F1xxx chips) are well suited for something like this.

I will start working on the lc calibration part tomorrow.

出0入0汤圆

 楼主| 发表于 2009-10-18 08:45:45 | 显示全部楼层
exactly what changes did I make to the code base to port it from 16F684 to 16F886?

1) CMCON0: 16F684's portc has comparators that are default to be on. we had to turn it off in the code. 16F886 doesn't need that.

2) timer1 gate enable: 16F684's timer1 gate enable is called T1GE, while 16F886's is called TMR1GE. a quick name change.

3) T1CKI: timer1's input is on porta on 16F684 and it is on portC (RC0) on 16F886. so we have to more LCD_EN and LCD_RS to two other pins on portc (and make appropriate changes in lcd.c to reflect that - see how LCD_RS, LCD_EN and init_ctrl are defined in lcd.c for details).

4) turn off LVP: 16F684 doesn't have low voltage programming and 16F886 does so we have to turn that off.

Other than that, the code compiles correctly and in my brief testing it works properly as well.

this is a great example of the benefits of writing code in a high level language like C: if you want, the same code will work on an ARM chip as well, with some changes.

出0入0汤圆

 楼主| 发表于 2009-10-18 20:08:14 | 显示全部楼层
we are DONE!

for the most part anyway, :)

here is the complete code base, and some screen shots of how the Ccal calibration process works.

But first, we need to fresh our math.

the purpose of the Ccal calibration is to allow the use of unknown value inductor (L1) and capacitor (C3) in the l/c tank oscillator. the mcu will measure the oscillation frequency of the l/c tank (f1), and the oscillation frequency of the l/c tank + Ccal in parallel (f2), plus the user inputed value of Ccal (Ccal), the mcu will calculate the value of L1 (=L) and C3 (=C) so you don't need to use precision parts to build this meter.

we know
f1=1/(2*pi*sqrt(L*C));
f2=1/(2*pi*sqrt(L*(C+Ccal));

from that, we can solve for L and C:
C=Ccal / ((f1/f2)^2-1);
L=1/((2*pi*f)^2*C);

出0入0汤圆

 楼主| 发表于 2009-10-18 20:19:17 | 显示全部楼层
here are the screen shots, from one simulation.

you enter the Ccal calibration module through "Freq->Clbr".

f1 measurement: f1=299944.96hz (notice SW3 is on the 300khz clock position).


(原文件名:mcu-based LC meter - 18 - f1.PNG)

push the right button ("Done"), and the mcu will prompt you to put in Ccal so it can measure f2. here, you will need to switch SW3 to the lower frequency source (100khz) to simulate the lower tank frequency when Ccal is in parallel with C and L.


(原文件名:mcu-based LC meter - 18 - prompt1.PNG)

now, you are about to enter the value of Ccal. the initial value of Ccal is defined by Ccal in the software, as 1100pf. you first can step it up or down by 10% (as suggested by the upper case menu items) by pushing the left (increase) or right (decrease) button. Pushing the middle button (to simulate pushing both left and right buttons) gets you to the next screen.

we will leave it alone for now by pushing the middle button.


(原文件名:mcu-based LC meter - 18 - Ccal 10per.PNG)

出0入0汤圆

 楼主| 发表于 2009-10-18 20:24:27 | 显示全部楼层
the next screen is for finer adjustment of Ccal, at 1% stepping, as indicated by the lower case menu - finer (0.1%) adjustment is possible as well. you just need to uncomment one line.


(原文件名:mcu-based LC meter - 18 - Ccal 1per.PNG)

we will just leave it unchanged by pushing the middle button.

now the mcu starts to measure f2: f2=99978.24hz (the true value is 100khz).


(原文件名:mcu-based LC meter - 18 - f2.PNG)

出0入0汤圆

 楼主| 发表于 2009-10-18 20:30:43 | 显示全部楼层
after that, the mcu will display f1, f2, Ccal, and C and L. you just need to click the right button a few times to cycle through.



(原文件名:mcu-based LC meter - 18 - C.PNG)

so now, we have f1=299944.96hz, f2=99978.24hz, and Ccal=1100ph.

we can calculate C and L to be, by the formula we suggested earlier:

C=137.49pf, vs. 137.48pf calculated by the mcu., an error rate of 0.01%!

once we have f1 and C, we have L = 2047.80pf.

the next parameter to display is L.


(原文件名:mcu-based LC meter - 18 - L.PNG)

error rate = 0%! the mcu is right on.

出0入0汤圆

 楼主| 发表于 2009-10-18 20:34:55 | 显示全部楼层
here is the code base + .hex file.

点击此处下载 ourdev_493204.rar(文件大小:31K) (原文件名:16F886 LCF Meter.rar)

the code takes A42h words, out of 2000h total available. so we wouldn't have fit it inside of a 16F684.

出0入0汤圆

 楼主| 发表于 2009-10-18 20:47:47 | 显示全部楼层
one interesting feature of this LCF meter is that once the meter has been calibrated, it will save the calibration data into the eeprom automatically so that on the next power-up, you don't need to calibrate it anymore. all you need to do is to put an "eeprom" qualifier in front of f1, L, C and Ccal.

because of the use of calib_f factor, the meter also does not require high precision crystal and can using internal RC oscillator, making it simpler and cheaper to build.

出0入0汤圆

 楼主| 发表于 2009-10-18 21:02:48 | 显示全部楼层
I fixed a minor bug, and turned on the support for sprintf() in the my_ftoa() routine, since we have a lot more space. now, the program size is approaching 1000h words (50% used).

you will need to turn on floating point support in the printf() routine for the code to compile and work properly.

if you don't want to use sprintf(), you can simple switch base to the previous my_ftoa() routine.

点击此处下载 ourdev_493221.rar(文件大小:33K) (原文件名:16F886 LCF Meter.rar)

出0入0汤圆

 楼主| 发表于 2009-10-19 04:17:15 | 显示全部楼层
on a side note, I just ported a GLCD driver I wrote for LPC21xx ARM chips and ported it to PIC. in the future, I could swap the GLCD for the 16x2 LCD I am using right now and potentially adding some graphics capabilities to the meter.

the driver takes about 400h words (15% of total space on a 16F886) so it shouldn't be too much over the existing driver for the 16x2 LCD.


(原文件名:mcu-based LC meter - 19 - NOKIA7110.PNG)

出0入0汤圆

 楼主| 发表于 2009-10-20 08:46:48 | 显示全部楼层
two minor changes:

1) the meter defaults to its L/C meter mode upon power-up. It used to power up to a frequency meter.

2) the meter now auto ranges. it will try to produce 6 effective digits (the effective digits for 24-bit floating math in picc libraries).


(原文件名:mcu-based LC meter - 20 - autoranging.PNG)

and the source code + .hex file.

点击此处下载 ourdev_493665.rar(文件大小:33K) (原文件名:16F886 LCF Meter.rar)

出0入0汤圆

发表于 2009-11-1 01:43:53 | 显示全部楼层
nice work

出0入0汤圆

发表于 2009-11-1 15:37:21 | 显示全部楼层
good!

出0入0汤圆

 楼主| 发表于 2009-11-3 21:20:12 | 显示全部楼层
the sucker works!

just got my lcd display - mailed from China!

put the mcu portion of it on a breadboard and hooked up my power supply (a 4x battery pack, at 5.25v roughly).

here is a picture of the setup. Vlcd is a 100k pot that controls the contrast on the lcd.

no frequency source is connected. no switch connected either.

the programming is done via a microchip pickit2, through loose wires.


点击此处打开 ourdev_499389.PNG(文件大小:758K,只有400K以内的图片才能直接显示) (原文件名:mcu-based LC meter - 21 - setup.PNG)

a close up picture of the display.

点击此处打开 ourdev_499390.PNG(文件大小:574K,只有400K以内的图片才能直接显示) (原文件名:mcu-based LC meter - 22 - display.PNG)

which is consistent with the sim, if we feed T1CKI with a DC signal (0.0001hz in this case).


(原文件名:mcu-based LC meter - 22 - sim.PNG)

the entire code base, including the .hex file, compiled using picc-std 9.60pl0, under hi-tide.

点击此处下载 ourdev_499393.rar(文件大小:35K) (原文件名:16F886 LCF Meter.rar)

enjoy.

出0入0汤圆

 楼主| 发表于 2009-11-3 21:22:23 | 显示全部楼层
next, we need to put in the buttons, and build a frequency source so we can test the frequency meter.

出0入0汤圆

 楼主| 发表于 2009-11-3 22:17:48 | 显示全部楼层
I picked up two wires to use as buttons and a gated oscillator (based on a 4093) as the frequency source.

here is a shot of the meter in frequency meter mode.

and then in the lc meter mode.


(原文件名:mcu-based lcf meter 22 - freq meter.JPG)


(原文件名:mcu-based lcf meter 22 - lc meter.JPG)

the code is almost identical to the one I posted earlier, except that the one posted earlier had its key input function defeated in order to run permanently in the lc meter mode. take out "Key=NoKeyPressed;" in the main loop and you will be fine.

we will need to build a pcb at this point.

出0入0汤圆

 楼主| 发表于 2009-11-4 01:02:48 | 显示全部楼层
a potential extension of this project is to make it to use a graphics display - nokia 7110 lcd (sed1565 driver) is one possibility; another is to use different mcu. I have some lm3s628 / 801 chips and we can migrate to as well.

or pic18f chips.

出0入0汤圆

 楼主| 发表于 2009-11-4 04:25:19 | 显示全部楼层
here is an example of running the same lcd driver library on lpc210x.


(原文件名:lpc210x lcd driver.PNG)

出0入0汤圆

 楼主| 发表于 2009-11-4 06:58:41 | 显示全部楼层
I wrote this away ago. it is essentially a full graphics driver for nokia 7110 (can be easily adapted to other displays as well).


(原文件名:lpc210x glcd driver.PNG)

出0入0汤圆

 楼主| 发表于 2009-11-8 00:37:44 | 显示全部楼层
OK. a little bit of progress.

here is a picture of the lcd mounted on a protoboard. with power supply, V0 generator (to control lcd contrast), icsp interface, and the mcu flashed with the program.

点击此处打开 ourdev_501013.PNG(文件大小:622K,只有400K以内的图片才能直接显示) (原文件名:mcu-based LC meter - 23 - module front.PNG)

here is the icsp interface, connected to a pickit2:

点击此处打开 ourdev_501014.PNG(文件大小:778K,只有400K以内的图片才能直接显示) (原文件名:mcu-based LC meter - 23 - icsp.PNG)

this is the V0 generator (22k resistor + a 20k pot. the contrast is minimum when Vlcd=0.67v, and washed out when Vlcd=3.2v. so your resistor combination may be different for your lcd module).

点击此处打开 ourdev_501015.PNG(文件大小:746K,只有400K以内的图片才能直接显示) (原文件名:mcu-based LC meter - 23 - v0 gen.PNG)

here is the latest sim file.


(原文件名:mcu-based LC meter - 23 - sim.PNG)

here is the code base. as you can see, it is configured to use certain ports that worked for me. I also left the possibility to put in a third button (on RC2).

点击此处下载 ourdev_501017.rar(文件大小:35K) (原文件名:16F886 LCF Meter.rar)

To do:
1) put in buttons.
2) build the lc oscillator.
3) put in a voltage regulator.

出0入0汤圆

发表于 2009-11-8 09:29:34 | 显示全部楼层
相当的 支持

出0入0汤圆

发表于 2009-11-18 18:01:28 | 显示全部楼层
MARK, thanks!

出0入0汤圆

发表于 2009-12-1 18:57:09 | 显示全部楼层
mark

出0入0汤圆

发表于 2009-12-1 21:41:31 | 显示全部楼层
I have wholly read all text above——very nice work~!

MARK LCF-Meter

出0入0汤圆

发表于 2009-12-4 11:37:10 | 显示全部楼层
LC meter是什么东东?

出0入0汤圆

发表于 2010-1-30 21:49:07 | 显示全部楼层
帮顶!
谢谢分享!

出0入0汤圆

发表于 2010-6-8 15:40:33 | 显示全部楼层
PIC的 counter is 非同步的?   频率这么高

出0入0汤圆

发表于 2010-6-17 11:27:55 | 显示全部楼层
回复【79楼】longquan
-----------------------------------------------------------------------

楼主强!

出0入0汤圆

发表于 2011-4-20 19:33:04 | 显示全部楼层
MARK

出0入0汤圆

发表于 2011-4-30 15:36:33 | 显示全部楼层
pic LC meter 1602—— c、 mark

出0入0汤圆

发表于 2011-4-30 22:57:33 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-5-28 12:30:15 | 显示全部楼层
mark~

出0入0汤圆

发表于 2011-5-29 10:23:58 | 显示全部楼层
good

出0入0汤圆

发表于 2011-8-5 16:53:39 | 显示全部楼层
cool!thanks for sharing!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-8 03:06

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表