Testing Pic code to generate PWM signal Testing Pic code for I2C Master/Slave communication
2007 20 Nov

This test will be part of one of the main module that I will need to communicate between PC and all sensors/modules. I want for the PC from the robot to communicate with one Master PIC by RS232, exchanging commands and information to/from sensors/modules. Than the Master PIC will make the interface to all sensor/modules by I2C Bus.

Still have a lot of work to do on the H-Bridge module, and the I2C Bus, but decided to test RS232! It was fun to play with and my test worked well!

I’ve just assembled on a test board this simple circuit:

Pic16F628A RS232 Test

Then I’ve made the PicC code to test RS232 communication:

  1. #include <16F628A.h>
  2. #use delay(clock=4000000)
  3. #use rs232(baud=9600, parity=N, xmit=PIN_B2, rcv=PIN_B1,bits=8,errors)
  4.  
  5. #fuses NOWDT,XT, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
  6. // PORTB.1 [RB1 pin7]  -> PicRX (RS232 input)
  7. // PORTB.2 [RB2 pin8]  -> PicTX (RS232 output)
  8. // PORTB.4 [RB4 pin10] -> Led Test
  9. //————————————————————————————————————————————————————————————
  10. void main()
  11. {
  12.  int8 rx_char;
  13.  
  14.  setup_comparator(NC_NC_NC_NC);
  15.  setup_vref(FALSE);
  16.  set_tris_a(0b11111111);
  17.  set_tris_b(0b11101011);
  18.  
  19.  output_low(PIN_B4);
  20.  
  21.  while(TRUE)
  22.  {
  23.   if(kbhit()) // test is any data has arrived by RS232 interface
  24.   {
  25.    rx_char = getc(); // fetch char from RS232 -> PC
  26.    rx_char = rx_char + 1; // will convert char to is next value
  27.                           // if ‘a’ was received will send ‘b’
  28.    putc(rx_char); // send new char to PC
  29.  
  30.    output_high(PIN_B4); // will blink external led for 0.5 seconds
  31.    delay_ms(500);
  32.    output_low(PIN_B4);
  33.    delay_ms(500);
  34.   }
  35.  }
  36. }

To test communication between PC and PIC I’ve used WinCommV1.2 at 9600 bps with 8 data bits, 1 stop bit, No parity, but hyper-terminal should be fine!

Leave a Reply