aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlynxeyed-atsu <lynxeyed@xj9.so-net.ne.jp>2012-05-24 06:25:00 +0900
committerlynxeyed-atsu <lynxeyed@xj9.so-net.ne.jp>2012-05-24 06:25:00 +0900
commite60b2d72d02f0200d75bf10932b8cacbb7d0f809 (patch)
tree4723d8b553674be7c84e55c3a5d0418330b92dae
parent2d48de714976bf9d8dcdb43d35370f182eb47b36 (diff)
fixed print class and PWM function
-rwxr-xr-xsrc/core/Print.cpp2
-rwxr-xr-xsrc/core/analogio.cpp136
-rwxr-xr-xsrc/core/analogio.h42
-rwxr-xr-xsrc/core/gpio.h3
-rwxr-xr-xsrc/core/libmary.h2
5 files changed, 183 insertions, 2 deletions
diff --git a/src/core/Print.cpp b/src/core/Print.cpp
index d2c3848..b26848f 100755
--- a/src/core/Print.cpp
+++ b/src/core/Print.cpp
@@ -17,7 +17,7 @@ void Print::print(int val, RADIX_FORMAT format)
write((char)(val & 0xff));
return;
}
- char s[32];
+ char s[33];
int i,j;
i=j=0; // header of strings
if (val<0)
diff --git a/src/core/analogio.cpp b/src/core/analogio.cpp
new file mode 100755
index 0000000..8ac240d
--- /dev/null
+++ b/src/core/analogio.cpp
@@ -0,0 +1,136 @@
+#include "analogio.h"
+
+#define PWM_MAX_PINS 2
+static int pwm_pin_num[PWM_MAX_PINS]; // PWM出力に使うピン番号を格納する
+static int pwm_duty_value[PWM_MAX_PINS]; // それぞれのピンのデューティを格納する
+static int pwm_already_inuse = 0; // すでに登録しているピンの数
+static int pwm_init_done = 0;
+//static int pwm_interrpt_loopnum[PWM_MAX_PINS]; // ユークリッドの互除法を使うので割り込み回数をカウント
+
+
+void analogWrite(int pinnum, int value)
+{
+ int i;
+ int pwm_using_num = -1; // PWMを割り当てているピン
+
+ if(pwm_init_done == 0)
+ {
+ setup_TIMER16_0();
+ pwm_init_done = 1;
+ }
+
+ for(i = 0; i < PWM_MAX_PINS; i++)
+ {
+ if(pwm_pin_num[i] == pinnum) // すでに登録されているピン
+ { // デューティをセット
+ pwm_using_num = i;
+ pwm_duty_value[i] = value;
+ if(value == -1)pwm_already_inuse--;
+ break;
+ }
+ }
+
+ if(pwm_using_num == -1)//新規登録の場合
+ {
+ if(PWM_MAX_PINS > pwm_already_inuse) // まだ登録可能
+ {
+ for(i = 0; i < PWM_MAX_PINS; i++)
+ {
+ if((pwm_duty_value[i] == -1)&&(value != -1)) // 登録されていないピン
+ {
+ pwm_using_num = i;
+ pwm_pin_num[i] = pinnum; // ピン番号登録
+
+ pwm_duty_value[i] = value; // dutyセット
+ pwm_already_inuse++; // 使用中のピン数を+1
+ pinMode(pwm_pin_num[i],OUTPUT);
+ break;
+ }
+ }
+ }
+
+ }
+
+ switch(pwm_using_num)
+ {
+ case 0: LPC_TMR16B0->MR1 = pwm_duty_value[i]; break;
+ case 1: LPC_TMR16B0->MR2 = pwm_duty_value[i]; break;
+ //case 2: LPC_TMR16B1->MR1 = pwm_duty_value[i]; break;
+ //case 3: LPC_TMR16B1->MR2 = pwm_duty_value[i]; break;
+ default: break;
+ }
+
+ //pwm_eucledean_algorithm(pwm_using_pins);// 互除法演算ルーチンへ
+ return;
+}
+
+void pwm_eucledean_algorithm(int pwm_using_pins)
+{
+ // reserved for future use.
+}
+
+//low level functions for software PWM
+void setup_TIMER16_0(void)
+{
+ LPC_SYSCON->SYSAHBCLKCTRL |= (1U << 7);
+
+ // 1周期490Hzで分解能8bit => 約125kHz (125kHz = 48MHz / 382)
+ LPC_TMR16B0->PR = 382; // Pre-Scaler
+
+ LPC_TMR16B0->MR3 = 255; // Interval
+ LPC_TMR16B0->MR2 = 200;
+ LPC_TMR16B0->MR1 = 200;
+ // Interval Interrupt Operation
+ // Clear TC when MR3 match
+ // Interrupt when MR1 reset /MR2 reset /MR3 match
+ LPC_TMR16B0->MCR = ((3U << 9)|(1U << 6)|(1U << 3));
+
+ // Enable Interrupt
+ NVIC_EnableIRQ(TIMER_16_0_IRQn);
+ NVIC_SetPriority(TIMER_16_0_IRQn, 3);
+
+ // Start Timer
+ LPC_TMR16B0->TCR = 1;
+
+ for(int i=0; i < PWM_MAX_PINS; i++) // まだ登録可能
+ {
+ pwm_duty_value[i] = -1; // 登録されていないピン
+ }
+}
+
+//===========================
+// Timer16 Interrupt Handler
+//===========================
+void TIMER16_0_IRQHandler(void)
+{
+ if (LPC_TMR16B0->IR & (1U << 1)) //MR1INT = case0
+ {
+ //Do something
+ if(pwm_duty_value[0] != -1) digitalWrite(pwm_pin_num[0],LOW);
+ // Clear Flag
+ LPC_TMR16B0->IR = (1U << 1);
+ }
+
+ if (LPC_TMR16B0->IR & (1U << 2)) //MR2INT = case1
+ {
+ //Do something
+ if(pwm_duty_value[1] != -1) digitalWrite(pwm_pin_num[1],LOW);
+ // Clear Flag
+ LPC_TMR16B0->IR = (1U << 2);
+ }
+
+ else if(LPC_TMR16B0->IR & (1U << 3)) //MR3INT
+ {
+ //Do something
+ for(int i = 0; i < PWM_MAX_PINS; i++)
+ {
+ if(pwm_duty_value[i] != -1)
+ {
+ if(pwm_duty_value[i] > 1) digitalWrite(pwm_pin_num[i],HIGH);
+ }
+ }
+ // Clear Flag
+ LPC_TMR16B0->IR = (1U << 3);
+ }
+
+}
diff --git a/src/core/analogio.h b/src/core/analogio.h
new file mode 100755
index 0000000..caa7608
--- /dev/null
+++ b/src/core/analogio.h
@@ -0,0 +1,42 @@
+#ifndef ANALOGIO_H_
+#define ANALOGIO_H_
+/**************************************************************************//**
+ * @file libmary.h
+ * @brief Arduino-like library for eXodusino(LPC1114)
+ * @version v.0.50
+ * @date 3. May 2012.
+ * @author @@lynxeyed_atsu
+ * @note Copyright (c) 2011 Lynx-EyED's Klavier and Craft-works. <BR>
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions: <BR>
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.<BR>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ ******************************************************************************/
+
+#include "LPC11xx.h"
+#include "gpio.h"
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+void analogWrite(int pinnum, int value);
+//low level functions for software PWM
+void setup_TIMER16_0(void);
+void TIMER16_0_IRQHandler(void);
+
+#ifdef __cplusplus
+ }
+#endif
+#endif /* ANALOGIO_H_ */
diff --git a/src/core/gpio.h b/src/core/gpio.h
index b08c0c0..2046a1f 100755
--- a/src/core/gpio.h
+++ b/src/core/gpio.h
@@ -14,6 +14,9 @@
extern "C" {
#endif
+
+#define PORT_MAX_PINS 125 // Arduino/MARYで使うピン番号の最大の値
+
//#endif
//=======================================
// Define LPC_GPIO[4] (each top address)
diff --git a/src/core/libmary.h b/src/core/libmary.h
index 4df05c7..b49a339 100755
--- a/src/core/libmary.h
+++ b/src/core/libmary.h
@@ -50,7 +50,7 @@
#include "delay.h"
#include "gpio.h"
#include "uart.h"
-
+#include "analogio.h"
// init for arduino-like settings