单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <oled.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define delay4us() {_nop_();_nop_();_nop_();_nop_();}
- //mep3201的引脚
- sbit CS_MCP3201 =P3^2;
- sbit DO_MCP3201 =P3^3;
- sbit CLK_MCP3201 =P3^4;
- //输入选择的引脚
- sbit SR1 =P0^5;
- sbit SR2 =P0^4;
- sbit SR3 =P0^3;
- sbit SR4 =P0^2;
- sbit SR5 =P0^1;
- sbit SR6 =P0^0;
- //LED选择的引脚
- sbit LED1 =P2^0;
- sbit LED2 =P2^1;
- sbit LED3 =P2^2;
- sbit LED4 =P2^3;
- sbit LED5 =P2^4;
- sbit LED6 =P2^5;
- sbit LED7 =P2^6;
- sbit LED8 =P2^7;
- //报警选择的引脚
- sbit BEEP =P1^7;
- uchar Display_Buffek[] = "STC89C52 MEP3201";
- uchar Display_Buffer[] = "Flow 00.00V\r\n";
- void DelayMS(uint ms)
- {
- uchar i;
- while(ms--)
- {
- for(i=0;i<120;i++);
- }
- }
- void putc_to_SerialPort(uchar c)
- {
- SBUF = c;
- while(TI == 0);
- TI = 0;
- }
- void puts_to_SerialPort(uchar *s)
- {
- while(*s != '\0')
- {
- putc_to_SerialPort(*s);
- s++;
- DelayMS(5);
- }
- }
- uint AD3201()
- {
- uint temp;
- uchar k;
- // TRISC=OB000100;
- CLK_MCP3201=0;
- CS_MCP3201=0;
- CLK_MCP3201=1;
- delay4us();
- CLK_MCP3201=0;
- delay4us();
- CLK_MCP3201=1;
- delay4us();
- CLK_MCP3201=0;
- delay4us();
- for(k=0;k<12;k++)
- {
- CLK_MCP3201=1;
- delay4us();
- CLK_MCP3201=0;
- if(DO_MCP3201==0)temp=temp*2;
- else temp=temp*2+1;
- }
- CS_MCP3201=1;
- return temp;
- }
- void main()
- {
- int dw;
- uint d;
- uint d2,l=0;
- SCON = 0x40; //设置波特率
- TMOD = 0x20;
- PCON = 0x00;
- TH1 = 0xfd;
- TL1 = 0xfd;
- TI = 0;
- TR1 = 1; //ok
- DelayMS(10);
- Initial_LY096BG30();
- fill_picture(0x00);
- Picture_1();
-
- while(1)
- {
- l++;
- if(l%50==0)
- {
- d=AD3201();//
- if(SR1==0)d2 =d/6,dw=1;
- if(SR2==0)d2 =d/3,dw=2;
- if(SR3==0)d2 =d/2,dw=3;
- if(SR4==0)d2 =d/1.5,dw=4;
- if(SR5==0)d2 =d/1.2,dw=5;
- if(SR6==0)d2 =d,dw=6;
- Display_Buffer[10]=d2/1000+'0';
- Display_Buffer[11]=d2/100%10+'0';
- Display_Buffer[12]='.';
- Display_Buffer[13]=d2/10%10+'0';
- Display_Buffer[14]=d2%10+'0';
- Display_Buffer[15]='V';
- Display_Buffer[5]=d/1000+'0';
- Display_Buffer[6]=d/100%10+'0';
- Display_Buffer[7]=d/10%10+'0';
- Display_Buffer[8]=d%10+'0';
- }
- P2=P0;
- if(SR6==0&&d2>3000) BEEP=1;
- else BEEP=0;
- xssz(2,3,Display_Buffer[10]-'0');
- xssz(2,4,Display_Buffer[11]-'0');
- xssz(2,5,10);
- xssz(2,6,Display_Buffer[13]-'0');
- xssz(3,6,dw);
- if(l==100)
- {
- l=0;
- puts_to_SerialPort("STC89C52 MEP3201\r\n");
- DelayMS(50);
- puts_to_SerialPort(Display_Buffer);
- }
- }
- }
复制代码
#include <reg52.h>
#define high 1
#define low 0
//oled引脚定义
sbit SCL = P3^5;
sbit SDA = P3^6;
/****************************************************/
void Initial_LY096BG30();
void fill_picture(unsigned char fill_Data);
void Picture_1();
void IIC_Start();
void IIC_Stop();
void Write_IIC_Command(unsigned char IIC_Command);
void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Byte(unsigned char IIC_Byte);
/***********************Initial code*********************/
void Initial_LY096BG30()
{
Write_IIC_Command(0xAE); //display off
Write_IIC_Command(0x20); //Set Memory Addressing Mode
Write_IIC_Command(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
Write_IIC_Command(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
Write_IIC_Command(0xc8); //Set COM Output Scan Direction
Write_IIC_Command(0x02);//---set low column address
Write_IIC_Command(0x10);//---set high column address
Write_IIC_Command(0x40);//--set start line address
Write_IIC_Command(0x81);//--set contrast control register
Write_IIC_Command(0x7f);
Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
Write_IIC_Command(0xa6);//--set normal display
Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
Write_IIC_Command(0x3F);//
Write_IIC_Command(0xa4);//0xa4,Output follows RAM content;0xa5,Output ignores RAM content
Write_IIC_Command(0xd3);//-set display offset
Write_IIC_Command(0x00);//-not offset
Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
Write_IIC_Command(0xf0);//--set divide ratio
Write_IIC_Command(0xd9);//--set pre-charge period
Write_IIC_Command(0x22); //
Write_IIC_Command(0xda);//--set com pins hardware configuration
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);//--set vcomh
Write_IIC_Command(0x20);//0x20,0.77xVcc
Write_IIC_Command(0x8d);//--set DC-DC enable
Write_IIC_Command(0x14);//
Write_IIC_Command(0xaf);//--turn on oled panel
}
/***********************Picture Code**************************/
unsigned char code show1[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x90,0x52,0x34,0x10,0xFF,0x10,0x34,0x52,0x80,0x70,0x8F,0x08,0x08,0xF8,0x08,0x00,
0x82,0x9A,0x56,0x63,0x22,0x52,0x8E,0x00,0x80,0x40,0x33,0x0C,0x33,0x40,0x80,0x00,/*"数",0*/
0x10,0x0C,0x04,0x24,0x24,0x24,0x25,0x26,0xA4,0x64,0x24,0x04,0x04,0x14,0x0C,0x00,
0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,/*"字",1*/
0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00,
0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00,/*"电",2*/
0x00,0x00,0xFE,0x02,0x82,0x82,0x82,0x82,0xFA,0x82,0x82,0x82,0x82,0x82,0x02,0x00,
0x80,0x60,0x1F,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,0x40,0x44,0x58,0x40,0x40,0x00,/*"压",3*/
0x00,0x04,0x24,0x24,0x24,0x24,0x24,0xFF,0x24,0x24,0x24,0x24,0x24,0x04,0x00,0x00,
0x21,0x21,0x11,0x09,0xFD,0x83,0x41,0x23,0x05,0x09,0x11,0x29,0x25,0x41,0x41,0x00,/*"表",4*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,/*"Y",0*/
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,/*"S",1*/
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"5",0*/
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",1*/
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,/*"1",2*/
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0",3*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,/*" ",4*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*"1",5*/
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"5",6*/
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",7*/
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,/*"2",8*/
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0",9*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,/*" ",10*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,/*"2",11*/
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"5",12*/
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",13*/
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,/*"3",14*/
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0",15*/
0x00,0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,
0x00,0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,/*"电"*/
0x00,0x00,0x00,0xFE,0x02,0x82,0x82,0x82,0x82,0xFA,0x82,0x82,0x82,0x82,0x82,0x02,
0x00,0x80,0x60,0x1F,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,0x40,0x44,0x58,0x40,0x40,/*"压"*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*":"*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"V"*/
0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00,
0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00,/*"当",0*/
0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00,
0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00,/*"前",1*/
0x10,0x10,0xD0,0xFF,0x90,0x10,0x40,0x42,0x5C,0x40,0x7F,0x40,0x50,0xCE,0x00,0x00,
0x04,0x03,0x00,0xFF,0x00,0x01,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,/*"档",2*/
0x00,0x80,0x60,0xF8,0x07,0x10,0x90,0x10,0x11,0x16,0x10,0x10,0xD0,0x10,0x00,0x00,
0x01,0x00,0x00,0xFF,0x40,0x40,0x41,0x5E,0x40,0x40,0x70,0x4E,0x41,0x40,0x40,0x00,/*"位",3*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*":",4*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x10,0x10,0xD0,0xFF,0x90,0x10,0x40,0x42,0x5C,0x40,0x7F,0x40,0x50,0xCE,0x00,0x00,
0x04,0x03,0x00,0xFF,0x00,0x01,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,/*"档",7*/
};
unsigned char code show2[]=
{
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0"*/
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*"1"*/
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,/*"2"*/
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,/*"3"*/
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,/*"4"*/
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*"5"*/
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,/*"6"*/
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*"7"*/
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,/*"8"*/
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,/*"9"*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,/*"."*/
};
/**********************************************
//IIC Start
**********************************************/
void IIC_Start()
{
SCL = high;
SDA = high;
SDA = low;
SCL = low;
}
/**********************************************
//IIC Stop
**********************************************/
void IIC_Stop()
{
SCL = low;
SDA = low;
SCL = high;
SDA = high;
}
/**********************************************
// IIC Write byte
**********************************************/
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(IIC_Byte & 0x80) //1?0?
SDA=high;
else
SDA=low;
SCL=high;
SCL=low;
IIC_Byte<<=1; //loop
}
SDA=1;
SCL=1;
SCL=0;
}
/**********************************************
// IIC Write Command
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78); //Slave address,SA0=0
Write_IIC_Byte(0x00); //write command
Write_IIC_Byte(IIC_Command);
IIC_Stop();
}
/**********************************************
// IIC Write Data
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78);
Write_IIC_Byte(0x40); //write data
Write_IIC_Byte(IIC_Data);
IIC_Stop();
}
/********************************************
// fill_Picture
********************************************/
void fill_picture(unsigned char fill_Data)
{
unsigned char m,n;
for(m=0;m<8;m++)
{
Write_IIC_Command(0xb0+m); //page0-page1
Write_IIC_Command(0x00); //low column start address
Write_IIC_Command(0x10); //high column start address
for(n=0;n<130;n++)
{
Write_IIC_Data(fill_Data);
}
}
}
/******************************************
// picture
******************************************/
void picture_1()
{
unsigned char x,y,z,k;
unsigned int i=0;
for(k=0;k<4;k++)
{
for(z=0;z<8;z++)
{
for(y=2*k;y<2*(k+1);y++)
{
Write_IIC_Command(0xb0+y);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10+z);
for(x=0;x<16;x++)
{
Write_IIC_Data(show1[i++]);
}
}
}
}
}
void xssz(int k,int z,int sz) //k=行; z=个;
{
unsigned char x,y;
unsigned int i=sz*16;
for(y=2*k;y<2*(k+1);y++)
{
Write_IIC_Command(0xb0+y);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10+z);
for(x=0;x<8;x++)
{
Write_IIC_Data(show2[i++]);
}
}
}
全部资料51hei下载地址(带液晶屏的仿真目前 暂不分享):
MCP3201 oled ly.rar
(100.23 KB, 下载次数: 48)
|