|
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 |
05-09-2008, 03:22 PM | #1 |
Đệ tử 1 túi
Tham gia ngày: Apr 2007
Bài gửi: 20
: |
hỏi về giao tiếp I2C
Chào các bạn!
Mình đang gặp khó khăn trong giao tiếp I2C. Bài toán của mình là giao tiếp giữa 2 con PIC 16F877A. Khi mình sử dụng thạch anh 4 Mhz thì nó chạy tốt. Nhưng khi thay con thạch anh 20 Mhz thì không còn chạy được nữa code master: Code:
#include <16F877A.H> #fuses XT,NOWDT,NOPROTECT,NOLVP #use Delay(Clock=20000000) #use i2c(master, sda=PIN_C4, scl=PIN_C3) void write_I2C(int8 value, int8 slave_addr) { i2c_start(); delay_ms(1); i2c_write(slave_addr); delay_ms(1); i2c_write(value); delay_ms(1); i2c_stop(); } int8 read_I2C(int8 slave_addr) { int8 value_re; i2c_start(); delay_ms(1); i2c_write(slave_addr + 1); delay_ms(1); value_re = i2c_read(0); delay_ms(1); i2c_stop(); return value_re; } void main() { int8 value_re=2; int8 i; const int8 N = 8; const int8 slave_addr = 0x10; set_tris_b(0x00); while(1){ write_I2C(1, slave_addr); delay_ms(500); value_re = read_I2C(slave_addr); output_b(value_re); delay_ms(500); write_I2C(0, slave_addr); delay_ms(500); value_re = read_I2C(slave_addr); output_b(value_re); delay_ms(500); } } code slave: Code:
#include <16F877A.H> #fuses XT,NOWDT,NOPROTECT,NOLVP #use delay(Clock=20000000) #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x10) int8 value = 0x01; #INT_SSP void i2c_isr() { int8 state; state = i2c_isr_state(); if(state < 0x80 && state>0) value = i2c_read(); if(state == 0x80){ i2c_write(value); } } void main() { enable_interrupts(INT_SSP); enable_interrupts(GLOBAL); set_tris_b(0x00); while(1){ output_b(value); } } thay đổi nội dung bởi: namqn, 05-09-2008 lúc 04:31 PM. |
|
|