| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| X10 codes |  
				|  Posted: Fri Sep 16, 2011 2:26 am |   |  
				| 
 |  
				| Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! my name is William and im implementing a home automation system using power line communication. Basically im using a motion sensor to send information through the power line using x10 codes. i understand that i have to use extended codes. This is what i used but it is not working.
 so can u tell pls help with codes.
 
 
  	  | Code: |  	  | #include <18F4520.h> #include <stdlib.h>
 #include <Math.h>
 #fuses INTRC,NOWDT,NOPROTECT,NOLVP
 #use delay(clock=1000000)
 //#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
 int SensorL1=0;
 int SensorL2=0;
 
 
 void device_setup()
 {
 
 
 // Set the internal oscillator
 // setup_oscillator( OSC_4MHZ|OSC_INTRC );
 
 // Setup Timer
 setup_timer_0(RTCC_INTERNAL);
 setup_timer_2(T2_DIV_BY_16, 124, 1);
 
 // Setup ADC
 // Setup ADC Ports
 
 setup_adc( ADC_CLOCK_INTERNAL );
 setup_adc_ports(AN0_TO_AN3_ANALOG|VSS_VDD);
 
 // Setup PWM
 
 set_tris_c(0);
 
 setup_ccp1(CCP_PWM);
 setup_ccp2(CCP_PWM);
 
 
 }//end device_setup
 
 
 
 double read_sensor (int channel)
 {
 double value;
 setup_adc(ADC_CLOCK_INTERNAL);
 setup_adc_ports(ALL_ANALOG);
 set_adc_channel(channel);
 delay_ms(15);
 value =  read_adc();
 
 
 return value;
 }//End read_sensor
 
 
 /*=========================start_code===============================*/
 
 void start_code(void)
 {
 while ( !input(PIN_D0) );  // wait for zero crossing to go high
 output_high( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 
 while ( input(PIN_D0) );  // wait for zero crossing to go low
 output_high( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 
 while ( !input(PIN_D0) );  // wait for zero crossing to go high
 output_high( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 
 while ( input(PIN_D0) );  // wait for zero crossing to go low
 output_low ( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 }
 
 /*========================send a one============================*/
 void send_one(void)
 {
 while ( !input(PIN_D0) );  // wait for zero crossing
 output_high( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 
 while ( input(PIN_D0) );
 output_low ( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 
 }
 
 /*=======================send a zero===========================*/
 void send_zero(void)
 {
 while ( !input(PIN_D0) );  // wait for zero crossing
 output_low ( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 
 while ( input(PIN_D0) );
 output_high( PIN_C1);
 delay_ms(1);
 output_low ( PIN_C1);
 
 }
 
 /*==================sensor data=====================================================*/
 void sensor_data(void){
 send_one();
 send_one();
 send_one();
 send_one();
 send_zero();
 send_zero();
 send_zero();
 send_zero();
 }
 void house_code(void)
 {
 send_zero();
 send_one();
 send_one();
 send_zero();
 }
 /*==================extended_code=============================================*/
 void extended_code(void)
 {
 send_zero();
 send_one();
 send_one();
 send_one();
 send_one();
 }
 
 
 /*======================panicbutton data==============================================*/
 void panicbutton_data(void){
 send_zero();
 send_zero();
 send_zero();
 send_zero();
 send_one();
 send_one();
 send_one();
 send_one();
 }
 
 void main()
 {
 
 
 //  device_setup();
 
 
 while(1)
 {
 
 //Switch on LED --Power ON LED
 output_high(PIN_B0);
 delay_ms(100000);
 
 SensorL1 = read_sensor(1);// Sensor N1
 SensorL2 = read_sensor(3);// Sensor N1
 
 
 
 /*if (SensorL1 > 128){
 output_high(PIN_B1);
 //output_low(PIN_B1);
 
 }
 if (SensorL1 < 128){
 //output_high(PIN_B1);
 output_low(PIN_B1);
 }
 if  (SensorL2 >128){
 output_high(PIN_B2);
 }
 if (SensorL2<128){
 output_low(PIN_B2);
 }*/
 /*================sending x10 sensor data================================================*/
 if (SensorL1 > 128){
 // (output_high(PIN_B1)&&output_low(PIN_B2)){
 start_code();
 house_code();
 
 extended_code();
 sensor_data();
 
 
 while ( !input(PIN_D0) );   /* wait 3 half cycles of AC line*/
 while ( !input(PIN_D0) );   /*specified in protocol*/
 while ( !input(PIN_D0) );
 start_code();
 house_code();
 extended_code();
 sensor_data();
 }
 /*=========================sending x10panic button data=====================================*/
 if (SensorL2 > 128) {
 //(output_low(PIN_B1)&& output_high(PIN_B2)){
 
 start_code();
 house_code();
 extended_code();
 panicbutton_data();
 
 
 while ( !input(PIN_D0) );   /* wait 3 half cycles of AC line*/
 while ( !input(PIN_D0) );   /*specified in protocol*/
 while ( !input(PIN_D0) );
 start_code();
 house_code();
 extended_code();
 panicbutton_data();
 }
 if( (SensorL1 > 128) && (SensorL2 > 128) ) {
 //(output_high(PIN_B1)&&output_high(PIN_B2)){
 start_code();
 house_code();
 extended_code();
 panicbutton_data();
 
 
 while ( !input(PIN_D0) );   /* wait 3 half cycles of AC line*/
 while ( !input(PIN_D0) );   /*specified in protocol*/
 while ( !input(PIN_D0) );
 start_code();
 house_code();
 extended_code();
 panicbutton_data();
 }
 
 
 
 
 }
 }
 
 //End main
 | 
 |  |  
		|  |  
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 16, 2011 5:31 am |   |  
				| 
 |  
				| First, I'd get rid of the 'double' casting for the ADC as you're using it in 8 bit mode anyway... 2nd, Get rid of the math library, you don't need anything in there for your application,
 3rd, don't use the 'tris_x()' functions.Let the compiler handle do it automatically for you.
 4th,don't include the 'stdlib',asgain, you don't need it.
 5th, if you use RS232(...) be sure to add 'errors' to the options.
 6th,why are you setting up timers? if needed are the values correct?
 7th,  setup_ccp1(CCP_PWM);
 setup_ccp2(CCP_PWM);
 I couldn't see the value for 'CCP_PWM'....
 8th,ADC doesn't need 15ms, 1ms, even 20us is OK...
 9th, setup_adc only needs to be called or done once in the program
 10...read the X10 example that CCS supplies for 'how it works'...
 
 11..start with a simple 'turn on/off' a light bulb program.build from there...
 |  |  
		|  |  
		| ckielstra 
 
 
 Joined: 18 Mar 2004
 Posts: 3680
 Location: The Netherlands
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 16, 2011 6:00 am |   |  
				| 
 |  
				| A few more remarks: Don't revive a 9 years old thread. For new unrelated questions always start a new thread.
 
 
 A startup delay of 100 seconds is veeerrryyyy long. Besides, the delay function doesn't accept an int32 as parameter and truncates it to an int16, i.e. about 34 seconds with your parameter. 	  | Code: |  	  | delay_ms(100000); 
 SensorL1 = read_sensor(1);// Sensor N1
 | 
 
 SensorL1 is declared as int but the read_sensor function returns a double. This is a bug and I'm surprised the compiler doesn't give you a warning. Change the read_sensor function to return an integer as well. Using floating point arithmetic in a small processor like the PIC is wasting a lot of memory and speed, better use integers with 'fixed point' arithmetic (use Google to find more about this topic).
 
 +++++++++++++++++
 Posts moved to new thread per request.
 
 -- Forum Moderator
 +++++++++++++++++
 
 |  |  
		|  |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| help |  
				|  Posted: Sat Sep 17, 2011 5:46 am |   |  
				| 
 |  
				| my code still doesnt work so i used this one just to check if the modem was gonna blink but nothing happens.pls help 
  	  | Code: |  	  | 
 
 #include <18F4520.h>
 #fuses INTRC,NOWDT,NOPROTECT,NOLVP
 #use delay(clock=1000000)
 #include <x10.c>
 #USE Fast_IO(B)
 
 
 
 void main() {
 
 SET_TRIS_B( 0b00000011 );
 
 
 while(1){
 
 output_low(pin_b5);
 delay_ms(500);
 
 x10_write('A',5);
 output_high(pin_b5);
 
 delay_ms(500);
 }
 }
 
 
 
 
 
 | 
 |  |  
		|  |  
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Sep 17, 2011 6:04 am |   |  
				| 
 |  
				| Edit program as follows... 
 
  	  | Code: |  	  | #include <18F4520.h>
 #fuses INTRC,NOWDT,NOPROTECT,NOLVP
 #use delay(clock=1000000)
 //#include <x10.c>
 // #USE Fast_IO(B)
 
 void main() {
 
 //SET_TRIS_B( 0b00000011 );
 
 while(1){
 
 output_low(pin_b5);
 delay_ms(500);
 
 //x10_write('A',5);
 output_high(pin_b5);
 
 delay_ms(500);
 }
 }
 | 
 
 ...and report back if the LED blinks.
 You have to start with something that works.
 |  |  
		|  |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| help |  
				|  Posted: Sat Sep 17, 2011 7:40 am |   |  
				| 
 |  
				| yeah it does blink |  |  
		|  |  
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sat Sep 17, 2011 1:19 pm |   |  
				| 
 |  
				| Well the good news is the PIC is OK ! 
 The X10 problem is either the x10 driver or x10 modules.
 
 Now...confirm that they work using a standard controller.
 If they do, then it's either a bad code or x10 controller module.
 
 I can't help with that as I tossed out all my x10 stuff 30 years ago.....mainly due to reliablility issues..made my own after that.
 |  |  
		|  |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| help |  
				|  Posted: Sun Sep 18, 2011 4:23 am |   |  
				| 
 |  
				| ok this is my system.form the pic i have a the circuit to transmit the X10 codes. The circuit comes with the spec of the XM10 modem. i dont know whats happening but even the transmission is not goig through sinc nothing is blinking but its supposed to work so please. 
  	  | Code: |  	  | 
 
 
 #include <18F4520.h>
 #fuses INTRC,NOWDT,NOPROTECT
 #use delay(clock=1000000)
 #include <x10.c>
 #USE Fast_IO(B)
 
 
 
 void main() {
 
 SET_TRIS_B( 0b0000011 );
 
 
 while(1){
 
 output_high(pin_b5);
 delay_ms(500);
 
 x10_write('A',5);
 output_high(pin_b5);
 
 delay_ms(500);
 }
 }
 
 
 
 [img]
 
 | 
 |  |  
		|  |  
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Sep 18, 2011 5:44 am |   |  
				| 
 |  
				| Simple basic questions... Are you using a TW523 module and is it wired up exactly as the driver explains ?
 |  |  
		|  |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| help |  
				|  Posted: Sun Sep 18, 2011 5:51 am |   |  
				| 
 |  
				| yeah but im using the XM10E the european version.all the connections are good. |  |  
		|  |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| help |  
				|  Posted: Sun Sep 18, 2011 7:07 am |   |  
				| 
 |  
				| i removed the fast but still it doesnt work.since im in 220V and 50Hz isnt that affecting my code? |  |  
		|  |  
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Sep 18, 2011 9:35 am |   |  
				| 
 |  
				| YES!!!!! 
 The X10.c driver is designed for 60Hz operation NOT 50 Hz, so you'll have to adjust ALL the delays for 50Hz operation.
 
 Voltage doesn't matter but the frequency sure does.
 
 I suggest downloading the X10 manual/specifications and then figure out what has to be 'tweaked'.
 You can't be the first to use X10 in Europe,so perhaps google 'x10e timing' or somethinglike that to find the correct info.
 |  |  
		|  |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| help |  
				|  Posted: Sun Sep 18, 2011 10:29 am |   |  
				| 
 |  
				| ok cool i will look into it. can u help me understand this code
 
  	  | Code: |  	  | 
 #ifndef X10_ZERO_CROSS
 
 #define X10_ZERO_CROSS  PIN_B0
 #define X10_TO_PIC      PIN_B1
 #define X10_FROM_PIC    PIN_B2
 
 #endif
 
 char const X10_HOUSE_CODES[16] = {'M','N','O','P','C','D','A','B','E',
 'F','G','H','K','L','I','J'};
 BYTE const X10_KEY_CODES[16] = {13,14,15,16,3,4,1,2,5,6,7,8,11,12,9,10};
 
 
 void wait_for_zero_cross() {
 
 if(input(X10_ZERO_CROSS))
 while(input(X10_ZERO_CROSS)) ;
 else
 while(!input(X10_ZERO_CROSS)) ;
 }
 
 void x10_write_bits(BYTE data, BYTE n, BYTE start) {
 BYTE i;
 BOOLEAN the_bit;
 
 for(i=1;i<=n;++i) {
 wait_for_zero_cross();
 the_bit=shift_right(&data,1,0);
 output_bit(X10_FROM_PIC, the_bit);
 delay_ms(1);
 output_low(X10_FROM_PIC);
 if(start==0) {
 wait_for_zero_cross();
 output_bit(X10_FROM_PIC, !the_bit);
 delay_ms(1);
 output_low(X10_FROM_PIC);
 }
 }
 }
 
 void x10_write(BYTE house_code, BYTE key_code) {
 BYTE i;
 
 i=0;
 while (X10_HOUSE_CODES[i]!=house_code)
 i++;
 house_code=i;
 if(key_code<16) {
 i=0;
 while (X10_KEY_CODES[i]!=key_code)
 i++;
 key_code=i;
 }
 x10_write_bits(7,4,1);
 x10_write_bits(house_code,4,0);
 x10_write_bits(key_code,5,0);
 x10_write_bits(0,6,1);
 }
 
 BYTE x10_data_ready() {
 port_b_pullups(TRUE);
 return(!input(X10_TO_PIC));
 }
 
 BYTE x10_read_bits(BYTE n) {
 BYTE data,i;
 
 for(i=1;i<=n;++i) {
 wait_for_zero_cross();
 delay_us(300);
 shift_right(&data,1,input(X10_TO_PIC));
 wait_for_zero_cross();
 delay_us(300);
 }
 data>>=8-n;
 return(data);
 }
 
 void x10_read(BYTE *house_code,BYTE *key_code) {
 
 port_b_pullups(TRUE);
 x10_read_bits(2);
 *house_code=x10_read_bits(4);
 *house_code=X10_HOUSE_CODES[*house_code];
 *key_code=x10_read_bits(5);
 if(*key_code<16)
 *key_code=X10_KEY_CODES[*key_code];
 }
 
 | 
 |  |  
		|  |  
		| temtronic 
 
 
 Joined: 01 Jul 2010
 Posts: 9589
 Location: Greensville,Ontario
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Sun Sep 18, 2011 2:33 pm |   |  
				| 
 |  
				| To understand the code better, look for a 'Dummies for X10' webpage. Really, the C code is well written, at least for my old eyes! Each function is self explanatory, variables mean what they say, etc.
 However you should consult an X10 website (I'm sure they exist, everything else does !) as to -why- the code is written this way.
 30 years ago I did it all in Z80 assembler and the 'format' or 'construction' hasn't been changed, just the language.
 
 To control a lamp...the basic flow is to construct a sequence of ones and zeros, properly timed and in sync with the power line frequency. It is that simple...'bit bang' and I/O pin and 'magically' an X10 light goes on, off,etc.
 Once you see a flow chart or timing diagram, you should be able to see the pattern.
 
 You do have a problem in getting it to work at 50Hz but again, there must be websites with that information.
 |  |  
		|  |  
		| miamomoualeu 
 
 
 Joined: 16 Sep 2011
 Posts: 15
 
 
 
			    
 
 | 
			
				| help |  
				|  Posted: Mon Sep 19, 2011 1:54 am |   |  
				| 
 |  
				| I have tried to look on the internet for the timing issues and I don't see where to set it up properly for 50Hz. The only thing available is that the length of the pulse is 1ms. I wrote a new code but still not working can you check and see if there is something wrong with it? Or is my problem in the circuit interface between the pic and the modem? 
  	  | Code: |  	  | #include <18F4520.h>
 #fuses INTRC,NOWDT,NOPROTECT,
 #use delay(clock=1000000)
 
 void start_code();
 void house_code();
 void key_code();
 void send_one();
 void send_zero();
 
 void start_code()
 {
 while ( !input(PIN_B0) ){  // wait for zero crossing to go high
 output_high( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 
 while ( input(PIN_B0) ){  // wait for zero crossing to go low
 output_high( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 
 while ( !input(PIN_B0) ){  // wait for zero crossing to go high
 output_high( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 while ( input(PIN_B0) ){  // wait for zero crossing to go low
 output_low ( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 }
 
 
 /*========================send a one============================*/
 void send_one()
 {
 while ( !input(PIN_B0) ){  // wait for zero crossing
 output_high( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 
 while ( input(PIN_B0) ){
 output_low ( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 
 }
 
 /*=======================send a zero===========================*/
 void send_zero()
 {
 while ( !input(PIN_B0) ){  // wait for zero crossing
 output_low ( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 
 while ( input(PIN_B0) ){
 output_high( PIN_B2);
 delay_ms(1);
 output_low ( PIN_B2);
 }
 
 }
 void house_code()
 {
 send_zero();
 send_one();
 send_one();
 send_zero();
 }
 void key_code()
 {
 send_zero();
 send_one();
 send_one();
 send_zero();
 send_zero();
 }
 
 void main()
 {
 
 while(1)
 {
 output_high(PIN_B5);
 delay_ms(1000);
 start_code();
 output_low(PIN_B5);
 delay_ms(1000);
 house_code();
 output_high(PIN_B5);
 delay_ms(1000);
 key_code();
 output_low(PIN_B5);
 
 }
 }
 
 | 
 |  |  
		|  |  
		|  |  
  
	| 
 
 | You cannot post new topics in this forum You cannot reply to topics in this forum
 You cannot edit your posts in this forum
 You cannot delete your posts in this forum
 You cannot vote in polls in this forum
 
 |  
 Powered by phpBB © 2001, 2005 phpBB Group
 
 |