Đăng kí nhận bài viết qua Mail

Công nghệ mTouch

Sản phẩm demo công nghệ mTouch của công ty R&P được giới thiệu tại picvietnam và dientuvietnam ở các luồng sau:
http://www.picvietnam.com/forum/showthread.php?t=3187
http://www.dientuvietnam.net/forums/showthread.php?p=149856
Đây là source code và báo cáo về sản phẩm này.
http://falleaf.net/download/mTouch/REP01.02.MTOUCH.NGMT.041208.SRC.zip




source file
main.c
#include
#include
#include "touch.h"
#include "lcd.h"


__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);

extern volatile unsigned char touch_pressed;
   
void main()
{
    RE1 = 0;
    TRISE1 = 0;
    ANS6 = 0;
   
    lcd_init();
    touch_init();
   
    lcd_puts("\fLab - 8\n-----mTouch-----");
   
    OPTION = 0b10000100;        // timer 0 prescaler = 1:256
    T0IF = 0;
    T0IE = 1;
    GIE = 1;
       
    while(1){
       
        if(touch_pressed){
            RE1 = 1;
        }else{
            RE1 = 0;
        }
    }
}
void putch(char c)
{
    lcd_putc(c);
}
void interrupt isr()
{
    if(T0IF && T0IE){
        touch_service();
        TMR0 = 0;
        T0IF = 0;
    }
}
lcd.c

#include
#include
#include "lcd.h"

void lcd_init(){
    unsigned char i;
    LCD_EN_TRIS = 0;
    LCD_RS_TRIS = 0;
    LCD_RW_TRIS = 0;
    LCD_DATA4_TRIS = 0;
    LCD_DATA5_TRIS = 0;
    LCD_DATA6_TRIS = 0;
    LCD_DATA7_TRIS = 0;
    LCD_EN = 0;
    LCD_RS = 0;
    LCD_RW = 0;

    __delay_ms(100);                // delay for power on
   
    // reset LCD
    lcd_put_byte(0,0x30);
    __delay_ms(50);
    lcd_put_byte(0,0x30);
    __delay_ms(50);
    lcd_put_byte(0,0x32);
    __delay_ms(100);                // delay for LCD reset
    __delay_ms(100);                // delay for LCD reset
    __delay_ms(100);                // delay for LCD reset
   
    while(lcd_busy());
    lcd_put_byte(0,FOUR_BIT & LINES_5X7);            // Set LCD type
    while(lcd_busy());
   
    lcd_put_byte(0,DOFF&CURSOR_OFF&BLINK_OFF);        // display off
    while(lcd_busy());
    lcd_put_byte(0,DON&CURSOR_OFF&BLINK_OFF);        // display on
    while(lcd_busy());
   
    lcd_put_byte(0,0x01);                            // clear display and move cursor to home
    while(lcd_busy());
    lcd_put_byte(0,SHIFT_CUR_LEFT);                // cursor shift mode
    while(lcd_busy());
    lcd_put_byte(0,0x01);                            // clear display and move cursor to home
    while(lcd_busy());
}
unsigned char lcd_busy()
{
    unsigned char busy;
   
    LCD_DATA4_TRIS = 1;
    LCD_DATA5_TRIS = 1;
    LCD_DATA6_TRIS = 1;
    LCD_DATA7_TRIS = 1;
   
    LCD_RW = 1;
    LCD_RS = 0;
    __delay_us(20);
    LCD_EN = 1;
    __delay_us(20);
   
    busy = LCD_DATA7;
   
    LCD_EN = 0;
    __delay_us(20);
    LCD_EN = 1;
    __delay_us(20);
    LCD_EN = 0;

   
    return busy;
}
unsigned char lcd_get_byte(unsigned char rs)
{
    BYTE_VAL b;
   
    LCD_DATA4_TRIS = 1;
    LCD_DATA5_TRIS = 1;
    LCD_DATA6_TRIS = 1;
    LCD_DATA7_TRIS = 1;
   
    LCD_RW = 1;
    LCD_RS = 0;
    if(rs) LCD_RS = 1;
    __delay_us(20);
    LCD_EN = 1;
    __delay_us(20);
   
    b.bits.b7 = LCD_DATA7;
    b.bits.b6 = LCD_DATA6;
    b.bits.b5 = LCD_DATA5;
    b.bits.b4 = LCD_DATA4;
   
    LCD_EN = 0;
    __delay_us(20);
    LCD_EN = 1;
    __delay_us(20);
    b.bits.b3 = LCD_DATA7;
    b.bits.b2 = LCD_DATA6;
    b.bits.b1 = LCD_DATA5;
    b.bits.b0 = LCD_DATA4;
    LCD_EN = 0;
   
    return b.Val;
}
void lcd_put_byte(unsigned char rs, unsigned char b)
{
    BYTE_VAL temp;
   
    LCD_DATA4_TRIS = 0;
    LCD_DATA5_TRIS = 0;
    LCD_DATA6_TRIS = 0;
    LCD_DATA7_TRIS = 0;
   
    LCD_RS = 0;
    if(rs) LCD_RS = 1;
   
    __delay_us(20);
    LCD_RW = 0;
    __delay_us(20);
    LCD_EN = 0;
   
    temp.Val = b;
   
    // send the high nibble
    LCD_DATA4 = temp.bits.b4;
    LCD_DATA5 = temp.bits.b5;
    LCD_DATA6 = temp.bits.b6;
    LCD_DATA7 = temp.bits.b7;
    __delay_us(20);
    LCD_EN =  1;
    __delay_us(20);
    LCD_EN = 0;
    // send the low nibble
    LCD_DATA4 = temp.bits.b0;
    LCD_DATA5 = temp.bits.b1;
    LCD_DATA6 = temp.bits.b2;
    LCD_DATA7 = temp.bits.b3;
    __delay_us(20);
    LCD_EN =  1;
    __delay_us(20);
    LCD_EN = 0;
}
void lcd_putc(char c){
    switch(c){
        case '\f':
            lcd_put_byte(0, 1);
            while(lcd_busy());
            break;
        case '\n':
            lcd_gotoxy(0, 1);
            break;
        default:
            if(isprint(c)){
                lcd_put_byte(1, c);
                while(lcd_busy());
            }
            break;
    }
}

