Ðề tài: MTouch PCB
View Single Post
Old 10-02-2010, 10:24 AM   #14
rhinoceros
Đệ tử 1 túi
 
Tham gia ngày: Jul 2007
Bài gửi: 17
:
hic.giờ sử dụng M-touch cũng có nhiều cái hay.
không biết những dòng Pic có trang bị M-touch thì có đặc điểm gì để nhận hay không.

em cũng sưu tầm đc cái code này,mang cho mọi người tham khảo .

Trích:
Nguyên văn bởi Nguyen Thang View Post
#include <pic10f20x.h>
#define led GP1 //chan out, khi cham vao nut thi out=0 (led sang), nguoc lai khi khong cham, out=1 (led tat)
//Chuong trinh viet cho MCU PIC10F206

void delay_ms(unsigned int n)
{
unsigned int n1;
while(n!=0)
{
n1=120;
while(n1!=0)
{
n1--;
}
n--;
}
}
void delay_us(unsigned int n)
{
while(n!=0)
{
n--;
}
}

void ConfigIO()
{
//cau hinh cac chan la IO
TRISGPIO=0;
COUTEN=1;
CMPON=0;//comparation disable
CWU=1;

OPTION=0xc0;
FOSC4=0;//GP2/T0CKI/COUT applied to GP2
}





void main()
{

ConfigIO();

//CMCON0 Register:CMPOUT | COUTEN | POL | CMPT0CS | CMPON | CNREF | CPREF |CWU
/*
bit 7 CMPOUT: Comparator Output bit
1 = VIN+ > VIN-
0 = VIN+ < VINbit
bit 6 COUTEN: Comparator Output Enable bit(1, 2)
1 = Output of comparator is NOT placed on the COUT pin
0 = Output of comparator is placed in the COUT pin
bit 5 POL: Comparator Output Polarity bit(2)
1 = Output of comparator not inverted
0 = Output of comparator inverted
bit 4 CMPT0CS: Comparator TMR0 Clock Source bit(2)
1 = TMR0 clock source selected by T0CS control bit
0 = Comparator output used as TMR0 clock source
bit 3 CMPON: Comparator Enable bit
1 = Comparator is on
0 = Comparator is off
bit 2 CNREF: Comparator Negative Reference Select bit(2)
1 = CIN- pin(3)
0 = Internal voltage reference
bit 1 CPREF: Comparator Positive Reference Select bit(2)
1 = CIN+ pin(3)
0 = CIN- pin(3)
bit 0 CWU: Comparator Wake-up on Change Enable bit(2)
1 = Wake-up on comparator change is disabled
0 = Wake-up on comparator change is enabled.
*/
COUTEN=0;//Output of comparator is placed in the COUT pin
POL=0;//Output of comparator inverted
CMPT0CS=1;//Comparator output used as TMR0 clock source
CMPON=1;//Comparator is on
CNREF=0;//Internal voltage reference
CPREF=1;//CIN+ pin


//OPTION Register:GPWU | GPPU | T0CS | T0SE | PSA | PS2 | PS1 |PS0
/*
bit 7 GPWU: Enable Wake-up on Pin Change bit (GP0, GP1, GP3)
1 = Disabled
0 = Enabled
bit 6 GPPU: Enable Weak Pull-ups bit (GP0, GP1, GP3)
1 = Disabled
0 = Enabled
bit 5 T0CS: Timer0 Clock Source Select bit
1 = Transition on T0CKI pin (overrides TRIS on the T0CKI pin)
0 = Transition on internal instruction cycle clock, FOSC/4
bit 4 T0SE: Timer0 Source Edge Select bit
1 = Increment on high-to-low transition on the T0CKI pin
0 = Increment on low-to-high transition on the T0CKI pin
bit 3 PSA: Prescaler Assignment bit
1 = Prescaler assigned to the WDT
0 = Prescaler assigned to Timer0
bit 2-0 PS<2:0>: Prescaler Rate Select bits
*/
OPTION=0b11100000;
TRIS=0B00000000;
led=0;
delay_ms(100);
led=1;
delay_ms(100);
led=0;
delay_ms(100);
led=1;
delay_ms(100);
// while(1)
// {
// led=0;
// delay_ms(500);
// led=1;
// delay_ms(500);
//
// }

while(1)
{
TMR0=0;//reset timer 0
delay_us(100);//cho de counter
if(TMR0<120)//tang gia tri nay de tang do nhay(170 la gia tri khong cach dien)
{
led=0;//nhan
}
else
{
led=1;//khong nhan
}
delay_ms(1);

}

}
rhinoceros vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn