这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» 嵌入式开发» STM32» 余弦的STM32 L053开发板试用记录贴【更新中】

共18条 2/2 1 2 跳转至
工程师
2016-02-25 23:08:53 打赏
11楼

先从移植Arduino小制作开始更吧,我发现我的程序非常多,如果移植每个程序都做大量查找替换操作,那会非常繁琐。

还好C语言很灵活并且有强大的宏功能。

这是mbed上的BLink程序:

而这是Arduino Blink

我先创建了空模板项目

函数实现:

已有函数

Arduino程序复制过来加个头尾

头实现

其中pinMode是假函数,这样就能在移植时做最少修改了,加了头尾和宏定义引脚即可。

Delay函数也可以用宏替换实现,但是考虑到还有wait()函数,可以再定义个float参数的delay(float sec),所以我用函数实现。

编译运行通过。。。。。


工程师
2016-02-25 23:12:41 打赏
12楼

Blink过后,我移植了一个显示温度湿度的LED屏小制作。


main.cpp


#include "mbed.h" #include "Arduino.h" Serial serial(USBTX, USBRX); #include "LED_Array.h" #include "Dht11.h" Dht11 DHT11(D7); unsigned char RedData[70] = { 0 }; unsigned char GreenData[70] = { 0 }; unsigned char BlueData[70] = { 0 }; unsigned char humb = 0; unsigned char temp = 0; unsigned char old_humb = 0; unsigned char old_temp = 0; unsigned char count = 0; void ColouredPos(int pos, char color, char value) { boolean R = color & 0x01; boolean G = color & 0x02; boolean B = color & 0x04; //根据色权转二进制以对指定位置制定色板上色 if (R) DotToData(RedData, pos, value); else DotToData(RedData, pos, 0x10); if (G) DotToData(GreenData, pos, value); else DotToData(GreenData, pos, 0x10); if (B) DotToData(BlueData, pos, value); else DotToData(BlueData, pos, 0x10); } void ColouredPos(int pos, char color) { //模板套用简化 char value = 12; switch (pos) { case 0: value = 12; break; case 5: value = 13; break; case 2: value = 14; break; case 7: value = 15; break; } ColouredPos(pos, color, value); } void ColouredValue(int value, char color, boolean temp) { unsigned char LeftPos = 1; unsigned char RightPos = 4; if (!temp) { LeftPos = 3; RightPos = 6; } ColouredPos(LeftPos, color, value / 10 % 10); ColouredPos(RightPos, color, value % 10); } void testOne() { for (int i = 0; i < 8; i++) { ColouredPos(0, i); ColouredPos(5, i); ColouredPos(2, i); ColouredPos(7, i); ColouredValue(9, i, 1); ColouredValue(51, i, 0); for (int n = 80; n > 1; n--) { Write_LED_Array(RedData, RED); Write_LED_Array(GreenData, GREEN); Write_LED_Array(BlueData, BLUE); } } } void testTwo() { ColouredPos(0, RED); ColouredPos(5, YELLO); ColouredPos(2, BLUE); ColouredPos(7, YELLO); ColouredValue(16, CYAN, 1); ColouredValue(51, GREEN, 0); while (1) { Write_LED_Array(RedData, RED); Write_LED_Array(GreenData, GREEN); Write_LED_Array(BlueData, BLUE); } } void getDHT11() { count++; int chk; do { chk = DHT11.read(); } while (chk == DHTLIB_OK); if (count == 100) { count = 0; old_humb = humb; old_temp = temp; } humb = DHT11.getHumidity(); temp = DHT11.getFahrenheit(); if (humb >= old_humb) ColouredValue(humb, GREEN, 0); else ColouredValue(humb, CYAN, 0); if (temp >= old_temp) ColouredValue(temp, GREEN, 1); else ColouredValue(temp, CYAN, 1); serial.printf("Humidity (%%): %d\n",humb); serial.printf("Temperature (oC): %d\n\n",temp); // Serial.print("Humidity (%): "); // Serial.println(humb, DEC); // Serial.print("Temperature (oC): "); // Serial.println(temp, DEC); // Serial.println(); } void setup() { Init_LED_Array(); //Serial.begin(9600); ColouredPos(0, BLUE); ColouredPos(5, BLUE); ColouredPos(2, BLUE); ColouredPos(7, BLUE); ColouredValue(0, CYAN, 1); ColouredValue(0, GREEN, 0); //t.every(10000, getDHT11); } void loop() { //t.update(); getDHT11(); for(long i=0; i<100; i++) { Write_LED_Array(GreenData, GREEN); Write_LED_Array(BlueData, BLUE); } serial.printf("LOOP!!!\n"); } int main() { setup(); while(1) { loop(); } }



