本文主要是介绍51控制ST7735画正弦函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主控芯片:STC8A8KS4A12
显示屏参数:1.8寸 128X160像素彩屏 ST7735驱动芯片
一共三个文件:main.C ST7735.C ST7735.H
如有疑惑发邮件至3456939016@QQ.COM,本人也是小白,欢迎打扰
如图,代码在图片后:
main.C#include <STC8.H>
#include <math.h>#include "ST7735.H"#define PI 3.14159265xdata unsigned char arr[160];//储存波形数组unsigned char Magnification;//定义倍率
unsigned char num;//定义函数初始点//计算正弦函数
unsigned char SinX(unsigned int angle)
{//计算公式float Result = sin(angle * PI / 180) * 62;//将负数上移if(Result > 0){return Result + 64;}else{return Result + 64;}
}
//将计算的结果存入数组
void Draw()
{unsigned char i;for(i = 0; i < 160; i++){arr[i] = SinX(i * Magnification + num);}
}
//ST7735写入点
void XXX()
{unsigned char i, j;ST7735_Set_Region(0, 159, 0, 127);//这里是横屏,先刷新0-160,然后再128-0for(i = 128; i > 0; i--){for(j = 0; j < 160; j++){if(arr[j] == i){ST7735_WriteData16Bit(0x07e0);}else{ST7735_WriteData16Bit(0x0000);}}}
}void main()
{P55 = 0;//LED亮ST7735_Initial();//ST7735初始化while(1){P55 = 1;//LED灭Draw();//计算并保存到数组P55 = 0;//LED亮XXX();//画函数到屏幕P55 = 1;//LED灭//按下P43开启改变正弦函数图像while(!P43){//这个将函数左移50个单位if(P64 == 0){while(!P64);num -= 50;}//这个将函数右移50个单位if(P65 == 0){while(!65);num += 50;}//将函数放大if(P66 == 0){while(!P66);Magnification++;}//将函数缩小if(P67 == 0){while(!P67);Magnification--;}}P55 = 0;//LED亮}
}
ST7735.C#include "ST7735.H"void delay_ms(unsigned char time)
{unsigned char i, j;for(i = 0; i < time; i++)for(j = 0; j < 250; j++);
}//向ST7745发送重置指令
void Reset()
{RES = 0;delay_ms(100);RES = 1;delay_ms(100);
}
//SPI总线传输8位数据
void SPI_WriteData(unsigned char Data)
{unsigned char i = 0;for(i = 0 ; i < 8; i++){if(Data & 0X80){SDA = 1;}else{SDA = 0;}SCL = 0;SCL = 1;Data <<= 1;}
}
//向ST7735传输8位指令
void ST7735_WriteCmd(unsigned char Data)
{CS = 0;DC = 0;SPI_WriteData(Data);CS = 1;
}
//向ST7735传输8位数据
void ST7735_WriteData(unsigned char Data)
{CS = 0;DC = 1;SPI_WriteData(Data);CS = 1;
}
//向ST7735传输16位数据
void ST7735_WriteData16Bit(unsigned int Data)
{CS = 0;DC = 1;SPI_WriteData(Data >> 8);SPI_WriteData(Data);CS = 1;
}
//向ST7735发送初始化指令集
void ST7735_Initial()
{Reset();ST7735_WriteCmd(0x11);//Sleep EXIT//10.2.1 FRMCTR1 (B1h): Frame Rate Control (In normal mode/ Full colors)ST7735_WriteCmd(0XB1);ST7735_WriteData(0X05);ST7735_WriteData(0X3C);ST7735_WriteData(0X3C);//10.2.2 FRMCTR2 (B2h): Frame Rate Control (In Idle mode/ 8-colors)ST7735_WriteCmd(0XB2);ST7735_WriteData(0X05);ST7735_WriteData(0X3C);ST7735_WriteData(0X3C);//10.2.3 FRMCTR3 (B3h): Frame Rate Control (In Partial mode/ full colors)ST7735_WriteCmd(0XB3);ST7735_WriteData(0X05);ST7735_WriteData(0X3C);ST7735_WriteData(0X3C);ST7735_WriteData(0X05);ST7735_WriteData(0X3C);ST7735_WriteData(0X3C);//10.2.4 INVCTR (B4h): Display Inversion ControlST7735_WriteCmd(0XB4);ST7735_WriteData(0X03);//10.2.5 PWCTR1 (C0h): Power Control 1ST7735_WriteCmd(0XC0);ST7735_WriteData(0X28);ST7735_WriteData(0X08);ST7735_WriteData(0X04);//10.2.6 PWCTR2 (C1h): Power Control 2ST7735_WriteCmd(0XC1);ST7735_WriteData(0XC0);//10.2.7 PWCTR3 (C2h): Power Control 3 (in Normal mode/ Full colors)ST7735_WriteCmd(0XC2);ST7735_WriteData(0X0D);ST7735_WriteData(0X0D);//10.2.8 PWCTR4 (C3h): Power Control 4 (in Idle mode/ 8-colors)ST7735_WriteCmd(0xC3);ST7735_WriteData(0x8D);ST7735_WriteData(0x2A);//10.2.8 PWCTR4 (C3h): Power Control 4 (in Idle mode/ 8-colors)ST7735_WriteCmd(0xC4);ST7735_WriteData(0x8D);ST7735_WriteData(0xEE);//10.2.10 VMCTR1 (C5h): VCOM Control 1ST7735_WriteCmd(0xC5);ST7735_WriteData(0x18);//改变显示方向以及颜色模式//10.1.29 MADCTL (36h): Memory Data Access ControlST7735_WriteCmd(0x36);/**正常竖屏显示COH*正常横屏显示A0H*/ST7735_WriteData(0xA0);//10.2.17 GMCTRP1 (E0h): Gamma (‘+’polarity) Correction Characteristics SettingST7735_WriteCmd(0xE0);ST7735_WriteData(0x04);ST7735_WriteData(0x22);ST7735_WriteData(0x07);ST7735_WriteData(0x0A);ST7735_WriteData(0x2E);ST7735_WriteData(0x30);ST7735_WriteData(0x25);ST7735_WriteData(0x2A);ST7735_WriteData(0x28);ST7735_WriteData(0x26);ST7735_WriteData(0x2E);ST7735_WriteData(0x3A);ST7735_WriteData(0x00);ST7735_WriteData(0x01);ST7735_WriteData(0x03);ST7735_WriteData(0x13);//10.2.18 GMCTRN1 (E1h): Gamma ‘-’polarity Correction Characteristics SettingST7735_WriteCmd(0xE1);ST7735_WriteData(0x04);ST7735_WriteData(0x16);ST7735_WriteData(0x06);ST7735_WriteData(0x0D);ST7735_WriteData(0x2D);ST7735_WriteData(0x26);ST7735_WriteData(0x23);ST7735_WriteData(0x27);ST7735_WriteData(0x27);ST7735_WriteData(0x25);ST7735_WriteData(0x2D);ST7735_WriteData(0x3B);ST7735_WriteData(0x00);ST7735_WriteData(0x01);ST7735_WriteData(0x04);ST7735_WriteData(0x13);/**9.8.2 8-bit Data Bus for 12-bit/Pixel (RGB 4-4-4-bit Input), 4K-Colors, 3AH= “03h”*9.8.3 8-bit Data Bus for 16-bit/Pixel (RGB 5-6-5-bit Input), 65K-Colors, 3AH= “05h”*9.8.4 8-bit Data Bus for 18-bit/Pixel (RGB 6-6-6-bit Input), 262K-Colors, 3AH= “06h”*/ST7735_WriteCmd(0x3A);ST7735_WriteData(0x05);//10.1.19 DISPON (29h): Display OnST7735_WriteCmd(0x29);BLK = 1;
}
//设置ST7735显示范围
void ST7735_Set_Region(unsigned char XStart, unsigned char XEnd, unsigned char YStart, unsigned char YEnd)
{//10.1.20 CASET (2Ah): Column Address SetST7735_WriteCmd(0x2A);ST7735_WriteData(0x02);ST7735_WriteData(XStart);ST7735_WriteData(0x02);ST7735_WriteData(XEnd);//10.1.21 RASET (2Bh): Row Address SetST7735_WriteCmd(0x2B);ST7735_WriteData(0x01);ST7735_WriteData(YStart);ST7735_WriteData(0x01);ST7735_WriteData(YEnd);//10.1.21 RASET (2Bh): Row Address SetST7735_WriteCmd(0x2C);}
ST7735.H#include <STC8.H>#ifndef __ST7735_H__
#define __ST7735_H__//端口定义
sbit SCL = P3 ^ 2;
sbit SDA = P3 ^ 3;
sbit RES = P6 ^ 0;
sbit DC = P6 ^ 1;
sbit CS = P6 ^ 2;
sbit BLK = P6 ^ 3;
//端口定义-结束//常用颜色定义
#define RED 0xf800
#define GREEN 0x07e0
#define BLUE 0x001f
#define WHITE 0xffff
#define BLACK 0x0000
//常用颜色定义-结束//向ST7745发送重置指令
void Reset();
//SPI总线传输8位数据
void SPI_WriteData(unsigned char Data);
//向ST7735传输8位指令
void ST7735_WriteCmd(unsigned char Data);
//向ST7735传输8位数据
void ST7735_WriteData(unsigned char Data);
//向ST7735传输16位数据
void ST7735_WriteData16Bit(unsigned int Data);
//向ST7735发送初始化指令集
void ST7735_Initial();
//设置ST7735显示范围
void ST7735_Set_Region(unsigned char XStart, unsigned char YStart, unsigned char XEnd, unsigned char YEnd);#endif
这篇关于51控制ST7735画正弦函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!