void lcd_gotoxy(unsigned char col, unsigned char row)
{
    unsigned char address;
   
    if(row!=0)
        address=0x40;
    else
        address=0;
   
    address += col;
    lcd_put_byte(0,0x80|address);
    while(lcd_busy());
}
void lcd_puts(const char* s)
{
    while(*s){
        lcd_putc(*s++);
    }
}
touch.c
#include
#include
#include "touch.h"
volatile unsigned char touch_pressed=0;

void touch_init(void)
{
    TRISA0 = 1;        // C12IN0-
    TRISA2 = 1;        // C2IN+
    ANS2 = 1;        //
    ANS0 = 1;
   
    TRISA5 = 0;        // C2OUT
    ANS5 = 0;
   
    TRISC0 = 1;        // T1CKI
   
    // comparator 1
    C1OE = 1;
    C1POL = 1;
    C1R = 1;
    C1ON = 1;
    // comparator 2
    C2OE = 1;
    C2ON = 1;
   
    CM2CON1 = 0b00100000;
   
    // VREF = 2/3 Vdd
    //VRCON = 0b10001111;
    VRCON = 0b10001111;
    // SR latch control
    SRCON = 0xF0;
    // timer 1
    TMR1L = 0;
    TMR1H = 0;
    T1CON = 0b00000111;
}
void touch_service()
{
    static unsigned char calibrate = 64;
    static long average=0;
    long raw, percent;
   
    TMR1ON = 0;            // dung timer 1
    // 1: Doc gia tri timer 1
    raw = ((unsigned int)TMR1H<<8) + TMR1L;
   
    // 2: khi moi khoi dong can tinh toan gia tri Average ban dau de lam moc so sanh
    if(calibrate){
        average = (average * 15 + raw)/16;
        calibrate--;
        TMR1L = 0;
        TMR1H = 0;
        TMR1ON = 1;
        return;
    }
   
    if(average==0){
        TMR1L = 0;
        TMR1H = 0;
        TMR1ON = 1;
        return;
    }
   
    // 3: tinh toan % thay doi cua gia tri raw so voi gia tri trung binh Average
    percent = average - raw;
    if(percent<0)
        percent = 0;
    else{
        percent *= 1000;
        percent /= average;
    }
   
    // 4: kiem tra neu su thay doi vuot qua nguong dinh san
    if(percent > PERCENT_ON){            // pressed
        touch_pressed = 1;
       
    }else{
        touch_pressed = 0;
        average = (average * 15 + raw)/16;
        if(raw > average)
            average = (average * 15 + raw)/16;
    }
   
   
    // 5: reset timer1, dem lai tu dau
    TMR1L = 0;
    TMR1H = 0;
    TMR1ON = 1;
}
Header file
lcd.h
#ifndef _LCD_H_
#define _LCD_H_