工程师
2016-02-25 23:14:18 打赏
13楼

LED_Array.h & cpp

#ifndef LED_Array_H #define LED_Array_H #define HaltSize 1 #define LIGHT 160 #define R0 D2 #define R1 D3 #define G0 D8 #define G1 D9 #define L0 D11 #define L1 D12 #define STB A4//RCLK STCP//STB 正脉冲(几十纳秒)更新输出 #define CLK A5//SCLK SHCP//CLK 上升沿位移输入数据 #define EN D13//OE 低电平开启 #define RowA A0 #define RowB A1 #define RowC A2 #define RowD A3 /*色权 * 基本: * 0——无色 * 1——红色 * 2——绿色 * 4——蓝色 * 扩展: * 3——红绿:黄 * 5——红蓝:桃 * 6——蓝绿:青、 * 7——白 */ #define OFF 0 #define RED 1 #define GREEN 2 #define BLUE 4 #define PINK 5 #define YELLO 3 #define CYAN 6 #define WHITE 7 const unsigned char Dot[] = { 0x00, 0xFE, 0xC6, 0xCE, 0xD6, 0xE6, 0xC6, 0xFE,//G:\WPad\0.BMP0 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C,//G:\WPad\1.BMP0 0x00, 0xFE, 0x06, 0x06, 0xFE, 0xC0, 0xC0, 0xFE,//G:\WPad\2.BMP0 0x00, 0xFE, 0x06, 0x06, 0xFE, 0x06, 0x06, 0xFE,//G:\WPad\3.BMP0 0x00, 0xC6, 0xC6, 0xC6, 0xFE, 0x06, 0x06, 0x06,//G:\WPad\4.BMP0 0x00, 0xFE, 0xC0, 0xC0, 0xFE, 0x06, 0x06, 0xFE,//G:\WPad\5.BMP0 0x00, 0xFE, 0xC0, 0xC0, 0xFE, 0xC6, 0xC6, 0xFE,//G:\WPad\6.BMP0 0x00, 0xFE, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x60,//G:\WPad\7.BMP0 0x00, 0xFE, 0xC6, 0xC6, 0xFE, 0xC6, 0xC6, 0xFE,//G:\WPad\8.BMP0 0x00, 0xFE, 0xC6, 0xC6, 0xFE, 0x06, 0x06, 0xFE,//G:\WPad\9.BMP0 0x00, 0x10, 0x38, 0x7C, 0xFE, 0x00, 0x00, 0x00,//G:\WPad\up.BMP0 0x00, 0x00, 0x00, 0xFE, 0x7C, 0x38, 0x10, 0x00,//G:\WPad\down.BMP0 0x00, 0x18, 0x24, 0x24, 0x34, 0x34, 0x34, 0x18,//G:\WPad\temp2.BMP0 0x00, 0xE6, 0xA8, 0xE8, 0x08, 0x08, 0x08, 0x06,//G:\WPad\temp.BMP0 0x00, 0x10, 0x10, 0x38, 0x7C, 0x7C, 0x7C, 0x38,//G:\WPad\humb2.BMP0 0x00, 0xE2, 0xA4, 0xE8, 0x10, 0x2E, 0x4A, 0x8E,//G:\WPad\humb.BMP0 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//0x00 };//字模 void Write_595(unsigned char Data, PinName PIN_595); //this is the base driver of 74HC595 void Init_LED_Array(void); //put this function into setup() void Display(); //you can modify this function to display contents that you what to show void Write_LED_Array(unsigned char *Data, unsigned char color); //this is the base driver of LED array void DotToData(unsigned char *Data, unsigned char Pos, unsigned char Draw); //将字模放到数组中指定屏幕区域 #endif

