电子科技大学吴鉴鹰#include <c8051f020.h> // SFR declarations#include <intrins.h>#include <stdio.h>#include<math.h>#define uchar unsigned char#define uint unsigned intuchar i,j,colour=1,T=0;#define Lcd_Bus P5/*char code seg[360] = {0x80,0x82,0x84,0x86,0x88,0x8b,0x8d,0x8f,0x91,0x94, 0x96,0x98,0x9a,0x9c,0x9e,0xa1,0xa3,0xa5,0xa7,0xa9, 0xab,0xad,0xaf,0xb1,0xb4,0xb6,0xb8,0xba,0xbc,0xbe, 0xbf,0xc1,0xc3,0xc5,0xc7,0xc9,0xcb,0xcc,0xce,0xd0, 0xd2,0xd3,0xd5,0xd7,0xd8,0xda,0xdc,0xdd,0xdf,0xe0, 0xe2,0xe3,0xe4,0xe6,0xe7,0xe8,0xea,0xeb,0xec,0xed, 0xee,0xef,0xf0,0xf2,0xf3,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xf9,0xfa,0xfb,0xfb,0xfc,0xfc,0xfd,0xfd,0xfe,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfe,0xfd,0xfd,0xfc,0xfc,0xfb,0xfb,0xfa,0xf9,0xf9,/*0xf8,0xf7,0xf6,0xf5,0xf4,0xf4,0xf3,0xf2,0xf1,0xf0, 0xee,0xed,0xec,0xeb,0xea,0xe8,0xe7,0xe6,0xe4,0xe3, 0xe2,0xe0,0xdf,0xdd,0xdc,0xda,0xd9,0xd7,0xd5,0xd4, 0xd2,0xd0,0xce,0xcd,0xcb,0xc9,0xc7,0xc5,0xc3,0xc2, 0xc0,0xbe,0xbc,0xba,0xb8,0xb6,0xb4,0xb2,0xb0,0xae, 0xab,0xa9,0xa7,0xa5,0xa3,0xa1,0x9f,0x9c,0x9a,0x98, 0x96,0x94,0x92,0x8f,0x8d,0x8b,0x89,0x86,0x84,0x82, 0x80,0x7d,0x7b,0x79,0x77,0x75,0x72,0x70,0x6e,0x6c, 0x69,0x67,0x65,0x63,0x61,0x5f,0x5c,0x5a,0x58,0x56, 0x54,0x52,0x50,0x4e,0x4c,0x4a,0x48,0x46,0x44,0x42, 0x40,0x3e,0x3c,0x3a,0x38,0x36,0x34,0x33,0x31,0x2f, 0x2d,0x2c,0x2a,0x28,0x27,0x25,0x24,0x22,0x21,0x1f, 0x1e,0x1c,0x1b,0x19,0x18,0x17,0x16,0x14,0x13,0x12, 0x11,0x10,0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8,0x7, 0x7, 0x6, 0x5, 0x5, 0x4, 0x3, 0x3, 0x2, 0x2,0x1, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1,0x1, 0x2, 0x2, 0x3, 0x3, 0x4, 0x4, 0x5, 0x6, 0x6,0x7, 0x8, 0x9, 0xa, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,0x10,0x12,0x13,0x14,0x15,0x16,0x18,0x19,0x1a,0x1c, 0x1d,0x1f,0x20,0x22,0x23,0x25,0x26,0x28,0x2a,0x2b, 0x2d,0x2f,0x30,0x32,0x34,0x36,0x38,0x39,0x3b,0x3d, 0x3f,0x41,0x43,0x45,0x47,0x49,0x4b,0x4d,0x4f,0x51,0x53,0x55,0x58,0x5a,0x5c,0x5e,0x60,0x62,0x65,0x67,0x69,0x6b,0x6d,0x70,0x72,0x74,0x76,0x78,0x7b,0x7d};*/#define SYSCLK 22118400 // SYSCLK frequency in Hz #define INT_DEC 256 // integrate and decimate ratio #define _Nop() _nop_()//#define FIRST_ADDR 0//-----------------------------------------------------------------------------// 16-bit SFR Definitions for 'F02x//-----------------------------------------------------------------------------sfr16 DP = 0x82; // data pointersfr16 TMR3RL = 0x92; // Timer3 reload valuesfr16 TMR3 = 0x94; // Timer3 countersfr16 ADC0 = 0xbe; // ADC0 datasfr16 ADC0GT = 0xc4; // ADC0 greater than window sfr16 ADC0LT = 0xc6; // ADC0 less than windowsfr16 RCAP2 = 0xca; // Timer2 capture/reloadsfr16 T2 = 0xcc; // Timer2sfr16 RCAP4 = 0xe4; // Timer4 capture/reloadsfr16 T4 = 0xf4; // Timer4sfr16 DAC0 = 0xd2; // DAC0 datasfr16 DAC1 = 0xd5; // DAC1 data//-----------------------------------------------------------------------------// 液晶接口定义//-----------------------------------------------------------------------------//#define LEDCTL0Low P4&=~0x01;//#define LEDCTL0High P4|=0x01;#define LCDELow P4&=~0x02;#define LCDEHigh P4|=0x02;#define LCDRWLow P4&=~0x04;#define LCDRWHigh P4|=0x04;#define LCDRSLow P4&=~0x08;#define LCDRSHigh P4|=0x08;#define LCDPSBLow P4&=~0x10;#define LCDPSBHigh P4|=0x10;//==============================//uchar num;uint delay_time;/*-----------------------------------------------------*/void SYSCLK_Init (void);void PORT_Init (void);//----------------------------------------------------------------------------- ////-----------------------------------------------------------------------------void delay(unsigned int t){ unsigned int k,j;for(k=0;k<t;k++)for(j=0;j<10;j++) ;}/*void delay(unsigned int t){unsigned int i,j ;for(i=0;i<t;i++)for(j=0;j<10;j++);}*///----------------------------------------------------------------------------- ////-----------------------------------------------------------------------------void delay18(uint k){ while(k){ k--; }}//----------------------------------------------------------------------------- // 液晶显示写命令//----------------------------------------------------------------------------- void write_com(unsigned char cmdcode){LCDRSLow;LCDRWLow;LCDEHigh;Lcd_Bus=cmdcode;delay(50);delay(60); //在数据写入的时候加入适当的延时LCDELow;delay(50);delay(60);}//-----------------------------------------------------------------------------// 液晶显示写数据//-----------------------------------------------------------------------------void write_data(unsigned char Dispdata){LCDRSHigh;LCDRWLow;LCDEHigh;Lcd_Bus=Dispdata;delay(50);delay(60); // 在数据写入的时候加入适当的延时LCDELow;delay(50);delay(60);}void chk_busy(){LCDRSLow;LCDRWHigh;LCDEHigh;Lcd_Bus=0xff ;while((Lcd_Bus&0x80)==0x80);LCDELow;}/*------------------写命令到LCD------------------------------*//*void write_com(unsigned char cmdcode){chk_busy();LCDRSLow ;LCDRWLow ;LCDEHigh ;Lcd_Bus=cmdcode ;delay(5);//------------------在数据写入的时候加入适当的延时LCDELow ;delay(5);}*//*-------------------写数据到LCD----------------------------*//*void write_data(unsigned char Dispdata){chk_busy();LCDRSHigh ;LCDRWLow ;LCDEHigh ;Lcd_Bus=Dispdata ;delay(5);//------------------在数据写入的时候加入适当的延时LCDELow ;delay(5);}*///-----------------------------------------------------------------------------// 液晶显示,复位初始//-----------------------------------------------------------------------------void lcdreset(){delay(200);delay(250);write_com(0x30);delay(100);delay(250); //选择基本指令集write_com(0x30); //选择8bit数据流delay(50);delay(250);write_com(0x0c); //开显示(无游标、不反白)delay(50);delay(250);write_com(0x01); //清除显示,并且设定地址指针为00Hdelay(50);delay(250);write_com(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位}//-----------------------------------------------------------------------------// 液晶显示//-----------------------------------------------------------------------------/*void display(unsigned char code *s){while(*s>0){ write_data(*s);s++;delay(50);delay(100);}}*/void hzkdis(unsigned char code*s){while(*s>0){write_data(*s);s++;delay(50);}}/*------------------首屏显示--------------------------*///-----------------------------------------------------------------------------// 液晶显示内容//-----------------------------------------------------------------------------/*void zero_screen(){write_com(0x01);delay18(10000);//write_com(0x80);//display("电子科技大学");write_com(0x90);//display("省单片机精品课程");display("C8051F020 开发板");write_com(0x88);//display("C8051F020 开发板");display(" QQ:1811728618 ");//write_com(0x98);//display(" QQ:1811728618 ");}*//*------------------显示字符串--------------------------*/void ceshi(){write_com(0x01);//清除显示,并且设定地址指针为00Hdelay(5);write_com(0x80);//第一行(如果是地址是:80H,即LCD的第一行的第一个位置显示)hzkdis("电子科技大学");write_com(0x90);//第二行(如果是地址是:90H,即LCD的第二行的第一个位置显示)hzkdis("通信与信息工程");write_com(0x88);//第三行(如果是地址是:88H,即LCD的第二行的第一个位置显示)hzkdis("电子信息工程");write_com(0x98);//第四行(如果是地址是:98H,即LCD的第二行的第一个位置显示)hzkdis("郑海波测试程序");}/*------------------初始化LCD屏--------------------------*//*void lcdreset(){LCDPSBHigh;delay(2000);write_com(0x30);delay(10);//选择基本指令集write_com(0x30);//选择8bit数据流delay(5);write_com(0x0c);//开显示(无游标、不反白)delay(10);write_com(0x01);//清除显示,并且设定地址指针为00Hdelay(500);write_com(0x06);//指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位delay(0);}*///-----------------------------------------------------------------------------// 通用I/O口及交叉开关初始化//-----------------------------------------------------------------------------void PORT_Init (void){XBR2 = 0x40; // 交叉开关使能P74OUT=0xff;}//-----------------------------------------------------------------------------// 系统时钟初始化//-----------------------------------------------------------------------------/*void SYSCLK_Init() 本来就不用的{OSCICN =0x87; //使用内部时钟16Mwhile((OSCICN & 0x10)==0);//内部时钟按设定频率运行}*/// 配置系统时钟使用外部晶振11.0592MHzvoid SYSCLK_Init (void){int i; // delay counterOSCXCN = 0x67; // start external oscillator with// 22.1184MHz crystal for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settleOSCICN = 0x88; // select external oscillator as SYSCLK// source and enable missing clock// detector}//-----------------------------------------------------------------------------// 系统初始化//-----------------------------------------------------------------------------void init(){SYSCLK_Init();PORT_Init ();lcdreset();//zero_screen();}//-----------------------------------------------------------------------------// 主程序//-----------------------------------------------------------------------------/*void main(){WDTCN = 0xde;WDTCN = 0xad; //禁止看门狗定时器LCDPSBHigh;//LCDRSTHigh;init();zero_screen();P5=0xff;while(1){}} *///------------定义接口-------------///*sbit RS=P2^2 ;sbit RW=P2^3 ;sbit E=P2^4;sbit PSB= P2^5; //H=并口; L="串口";#define Lcd_Bus P0// P0 接LCM#define uchar unsigned char*///定义字符/汉字显示起始位置/*------------------检查忙位-----------------------------*//*------------------延时子程序-----------------------------*///------------------清整个GDRAM空间---------------------------- void clrgdram(){unsigned char x,y ;for(y=0;y<64;y++)for(x=0;x<16;x++){write_com(0x34);write_com(y+0x80);//行地址write_com(x+0x80);//列地址write_com(0x30);write_data(0x00);write_data(0x00);}}//------------------------------------------------------------void clrscreen(){write_com(0x01);delay(10);}unsigned char ReadByte(void){unsigned char byReturnValue ;chk_busy();Lcd_Bus=0xff ;LCDRSHigh ;LCDRWHigh ;LCDELow ;LCDEHigh;byReturnValue=Lcd_Bus ;LCDELow ;return byReturnValue ;}/*增加画点子程序函数功能:在坐标为(x,y)点画一个点参数意义X:12864屏幕的横坐标,范围是0到128(从左到右)Y:12864的纵坐标,范围是0到64(从上到下)Color:为1的时候表示为黑点*//*void DrawPoint(unsigned char X,unsigned char Y,unsigned char Color) {unsigned char Row,Tier,Tier_bit ;unsigned char ReadOldH,ReadOldL ;write_com(0x34);write_com(0x36);Tier=X>>4 ;Tier_bit=X&0x0f ;if(Y<32){Row=Y ;}{Row=Y-32 ;Tier+=8 ;}write_com(Row+0x80);write_com(Tier+0x80);ReadByte();ReadOldH=ReadByte();ReadOldL=ReadByte();write_com(Row+0x80);write_com(Tier+0x80);if(Tier_bit<8){switch(Color){case 0 :ReadOldH&=(~(0x01<<(7-Tier_bit))); break ;case 1 :ReadOldH|=((0x01<<(7-Tier_bit))); break ;case 2 :ReadOldH^=(0x01<<(7-Tier_bit)); break ;default :break ;}write_data(ReadOldH);write_data(ReadOldL);}else{switch(Color){case 0 :ReadOldL&=(~(0x01<<(15-Tier_bit))); break ;case 1 :ReadOldL|=((0x01<<(15-Tier_bit))); break ;case 2 :ReadOldL^=(0x01<<(15-Tier_bit)); break ;break ;}write_data(ReadOldH);write_data(ReadOldL);}write_com(0x30);}void main(void){//WDTCN = 0xde; // 关闭WDT//WDTCN = 0xad;SYSCLK_Init ();PORT_Init ();//LCDRWLow;//lcdreset();//zero_screen();//clrgdram();delay(2000);clrscreen();while(1){for(i=0;i<128;i++){/*unsigned char k;if(j>0x32) k=seg[i];else k=-seg[i];j=0x32-k;*//*j=sin(i);DrawPoint(i,j,colour);}//T+=4;delay(5000);}while(1);}*/void DrawPoint(unsigned char X,unsigned char Y,unsigned char Color) {unsigned char Row,Tier,Tier_bit ;unsigned char ReadOldH,ReadOldL ;write_com(0x34);write_com(0x36);Tier=X>>4 ;Tier_bit=X&0x0f ;{Row=Y ;}else{Row=Y-32 ;Tier+=8 ;}write_com(Row+0x80);write_com(Tier+0x80);ReadByte();ReadOldH=ReadByte();ReadOldL=ReadByte();write_com(Row+0x80);write_com(Tier+0x80);if(Tier_bit<8){switch(Color){case 0 :ReadOldH&=(~(0x01<<(7-Tier_bit))); break ;case 1 :ReadOldH|=(0x01<<(7-Tier_bit)); break ;case 2 :ReadOldH^=(0x01<<(7-Tier_bit)); break ;default :break ;}write_data(ReadOldH);write_data(ReadOldL);}else{switch(Color){case 0 :ReadOldL&=(~(0x01<<(15-Tier_bit))); break ;case 1 :ReadOldL|=(0x01<<(15-Tier_bit));case 2 :ReadOldL^=(0x01<<(15-Tier_bit)); break ;default :break ;}write_data(ReadOldH);write_data(ReadOldL);}write_com(0x30);}void main(void){uchar i,j,colour=1,T=0; LCDRWLow ;lcdreset();ceshi();clrgdram();delay(2000);clrscreen();while(1){for(i=0;i<128;i++){j=32-32*sin(2*i*3.14/(64+T)); DrawPoint(i,j,colour);}//T+=4;delay(5000);}while(1);}。