Skip to content

Commit

Permalink
Uploads pcDuino Example Codes
Browse files Browse the repository at this point in the history
RT
  • Loading branch information
nightseas committed Nov 13, 2015
1 parent fac23b9 commit e22f6f4
Show file tree
Hide file tree
Showing 56 changed files with 3,798 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Chapter1. Arduino Introduction/1-1 led_test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/********************************************
程序名称:led_test
功能:Arduino控制LED范例代码
版本:v1.0
作者:懒兔子 <[email protected]>
*********************************************/
int led_pin = 13; // 定义13脚为LED引脚
void setup()
{
pinMode(led_pin, OUTPUT); // 设置LED引脚为输出模式
}
void loop()
{
digitalWrite(led_pin, HIGH); // 设置引脚为高电平,LED点亮
delay(1000); // 等待1s
digitalWrite(led_pin, LOW); // 设置引脚为高电平,LED熄灭
delay(1000); // 等待1s
}
37 changes: 37 additions & 0 deletions Chapter10. Motor Control/10-1 dc_fan_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/********************************************
程序名称:dc_fan_test
功能:直流风扇控制范例代码
版本:v1.0
作者:懒兔子 <[email protected]>
*********************************************/
//包含头文件
#include <core.h>

//宏定义及全局变量
int fanPin=10;//GPIO10连接风扇

//功能函数部分
//设置PWM占空比
void setDuty(int pin, int percent)
{
int duty = percent*255/100;
analogWrite(pin, duty);
}

//初始化设置部分
void setup()
{
pinMode(fanPin, OUTPUT);
}

//循环执行部分
void loop()
{
//从0-100%循环改变风扇占空比
for(int i=0;i<=100;i+=10)
{
setDuty(fanPin, i);
printf("Duty cycle of the fan is %d\%\n", i);
delay(3000);
}
}
106 changes: 106 additions & 0 deletions Chapter10. Motor Control/10-2 h_bridge_motor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/********************************************
程序名称:h_bridge_motor
功能:H桥驱动直流电机
版本:v1.0
作者:懒兔子 <[email protected]>
*********************************************/
//包含头文件
#include <core.h>

//宏定义及全局变量
//方向定义
#define FORWARD 0
#define BACKWARD 1
#define LEFT 2
#define RIGHT 4

//控制信号引脚定义
int pinInA1 = 8;
int pinInA2 = 11;
int pinEnA = 9;
int pinInB1 = 12;
int pinInB2 = 13;
int pinEnB = 10;


//功能函数部分
//设置两个电机专项控制小车方向
void motorSetDirection(int method)
{
switch(method)
{
//前进
case FORWARD:
digitalWrite(pinInA1, HIGH);
digitalWrite(pinInA2, LOW);
digitalWrite(pinInB1, LOW);
digitalWrite(pinInB2, HIGH);
break;

//后退
case BACKWARD:
digitalWrite(pinInA1, LOW);
digitalWrite(pinInA2, HIGH);
digitalWrite(pinInB1, HIGH);
digitalWrite(pinInB2, LOW);
break;

//左转
case LEFT:
digitalWrite(pinInA1, LOW);
digitalWrite(pinInA2, HIGH);
digitalWrite(pinInB1, LOW);
digitalWrite(pinInB2, HIGH);
break;

//右转
case RIGHT:
digitalWrite(pinInA1, HIGH);
digitalWrite(pinInA2, LOW);
digitalWrite(pinInB1, HIGH);
digitalWrite(pinInB2, LOW);
break;

default:
digitalWrite(pinInA1, LOW);
digitalWrite(pinInA2, LOW);
digitalWrite(pinInB1, LOW);
digitalWrite(pinInB2, LOW);
break;
}
}

//通过PWM占空比设置电机转速
void motorSetSpeed(int spead)
{
analogWrite(pinEnA, spead);
analogWrite(pinEnB, spead);
}

//初始化设置部分
void setup()
{
pinMode(pinInA1, OUTPUT);
pinMode(pinInA2, OUTPUT);
pinMode(pinInB1, OUTPUT);
pinMode(pinInB2, OUTPUT);
pinMode(pinEnA, OUTPUT);
pinMode(pinEnB, OUTPUT);
}

