英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 3903|回复: 0
打印 上一主题 下一主题

picoDetector : How to detect metal with a PIC

[复制链接]
跳转到指定楼层
1#
发表于 2012-8-21 15:45:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Includes :
mikroC PRO project files for PIC12F683, should work also with most of PIC
picoDetector C source code
picoDetector .HEX files
picoDetector circuit schematic
/*

***************************************************************
****************
* picoDetector : an ultra simple and cheap metal detector

***************************************************************
****************
*
*
*
*
* source code for mikroC PRO compiler V1.65
* feel free to use this code at your own risks
*
* target : PIC12, oscillator in HS mode, watchdog enabled
*
* PIC PIN Assignemnt :
*
* GP0 : detect LED indicator
* GP1 : calibrate LED indicator
* GP2 : NC
* GP3 : NC
* GP4, GP5 : inductor
*

***************************************************************
****************
*/
#define MAXTRY 15       // number of watchdog restart to calibrate loop counter
unsigned char   ctr ;           // number of loops between two watchdog resets
unsigned char   previous ;      // previous value of ctr
unsigned char   calibr ;        // calibration value when oscillator runs free
unsigned char   restarts ;      // number of watchdog restarts
unsigned char   en ;            // enable flag, allows detection
/*
* main loop
*/
void    main()
        {
        unsigned char   i ;
        
        /*
         * configure GPIO as digital port
         */
        CMCON0 = 7 ;
        ANSEL = 0 ;
        TRISIO = 0 ;
        GPIO = 0 ;
        /*
         * power up ?
         */
        if(STATUS.NOT_TO)
                {
                /*
                 * yes, init variables
                 */
                restarts = 0 ;
                calibr = 1 ;
                }
        /*
         * watchdog reset counter
         */
        if(restarts < 255) restarts++ ;
        /*
         * if counter differs too much from calibration value
         */
        if((previous ^ ctr) > calibr)
                {
                /*
                 * turn detect LED on
                 */
                GPIO.F0 = en ;
               
                /*
                 * if not on power up
                 */
                if(STATUS.NOT_TO == 0)
                        {
                        /*
                         * while in calibration mode
                         */
                        if(restarts < MAXTRY)
                                {
                                /*
                                 * shift calibration value
                                 * and wait a little bit
                                 */
                                calibr <<= 1 ;
                                Delay_ms(5) ;
                                }
                        }
                else
                        {
                        /*
                         * turn detect LED off
                         */
                        GPIO.F0 = 0 ;
                        }
                }
        /*
         * save last counter
         */
        previous = ctr ;
        /*
         * is calibration over ?
         */
        if(restarts > MAXTRY)
                {
                /*
                 * yes, turn calibrate LED off
                 * and set enable flag
                 */
                GPIO.F1 = 0 ;
                en = 1 ;
                }
        else
                {
                /*
                 * no, turn calibrate LED on
                 * and clear enable flag
                 */
                GPIO.F1 = 1 ;
                en = 0 ;
                }
        /*
         * set watchdog prescaler
         */
        OPTION_REG = 0b11111001 ;
        
        /*
         * start counter, to be interrupted by watchdog
         */
        ctr = 0 ;
        for(;;)
               {
               ctr++ ;
               }
        }


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-5-1 04:04 , Processed in 0.050497 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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