|
本帖最后由 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[6];
- for( channel = 0; channel <= 5; channel++ ) {
- vi_meas[channel] = 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[channel] += ( vih_stop - vih_start + 256 ) % 256;
- vi_meas[channel] += ( vil_stop - vil_start + 256 ) % 256;
- }
- vi_meas[channel] /= 8;
- vi_meas[channel] -= vi_base[channel];
- }
- {
- static int currpos, lastpos, lastlastpos;
- static uint16_t kk = 100;
- char buf[34];
- for( channel = 0; channel <= 5; channel++ ) {
- if( vi_meas[channel] > 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
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|