#include "mbed.h" #include "Arduino.h" #include "LED_Array.h" void Display()//you can modify this function to display contents that you what to show { // unsigned char i; // for (i = 80; i > 1; i--) // { // Write_LED_Array(0, &cod1[0x0000], R0); //Write_LED_Array(pos,data,color); // Write_LED_Array(1, &cod1[64], R1); // } // for (i = 80; i > 1; i--) // { // Write_LED_Array(0, &cod1[128], R0); //Write_LED_Array(pos,data,color); // Write_LED_Array(1, &cod1[192], R1); // } } void Init_LED_Array(void)//put this function into setup() { pinMode(R0, OUTPUT); pinMode(R1, OUTPUT); pinMode(G0, OUTPUT); pinMode(G1, OUTPUT); pinMode(L0, OUTPUT); pinMode(L1, OUTPUT); digitalWrite(R0, LOW); digitalWrite(R1, LOW); digitalWrite(G0, LOW); digitalWrite(G1, LOW); digitalWrite(L0, LOW); digitalWrite(L1, LOW); pinMode(STB, OUTPUT); pinMode(CLK, OUTPUT); pinMode(EN, OUTPUT); pinMode(RowA, OUTPUT); pinMode(RowB, OUTPUT); pinMode(RowC, OUTPUT); pinMode(RowD, OUTPUT); digitalWrite(EN, HIGH); digitalWrite(STB, LOW); } void Write_595(unsigned char Data, PinName PIN_595) //this is the base driver of 74HC595 { digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x80)); digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x40)); digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x20)); digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x10)); digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x08)); digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x04)); digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x02)); digitalWrite(CLK, HIGH); digitalWrite(CLK, LOW); digitalWrite(PIN_595, (Data & 0x01)); digitalWrite(CLK, HIGH); } void Write_LED_Array(unsigned char *Data, unsigned char color) //this is the base driver of LED array { unsigned char *pTemp; unsigned char row; PinName DataPin = R0; PinName DataPin2 = R1; if (color == GREEN) { DataPin = G0; DataPin2 = G1; } else if (color == BLUE) { DataPin = L0; DataPin2 = L1; } for (row = 0; row < 16; row++) { digitalWrite(EN, HIGH); pTemp = (Data + row * 2); if (HaltSize && row >= 8) { Write_595(*pTemp, DataPin2); Write_595(*(pTemp + 1), DataPin2); Write_595(*(pTemp += 32), DataPin2); Write_595(*(pTemp + 1), DataPin2); } else { Write_595(*pTemp, DataPin); Write_595(*(pTemp + 1), DataPin); Write_595(*(pTemp += 32), DataPin); Write_595(*(pTemp + 1), DataPin); } digitalWrite(STB, HIGH); delayMicroseconds(1); digitalWrite(STB, LOW); delayMicroseconds(1); digitalWrite(RowA, (row & 0x01)); //set display line digitalWrite(RowB, (row & 0x02)); //Sequential scan digitalWrite(RowC, (row & 0x04)); if (!HaltSize) digitalWrite(RowD, (row & 0x08)); digitalWrite(EN, LOW); delayMicroseconds(LIGHT); } } void DotToData(unsigned char *Data, unsigned char Pos, unsigned char Draw) { //将字模放到数组中指定屏幕区域 /*区域 Pos: 0 1 4 5 2 3 6 7 */ const unsigned char *p = &Dot[Draw * 8];//字模编号取得指针 //区域处理 char n = Pos / 2; Pos = Pos % 2; for (char i = n * 16; i < (n + 1) * 16; i += 2) { if (Pos) Data[i + 1] = *(p++); else Data[i] = *(p++); } }





