public static void Main() { // Defining two 74HC165s daisychained on the SPI bus, pin 10 as latchpin Ic74hc165 IcInChain = new Ic74hc165(SPI_Devices.SPI1, Pins.GPIO_PIN_D10, 2); // Defining two 74HC595s daisychained on the SPI bus, pin 9 as latchpin Ic74hc595 IcOutChain = new Ic74hc595(SPI_Devices.SPI1, Pins.GPIO_PIN_D9, 2); // Defines all 16 leds for (uint Counter = 0; Counter < 16; ++Counter) { Leds[Counter] = IcOutChain.Pins[Counter]; } // Defines all 16 buttons IIRQPort[] Buttons = new IIRQPort[16]; for (uint Counter = 0; Counter < 16; ++Counter) { Buttons[Counter] = IcInChain.Pins[Counter]; Buttons[Counter].OnStateChange += new StateChange(Program_OnStateChange); Buttons[Counter].ID = Counter.ToString(); } // Enables interrupts IcInChain.EnableInterrupts(); // Wait infinite; let the events to their jobs Thread.Sleep(Timeout.Infinite); }
/// <summary>Initialises a new parallel output port</summary> /// <param name="MainChain">The object of the main chain</param> /// <param name="StartBit">The first bit to write</param> /// <param name="BitCount">The amount of bits to write</param> /// <param name="Inverted">When true, bits will be inverted</param> public Ic74hc595ParallelOut(Ic74hc595 MainChain, uint StartBit, uint BitCount, bool Inverted) { this._Chain = MainChain; this._StartBit = StartBit; this._BitCount = BitCount; this._Inverted = Inverted; }
public static void Main() { // Defines all 16 LEDs linked to two 74HC595 ICs in a chain Ic74hc595 IcChain = new Ic74hc595(SPI_Devices.SPI1, Pins.GPIO_PIN_D10, 2); Led = IcChain.Pins; // Defines the rotary encoder RotaryEncoder Knob = new RotaryEncoder(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1); Knob.Rotated += new NativeEventHandler(Knob_Rotated); // Links the event to the button Button.StateChanged += new AutoRepeatEventHandler(Button_StateChanged); // Wait infinitely Thread.Sleep(Timeout.Infinite); }
/// <summary> /// /// </summary> /// <param name="ClockPin">SPI Clock pin</param> /// <param name="EnablePin">SPI Enable pin</param> /// <param name="DataPin">SPI Data pin</param> /// <param name="LatchPin">SPI Latch pin</param> /// <param name="Motor1Pwm">Motor 1 PWM pin</param> /// <param name="Motor2Pwm">Motor 2 PWM pin</param> /// <param name="Motor3Pwm">Motor 3 PWM pin</param> /// <param name="Motor4Pwm">Motor 4 PWM pin</param> public AdafruitMotorshield( Cpu.Pin ClockPin, Cpu.Pin EnablePin, Cpu.Pin DataPin, Cpu.Pin LatchPin, IPWMPort Motor1Pwm, IPWMPort Motor2Pwm, IPWMPort Motor3Pwm, IPWMPort Motor4Pwm ) { // This one should always be false this._EnablePin = new OutputPort(EnablePin, false); // Defines the 74HC595 chip by bitbanging this._IcOut = new Ic74hc595(ClockPin, DataPin, LatchPin); // Defines all 8 pins on the 74HC595 this._Motor1aPin = this._IcOut.Pins[2]; // M1A this._Motor1bPin = this._IcOut.Pins[3]; // M1B this._Motor2aPin = this._IcOut.Pins[1]; // M2A this._Motor2bPin = this._IcOut.Pins[4]; // M3B this._Motor4aPin = this._IcOut.Pins[0]; // M4A this._Motor4bPin = this._IcOut.Pins[6]; // M4B this._Motor3aPin = this._IcOut.Pins[5]; // M3A this._Motor3bPin = this._IcOut.Pins[7]; // M3B // Motor PWM pins this._Motor1Pwm = Motor1Pwm; // PWM2A this._Motor2Pwm = Motor2Pwm; // PWM2B this._Motor3Pwm = Motor3Pwm; // PWM0A this._Motor4Pwm = Motor4Pwm; // PWM0B if (this._Motor1Pwm != null) { this._Motor1Pwm.SetDutyCycle(0); this._Motor1Pwm.StartPulse(); } if (this._Motor2Pwm != null) { this._Motor2Pwm.SetDutyCycle(0); this._Motor2Pwm.StartPulse(); } if (this._Motor3Pwm != null) { this._Motor3Pwm.SetDutyCycle(0); this._Motor3Pwm.StartPulse(); } if (this._Motor4Pwm != null) { this._Motor4Pwm.SetDutyCycle(0); this._Motor4Pwm.StartPulse(); } }
public static void Main() { // Initializes a 7-segment display over a bitshift IC using a single IC Ic74hc595 Mux = new Ic74hc595(SPI_Devices.SPI1, Pins.GPIO_PIN_D10); SevenSegment Display = new SevenSegment(Mux.CreateParallelOut()); while (true) { for (byte Value = 0; Value < 11; ++Value) { // Displays all values for 0,5 sec. (0-9 = 0-9, 10=blank) Display.SetDigit(Value); // Toggles the dot Display.SetDot(!Display.GetDot()); // Wait for 0,5 sec Thread.Sleep(500); } } }
public static void Main() { // We got 4 74HC595's in a chain Ic74hc595 IcChain = new Ic74hc595(SPI_Devices.SPI1, Pins.GPIO_PIN_D10, 4); // Led loop back and forward while (true) { for (int Counter = 0; Counter < 30; ++Counter) { IcChain.Pins[Counter].Write(true); Thread.Sleep(50); IcChain.Pins[Counter].Write(false); } for (int Counter = 28; Counter > 0; --Counter) { IcChain.Pins[Counter].Write(true); Thread.Sleep(50); IcChain.Pins[Counter].Write(false); } } }
public static void Main() { // Outputs: // - Buzzer (GPIO D3) // - Segment7 (7-Segment display/74HC595 IC) (SPI D4, D7, D8) // - Led1 (PWM D5) // - Led2 (PWM D6) #region "Output definitions" // The buzzer is connected directly to GPIO pin D3 BitBangBuzzer Buzzer = new BitBangBuzzer(Pins.GPIO_PIN_D3); // The 7-segment display is connected with a 74HC595 bitshift IC over GPIO pins D4 (MOSI), D7 (CS) and D8 (SCLK) Ic74hc595 Mux = new Ic74hc595(Pins.GPIO_PIN_D8, Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D7); SevenSegment Segment7 = new SevenSegment(Mux.CreateParallelOut()); // The DangerShield has the digits defined differently; these bits are used: // Top = 1 // UpperRight = 2 // LowerRight = 3 // Bottom = 4 // LowerLeft = 5 // UpperLeft = 6 // Middle = 7 // Dot = 8 Segment7.ChangeSignals(new byte[] { // (87654321) 0x3f, // 0 brights up: 0 1 2 3 4 5 (00111111) 0x06, // 1 brights up: 1 2 (00000110) 0x5b, // 2 brights up: 0 1 3 4 6 (01011011) 0x4f, // 3 brights up: 0 1 2 3 6 (01001111) 0x66, // 4 brights up: 1 2 5 6 (01100110) 0x6d, // 5 brights up: 0 2 3 5 6 (01101101) 0x7d, // 6 brights up: 0 2 3 4 5 6 (01111101) 0x07, // 7 brights up: 0 1 2 (00000111) 0x7f, // 8 brights up: 0 1 2 3 4 5 6 (01111111) 0x6f, // 9 brights up: 0 1 2 3 5 6 (01101111) 0x00, // all go down: 0 1 2 4 5 6 7 (00000000) }); Segment7.ChangeDotSignal(8); // Both leds IPWMPort Led1 = new Netduino.PWM(Pins.GPIO_PIN_D5); Led1.StartPulse(); IPWMPort Led2 = new Netduino.PWM(Pins.GPIO_PIN_D6); Led2.StartPulse(); #endregion // Inputs: // - PotentioMeter1 (ADC A0) // - PotentioMeter2 (ADC A1) // - PotentioMeter3 (ADC A2) // - Photocell (ADC A3) // - TemperatureSensor (ADC A4) // - KnockSensor (ADC A5) // - PushButton1 (GPIO D10) // - PushButton2 (GPIO D11) // - PushButton3 (GPIO D12) #region "Input definitions" // Potentio meters IADCPort PotentioMeter1 = new Netduino.ADC(Pins.GPIO_PIN_A0); PotentioMeter1.RangeSet(0, 100); // Same range as Led1.SetDutyCycle() IADCPort PotentioMeter2 = new Netduino.ADC(Pins.GPIO_PIN_A1); PotentioMeter2.RangeSet(0, 100); // Same range as Led2.SetDutyCycle() IADCPort PotentioMeter3 = new Netduino.ADC(Pins.GPIO_PIN_A2); PotentioMeter3.RangeSet(0, 9); // Same range as Segment7.SetDigit() // Photocell IADCPort Photocell = new Netduino.ADC(Pins.GPIO_PIN_A3); Photocell.RangeSet(0, 100); // Same range as Led2.SetDutyCycle() // Temperature Sensor Tmp36 TemperatureSensor = new Tmp36(new Netduino.ADC(Pins.GPIO_PIN_A4)); // Knock Sensor IADCPort KnockSensor = new Netduino.ADC(Pins.GPIO_PIN_A5); KnockSensor.RangeSet(0, 200); // Bigger range as Led1.SetDutyCycle() but you really need to smash hard to reach this value // Push buttons InputPort PushButton1 = new InputPort(Pins.GPIO_PIN_D10, false, Port.ResistorMode.Disabled); InputPort PushButton2 = new InputPort(Pins.GPIO_PIN_D11, false, Port.ResistorMode.Disabled); InputPort PushButton3 = new InputPort(Pins.GPIO_PIN_D12, false, Port.ResistorMode.Disabled); #endregion // This value contains which demo is currently active int Demo = 1; // Contains the last second, so we can switch between two numbers on the 7-segment display (to display the temperature) int LastSecond = Utility.GetMachineTime().Seconds; // This digit should be currently shown bool ShowSecondDigit = false; // Infinite loop while (true) { // Switches the demo, when required (NOT statement because of the pullup resistors) if (!PushButton1.Read()) Demo = 1; if (!PushButton2.Read()) Demo = 2; if (Demo == 1) { // First demo is currently active Led1.SetDutyCycle((uint)PotentioMeter1.RangeRead()); Led2.SetDutyCycle((uint)PotentioMeter2.RangeRead()); Segment7.SetDigit((byte)PotentioMeter3.RangeRead()); } else { // Second demo is currently active uint Knocking = (uint)KnockSensor.RangeRead(); Led1.SetDutyCycle((uint)(Knocking > 100 ? 100 : Knocking)); // We want to limit to 100 Led1.SetDutyCycle((uint)KnockSensor.RangeRead()); Led2.SetDutyCycle((uint)Photocell.RangeRead()); // Okay, we want two temperature digits seprated, for display purposes float Temp = TemperatureSensor.Temperature; byte Digit1 = (byte)(Temp / 10); byte Digit0 = (byte)(Temp - (10 * Digit1)); // Switch the digit to be displayed if (LastSecond != Utility.GetMachineTime().Seconds) { LastSecond = Utility.GetMachineTime().Seconds; Segment7.SetDigit(ShowSecondDigit ? Digit1 : Digit0); Segment7.SetDot(!ShowSecondDigit); ShowSecondDigit = !ShowSecondDigit; } } // Links the buzzer to the 3rd pushbutton's value Buzzer.Write(!PushButton3.Read()); } }
/// <summary> /// Defines a GPO Port /// </summary> /// <param name="MainChain">The object of the main chain</param> /// <param name="BitNo">The number of the bit</param> public Ic74hc595GPOPort(Ic74hc595 MainChain, uint BitNo) { // Copies the parameters to local values this._Chain = MainChain; this._BitNo = BitNo; }
/// <summary>Initialises a new parallel output port</summary> /// <param name="MainChain">The object of the main chain</param> /// <param name="StartBit">The first bit to write</param> /// <param name="BitCount">The amount of bits to write</param> /// <param name="Inverted">When true, bits will be inverted</param> public Ic74hc595ParallelOut(Ic74hc595 MainChain, uint StartBit, uint BitCount, bool Inverted) { this._Chain = MainChain; this._StartBit = StartBit; this._BitCount = BitCount; this._Inverted = Inverted; }
/// <summary> /// Defines a GPO Port /// </summary> /// <param name="MainChain">The object of the main chain</param> /// <param name="BitNo">The number of the bit</param> public Ic74hc595GPOPort(Ic74hc595 MainChain, uint BitNo) { // Copies the parameters to local values this._Chain = MainChain; this._BitNo = BitNo; }
/// <summary> /// /// </summary> /// <param name="ClockPin">SPI Clock pin</param> /// <param name="EnablePin">SPI Enable pin</param> /// <param name="DataPin">SPI Data pin</param> /// <param name="LatchPin">SPI Latch pin</param> /// <param name="Motor1Pwm">Motor 1 PWM pin</param> /// <param name="Motor2Pwm">Motor 2 PWM pin</param> /// <param name="Motor3Pwm">Motor 3 PWM pin</param> /// <param name="Motor4Pwm">Motor 4 PWM pin</param> public AdafruitMotorshield( Cpu.Pin ClockPin, Cpu.Pin EnablePin, Cpu.Pin DataPin, Cpu.Pin LatchPin, IPWMPort Motor1Pwm, IPWMPort Motor2Pwm, IPWMPort Motor3Pwm, IPWMPort Motor4Pwm ) { // This one should always be false this._EnablePin = new OutputPort(EnablePin, false); // Defines the 74HC595 chip by bitbanging this._IcOut = new Ic74hc595(ClockPin, DataPin, LatchPin); // Defines all 8 pins on the 74HC595 this._Motor1aPin = this._IcOut.Pins[2]; // M1A this._Motor1bPin = this._IcOut.Pins[3]; // M1B this._Motor2aPin = this._IcOut.Pins[1]; // M2A this._Motor2bPin = this._IcOut.Pins[4]; // M3B this._Motor4aPin = this._IcOut.Pins[0]; // M4A this._Motor4bPin = this._IcOut.Pins[6]; // M4B this._Motor3aPin = this._IcOut.Pins[5]; // M3A this._Motor3bPin = this._IcOut.Pins[7]; // M3B // Motor PWM pins this._Motor1Pwm = Motor1Pwm; // PWM2A this._Motor2Pwm = Motor2Pwm; // PWM2B this._Motor3Pwm = Motor3Pwm; // PWM0A this._Motor4Pwm = Motor4Pwm; // PWM0B if (this._Motor1Pwm != null) { this._Motor1Pwm.SetDutyCycle(0); this._Motor1Pwm.StartPulse(); } if (this._Motor2Pwm != null) { this._Motor2Pwm.SetDutyCycle(0); this._Motor2Pwm.StartPulse(); } if (this._Motor3Pwm != null) { this._Motor3Pwm.SetDutyCycle(0); this._Motor3Pwm.StartPulse(); } if (this._Motor4Pwm != null) { this._Motor4Pwm.SetDutyCycle(0); this._Motor4Pwm.StartPulse(); } }