#include

#ifndef _XTAL_FREQ
#define _XTAL_FREQ    4000000
#endif


/* Display ON/OFF Control defines */
#define DON         0b00001111  /* Display on      */
#define DOFF        0b00001011  /* Display off     */
#define CURSOR_ON   0b00001111  /* Cursor on       */
#define CURSOR_OFF  0b00001101  /* Cursor off      */
#define BLINK_ON    0b00001111  /* Cursor Blink    */
#define BLINK_OFF   0b00001110  /* Cursor No Blink */

/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
#define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
#define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
#define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */

/* Function Set defines */
#define FOUR_BIT   0b00101100  /* 4-bit Interface               */
#define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
#define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
#define LINE_5X10  0b00110100  /* 5x10 characters               */
#define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */


/* Pins mapping */
#ifndef        LCD_RS
#define    LCD_RS        RD1
#define    LCD_EN        RD3
#define    LCD_RW        RD2
#define    LCD_DATA4    RD4
#define    LCD_DATA5    RD5
#define    LCD_DATA6    RD6
#define    LCD_DATA7    RD7

#define    LCD_RS_TRIS        TRISD1
#define    LCD_EN_TRIS        TRISD3
#define    LCD_RW_TRIS        TRISD2
#define    LCD_DATA4_TRIS    TRISD4
#define    LCD_DATA5_TRIS    TRISD5
#define    LCD_DATA6_TRIS    TRISD6
#define    LCD_DATA7_TRIS    TRISD7
#endif

//typedef unsigned char        unsigned char;                // 8-bit unsigned

typedef union _BYTE_VAL
{
    unsigned char Val;
    struct
    {
        unsigned char b0:1;
        unsigned char b1:1;
        unsigned char b2:1;
        unsigned char b3:1;
        unsigned char b4:1;
        unsigned char b5:1;
        unsigned char b6:1;
        unsigned char b7:1;
    } bits;
} BYTE_VAL;

void lcd_init();
unsigned char lcd_busy();
unsigned char lcd_get_byte(unsigned char rs);
void lcd_put_byte(unsigned char a,unsigned char b);
void lcd_gotoxy(unsigned char col, unsigned char row);
void lcd_putc(char c);
void lcd_puts(const char* s);
#endif
touch.h
#ifndef _TOUCH_H_
#define _TOUCH_H_

#define PERCENT_ON        35

void touch_init(void);
void touch_service();

#endif
 code main.c(bật tắt)
#include
#include
#include "touch.h"
#include "lcd.h"


__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);

extern volatile unsigned char touch_pressed;
void delay_ms(int ms);   
void main()
{
    RE1 = 0;
    TRISE1 = 0;
    ANS6 = 0;
   
    lcd_init();
    touch_init();
   
    lcd_puts("\fLab - 8\n-----mTouch-----");
   
    OPTION = 0b10000100;        // timer 0 prescaler = 1:256
    T0IF = 0;
    T0IE = 1;
    GIE = 1;
       
    while(1){
       
        if(touch_pressed){
            RE1 = 1;    
                while(touch_pressed){                    // cho toi khi phim bam duoc nha
                __delay_ms(1);
            }
        }
        if(touch_pressed){
            RE1 = 0;    
                while(touch_pressed){                    // cho toi khi phim bam duoc nha
                __delay_ms(1);
            }
       
        }
    }
}
void putch(char c)
{
    lcd_putc(c);
}
void interrupt isr()
{
    if(T0IF && T0IE){
        touch_service();
        TMR0 = 0;
        T0IF = 0;
    }
}
void delay_ms(int ms)
{
    int i;
    for(i=0; i
        _delay(1000);//1ms   
    }
}

Post a Comment
Related Posts Plugin for WordPress, Blogger...

Cùng tôi học Điện Tử ..(http://ilovec.tk)..