![]() |
|
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ử 5 túi
Tham gia ngày: Sep 2010
Bài gửi: 107
: |
Mô phỏng chạy nhưng ra phần cứng chưa chắc chạy đâu bạn. Thân
|
![]() |
![]() |
![]() |
#2 |
Nhập môn đệ tử
Tham gia ngày: Sep 2011
Bài gửi: 5
: |
đã viết code nhận và gửi 2 byte cho từ 1 master và 2 slave chạy good
|
![]() |
![]() |
![]() |
#3 |
Nhập môn đệ tử
Tham gia ngày: Sep 2011
Bài gửi: 5
: |
Các bạn lưu ý khi dùng Pic 18F4431 và trình dịch CCS thì khi giao tiếp I2C mặc dù trên code viết cho cả master và slaver dùng chân C4 và C5 nhưng khi ráp mạch thực tế thì trên master chúng ta nối chân C4,C5 và slaver là D2 và D3 thì mới chạy được nhé. Cái này không biết lỗi gì chắc là do CCS
|
![]() |
![]() |
![]() |
#4 |
Nhập môn đệ tử
Tham gia ngày: Sep 2011
Bài gửi: 5
: |
Code hoàn chỉnh đã test mạch thực tế giao tiếp giữa 1 master và 2 slaver, truyền và nhận 2 byte luôn, các bạn có thể tham khảo. Master sẽ gửi 1 số int16 (gừi từ 0, mỗi lần gửi xong tăng lên 1 đơn vị) xuống slave, slaver nhận được và gửi trở lại master. các bạn nhớ dùng Rs232 dọc về máy tính dể kiểm tra chính xác nhé
Code master: #include <18f4431.h> #include <def_4331.h> #device 18f4431*=16 ADC=10 #include <stdlib.h> //tien xu li khi su dung ham ATOI() #include <stdio.h> // for printf() call #fuses nowdt,noprotect,nolvp,xt,put,hs #use delay(clock=20000000) #use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,b its=8) #define slave_address_1 0x10 #define slave_address_2 0x20 #use i2c(master, sda=PIN_C4, scl=PIN_C5) #use fast_io(b) #use fast_io(c) #use fast_io(d) #use fast_io(a) void write_slave_1(int16 a) { int8 hi,lo; hi=make8(a,1); lo=make8(a,0); i2c_start(); i2c_write(slave_address_1); delay_ms(1); i2c_write(hi); delay_ms(1); i2c_write(lo); i2c_stop(); } void write_slave_2(int16 b) { int8 hi,lo; hi=make8(b,1); lo=make8(b,0); i2c_start(); i2c_write(slave_address_2); delay_ms(1); i2c_write(hi); delay_ms(1); i2c_write(lo); i2c_stop(); } int16 read_slave_1() { int16 value_re_1; int8 hi,lo; i2c_start(); i2c_write(slave_address_1 + 1); hi = i2c_read(1); lo = i2c_read(0); i2c_stop(); value_re_1 = make16(hi,lo); return value_re_1; } int16 read_slave_2() { int16 value_re_2; int8 hi,lo; i2c_start(); i2c_write(slave_address_2 + 1); hi = i2c_read(1); lo = i2c_read(0); i2c_stop(); value_re_2 = make16(hi,lo); return value_re_2; } void main() { int16 value,value_re_1,value_re_2; value = 0; while(1) { write_slave_1(value); write_slave_2(-value); value++; delay_ms(500); value_re_1=read_slave_1(); value_re_2=read_slave_2(); printf("%ld",value_re_1); printf(" "); printf("%ld",value_re_2); printf(" "); delay_ms(100); } } Code slaver 1: #include <18f4431.h> #include <def_4331.h> #device 18f4431*=16 ADC=10 #include <stdlib.h> //tien xu li khi su dung ham ATOI() #include <stdio.h> // for printf() call #fuses nowdt,noprotect,nolvp,xt,put,hs #use delay(clock=20000000) #use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,b its=8) #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0x10) #use fast_io(b) #use fast_io(c) #use fast_io(d) #use fast_io(a) boolean a; int16 value; int8 hi,lo; #INT_SSP void i2c_isr() { int8 state; state = i2c_isr_state(); if (state<= 0x80) { if(state == 1 ) { hi = i2c_read(1); } if(state == 2) { lo = i2c_read(1); a=true; } } if (state >= 0x80) { if(state==0x80) i2c_write(hi); if(state==0x81) i2c_write(lo); } } void main() { enable_interrupts(GLOBAL); enable_interrupts(INT_SSP); a=false; while(1) { if (a==true) {value=make16(hi,lo); printf("%ld",value); printf(" "); a=false; } } } Code slave 2: #include <18f4431.h> #include <def_4331.h> #device 18f4431*=16 ADC=10 #include <stdlib.h> //tien xu li khi su dung ham ATOI() #include <stdio.h> // for printf() call #fuses nowdt,noprotect,nolvp,xt,put,hs #use delay(clock=20000000) #use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,b its=8) #use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0x20) #use fast_io(b) #use fast_io(c) #use fast_io(d) #use fast_io(a) boolean a; int16 value; int8 hi,lo; #INT_SSP void i2c_isr() { int8 state; state = i2c_isr_state(); if (state<= 0x80) { if(state == 1 ) { hi = i2c_read(1); } if(state == 2) { lo = i2c_read(1); a=true; } } if (state >= 0x80) { if(state==0x80) i2c_write(hi); if(state==0x81) i2c_write(lo); } } void main() { enable_interrupts(GLOBAL); enable_interrupts(INT_SSP); a=false; while(1) { if (a==true) {value=make16(hi,lo); printf("%ld",value); printf(" "); a=false; } } } CHÚC CÁC BẠN MAY MẮN |
![]() |
![]() |
![]() |
#5 |
Nhập môn đệ tử
Tham gia ngày: May 2013
Bài gửi: 6
: |
cac bac cho em hoi la e dung con 16f685 hok co ngat SSP cho giao tiep I2C.e lai dang lam cai gaio tiep I2C giua 16f685 va PCF8583.. bac nao biet chi giao em vs. thanhks all
|
![]() |
![]() |
![]() |
#6 |
Nhập môn đệ tử
Tham gia ngày: Dec 2013
Bài gửi: 1
: |
Pic18f4520 giao tiếp Ds1307
Anh Chị thân!
Hiện tại em đang viết chương trình giao tiếp I2C giữa Pic18f4550 và DS1307 đã chạy trên proteus nhưng ko hiểu tại sao khi chuyển sang con pic18f4520 giao tiếp với ds1307 thì lại ko chạy mà theo như em tìm hiểu thì cấu trúc thanh ghi về I2C của 2 con này ko khác nhau ko hiểu vì lý do j mà lại ko chạy.Anh Chị nào đã từng viết cho pic18f4520 có thể chỉ giúp em với đc ko ạ?còn có 1 tuần nữa nộp đồ án rùi mà mô phỏng chưa chạy thì mạch ko biết thế nào đây còn dưới đây là chương trình của em Anh chị nào giúp em với,Help Me! em cảm ơn! #include "p18f4520.h" #include "stdio.h" #include "i2c.h" #include "delays.h" #pragma config OSC = HS #pragma config IESO = OFF #pragma config PWRT = OFF #pragma config WDT = OFF #pragma config WDTPS = 32768 #pragma config MCLRE = ON #pragma config LVP = OFF #pragma config DEBUG = ON #define l1 PORTDbits.RD0 #define l2 PORTDbits.RD1 #define l3 PORTDbits.RD2 #define l4 PORTDbits.RD3 #define l5 PORTDbits.RD4 #define l6 PORTDbits.RD5 unsigned char M[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0}; char m[32]; unsigned char s,giay,phut,gio; unsigned char x,a; void ghids(unsigned char diachi,unsigned char dulieu) { unsigned char b; b=dulieu;//((((dulieu/10)<<4)&0xf0)+((dulieu%10)&0x0f)); StartI2C(); IdleI2C(); WriteI2C(0xd0); IdleI2C(); WriteI2C(diachi); IdleI2C(); WriteI2C(b); IdleI2C(); StopI2C(); } void docds1307(unsigned char slave1,unsigned char diachi) { unsigned char x; StartI2C(); IdleI2C(); WriteI2C(slave1); IdleI2C(); WriteI2C(diachi); IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(slave1+1); IdleI2C(); x=ReadI2C(); a=(x>>4)*10+(x&0x0f); NotAckI2C(); IdleI2C(); StopI2C(); return x; } void hienthi(void) { docds1307(0xd0,0x00);giay=x;Delay1KTCYx(10); l1=1;PORTB=M[(giay/10)];Delay1KTCYx(10);l1=0; l2=1;PORTB=M[(giay%10)];Delay1KTCYx(10);l2=0; docds1307(0xd0,1);phut=a;Delay1KTCYx(10); l3=1;PORTB=M[(phut/10)];Delay1KTCYx(10);l3=0; l4=1;PORTB=M[(phut%10)];Delay1KTCYx(10);l4=0; docds1307(0xd0,2);gio=a;Delay1KTCYx(10); l5=1;PORTB=M[(gio/10)];Delay1KTCYx(10);l5=0; l6=1;PORTB=M[(gio%10)];Delay1KTCYx(10);l6=0; } void main() { TRISB=0X00; TRISC=0b11110111; TRISD=0x00; ADCON1=0x0f; Delay1KTCYx(10); OpenI2C(MASTER,SLEW_OFF); SSPADD=0x63;//49; //xung nhip=Fosc/(4*(SSPADD=49+1); Delay1KTCYx(10); //ghids1307(0x19,0x19,0x57); gio=0; giay=0; phut=0; ghids(0x00,0x14); SSPCON1bits.SSPM3=1; SSPCON1bits.SSPM2=0; SSPCON1bits.SSPM1=0; SSPCON1bits.SSPM0=0; SSPCON2bits.RCEN=1; Delay1KTCYx(10); while(1) { hienthi(); } } |
![]() |
![]() |
![]() |
#7 |
Nhập môn đệ tử
Tham gia ngày: Aug 2016
Bài gửi: 3
: |
Cảm anh đã chia sẻ bài viết!!!
__________________
Cung cấp biến tần mitsubishi| plc mitsubishi|servo mitsubishi | mccb mitsubishi chính hãng giá cạnh tranh. |
![]() |
![]() |
![]() |
#8 |
Đệ tử 2 túi
|
@pump_upp - best crypto pumps on telegram !
https://t.me/pump_upp - best crypto pumps on telegram
Make 1000% and more within 1 day, join channel @pump_upp !
__________________
Pretty Girls in your city |
![]() |
![]() |
![]() |
|
|