工程师
2016-02-25 23:15:19 打赏
14楼

Arduino.h & cpp



#ifndef Arduino_H #define Arduino_H #define HIGH 1 #define LOW 0 #define pinMode(PinName,Mode) #define digitalWrite(PinName,Value) DigitalOut(PinName,Value) #define digitalRead(PinName,Value) DigitalIn(PinName,Value) #define delayMicroseconds(us) wait_us(us); typedef bool boolean; //#define print void delay(int ms); #endif




#include "mbed.h" #include "Arduino.h" void delay(int ms) { wait_ms(ms); }



工程师
2016-02-25 23:17:06 打赏
15楼

Dth11.h & cpp


#ifndef DHT11_H #define DHT11_H #include "mbed.h" #define DHTLIB_OK 0 #define DHTLIB_ERROR_CHECKSUM -1 #define DHTLIB_ERROR_TIMEOUT -2 /** Class for the DHT11 sensor. * * Example: * @code * #include "mbed.h" * #include "Dht11.h" * * Serial pc(USBTX, USBRX); * Dht11 sensor(PTD7); * * int main() { * sensor.read() * pc.printf("T: %f, H: %d\r\n", sensor.getFahrenheit(), sensor.getHumidity()); * } * @endcode */ class Dht11 { public: /** Construct the sensor object. * * @param pin PinName for the sensor pin. */ Dht11(PinName const &p); /** Update the humidity and temp from the sensor. * * @returns * 0 on success, otherwise error. */ int read(); /** Get the temp(f) from the saved object. * * @returns * Fahrenheit float */ float getFahrenheit(); /** Get the temp(c) from the saved object. * * @returns * Celsius int */ int getCelsius(); /** Get the humidity from the saved object. * * @returns * Humidity percent int */ int getHumidity(); private: /// percentage of humidity int _humidity; /// celsius int _temperature; /// pin to read the sensor info on DigitalInOut _pin; /// times startup (must settle for at least a second) Timer _timer; }; #endif


#include "Dht11.h" Dht11::Dht11(PinName const &p) : _pin(p) { // Set creation time so we can make // sure we pause at least 1 second for // startup. _timer.start(); _temperature = 0; _humidity = 0; } int Dht11::read() { // 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; // Verify sensor settled after boot while(_timer.read_ms() < 1500) {} _timer.stop(); // Notify it we are ready to read _pin.output(); _pin = 0; wait_ms(18); _pin = 1; wait_us(40); _pin.input(); // ACKNOWLEDGE or TIMEOUT unsigned int loopCnt = 10000; while(_pin == 0) if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; loopCnt = 10000; while(_pin == 1) 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(_pin == 0) if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; //unsigned long t = micros(); Timer t; t. start(); loopCnt = 10000; while(_pin == 1) if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT; if (t.read_us() > 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; } float Dht11::getFahrenheit() { return((_temperature * 1.8) + 32); } int Dht11::getCelsius() { return(_temperature); } int Dht11::getHumidity() { return(_humidity); }


工程师
2016-02-25 23:22:07 打赏
16楼

源代码打包:

WeatherPad_zip_nucleo_l053r8.zip(LED屏连接方式在宏定义中)

运行后串口输出

输出没有换行,估计是串口工具有问题

实物效果:


移植完这个小玩意,感觉mbed平台特容易上手,非常适合新手入门



菜鸟
2016-05-21 14:48:08 打赏
17楼

兄弟球GH60 论坛定制版的pcb文件,想自己做一块板子。


专家
2016-05-21 17:45:21 打赏
18楼
哇,著名创客在这呢啊

共18条 2/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]