本帖最后由 tom 于 2013-8-6 22:45 编辑
/*
作者:tom
时间:2013年08月05日
IDE版本号:1.01
发表地址:www.geek-workshop.com
作用:使用OLED12864显示正弦余弦图形
*/
Miscrodruino-OLED12864正弦余弦图形显示
使用U8glib-V2.1
正弦表达式Y=A*sin(B*x)+C
其中A为正弦波的振幅,它的值越大,它波的最上点与最下点之间距离越大。
C 值越大,正弦波水平线越来越高。
Y Y轴的值
12864高度是64,一半是32,为了不至于看不见最高点,A值 选取31。
12864 屏幕显示128*64。
如是描绘一个水平轴在屏幕的中间线它的坐标是从(0,32)到 (128,32);竖直线是从(10,0)到 (10,64);为了能方便看到竖直轴,它的横坐标定位于10。
u8g.drawLine(0,32,128,32);//描绘坐标系X轴
u8g.drawLine(10,0,10,64);//描绘坐标系Y轴
再给它再上小箭头
//Y轴小箭头从(10,0)开始画线,X轴小箭头从(128,32)开始画线
u8g.drawLine(10,0,7,4);//描绘小箭头
u8g.drawLine(10,0,13,4);
u8g.drawLine(128,32,124,28);//另一个小箭头
u8g.drawLine(128,32,124,36);
这样看上去有点坐标的感觉了。
正弦函数程序段
为了能让起点落在绘出的坐标系的原点上 ,在使用drawPixel()正弦函数的横坐标值增加10
for (u8g_uint_t x=0;x<128;x++)
{
u8g_uint_t y;
y=32-31*sin(x*3.14/28);
//正弦波显示
u8g.drawPixel(x+10,y);
}
Arduino 代码如下:
- //使用mircoOLED绘制余弦图形
- #include "U8glib.h"
- U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are SCK = 13 and MOSI = 11)
- #define u8g_logo_width 40
- #define u8g_logo_height 24
- void draw_rect(void)//绘制一个十字形,水平线居中,竖线靠左
- {
- //将坐标的原点定在屏幕的中央,为了方便看见,Y轴定在X=10位置上
- //坐标的原点为(10,32)
- u8g.drawLine(0,32,128,32);//描绘坐标系X轴
- u8g.drawLine(10,0,10,64);//描绘坐标系Y轴
- //Y轴小箭头从(10,0)开始画线,X轴小箭头从(128,32)开始画线
- u8g.drawLine(10,0,7,4);//描绘小箭头
- u8g.drawLine(10,0,13,4);
- u8g.drawLine(128,32,124,28);//另一个小箭头
- u8g.drawLine(128,32,124,36);
- }
- void aa(void) //描绘余弦函数图形,用点函数
- {
- for (u8g_uint_t x=0;x<128;x++)
- {
- u8g_uint_t y;
- y=32-31*cos(x*3.14/28);
- //余弦波显示
- u8g.drawPixel(x+10,y);//点的坐标X值+10这样起点就正好到了坐标的交叉点
- }
- }
- void bb(void) //描绘正弦函数图形,用点函数
- {
- for (u8g_uint_t x=0;x<128;x++)
- {
- u8g_uint_t y;
- y=32-31*sin(x*3.14/28);
- //正弦波显示
- u8g.drawPixel(x+10,y);
- }
- }
- void draw(void) {
- draw_rect();//绘制坐标系----十字型
- // aa();//余弦图形
- bb();
- }
- void setup(void) {
- }
- void loop(void) {
- // picture loop
- u8g.firstPage();
- do {
- draw();
- }
- while( u8g.nextPage() );
- // rebuild the picture after some delay
- delay(500);
- }
复制代码
运行后的效果:
在屏幕在有一个由点组成的正弦/余弦图形在坐标上。
|