![]() |
|
Tài trợ cho PIC Vietnam |
Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC |
|
Ðiều Chỉnh | Xếp Bài |
![]() |
#1 |
Đệ tử 1 túi
Tham gia ngày: Oct 2010
Bài gửi: 22
: |
Pic18f giao tiếp màn hình cảm ứng
Mình dùng PIC8F4620 giao tiếp IC ADS7843 (chíp điều khiển miếng cảm ứng) con này giao tiếp qua chuẩn SPI. Đây là code mình viết ko biết sai chổ nào mà no ko chạy. mong các bạn chỉ giúp.
#include <18f4620.h> #FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz) #FUSES NOPROTECT //Code not protected from reading #FUSES NOBROWNOUT //Reset when brownout detected #FUSES NOPUT //No Power Up Timer #FUSES NODEBUG //No Debug mode for ICD #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #use delay(clock=20000000) #define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H) //////////////////////////////////////////////////////////////////////////////// //////////////dinh nghia PIN///////////////////// #define ADS7843_CS PIN_C1 #define ADS7843_BUSY PIN_C2 #define ADS7843_PENIRQ PIN_B0 //////////////////////////////////////////////////////////////////////////////// //////////////Cchuong trinh con///////////////////// int16 ads7843_read(mode) { int8 lsb, msb; int16 retval; output_low(ADS7843_CS); //cho phep chon chip spi_write(mode); while (input (ADS7843_BUSY)== 1){;} msb = spi_read(0); lsb = spi_read(0); output_high(ADS7843_CS); //khong phep chon chip retval = make16(msb, lsb); // tra ve gia tri 16bit ket hop tu 2 bien (msb va lsb) msb la byte cao retval >>= 4; return(retval); //tra ve gia tri la: [x][x][x][x][x][x][x][x][x][x][x][x][] [] [] [] } /////////////////////////////////////////////////////////////////////////////// void main() { output_high(ADS7843_CS); // Inactive state (disenable) setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4); //khoi tao chuan SPI int16 x,y; output_low(ADS7843_CS); // Inactive state (enable) delay_ms(5); while (TRUE) { while( input(ADS7843_PENIRQ) ==0 ) // is screen touch { x = ads7843_read (0x98); delay_us (2); y = ads7843_read (0xD8); delay_ms (1); if ((x<50)&& (y <50)) //neu vung duoc bam co gtri x,y <50 thi bat den chi thi RB7 { output_bit( PIN_B7, 1); delay_ms (100); output_bit( PIN_B7, 0); delay_ms (50); } } } } |
![]() |
![]() |
|
|