Testing Pic code for I2C Master/Slave communication Extra Lab Tools
2008 02 Feb

I have just acquire this PIC 16F690 due to lack of hardware support from my old PIC 16F628a. Didn’t have I2C Slave by hardware and only 1 PWM (pulse width modulation) module available! I2C Slave by hardware was already tested on my previous post!

I have tested the PWM on this new PIC and found the results to be beyond my expectations! It is also configured with 1 PWM module but with a special capability! This signal will be available in pin RC5/CCP1/P1A and can be multiplexed to 3 other outputs, RC4/C2OUT/P1B, RC3/AN7/P1C and RC2/AN6/P1D. For P1A output the PWM signal will always be present but for P1B, P1C and P1D can be set/reset at any time. In my next example I will configure a PWM pulse with frequency of 500Hz (2ms period) and Duty-Cycle to 25% available to outputs P1A and P1B. From 3 to 3 seconds will switch configuration to activate/deactivate PWM pulse on output P1B.

This will be required to drive my previous module that will control the DC motors (will need to have PWM switching between P1B and P1C - this will invert DC motor direction): http://www.botdream.com/blog/2007/11/01/2nd-h-bridge-circuit-is-now-completed/

For this test I’ve assembled the following circuit:
PWM 2Channels

PWM C code:

  1. #include <16F690.h>
  2. #use delay(clock=4000000)
  3. #fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOCPD
  4. //————————————————————————————————————————————————————————————
  5. #use rs232(baud=9600, parity=N, xmit=PIN_B7, rcv=PIN_B5, bits=8, errors)
  6. //————————————————————————————————————————————————————————————
  7. // PORTB.5 [RB5 pin12]  -> PicRX (RS232 input)
  8. // PORTB.7 [RB7 pin10]  -> PicTX (RS232 output)
  9. //————————————————————————————————————————————————————————————
  10. #define P1A PIN_C5
  11. #define P1B PIN_C4
  12. #define P1C PIN_C3
  13. #define P1D PIN_C2
  14. //————————————————————————————————————————————————————————————
  15.  
  16. void main(void)
  17. {
  18.   delay_ms(200); // power up delay
  19.  
  20.   setup_adc_ports(NO_ANALOGS | VSS_VDD);
  21.   setup_comparator(NC_NC_NC_NC);
  22.   setup_oscillator(OSC_4MHZ);
  23.   setup_vref(FALSE);
  24.  
  25.   setup_timer_2(T2_DIV_BY_16, 124, 1);  // 500 Hz
  26.   set_pwm1_duty(31);  // 25% duty cycle
  27.  
  28.   // Enable PWM pins P1B, P1C, and P1D for output.
  29.   // Note:  P1A is done by the compiler.
  30.   output_low(P1B);
  31.   output_low(P1C);
  32.   output_low(P1D);
  33.  
  34.   // Steer the PWM output to all four P1x pins.
  35.   setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A
  36.                          | CCP_PULSE_STEERING_B
  37.                          | CCP_PULSE_STEERING_C
  38.                          | CCP_PULSE_STEERING_D );
  39.  
  40.   while(1)
  41.   {
  42.     // Steer the PWM output to P1A and P1B
  43.     setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A
  44.                            | CCP_PULSE_STEERING_B);
  45.  
  46.     output_low(PIN_C6);
  47.     delay_ms(500);
  48.     delay_ms(500);
  49.     delay_ms(500);
  50.     delay_ms(500);
  51.     delay_ms(500);
  52.     delay_ms(500);
  53.  
  54.     // Steer the PWM output to P1A only
  55.     setup_ccp1(CCP_PWM_H_H | CCP_PULSE_STEERING_A);
  56.     output_low(P1B);
  57.  
  58.     output_high(PIN_C6);
  59.     delay_ms(500);
  60.     delay_ms(500);
  61.     delay_ms(500);
  62.     delay_ms(500);
  63.     delay_ms(500);
  64.     delay_ms(500);
  65.   }
  66. }
  67. //————————————————————————————————————————————————————————————

Also made some measurements with my digital oscilloscope to check if it was working has excepted:

-PWM signal assigned to P1A an P1B outputs

PWM scope1 thumb

- PWM pulse only assigned to P1A

PWM scope2 thumb

- Signal properties

PWM scope3 thumb

PWM scope4 thumb

PWM scope5 thumb

I’m quite happy with the results form this test. I can now use 1 PIC 16F690 to fully control 1 H-Bridge channel (1 DC motor) without any external hardware.

In my next post I will explain how all pieces and modules will come together!

Leave a Reply