tomzbj 发表于 2013-3-27 21:14:55

分享~滑动触摸实验

本帖最后由 tomzbj 于 2013-3-27 23:01 编辑

原理见两个PDF,




效果见视频 http://v.youku.com/v_show/id_XNTMzNTkxMDQ0.html
程序如下,lcd驱动跟大家的没啥区别就不贴了#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "lcd_2wire.h"

#define set_load() do { PORTB |= _BV(0); } while(0)
#define set_sense(x) do { DDRC |= _BV(x); PORTC |= _BV(x); } while(0)
#define clr_load() do { PORTB &= ~_BV(0); } while(0)
#define clr_sense(x) do { DDRC |= _BV(x); PORTC &= ~_BV(x); } while(0)
#define in_sense(x) do { DDRC &= ~_BV(x); PORTC &= ~_BV(x);} while(0)
#define read_sense(x) (PINC & _BV(x))

void init_devices(void);
void timer0_init(void);
ISR( TIMER0_OVF_vect );
void port_init(void);
void init_devices(void);

int main(void) {

    char    str[] = "insert your functional code here";
    init_devices();
    lcd_puts( str );

    while(1) {
      int i, channel = 5;
      uint16_t    vi_base[] = { 112, 84, 84, 84, 98, 112 }; // 实测基数
      uint16_t    vih_start, vih_stop, vil_start, vil_stop, vi_meas;

      for( channel = 0; channel <= 5; channel++ ) {
            vi_meas = 0;
            for( i = 0; i < 8; i++ ) {
                set_load();
                clr_sense(channel);
                vih_start = TCNT0;
                in_sense(channel);
                while( !read_sense(channel) ); // 等待sense(channel)达到高电平
                vih_stop = TCNT0;
                set_sense(channel);
                clr_load();
                vil_start = TCNT0;
                in_sense(channel);
                while( read_sense(channel) ); // 等待sense(channel)达到低电平
                vil_stop = TCNT0;
                vi_meas += ( vih_stop - vih_start + 256 ) % 256;
                vi_meas += ( vil_stop - vil_start + 256 ) % 256;
            }
            vi_meas /= 8;
            vi_meas -= vi_base;
      }
      {
            static int currpos, lastpos, lastlastpos;
            static uint16_t kk = 100;
            char buf;
            for( channel = 0; channel <= 5; channel++ ) {
                if( vi_meas > 32 ) { // 触摸判定,32是灵敏度
                  if( currpos != channel ) {
                        lastlastpos = lastpos;
                        lastpos = currpos;
                        currpos = channel;

                        // 判断滑动算法,有待进一步优化
                        if( ( (currpos == lastpos + 1 ) || (currpos == lastpos + 2 ) )
                              && ( ( lastpos == lastlastpos + 1 ) || (lastpos == lastlastpos + 2 ) ) )
                            kk++;
                        else if( ( (currpos == lastpos - 1 ) || (currpos == lastpos - 2 ) )
                              && ( ( lastpos == lastlastpos - 1 ) || (lastpos == lastlastpos - 2 ) ) )
                            kk--;
                  }
                  break;
                }
            }
            sprintf( buf, "%d, %d, %d, %d", currpos, lastpos, lastlastpos, kk );
            lcd_clear();
            lcd_puts( buf );
      }
      
      _delay_ms(30);
    }
}

void port_init(void) {
    DDRB= 0xff; PORTB = 0xff;
    DDRC= 0xff; PORTC = 0x00;
    DDRD= 0xff; PORTD = 0x00;
}

void timer0_init(void) {

    TCCR0B = 0x00; //stop
    TCNT0 = 0x0; //set count
    TCCR0A = 0x00;
    TCCR0B = 0x01; //start timer
    TIMSK0 = 0x0;
}


//call this routine to initialize all peripherals
void init_devices(void) {
    cli(); //stop errant interrupts until set up
    port_init();
    timer0_init();
    lcd_init();
    sei(); //all peripherals are now initialized
}

binaimei2007 发表于 2013-3-27 21:37:00

顶楼主啊

worldly_guest 发表于 2013-3-27 22:35:20

顶!{:victory:}{:victory:}

185724132 发表于 2013-3-27 22:35:59

本帖最后由 185724132 于 2013-3-27 22:37 编辑

什么意思?没明白。不好意思网速慢,没看见视频,嘿嘿,我下回注意,这回知道了

automaticdai 发表于 2013-3-27 22:55:32

没看懂{:smile:}

aaronhuang 发表于 2013-6-21 13:04:20

楼主的滑动触摸能不能从左滑到右:从0滑到最大值,
                  从右滑到左:从最大值滑到0。
页: [1]
查看完整版本: 分享~滑动触摸实验