|
电设之F5529(1)UCS
电设之F5529(2)OLED(外设)
电设之F5529(3)SPI
电设之F5529(4)定时器
电设之F5529——我的核心系统板
前边是时钟,时钟搞定了,就差了数据什么的显示,就用OLED作为显示的外设
串口后边发吧,家里有事,几个月后再补充吧!
本人新手,差的东西还很多,希望大家多多指出不足,提出好的建议,相互学习!
这个是某宝买的OLED,自己写了一些函数,又在网上整理了一些精华的函数,字母、数字(10进制、16进制、浮点数。。。)、汉字(自己取模)、logo。。。。。。。
什么的都有,不扯了,直接上代码
工程上,先包含这些代码
- /************************************工程中请包含此部分代码******************************/
- /*
- * Stdint.h 该头文件 重定义了 unsigned char 、unsigned int 等等。。。
- */
- #include <stdint.h>
- /*
- * Macos : 系统延时,直接依据时钟定义
- */
- //#define CPU_F (1200000) //Normal
- #define CPU_F ((double)25000000) //25Mhz
- #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0) )//x MHz----us
- #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
- #define delay_s(x) __delay_cycles((long)(CPU_F*(double)x/1.0))
- /*******************End of*****工程中请包含此部分代码******************************/
复制代码 函数:外设OLED (需包含Font.H 该头文件,提供自定义字库)
说明;该函数为OLED 模拟SPI驱动
void LCD_Init(uint8_t Brightness);// 入口参数为屏幕亮度
硬件接口 定义在LCD.H里
先上源文件
- /*
- * LCD.c
- *
- * Created on: 2015年7月23日
- * Author: Clover
- */
- #include <LCD.h>//P1.2->D0 P1.3->D1 P1.4->RST P1.5->DC
- #include <msp430.h>
- #include "Font.h"
- const unsigned long numtab[]={
- 1,10,100,1000,10000,100000,1000000,10000000
- };
- /*********************LCD写数据************************************/
- void LCD_WrDat(uint8_t dat)
- {
- uint8_t i=8, temp=0;
- PORT_OUT |= LCD_DC;
- for(i=0;i<8;i++) //发送一个八位数据
- {
- PORT_OUT &=~ LCD_SCL;
- temp = dat&0x80;
- if (temp == 0)
- {
- PORT_OUT &=~ LCD_SDA;
- }
- else
- {
- PORT_OUT |= LCD_SDA;
- }
- PORT_OUT |= LCD_SCL;
- dat<<=1;
- }
- }
- /*********************LCD写命令************************************/
- void LCD_WrCmd(uint8_t cmd)
- {
- uint8_t i=8, temp=0;
- PORT_OUT &=~ LCD_DC;
- for(i=0;i<8;i++) //发送一个八位数据
- {
- PORT_OUT &=~ LCD_SCL;
- temp = cmd&0x80;
- if (temp == 0)
- {
- PORT_OUT &=~ LCD_SDA;
- }
- else
- {
- PORT_OUT |= LCD_SDA;
- }
- PORT_OUT |= LCD_SCL;
- cmd<<=1;;
- }
- }
- /*********************LCD 设置坐标************************************/
- void LCD_Set_Pos(uint8_t x, uint8_t y)
- {
- LCD_WrCmd(0xb0+y);
- LCD_WrCmd(((x&0xf0)>>4)|0x10);
- LCD_WrCmd((x&0x0f)|0x00); ////
- }
- /*********************LCD全屏************************************/
- void LCD_Fill(uint8_t bmp_dat)
- {
- uint8_t y,x;
- for(y=0;y<8;y++)
- {
- LCD_WrCmd(0xb0+y);
- LCD_WrCmd(0x01);
- LCD_WrCmd(0x10);
- for(x=0;x<X_WIDTH;x++)
- {
- LCD_WrDat(bmp_dat);
- }
- }
- }
- /*********************LCD复位************************************/
- void LCD_CLS(void)
- {
- uint8_t y,x;
- for(y=0;y<8;y++)
- {
- LCD_WrCmd(0xb0+y);
- LCD_WrCmd(0x01);
- LCD_WrCmd(0x10);
- for(x=0;x<X_WIDTH;x++)
- {
- LCD_WrDat(0);
- }
- }
- }
- /*********************LCD初始化************************************/
- void LCD_Init(uint8_t Brightness)
- {
- P1DIR |=BIT2+BIT3+BIT4+BIT5;
- PORT_OUT |= LCD_SCL;
- PORT_OUT &=~ LCD_RST;
- delay_us(100);
- PORT_OUT |= LCD_RST; //从上电到下面开始初始化要有足够的时间,即等待RC复位完毕
- LCD_WrCmd(0xae);//--turn off oled panel
- LCD_WrCmd(0x00);//---set low column address
- LCD_WrCmd(0x10);//---set high column address
- LCD_WrCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- LCD_WrCmd(0x81);//--set contrast control register
- LCD_WrCmd(Brightness); // Set SEG Output Current Brightness 控制显示亮度
- LCD_WrCmd(0xa1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- LCD_WrCmd(0xc8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- LCD_WrCmd(0xa6);//--set normal display
- LCD_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
- LCD_WrCmd(0x3f);//--1/64 duty
- LCD_WrCmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- LCD_WrCmd(0x00);//-not offset
- LCD_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
- LCD_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
- LCD_WrCmd(0xd9);//--set pre-charge period
- LCD_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- LCD_WrCmd(0xda);//--set com pins hardware configuration
- LCD_WrCmd(0x12);
- LCD_WrCmd(0xdb);//--set vcomh
- LCD_WrCmd(0x40);//Set VCOM Deselect Level
- LCD_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
- LCD_WrCmd(0x02);//
- LCD_WrCmd(0x8d);//--set Charge Pump enable/disable
- LCD_WrCmd(0x14);//--set(0x10) disable
- LCD_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
- LCD_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
- LCD_WrCmd(0xaf);//--turn on oled panel
- LCD_Fill(0x00); //初始清屏
- LCD_Set_Pos(0,0); ////
- }
- /***************功能描述:显示6*8一组标准ASCII字符串 显示的坐标(x,y),y为页范围0~7****************/
- void LCD_P6x8Str(uint8_t x, uint8_t y,uint8_t ch[])
- {
- uint8_t c=0,i=0,j=0;
- while (ch[j]!='\0')
- {
- c =ch[j]-32;
- if(x>126){x=0;y++;}
- LCD_Set_Pos(x,y);
- for(i=0;i<6;i++)
- {
- LCD_WrDat(F6x8[c][i]);
- }
- x+=6;
- j++;
- }
- }
- /*******************功能描述:显示8*16一组标准ASCII字符串 显示的坐标(x,y),y为页范围0~7****************/
- void LCD_P8x16Str(uint8_t x, uint8_t y,uint8_t ch[])
- {
- uint8_t c=0,i=0,j=0;
- while (ch[j]!='\0')
- {
- c =ch[j]-32;
- if(x>120){x=0;y++;}
- LCD_Set_Pos(x,y);
- for(i=0;i<8;i++)
- {
- LCD_WrDat(F8X16[c*16+i]);
- }
- LCD_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- {
- LCD_WrDat(F8X16[c*16+i+8]);
- }
- x+=8;
- j++;
- }
- }
- /*****************功能描述:显示16*16点阵 显示的坐标(x,y),y为页范围0~7****************************/
- void LCD_P16x16Ch(uint8_t x, uint8_t y, uint8_t n)
- {
- uint8_t wm=0;
- unsigned int adder=32*n; //
- LCD_Set_Pos(x , y);
- for(wm = 0;wm < 16;wm++) //
- {
- LCD_WrDat(F16x16[adder]);
- adder += 1;
- }
- LCD_Set_Pos(x,y + 1);
- for(wm = 0;wm < 16;wm++) //
- {
- LCD_WrDat(F16x16[adder]);
- adder += 1;
- }
- }
- /***************功能描述:行填充, y为页范围0~7****************/
- //Eg: LCD_FillLine(1,1);
- void LCD_FillLine(unsigned char y,unsigned char ch)
- {
- unsigned char x;
- LCD_WrCmd(0xb0+y);
- LCD_WrCmd(0x01);
- LCD_WrCmd(0x10);
- for(x=0;x<X_WIDTH;x++)
- {
- LCD_WrDat(ch);
- }
- }
- /***********功能描述:显示一行ascii 码?无显示的点阵显示黑色, y为页的范围0~7*****************/
- //Eg: Lcd_WriteLine(3,"www.ABC.com");
- void Lcd_WriteLine(unsigned char line, const char *pText)
- {
- LCD_FillLine(line-1, 0); //先把这一行显示成黑色
- LCD_P8x16Str(0, line-1, (unsigned char *)pText);
- }
- //======================================================
- // 函数名称:LCD_Dis_Minus
- // 格式:void LCD_Dis_Minus(uint8_t x, uint8_t y,int dat,uint8_t len);
- // 实现功能: 显示负数
- // 参数:
- // 返回值: 无
- //======================================================
- void LCD_Dis_Minus(uint8_t x, uint8_t y,int dat,uint8_t len)
- {
- if(dat >= 0)
- {
- DispDecAt(x, y, dat, len);
- }
- else
- {
- dat*=-1;
- LCD_P8x16Str(x,y,"-");
- DispDecAt(x+8, y, dat, len);
- }
- }
- /*显示10进制数
- * Eg:DispDecAt(80,1,123456,6);
- */
- void DispDecAt(uint8_t x,uint8_t y,int dat,uint8_t len)
- {
- uint8_t str[12];
- uint8_t i,dl;
- str[len]='\0';
- for(i=0;i<len;i++)
- {
- dl=dat%10;
- dat/=10;
- str[len-i-1]=dl+'0';
- }
- LCD_P8x16Str(x,y,str);
- }
- /*显示16进制数
- * Eg:DispHexAt(80,1,0x1f3f6,5);
- */
- void DispHexAt(uint8_t x,uint8_t y,long dat,uint8_t len)
- {
- uint8_t i,dl;
- uint8_t str[12];
- for(i=0;i<len;i++)
- {
- dl=dat&0xf;
- dat=dat>>4;
- if(dl<10)
- {
- str[len-i-1]=dl+'0';
- }
- else
- {
- str[len-i-1]=dl-10+'a';
- }
- }
- LCD_P8x16Str(x,y,str);
- }
- /*显示浮点数
- * uint8_t x(坐标X),uint8_t y(坐标Y),float dat(待显示的数据),uint8_t len1(整数部分长度),uint8_t len2(小数部分长度) )
- */
- void DispFloatAt(uint8_t x,uint8_t y,float dat,uint8_t len1,uint8_t len2 )
- {
- int dat1,dat2;
- dat1=(int)dat;//整数部分
- dat2=(int)((dat-dat1)*numtab[len2]);
- DispDecAt(x,y,dat1,len1);
- LCD_P8x16Str(x+8*len1,y,".");
- DispDecAt(x+8*(len1+1),y,dat2,len2);
- }
- /***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
- //void Draw_BMP(uint8_t x0, uint8_t y0,uint8_t x1, uint8_t y1,uint8_t BMP[])
- //{
- // unsigned int j=0;
- // uint8_t x,y;
- //
- // if(y1%8==0) y=y1/8;
- // else y=y1/8+1;
- // for(y=y0;y<y1;y++)
- // {
- // LCD_Set_Pos(x0,y);
- // for(x=x0;x<x1;x++)
- // {
- // LCD_WrDat(BMP[j++]);
- // }
- // }
- //}
- void Draw_Logo(void)
- {
- uint16_t i=0;
- uint8_t x,y;
- for(y=0;y<8;y++)
- {
- LCD_Set_Pos(34,y);
- for(x=34;x<98;x++)
- {
- LCD_WrDat(Clover_LOGO_64x64[i++]);
- }
- }
- }
复制代码 头文件
- /*
- * LCD.h
- *
- * Created on: 2015年7月23日
- * Author: Clover
- */
- #ifndef LCD_H_
- #define LCD_H_
- #include <msp430.h>
- //Macros
- #define PORT_OUT P1OUT
- #define PORT_IN P1IN
- #define LCD_SCL (1<<5) //SCLK 时钟 D0(SCLK) 15
- #define LCD_SDA (1<<4) //SDA D1(MOSI) 数据 16
- #define LCD_RST (1<<3) //_RES hardware reset 复位 17
- #define LCD_DC (1<<2) //A0 H/L 命令数据选通端,H:数据,L:命令 12
- //#define Brightness 0xCF//最亮
- //#define Brightness 0x64//一半亮度
- #define X_WIDTH 128
- #define Y_WIDTH 64
- extern void LCD_Fill(uint8_t bmp_dat);//LCD全屏
- extern void LCD_CLS(void);//LCD复位
- extern void LCD_Init(uint8_t Brightness);//初始化 ,入口参数:亮度
- extern void LCD_P6x8Str(uint8_t x, uint8_t y,uint8_t ch[]);//显示6*8一组标准ASCII字符串
- extern void LCD_P8x16Str(uint8_t x, uint8_t y,uint8_t ch[]);//显示8*16一组标准ASCII字符串
- extern void LCD_P16x16Ch(uint8_t x, uint8_t y, uint8_t n);//显示16*16一组标准ASCII字符串
- extern void LCD_FillLine(unsigned char y,unsigned char ch);//行填充, y为页范围0~7
- extern void Lcd_WriteLine(unsigned char line, const char *pText);//显示一行ascii 码?无显示的点阵显示黑色
- extern void LCD_Dis_Minus(uint8_t x, uint8_t y,int dat,uint8_t len);
- extern void DispDecAt(uint8_t x,uint8_t y,int dat,uint8_t len);//显示10进制数
- extern void DispHexAt(uint8_t x,uint8_t y,long dat,uint8_t len);//显示16进制数
- extern void DispFloatAt(uint8_t x,uint8_t y,float dat,uint8_t len1,uint8_t len2 );//显示浮点数
- extern void Draw_Logo(void);
- #endif /* LCD_H_ */
复制代码
专门说明一下,这个函数里边也有接口定义,发了发现。。。带在身上的OLED是坏的,宏定义就先不改了
extern void LCD_Init(uint8_t Brightness);//初始化 ,入口参数:亮度
|
|