SlicePWM.cs

Setup a TSIO-9001/9002 for PWM output and change the duty cycle

/*  
 Copyright(c) 1998-2009 by Robotic Systems Integration, Inc. All rights reserved.
 This software contains proprietary and confidential information of Robotic 
 Systems Integration, Inc. (RSI) and its suppliers. Except as may be set forth 
 in the license agreement under which this software is supplied, disclosure, 
 reproduction, or use with controls other than those provided by RSI or suppliers
 for RSI is strictly prohibited without the prior express written consent of 
 Robotic Systems Integration.

 Warning!  This is a sample program to assist in the integration of your motion 
 controller with your application.  It may not contain all of the logic and safety
 features that your application requires.

 For any questions regarding this sample code please visit www.roboticsys.com.
 ==================================================================================
*/

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using RSI.RapidCode.SynqNet.dotNET;
using RSI.RapidCode.SynqNet.dotNET.Enums;

namespace SampleApplications
{
    [TestFixture]
    public class SlicePWM
    {
   
        const int SLICE_NODE = 0;               // node number as discovered on SynqNet network
        const int SEGMENT_NUMBER = 0;           // segment (slice) number as found on Slice I/O 
        
        const int PARAMTER_NUMBER = 0;          // parameter to set the PWM mode
        const string PARAMETER_VALUE = "8";     // counter mode 8 = PWM Output Mode
        const int OUTPUT_TERMINAL_SELECT = 224; // Output Terminal Selection byte 0: "1110 0000" = PWM mode

        const int A_FEW_SECONDS = 3000;         // milliseconds

        int frequency = 20000;                  // frequency in Hz
        int dutyCycle = 75;                     // valid range is 0 to 100, for 0% to 100% duty cycle

        MotionController controller;             // RapidCode objects
        IO slice;


        private void ConfigurePWM(int frequency)
        {
            // char array to convert int freq into array of ascii chars
            char[] c = new char[3];                 
   
            //convert frequency into ascii character array
            c[0] = Convert.ToChar(frequency & 0xFF);                 // mask low byte
            c[1] = Convert.ToChar((frequency & 0xFF00) >> 8);        // mask middle byte and shift 1 byte
            c[2] = Convert.ToChar((frequency & 0xFF0000) >> 16);     // mask upper byte and shift 2 bytes
  
            //Set three bytes in memory allocated for PWM frequency value
            slice.SegmentMemorySet(SEGMENT_NUMBER, 16, 1, c[0].ToString());     //load low byte
            slice.SegmentMemorySet(SEGMENT_NUMBER, 17, 1, c[1].ToString());     //load middle byte
            slice.SegmentMemorySet(SEGMENT_NUMBER, 18, 1, c[2].ToString());     //load upper byte

            //Configure to PWM mode
            slice.SegmentParameterSet(SEGMENT_NUMBER, PARAMTER_NUMBER, 1, PARAMETER_VALUE);

            //Configure Output Data: this sends the PWM output to Segment's output pin
            slice.DigitalOutSet(0, 8, OUTPUT_TERMINAL_SELECT);  // start at bit 0, set 8 bits
        }


        private void SetPWMDutyCycle(int dutyCycle)
        {
            slice.DigitalOutSet(8, 8, dutyCycle);  // start at bit 8 and set 8 bits from there
        }

        [Test]
        public void Main()
        {
            try
            {
                //RapidCode Objects
                controller = MotionController.Create();
                slice = controller.IOGet(SLICE_NODE);

                // setup PWM on the TSIO-9001/2
                ConfigurePWM(frequency);
                
                // set duty cycle (0% to 100%)
                SetPWMDutyCycle(dutyCycle);

                // wait a few seconds so we can see it on the scope
                controller.OS.Sleep(A_FEW_SECONDS);

                // 50%
                SetPWMDutyCycle(50);

                controller.OS.Sleep(A_FEW_SECONDS);  
                SetPWMDutyCycle(25);

                controller.OS.Sleep(A_FEW_SECONDS);  
                SetPWMDutyCycle(0);                     
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}