//循环执行部分
void loop()
{
//设置小车速度
motorSetSpeed(120);

//循环执行运动指令
motorSetDirection(FORWARD);
delay(3000);
motorSetDirection(BACKWARD);
delay(3000);
motorSetDirection(LEFT);
delay(3000);
motorSetDirection(RIGHT);
delay(3000);
}
49 changes: 49 additions & 0 deletions Chapter10. Motor Control/10-3 servo_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/********************************************
程序名称:servo_test
功能:舵机控制范例代码
版本:v1.0
作者:懒兔子 <[email protected]>
*********************************************/
//包含头文件
#include <core.h>

//宏定义及全局变量
#define PWM_PERIOD 20000 //PWM周期为20ms(20000us)
int servoPin = 0;

//功能函数部分
//设置高电平时间,范围:1000-2000us
void servoSetPulse(int pin, int pulse)
{
digitalWrite(pin, HIGH);
delayMicroseconds(pulse);
digitalWrite(pin, LOW);
delayMicroseconds(PWM_PERIOD - pulse);
}

//设置舵机转动角度,范围0-180°
void servoSetAngle(int pin, int angle)
{
if(angle >= 0 && angle <= 180)
servoSetPulse(pin, angle*11+1000);
}

//初始化设置部分
void setup()
{
pinMode(servoPin,OUTPUT);
}

//循环执行部分
void loop()
{
//0-180°之间循环转动舵机
for(int i=0;i<=180;i++)
{
servoSetAngle(servoPin, i);
}
for(int i=180;i>=0;i--)
{
servoSetAngle(servoPin, i);
}
}
56 changes: 56 additions & 0 deletions Chapter10. Motor Control/10-4 stepper_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/********************************************
程序名称:stepper_test
功能:步进电机驱动范例代码
版本:v1.0
作者:懒兔子 <[email protected]>
*********************************************/
//包含头文件
#include <core.h>

//宏定义及全局变量
//定义四相输入连接的GPIO
//A -- GPIO0
//B -- GPIO1
//C -- GPIO2
//D -- GPIO3
int pinA = 0;
int pinB = 1;
int pinC = 2;
int pinD = 3;

//功能函数部分

//初始化设置部分
void setup()
{
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
}

//循环执行部分
void loop()
{
//双四拍驱动
digitalWrite(pinA,1);
digitalWrite(pinB,0);
digitalWrite(pinC,0);
digitalWrite(pinD,1);
delay(2);
digitalWrite(pinA,1);
digitalWrite(pinB,1);
digitalWrite(pinC,0);
digitalWrite(pinD,0);
delay(2);
digitalWrite(pinA,0);
digitalWrite(pinB,1);
digitalWrite(pinC,1);
digitalWrite(pinD,0);
delay(2);
digitalWrite(pinA,0);
digitalWrite(pinB,0);
digitalWrite(pinC,1);
digitalWrite(pinD,1);
delay(2);
}
91 changes: 91 additions & 0 deletions Chapter11. Sensors/11-1 DHT11_Lib/dht11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// FILE: dht11.cpp
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// HISTORY:
// George Hadjikyriacou - Original version (??)
// Mod by SimKard - Version 0.2 (24/11/2010)
// Mod by Rob Tillaart - Version 0.3 (28/03/2011)
// + added comments
// + removed all non DHT11 specific code
// + added references
// Mod by Rob Tillaart - Version 0.4 (17/03/2012)
// + added 1.0 support
// Mod by Rob Tillaart - Version 0.4.1 (19/05/2012)
// + added error codes
//

#include "dht11.h"
#include <Arduino.h>

// Return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht11::read(int pin)
{
// BUFFER TO RECEIVE
uint8_t bits[5];
uint8_t cnt = 7;
uint8_t idx = 0;

// EMPTY BUFFER
for (int i=0; i< 5; i++) bits[i] = 0;


// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(36);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);

// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++)
{
loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

unsigned long t = micros();

loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}

// WRITE TO RIGHT VARS
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
humidity = bits[0];
temperature = bits[2];

uint8_t sum = bits[0] + bits[2];

if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
}
//
// END OF FILE
//
24 changes: 24 additions & 0 deletions Chapter11. Sensors/11-1 DHT11_Lib/dht11.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


#ifndef dht11_h
#define dht11_h



#define DHT11LIB_VERSION "0.4.1"

#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2

class dht11
{
public:
int read(int pin);
int humidity;
int temperature;
};
#endif
//
// END OF FILE
//
Loading

0 comments on commit e22f6f4

Please sign in to comment.