You have reached the legacy GHI Electronics, LLC website, for the new website please visit here. For the new forum please visit here.

This legacy website will be taken offline at the end of this year. If there is anything that you would like to archive and save for future reference please do so.

The new TinyCLR using a Panda III by willgeorge

Feb. 6, 2017   |   Snippet   |   Licensed as Apache 2.0   |   851 views

Of the order Primates I must be way down on the list.

I originally posted code that had a error.

Somehow I sent text from a older file. I tried to post a revision but somehow I screw that up also.
So I deleted the post in hopes that I will do it correctly this time.

The error was in method
// On LDR1.Read()
private static void BrighttoDimPWM()

It was using the wrong color LED.

I also moved the PWM pin D4 for the Red LED in order to free up the pins for I2C D3 and D4.

Comments or questions?   Discuss on the forum.



Author Version Date
willgeorge 1 02/06 '17 at 10:12am
1 — Source
  1. /*
  2. Using:
  3. Microsoft Visual C# 2017 RC
  4. FEZ Panda III Rev 1.3
  5.   Tera Term - USB FTDI Serial connection to a PC USB Port
  6. Installed:
  7. G80 Firmware.ghi
  8. GHIElectronics.TinyCLR.Core.0.1.0.nupkg
  9. GHIElectronics.TinyCLR.Devices.0.1.0.nupkg
  10. GHIElectronics.TinyCLR.Pins.0.1.0.nupkg
  11.  
  12.   ************************************
  13.   IMPORTANT Tera Term : Terminal setup
  14.   For this app: new-line is set to: Transmit CR+LF - Receive CR
  15.   NOTE: If using Tera Term for the loader, Transmit CR+LF will not work. Set Transmit to CR
  16. */
  17.  
  18. /*
  19. Four leg LED (They come as common Cathode (-) and common Anode (+)
  20.  1: Red -
  21.  2: Longest leg + common Anode (+)
  22.  3: Green -
  23.  4: Blue -
  24.  
  25.  Or use individual LED's
  26. */
  27.  
  28. //System.Diagnostics.Debugger.Break();
  29.  
  30.  
  31.  
  32. using System;
  33. using System.Threading;
  34.  
  35. using GHIElectronics.TinyCLR.Devices.Gpio;
  36. using GHIElectronics.TinyCLR.Pins;
  37.  
  38. using GTC_DS = GHIElectronics.TinyCLR.Devices.SerialCommunication;
  39. using GTC_SS = GHIElectronics.TinyCLR.Storage.Streams;
  40.  
  41.  
  42. namespace TinyCLR_PWMSERIAL
  43. {
  44. public class Program
  45. {
  46. public static bool loop = true;
  47. public static bool blink = true;
  48. public static bool sendDutyCycleText = true;
  49.  
  50. // On board LED's
  51. public static GpioPin LED1; //Blue PE14/PWM3
  52. public static GpioPin LED2; //Green PE13/PWM2
  53. public static GpioPin LED3; //Orange PE11/PWM1
  54. public static GpioPin LED4; //Red PE9/PWM0
  55.  
  56. // On board push buttons
  57. public static GpioPin LDR0; //PE3
  58. public static GpioPin LDR1; //PE4
  59.  
  60. public static GpioPin PA8; //CPU Pin 67 -External Push Button
  61. public static GpioPin PD1; //CPU Pin 82 -External Red LED
  62.  
  63. // COM1
  64. public static GTC_DS.SerialDevice _com1;
  65. public static GTC_SS.IOutputStream _outStream;
  66. public static GTC_SS.IInputStream _inStream;
  67. public static GTC_SS.Buffer _msgbuffer;
  68. public static GTC_SS.Buffer _readbuffer;
  69.  
  70. public static char[] charsToTrim = { '-', '0' };
  71.  
  72. private static string s = String.Empty;
  73. private static bool isactive = false;
  74. private static bool isactiveBlue = false;
  75. private static bool toggleRED = false;
  76.  
  77. public static Timer t;
  78.  
  79. // 100 milliseconds.
  80. //public static TimeSpan spanin_ms = new TimeSpan(0, 0, 0, 0, 100); // Not used
  81.  
  82. // Use this if you want to use PWM for a External LED
  83.  
  84. private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm5; // PB3/PWM5/SPI1.CLK - X1 8 pin header
  85. private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm8; // PB4/PWM8/SPI1.MISO - X1 8 pin header
  86.  
  87. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm4; // PA15/PWM4 - 8 pin header
  88. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm5; // PB3/PWM5/SPI1.CLK - 8 pin header
  89. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm6; // PB10/PWM6/SPI2.CLK - 40 pin header
  90. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm7; // PB11 - 40 pin header
  91. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm8; // PB4/PWM8/SPI1.MISO - 8 pin header
  92. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm9; // PB5/PWM9/SPI1.MOSI - 8 pin header
  93. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm10; // PB0/ADC8/PWM10 - 8 pin header
  94. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm11; // PB1/ADC9/PWM11 - 8 pin header
  95. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm12; // PD12PWM12/UART3.RTS - 40 pin header
  96. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm22; // PB8/PWM22 - 8 pin header
  97. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm23; // PB9/PWM23 - 8 pin header
  98. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm16; // PC6/PWM16/UART6.TX - 40 pin header
  99. //private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm17; // PC7/PWM17/UART6.RX - 40 pin header
  100.  
  101. // Option - Use the following if you want to use a on board LED for PWM
  102. // private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm3; //Blue PE14/PWM3 CPU Pin 45
  103. // private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm2; //Green PE13/PWM2 CPU Pin 44
  104. // private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm1; //Orange PE11/PWM1 CPU Pin 42
  105. // private static GHIElectronics.TinyCLR.Devices.Pwm.PwmPin pwm0; //Red PE9/PWM0 CPU Pin 40
  106.  
  107. // Here we go!
  108. public static void Main()
  109. {
  110. // Timer(TimerCallback callback, object state, int dueTime, int period);
  111. // dueTime - The amount of time to delay before callback is invoked, in milliseconds.
  112. // Specify Timeout.Infinite to prevent the timer from starting.
  113. // Use zero(0) to start the timer immediately.
  114.  
  115. // Not used - Provided for your use
  116. //t = new Timer(MyTimerCallback, true, Timeout.Infinite, 0);
  117.  
  118. // Our COM port
  119. _com1 = GTC_DS.SerialDevice.FromId("COM1");
  120. _outStream = _com1.OutputStream;
  121. _inStream = _com1.InputStream;
  122.  
  123. var gpio = GpioController.GetDefault(); //Pin count 80
  124.  
  125. PD1 = gpio.OpenPin(FEZPandaIII.Gpio.D7); // X2 8 pin header D7 Marked CAN
  126. PD1.SetDriveMode(GpioPinDriveMode.Output);
  127.  
  128. // A external push button with a 10K pullup connected to pin D52 (Pin 37) of the 40 pin header
  129. PA8 = gpio.OpenPin(FEZPandaIII.Gpio.D52, GpioSharingMode.Exclusive); // CPU Pin 67
  130. PA8.SetDriveMode(GpioPinDriveMode.Input);
  131.  
  132. // Set Pins for LDR Buttons
  133. // This button DimtoBrightPWM()
  134. LDR0 = gpio.OpenPin(FEZPandaIII.Gpio.Ldr0, GpioSharingMode.Exclusive); // PE3 CPU Pin 2
  135. LDR0.SetDriveMode(GpioPinDriveMode.InputPullUp);
  136.  
  137. // This button BrighttoDimPWM()
  138. LDR1 = gpio.OpenPin(FEZPandaIII.Gpio.Ldr1, GpioSharingMode.Exclusive); // PE4 CPU Pin 3
  139. LDR1.SetDriveMode(GpioPinDriveMode.InputPullUp);
  140.  
  141. // Set the PC Board LED pins
  142. LED1 = gpio.OpenPin(FEZPandaIII.Gpio.Led1); // PE14/PWM3 Blue CPU Pin 45
  143. LED1.SetDriveMode(GpioPinDriveMode.Output);
  144.  
  145. LED2 = gpio.OpenPin(FEZPandaIII.Gpio.Led2); // PE13/PWM2 Green CPU Pin 44
  146. LED2.SetDriveMode(GpioPinDriveMode.Output);
  147.  
  148. LED3 = gpio.OpenPin(FEZPandaIII.Gpio.Led3); // PE11/PWM1 Orange CPU Pin 42
  149. LED3.SetDriveMode(GpioPinDriveMode.Output);
  150.  
  151. LED4 = gpio.OpenPin(FEZPandaIII.Gpio.Led4); // PE9/PWM0 Red CPU Pin 40
  152. LED4.SetDriveMode(GpioPinDriveMode.Output);
  153.  
  154. // Configure the COM port
  155. SerialSetup();
  156.  
  157. PD1.Write(GpioPinValue.High); // Turn off the external Red LED
  158.  
  159. // Tera Term should be running before we start this app
  160. Menu();
  161.  
  162. // Blink to inform we are active
  163. InformUser();
  164.  
  165. while (loop)
  166. {
  167. _readbuffer = new GTC_SS.Buffer(2);
  168.  
  169. if (_inStream.Read(_readbuffer, _readbuffer.Capacity, GTC_SS.InputStreamOptions.None) > 0)
  170. {
  171. byte[] x = _readbuffer.Data;
  172. string s = x.GetValue(0).ToString();
  173. SendMsg(" String length = " + s.Length.ToString());
  174.  
  175. string hex = BitConverter.ToString(x);
  176. hex = hex.TrimEnd(charsToTrim); // Remove the ending -00
  177.  
  178. SendMsg(" Char is ");
  179. _outStream.Write(_readbuffer);
  180.  
  181. SendMsg(" Dec " + s + " Hex " + hex + "\r\n"); // ECHO character as decimal
  182.  
  183. // Q ascii - 81 Dec , 51 Hex
  184. // q ascii - 113 Dec , 71 Hex
  185. if (s == "81") // Is character received a uppercase Q?
  186. {
  187. SendMsg("\r\n !!! Upper case Q received. Exit while loop.");
  188. SendMsg("\r\nReset the board.\r\n");
  189. loop = false;
  190. }
  191. //
  192.  
  193. if (s == "27") //Char ec - 27 Dec , 1B Hex
  194. {
  195. SendMsg("\r\nThe esc (Escape) key was pressed\r\n");
  196. SendMsg("Duty Cycle text will not be shown.\r\n");
  197. sendDutyCycleText = false;
  198. }
  199. //
  200. if (s == "49") //Char 1 - 49 Dec , 31 Hex
  201. {
  202. SendMsg("\r\nThe number 1 key was pressed\r\n");
  203. ToggleRed();
  204. }
  205. //
  206. if (s == "109") //Char m - 109 Dec , 6D Hex
  207. {
  208. Menu();
  209. }
  210. //
  211. }
  212. //
  213.  
  214. if (LDR0.Read() == GpioPinValue.Low)
  215. {
  216. SendMsg("\r\nLDR0.Read()\r\n");
  217. DimtoBrightPWM();
  218. }
  219. //
  220. if (LDR1.Read() == GpioPinValue.Low)
  221. {
  222. SendMsg("\r\nLDR1.Read()\r\n");
  223. BrighttoDimPWM();
  224. }
  225. //
  226.  
  227. // External button - With 10K pullup - Ground on button press
  228. // Uses 40 pin connector D52 (Pin 37)
  229. if (PA8.Read() == GpioPinValue.Low)
  230. {
  231. SendMsg("\r\nPA8.Read() External button was pressed.\r\n");
  232. PWMBlue();
  233. }
  234. //
  235.  
  236. Thread.Sleep(50);
  237. }
  238. // while
  239. }
  240. // Main
  241.  
  242.  
  243. // On LDR0.Read()
  244. private static void DimtoBrightPWM()
  245. {
  246. SendMsg("\r\nDim to Bright Green\r\n");
  247. InformUser();
  248. Thread.Sleep(2000);
  249.  
  250. // Only if first instance of DimtoBrightPWM() or BrighttoDimPWM()
  251. if (!isactive)
  252. {
  253. // All PWM channels use the same frequency.
  254. GHIElectronics.TinyCLR.Devices.Pwm.PwmController controller = GHIElectronics.TinyCLR.Devices.Pwm.PwmController.GetDefault();
  255.  
  256. // PwmController.SetDesiredFrequency must be called before any call to PwmController.OpenPin.
  257. controller.SetDesiredFrequency(50); // 50 Hz
  258. pwm5 = controller.OpenPin(FEZPandaIII.PwmOutput.D5); // 8 pin header PWM D5
  259. pwm5.Start();
  260. // PwmPin.SetActiveDutyCyclePercentage must be called after Start.
  261. pwm5.SetActiveDutyCyclePercentage(0.5); // set duty cycle to 50%
  262.  
  263. isactive = true;
  264. }
  265. //
  266.  
  267. if (!sendDutyCycleText)
  268. {
  269. SendMsg("\r\nWorking.... DutyCycle % will not be sent\r\n");
  270. }
  271. //
  272.  
  273. for (int i = 100; i >= 1; i--)
  274. {
  275. double DCP = (i * 0.01);
  276. pwm5.SetActiveDutyCyclePercentage(DCP);
  277. Thread.Sleep(20);
  278.  
  279. // Show current DutyCycle value
  280. if (sendDutyCycleText)
  281. {
  282. s = DCP.ToString("F");
  283. SendMsg(" % " + s + "\r\n"); // Show current value
  284. }
  285. //
  286. Thread.Sleep(50);
  287. }
  288. //
  289.  
  290. SendMsg("Dim to Bright Finished\r\n");
  291. InformUser();
  292. }
  293. //
  294.  
  295. // On LDR1.Read()
  296. private static void BrighttoDimPWM()
  297. {
  298. SendMsg("\r\nBright to Dim Green\r\n");
  299. InformUser();
  300. Thread.Sleep(2000);
  301.  
  302. // Only if first instance of DimtoBrightPWM() or BrighttoDimPWM()
  303. if (!isactive)
  304. {
  305. // All PWM channels use the same frequency
  306. GHIElectronics.TinyCLR.Devices.Pwm.PwmController controller = GHIElectronics.TinyCLR.Devices.Pwm.PwmController.GetDefault(); //Pin count 26
  307.  
  308. // PwmController.SetDesiredFrequency must be called before any call to PwmController.OpenPin.
  309. controller.SetDesiredFrequency(50); // 50 Hz
  310. pwm5 = controller.OpenPin(FEZPandaIII.PwmOutput.D5); // 8 pin header PWM D5
  311. pwm5.Start();
  312. // PwmPin.SetActiveDutyCyclePercentage must be called after Start.
  313. pwm5.SetActiveDutyCyclePercentage(0.5); // set duty cycle to 50%
  314.  
  315. isactive = true;
  316. }
  317. //
  318.  
  319. if (!sendDutyCycleText)
  320. {
  321. SendMsg("\r\nWorking.... DutyCycle % will not be sent\r\n");
  322. }
  323. //
  324.  
  325. for (int i = 0; i <= 100; i++)
  326. {
  327. double DCP = (i * 0.01);
  328. pwm5.SetActiveDutyCyclePercentage(DCP);
  329. Thread.Sleep(20);
  330.  
  331. // Show current DutyCycle value
  332. if (sendDutyCycleText)
  333. {
  334. s = DCP.ToString("F");
  335. SendMsg(" % " + s + "\r\n"); // Show current value
  336. }
  337. //
  338. Thread.Sleep(50);
  339. }
  340. //
  341.  
  342. SendMsg("Bright to Dim Finished\r\n");
  343. InformUser();
  344. }
  345. //
  346.  
  347. // On external button press. Using Blue LED
  348. private static void PWMBlue()
  349. {
  350. SendMsg("\r\nPWM Blue\r\n");
  351. InformUser();
  352. Thread.Sleep(1000);
  353.  
  354. // Only if first instance of DimtoBrightPWMBlue() or BrighttoDimPWMBlue()
  355. if (!isactiveBlue)
  356. {
  357. // All PWM channels use the same frequency. 8 pin header
  358. GHIElectronics.TinyCLR.Devices.Pwm.PwmController controller = GHIElectronics.TinyCLR.Devices.Pwm.PwmController.GetDefault();
  359.  
  360. // PwmController.SetDesiredFrequency must be called before any call to PwmController.OpenPin.
  361. controller.SetDesiredFrequency(50); // 50 Hz
  362.  
  363. pwm8 = controller.OpenPin(FEZPandaIII.PwmOutput.D6);
  364. pwm8.Start();
  365.  
  366. // PwmPin.SetActiveDutyCyclePercentage must be called after Start.
  367. pwm8.SetActiveDutyCyclePercentage(0.5); // set duty cycle to 50%
  368.  
  369. isactiveBlue = true;
  370. }
  371. //
  372.  
  373. Thread.Sleep(500);
  374.  
  375. SendMsg("\r\nDim to Bright Blue LED\r\n");
  376. if (!sendDutyCycleText)
  377. {
  378. SendMsg("\r\nWorking.... DutyCycle % will not be sent\r\n");
  379. }
  380. //
  381.  
  382. InformUser();
  383. Thread.Sleep(1000);
  384.  
  385. for (int i = 100; i >= 1; i--)
  386. {
  387. double DCP = (i * 0.01);
  388. pwm8.SetActiveDutyCyclePercentage(DCP);
  389. Thread.Sleep(20);
  390.  
  391. //100 * 0.01 ToString("F") format = 1.0z < I have no idea where the z comes from.
  392.  
  393. // Show current DutyCycle value
  394. if (sendDutyCycleText)
  395. {
  396. s = DCP.ToString("F");
  397. SendMsg(" % " + s + "\r\n"); // Show current value
  398. }
  399. //
  400. Thread.Sleep(50);
  401. }
  402. //
  403.  
  404. SendMsg("\r\nBright to Dim Blue LED\r\n");
  405. InformUser();
  406. Thread.Sleep(1000);
  407.  
  408. if (!sendDutyCycleText)
  409. {
  410. SendMsg("\r\nWorking.... DutyCycle % will not be sent\r\n");
  411. }
  412. //
  413.  
  414. for (int i = 0; i <= 100; i++)
  415. {
  416. double DCP = (i * 0.01);
  417. pwm8.SetActiveDutyCyclePercentage(DCP);
  418. Thread.Sleep(20);
  419.  
  420. // Show current DutyCycle value
  421. if (sendDutyCycleText)
  422. {
  423. s = DCP.ToString("F");
  424. SendMsg(" % " + s + "\r\n"); // Show current value
  425. }
  426. //
  427. Thread.Sleep(50);
  428. }
  429. //
  430.  
  431. SendMsg("PWMBlue Finished\r\n");
  432. InformUser();
  433. }
  434. //
  435.  
  436. private static void SerialSetup()
  437. {
  438. _com1.BaudRate = 9600;
  439. _com1.DataBits = 8;
  440. _com1.StopBits = GTC_DS.SerialStopBitCount.One;
  441. _com1.Parity = GTC_DS.SerialParity.None;
  442. _com1.Handshake = GTC_DS.SerialHandshake.None; //Flow
  443. // Set the read/write timeouts
  444. _com1.ReadTimeout = new System.TimeSpan(0, 0, 0, 0, 500); //500 milliseconds
  445. _com1.WriteTimeout = new System.TimeSpan(0, 0, 0, 0, 500); //500 milliseconds
  446. }
  447. //
  448.  
  449. private static void SendMsg(string theMsg)
  450. {
  451. // Send theMsg to the serial terminal.
  452. _msgbuffer = new GTC_SS.Buffer(System.Text.Encoding.UTF8.GetBytes(theMsg));
  453. _outStream.Write(_msgbuffer);
  454. }
  455. //
  456.  
  457. // Blinkies
  458. private static void InformUser()
  459. {
  460. for (int i = 0; i < 2; i++)
  461. {
  462. LED2.Write(GpioPinValue.High);
  463. Thread.Sleep(300);
  464. LED2.Write(GpioPinValue.Low);
  465. LED3.Write(GpioPinValue.High);
  466. Thread.Sleep(300);
  467. LED3.Write(GpioPinValue.Low);
  468. LED4.Write(GpioPinValue.High);
  469. Thread.Sleep(300);
  470. LED4.Write(GpioPinValue.Low);
  471. }
  472. //
  473. }
  474. //
  475.  
  476. private static void ToggleRed()
  477. {
  478. if (toggleRED)
  479. {
  480. PD1.Write(GpioPinValue.High);
  481. }
  482. else
  483. {
  484. PD1.Write(GpioPinValue.Low);
  485. }
  486. //
  487. toggleRED = !toggleRED;
  488. }
  489. //
  490.  
  491. private static void Menu()
  492. {
  493. SendMsg("\r\nType m for this menu\r\n");
  494. SendMsg("\r\nType a upper case Q to EXIT while loop.\r\n");
  495. SendMsg("\r\nType number 1 to toggle the Red LED.\r\n");
  496. SendMsg("\r\nType the esc (Escape) key to not send Duty Cycle text\r\n");
  497. SendMsg("\r\nPress button LDR0 to begin PWM Dim to Bright.\r\n");
  498. SendMsg("Press button LDR1 to begin PWM Bright to Dim.\r\n");
  499. SendMsg("\r\nPress external button to begin PWM for the Blue LED.\r\n");
  500. SendMsg("\r\nType a character for info about it.\n");
  501. }
  502. //
  503.  
  504. /* Not used
  505.   private static int tCount = 1;
  506.  
  507.   private static void MyTimerCallback(object state)
  508.   {
  509.   if (LDR0.Read() == GpioPinValue.Low)
  510.   {
  511.   AllLED_Off();
  512.   LED1.Write(GpioPinValue.High);
  513.   LED2.Write(GpioPinValue.High);
  514.   }
  515.   if (LDR1.Read() == GpioPinValue.Low)
  516.   {
  517.   AllLED_Off();
  518.   LED3.Write(GpioPinValue.High);
  519.   LED4.Write(GpioPinValue.High);
  520.   }
  521.   Thread.Sleep(500);
  522.   AllLED_Off();
  523.  
  524.   SendMsg("tick " + tCount.ToString() + "\r\n");
  525.  
  526.   tCount++;
  527.  
  528.   if (tCount > 60)
  529.   {
  530.   tCount = 1;
  531.   }
  532.   }
  533.   //
  534.  
  535.   private static void AllLED_Off()
  536.   {
  537.   LED1.Write(GpioPinValue.Low);
  538.   LED2.Write(GpioPinValue.Low);
  539.   LED3.Write(GpioPinValue.Low);
  540.   LED4.Write(GpioPinValue.Low);
  541.   }
  542.   //
  543.  
  544.   private static void AllLED_On()
  545.   {
  546.   LED1.Write(GpioPinValue.High);
  547.   LED2.Write(GpioPinValue.High);
  548.   LED3.Write(GpioPinValue.High);
  549.   LED4.Write(GpioPinValue.High);
  550.   }
  551.   //
  552.   */
  553.  
  554. } // Class
  555. } // Namespace