Chạy rồi, em cảm ơn a nhiều nha
Nguyên văn bởi queduong
Xem bài viết
#include <16F877A.h> #device adc=8 #FUSES NOWDT, XT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, WRT_50% #use delay(clock=4000000) #define RX_ID 0x30 // Receiver ID #define RX PIN_C7 // Chan nhan xung ma hoa #define Led PIN_D1 // power led unsigned char RX_packet[3],x; int1 rx_bit; // rx_bit range value : 0 or 1 unsigned int checksum; int1 Receiver(unsigned char *buff); void main() { setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_comparator(NC_NC_NC_NC); set_tris_b(0x00); // Port b all out put output_b(0x00); // port b all low set_tris_c(0xFF); // port C all input ( RC7 input) set_tris_d(0xFD); output_high(Led); while(TRUE) { checksum=0; while(Receiver(RX_packet)); for(x=0;x<2;x++) checksum+= RX_packet[x]; checksum&= 0x0FF; if(checksum==RX_packet[2]) // Kiem tra tong cua goi du lieu (packet) { if(RX_packet[0]==RX_ID) // Kiem tra ID { output_b(RX_packet[1]); // Xuat ra port b } } } // End while(1) } //////////////////////////// Functions //////////////////////////// int1 Receiver(unsigned char *buff) { unsigned char i,j,T0,count; i=32; count=0; setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2); // setup timer 0 div 2 while(--i) // wait preamble { #asm NOP; NOP; NOP; #endasm while(input(RX)); set_timer0(0); // reset timer0 = 0 #asm NOP; NOP; NOP; #endasm while(!input(RX)); T0=get_timer0(); // doc timer 0 if (T0==0xFF) {return 1;} // error if ((T0>=198) && (T0<=218)) // check pulse 396us --> 436us (OK ) { count++; if ((count>=1)|| (i==0)) break; } } if ((count==0)&&(i==0)) {return 1;} // error setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16); // setup time0 div 16 T0=0; while (!((T0>=94) && (T0<=114))) // 1504 - 1824 uS it OK. syn bit for 4Te or 1664 uS { #asm NOP; NOP; NOP; #endasm while(input(RX)); // wait syn set_timer0(0); // reset timer0 = 0 #asm NOP; NOP; NOP; #endasm while(!input(RX)); T0=get_timer0(); // doc timer 0 } while(input(RX)); //wait start bit go low delay_us(624); // delay before sampling (1.5Te) for (j=0;j<3;j++) // build packet bytes ( 3 bytes) { i=8; // build paket bit (8 bits ) buff[j]=0; while(i--) { buff[j]=buff[j]<<1; rx_bit=input(RX); // check logic (0,1) chan RX buff[j]=buff[j] | rx_bit; if (rx_bit==0) while(!input(RX)); else while(input(RX)); delay_us(624); // delay for 1,5Te } } if (rx_bit==0) if (!input(RX)) return 1; // error (no stop bit found) else if (!input(RX)) return 1; // error (no stop bit found) return 0; }
#include <16F877A.h> #device adc=8 #FUSES NOWDT, XT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, WRT_50% #use delay(clock=4000000) #define TX_ID 0x30 // Transmitter ID #define TX PIN_C6 // Chan phat xung ma hoa #define Button PIN_D0 // nut bam #define Led PIN_D1 // Power led unsigned char TX_packet[3]; unsigned char i,j; unsigned int checksum; void Transmitter(); void main() { setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_comparator(NC_NC_NC_NC); set_tris_c(0b10111111); output_c(0x00); set_tris_a(0x00); output_a(0x00); set_tris_d(0xFD); // rd1 output output_high(Led); while(TRUE) { if(!input(Button)) { do { // Dong goi du lieu // ( data packet ) checksum=0; TX_packet[0]=TX_ID; // Transmitter ID checksum+= TX_packet[0]; TX_packet[1]=input_b(); // doc trang thai port b checksum+= TX_packet[1]; TX_packet[2]=checksum; // goi du lieu ( Tong ( Sum ) ) // gui du lieu qua chan TX Transmitter(); } while(!input(Button)); delay_ms(200); // Chong rung phim } } } ///////////////// functions //////////////////////// void Transmitter() { for (i=0;i<32;i++) // send preamble { output_high(TX); delay_us(416); // Delay 416 uS output_low(TX); delay_us(416); // } delay_us(1248); // delay 1248 uS or 3Te for syn bit //----------------- output_high(TX); // start bit delay_us(416); output_low(TX); delay_us(416); //----------------- for (j=0;j<3;j++) // 3 bytes packet transmitter { for(i=0;i<8;i++) { if ((TX_packet[j] & 0x80) == 0x80) { output_high(TX); delay_us(416); output_low(TX); delay_us(416); } else { output_low(TX); delay_us(416); output_high(TX); delay_us(416); } TX_packet[j]=TX_packet[j]<<1; } } //------ send stop bit ------- output_high(TX); delay_us(416); output_low(TX); delay_us(416); delay_ms(20); // delay gaurd } //////////////////////////////
#include <16F877A.h> #device adc=8 #FUSES NOWDT, XT, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, WRT_50% #use delay(clock=4000000) #define RX_ID 0x30 // Receiver ID #define RX PIN_C7 // Chan nhan xung ma hoa #define Led PIN_D1 // power led unsigned char RX_packet[3],x; int1 rx_bit; // rx_bit range value : 0 or 1 unsigned int checksum; int1 Receiver(unsigned char *buff); void main() { setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_comparator(NC_NC_NC_NC); set_tris_b(0x00); // Port b all out put output_b(0x00); // port b all low set_tris_c(0xFF); // port C all input ( RC7 input) set_tris_d(0xFD); output_high(Led); while(TRUE) { checksum=0; while(Receiver(RX_packet)); for(x=0;x<2;x++) checksum+= RX_packet[x]; checksum&= 0x0FF; if(checksum==RX_packet[2]) // Kiem tra tong cua goi du lieu (packet) { if(RX_packet[0]==RX_ID) // Kiem tra ID { output_b(RX_packet[1]); // Xuat ra port b } } } // End while(1) } //////////////////////////// Functions //////////////////////////// int1 Receiver(unsigned char *buff) { unsigned char i,j,T0,count; i=32; count=0; setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2); // setup timer 0 div 2 while(--i) // wait preamble { #asm NOP; NOP; NOP; #endasm while(input(RX)); set_timer0(0); // reset timer0 = 0 #asm NOP; NOP; NOP; #endasm while(!input(RX)); T0=get_timer0(); // doc timer 0 if (T0==0xFF) {return 1;} // error if ((T0>=198) && (T0<=218)) // check pulse 396us --> 436us (OK ) { count++; if ((count>=1)|| (i==0)) break; } } if ((count==0)&&(i==0)) {return 1;} // error setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16); // setup time0 div 16 T0=0; while (!((T0>=94) && (T0<=114))) // 1504 - 1824 uS it OK. syn bit for 4Te or 1664 uS { #asm NOP; NOP; NOP; #endasm while(input(RX)); // wait syn set_timer0(0); // reset timer0 = 0 #asm NOP; NOP; NOP; #endasm while(!input(RX)); T0=get_timer0(); // doc timer 0 } while(input(RX)); //wait start bit go low delay_us(624); // delay before sampling (1.5Te) for (j=0;j<3;j++) // build packet bytes ( 3 bytes) { i=8; // build paket bit (8 bits ) buff[j]=0; while(i--) { buff[j]=buff[j]<<1; rx_bit=input(RX); // check logic (0,1) chan RX buff[j]=buff[j] | rx_bit; if (rx_bit==0) while(!input(RX)); else while(input(RX)); delay_us(624); // delay for 1,5Te } } if (rx_bit==0) if (!input(RX)) return 1; // error (no stop bit found) else if (!input(RX)) return 1; // error (no stop bit found) return 0; }
Comment