国旗升降系统原理图如下
单片机源程序如下:
- //此程序只供参考//
- //升国旗中 还有万年历显示 现在没有加进去//
- //下次我把他加进去
- #include<reg52.h>
- #define uchar unsigned char //宏定义
- #define uint unsigned int
- #define lcdbus P1
- sbit RS = P3^0;
- sbit RW = P3^1;
- sbit E = P3^2;
- //st7920字模取模方式为逐行式 顺向去摸
- sbit key1=P2^4; //升国旗
- sbit key2=P2^5; //升半旗
- sbit key3=P2^6; //降国旗
- sbit zdbz1=P0^2;
- sbit zdbz2=P0^1;
- sbit zdbz3=P0^0;
- uchar flag1,flag2,flag3,flag4,aa,bb,cc; //flag4为正反转标志
- uchar code table[]={0xf1,0xf2,0xf4,0xf8};
- uchar tt,tt0,tt1;//定时器时间设置标志
- uchar sj,sj1,sj2,sj3,gd1,gd13,gd12,gd11,gd2,gd21,gd22,gd23;//显示设置变量
- uchar xx;//电机脉冲顺序变量
- uint xh; //升半旗时候在最高点停留时间
- /*自定义液晶字库中没有的字模*/
- uchar code zdy[][32]={
- {0x00,0x00,0x07,0x20,0x3C,0x20,0x04,0x20,0x04,0x20,0x04,0x24,0x7F,0xFE,0x04,0x20,
- 0x04,0x20,0x04,0x20,0x08,0x20,0x08,0x20,0x10,0x20,0x20,0x20,0x40,0x20,0x00,0x00},/*"升",0*/
- {0x00,0x80,0x78,0xF8,0x49,0x10,0x52,0xA0,0x50,0x40,0x60,0xB0,0x53,0x4E,0x48,0x40,
- 0x4B,0xF8,0x68,0x40,0x52,0x40,0x43,0xFC,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40},/*"降",1*/
- {0x00,0x7C,0x3F,0x80,0x02,0x20,0x04,0x20,0x08,0x40,0x1F,0x80,0x03,0x20,0x0C,0x10,
- 0x3F,0xF8,0x10,0x8C,0x04,0xA0,0x08,0x90,0x10,0x88,0x20,0x84,0x42,0x84,0x01,0x00},/*"系",2*/
- {0x10,0x40,0x10,0x20,0x23,0xFE,0x20,0x40,0x44,0x40,0xF8,0x88,0x09,0x04,0x13,0xFE,
- 0x20,0x94,0x7C,0x90,0x00,0x90,0x00,0x90,0x1D,0x12,0xE1,0x12,0x02,0x0E,0x04,0x00},/*"统",3*/
- };
- //下面代码横是列地址 竖是行地址 16*64 拆开16*32+16*32*/
- uchar code bmp1[]={
- 0x02,0x08,0x00,0x08,0x00,0x00,0x04,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0xD0,0x06,0x04,0x08,0x03,0x02,0x00,0x38,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x41,0x00,0x09,0x43,0x00,0x04,0xA1,0x80,0x41,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x42,0x00,0x29,0x00,0x00,0x14,0x80,0x00,0x8E,0x00,0xE7,0x00,0x00,0x00,0x00,0x00,
- 0x32,0x88,0x29,0x30,0x04,0x14,0x98,0x00,0x70,0x00,0x1C,0x80,0x00,0x00,0x00,0x00,
- 0x0C,0x00,0x16,0x48,0x00,0x0B,0x24,0x00,0x20,0x00,0x08,0x60,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x4D,0x80,0x00,0x26,0xC0,0x00,0x40,0x00,0x08,0x10,0x00,0x00,0x00,0x00,
- 0x40,0x20,0x00,0x10,0x00,0x00,0x08,0x00,0x4C,0x00,0x08,0x10,0x00,0x00,0x00,0x00,
- 0x04,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x00,0x08,0x20,0x00,0x00,0x00,0x00,
- 0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x08,0x09,0xC0,0x00,0x00,0x00,0x00,
- 0x18,0x10,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x40,0x00,0x00,0x00,0x00,
- 0x25,0x3F,0xC3,0xFE,0x3F,0xE0,0x00,0x01,0x10,0x04,0x00,0x40,0x00,0x00,0x00,0x00,
- 0xA4,0x08,0x40,0x20,0x00,0x41,0x80,0x01,0x10,0x00,0x00,0x20,0x00,0x00,0x00,0x00,
- 0xA4,0xC8,0x47,0xFF,0x00,0x83,0xC0,0x01,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,
- 0x59,0x28,0x40,0x20,0x03,0x03,0xC0,0x02,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,
- 0x36,0x08,0x81,0xFC,0x02,0x03,0xC0,0x02,0xF0,0x40,0x10,0x20,0x00,0x00,0x00,0x00,
- 0x00,0x44,0x81,0x24,0x02,0x01,0x80,0x05,0x0C,0x04,0x10,0x20,0x00,0x00,0x00,0x00,
- 0x00,0x05,0x01,0xFC,0x02,0x01,0x80,0x05,0x42,0x00,0x14,0x40,0x00,0x00,0x00,0x00,
- 0x00,0x02,0x01,0x24,0x02,0x00,0x00,0x05,0x51,0x00,0x1C,0x40,0x00,0x00,0x00,0x00,
- 0x00,0x05,0x03,0xFE,0x02,0x01,0x80,0x04,0x91,0x03,0xC0,0x40,0x00,0x00,0x00,0x00,
- 0x00,0x18,0xC0,0x20,0x02,0x01,0x80,0x04,0x61,0x00,0x00,0x40,0x00,0x00,0x00,0x00,
- 0x02,0x60,0x37,0xFF,0x0E,0x00,0x00,0x04,0x1E,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
- 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
- 0x40,0x7F,0xF4,0x0A,0x3B,0xF3,0xFE,0x01,0x01,0xF0,0x01,0x00,0x00,0x00,0x00,0x00,
- 0x38,0x05,0x02,0x0A,0x2A,0x90,0x04,0x00,0xC7,0xF8,0x02,0x00,0x00,0x00,0x00,0x00,
- 0x04,0x3F,0xE2,0xFF,0x2A,0x90,0x08,0x00,0x70,0xF8,0x04,0x00,0x00,0x00,0x00,0x00,
- 0x20,0x25,0x20,0x88,0x3A,0x90,0x30,0x00,0xC7,0x00,0x08,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x3F,0xE2,0xF9,0x2B,0xF0,0x20,0x01,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x04,0x02,0x89,0x2A,0x00,0x20,0x02,0x20,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
- 0x41,0x7F,0xF4,0xFA,0x3A,0x00,0x20,0x04,0x48,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
- 0x18,0x08,0x84,0xD4,0x2A,0x00,0x20,0x08,0x80,0x08,0x00,0x80,0x00,0x00,0x00,0x00,
- 0x20,0x1D,0x05,0x75,0x2A,0x10,0x20,0x09,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,
- 0x40,0x03,0x85,0x4B,0x4A,0x10,0x20,0x12,0x30,0x00,0x80,0x40,0x00,0x00,0x00,0x00,
- 0x50,0x7C,0x62,0x11,0x5B,0xF0,0xE0,0x13,0x8F,0xC0,0x80,0x20,0x00,0x00,0x00,0x00,
- 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x78,0x3F,0x00,0x20,0x00,0x00,0x00,0x00,
- 0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x03,0xC0,0x80,0x20,0x00,0x00,0x00,0x00,
- 0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x28,0x00,0x0F,0x80,0x20,0x00,0x00,0x00,0x00,
- 0x84,0x80,0x00,0x1C,0x08,0x0E,0x04,0x28,0x00,0x00,0x40,0x20,0x00,0x00,0x00,0x00,
- 0x04,0x1C,0x08,0x22,0x00,0x11,0x00,0x18,0x00,0x00,0x20,0x20,0x00,0x00,0x00,0x00,
- 0x02,0x22,0x00,0x21,0x00,0x10,0x80,0x08,0x00,0x00,0x20,0x20,0x00,0x00,0x00,0x00,
- 0xA1,0xA1,0x00,0x24,0xE0,0x12,0x70,0x10,0x00,0x00,0x30,0x20,0x00,0x00,0x00,0x00,
- 0x80,0x24,0xE0,0x18,0x10,0x0C,0x08,0x10,0x00,0x00,0x28,0x20,0x00,0x00,0x00,0x00,
- 0x98,0x18,0x10,0x00,0x80,0x00,0x40,0x10,0x08,0x00,0x25,0xE0,0x00,0x00,0x00,0x00,
- 0x24,0x00,0x81,0x00,0x00,0x80,0x00,0x1A,0x08,0x00,0x22,0x20,0x00,0x00,0x00,0x00,
- 0xC1,0x00,0x00,0x00,0x00,0x00,0x00,0x1D,0x46,0x00,0x22,0xA0,0x00,0x00,0x00,0x00,
- 0x08,0x00,0x00,0x01,0x04,0x00,0x82,0x0F,0x93,0x40,0x21,0x40,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,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- };
- void csh();
- void delay(uint z) //0.1MS 准确延时 z=1
- {
- uint x,y;
- for (x=0;x<z;x++)
- for(y=0;y<11;y++);
- }
- void busy() //查忙
- { RS=0;
- RW=1;
- E=1;
- lcdbus=0xff;
- while((lcdbus&0x80)==0x80);
- E=0;
- }
- void write_com(uchar xiezl) //写指令
- {
- busy();
- RS=0;
- RW=0;
- E=1;
- lcdbus=xiezl;
- delay(5);
- E=0;
- delay(5);
- }
- void write_data(uchar xiesj) //写数据
- {
- busy();
- RS=1;
- RW=0;
- E=1;
- lcdbus=xiesj;
- delay(5);
- E=0;
- delay(5);
- }
- void write_tp(uchar code *p) /*显示图片-*/
- {
- uint j=0;
- uchar x,y,i;
- for(i=0;i<9;i+=8)
- for(y=0;y<32;y++)
- for(x=0;x<8;x++)
- { write_com(0x36); //扩充指令集
- write_com(y+0x80); //行地址
- write_com(x+0x80+i); //列地址
- write_com(0x30);
- write_data(p[j++]);
- write_data(p[j++]);
- }
- }
-
- void clrscreen() //清液晶屏
- {
- write_com(0x01);
- delay(10);
- }
- void clrgdram() //清液晶ram
- {
- uchar 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 lcdcsh() //液晶初始化
- { delay(2000);
- write_com(0x30);
- delay(10);
- write_com(0x30);
- delay(5);
- write_com(0x0c); //开显示(无游标、不反白)
- delay(10);
- write_com(0x01); //清除显示,并且设定地址指针为00H
- delay(500);
- write_com(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位
- delay(0);
- }
- void xianshi(uchar code *s) //用指针显示字符串
- { //各种字符都用字符串显示出来 方法简单
- while(*s>0)
- {
- write_data(*s);
- s++;
- delay(50);
- }
- }
- void jin88() //液晶上电显示
- {
- int i=0;
- write_com(0x01);
- delay(5);
- write_com(0x80);
- xianshi("题目:单片机控制");
- write_com(0x90);
- xianshi("国旗自动");
- write_com(0x95);
- write_com(0x88);
- xianshi("本项目由锦88独立");
- write_com(0x98);
- xianshi("制作完成");
-
- }
- void CGRAM() //自定义 四个汉字字模
- {
- //四个汉字分别是 升降系统
- int i,j;
- int jj=0;
- write_com(0x30);
- for(j=0;j<4;j++)
-
- {
- write_com(0x40+jj);
- for(i=0;i<16;i++)
- {
- write_data(zdy[j][i*2]);
- write_data(zdy[j][i*2+1]);
- }
- jj=jj+0x10;
-
- }
- }
- //电机部分
- void djyz1() //升国旗 正转
- {
- clrscreen();
- write_com(0x81);
- xianshi("大家请起立");
- write_com(0x91) ;
- xianshi("奏国歌 国旗");
- write_com(0x95); //调用自定义的CGRAM的代码
- write_data(0x00);
- write_data(0x00);
- write_com(0x89);
- xianshi("请行注目礼");
- delay(700000);
- //此处再加声音 用两个口控制apr9600 使其放音
- clrscreen();
- write_com(0x80);
- xianshi("国旗高度:000厘米");
- write_com(0x90);
- xianshi("所用时间: 00秒");
-
- flag4=1; //电机正转
- TR1=1;
- xx=0;
- TR0=1;
- sj=0;
- gd1=0;
- while(zdbz1)
- {
- /*此处再加声音 用两个口控制apr9600 使其放音 */
- write_com(0x84);
- write_data(0x7c);
- write_data(0x30+gd13); //时间高度自加
- write_com(0x85);
- write_data(0x30+gd12);
- write_data(0x30+gd11);
- write_com(0x94);
- write_data(0x7c);
- write_data(0x30+sj3);
- write_com(0x95);
- write_data(0x30+sj2);
- write_data(0x30+sj1);
- }
- TR1=0;
- TR0=0;
-
- flag1=0;
- flag2=0;
- flag3=1;
- }
- void djyz2() //升半旗 先正转 再反转
- {
- clrscreen();
- write_com(0x81);
- xianshi("大家请起立");
- write_com(0x91) ;
- xianshi("奏国歌 国旗");
- write_com(0x95); //调用自定义的CGRAM的代码 显示‘升’
- write_data(0x00);
- write_data(0x00);
- write_com(0x89);
- xianshi("请行注目礼");
- delay(700000);
- //此处再加声音
- clrscreen();
- write_com(0x80);
- xianshi("国旗高度:000厘米");
- write_com(0x90);
- xianshi("所用时间: 00秒");
-
- xx=0;
- flag4=1; //电机正转
- TR1=1;
- TR0=1;
- sj=0;
- gd1=0;
- while(zdbz1)
- {
- write_com(0x84);
- write_data(0x7c);
- write_data(0x30+gd13);
- write_com(0x85);
- write_data(0x30+gd12);
- write_data(0x30+gd11);
- write_com(0x94);
- write_data(0x7c);
- write_data(0x30+sj3);
- write_com(0x95);
- write_data(0x30+sj2);
- write_data(0x30+sj1);
- }
- TR1=0;
- for(xh=0;xh<30000;xh++)
- {
- write_com(0x94);
- write_data(0x7c);
- write_data(0x30+sj3);
- write_com(0x95);
- write_data(0x30+sj2);
- write_data(0x30+sj1);
- }
- xx=4;
- flag4=0; //电机反转
- gd21=gd11;
- gd22=gd12;
- gd23=gd13;
- TR1=1;
- while(zdbz2)
- {
- write_com(0x84);
- write_data(0x7c);
- write_data(0x30+gd13);
- write_com(0x85);
- write_data(0x30+gd12);
- write_data(0x30+gd11);
- write_com(0x94);
- write_data(0x7c);
- write_data(0x30+sj3);
- write_com(0x95);
- write_data(0x30+sj2);
- write_data(0x30+sj1);
-
- }
- TR1=0;
- TR0=0;
- flag1=0;
- flag2=0;
- flag3=1;
- }
- void djyz3() //降
- {
- xx=4;
- TR1=1;
- flag4=0; //电机反转 此处没有时间高度显示
- while(zdbz3);
- TR1=0;
- csh();
- flag1=1;
- flag2=1;
- flag3=0;
- }
- void djyz()
- {
- if (aa==1)
- {
- aa=0;
- if(flag1)djyz1();
- }
- if(bb==1)
- {
- bb=0;
- if(flag2)djyz2();
- }
- if(cc==1)
- {
- cc=0;
- if(flag3)djyz3();
- }
- }
- void jpsm() //键盘扫描
- {
- do
- {
- if(((key1||key2)==1)||((key1||key3)==1)||((key3||key2)==1) ) //防止两键同时按下
- {
- if(key1==0)
- {
- delay(2);
- if(key1==0)
- aa=1;
- }
- else if(key2==0)
- {
- delay(2);
- if(key2==0)
- bb=1;
- }
- else if(key3==0)
- {
- delay(2);
- if(key3==0)
- cc=1;
- }
- }
-
- }while((aa||bb||cc)==0) ;
- }
- void csh()
- {
- //初始化程序
- flag1=1;
- flag2=1;
- flag3=0;
- zdbz1=1;
- zdbz2=1;
- zdbz3=1;//三个干簧管 低电平时 电机停转
- sj=0; //时间高度开始值0
- gd1=0;
-
- TMOD=0x11;
- TH0=(65536-50000)/256; //定时50ms
- TL0=(65536-50000)%256;
- TH1=(65536-100)/256;
- TL1=(65536-100)%256; //定时0.1ms
- EA=1;
- ET0=1;
- ET1=1;
- TR0=0;
- TR1=0;
- }
- void yjcsxs()
- {
- jin88();//液晶初始化后显示
- /*因为升降系统无法直接 显示 我就自定义了四个字的字模*/
- write_com(0x94); //调用自定义的CGRAM的代码
- write_data(0x00);
- write_data(0x00); //
- delay(10);
- write_com(0x95); //调用字库中没有的汉字
- write_data(0x00);
- write_data(0x02); //
- write_com(0x96);
- write_data(0x00);
- write_data(0x04); //
- delay(10);
- write_com(0x97);
- write_data(0x00);
- write_data(0x06);
- delay(10);
- /*再显示一幅图画*/
- clrscreen();
- clrgdram();
- write_tp(bmp1); //
- }
-
-
- void main()
- {
- lcdcsh();//液晶初始化
- CGRAM();//定义字库中没有的字符 升降系统 四个字
- yjcsxs();
- csh(); //电机初始化
- while(1)
- {
- jpsm();//键盘扫描
- djyz();//电机运行
- }
- }
- void time1() interrupt 3 //定时器1控制电机正反转速度
- {
- TH1=(65535-100)/256; //定时0.1ms
- TL1=(65535-100)%256;
- tt1++;
- if(flag4) //正转
- {
- if(tt1==70)
- {
- tt1=0;
- P2=table[xx++] ; //P2口控制电机
- if(xx==4) xx=0;
- }
- }
- else //反转
- {
- if(tt1==80)
- {
- tt1=0;
- P2=table[--xx];
- if(xx==0) xx=4;
- }
- }
- }
- void time0() interrupt 1 //定时器0控制时间高度
- {
-
- TH0=(65535-50000)/256; //定时 50ms
- TL0=(65535-50000)%256;
- tt0++;
- tt++;
- if(tt0==6)
- {
- tt0=0;
- gd12=gd1%100/10;
- gd11=gd1%10 ;
- gd13=gd1/100;
-
- gd21=gd2%10;
- gd22=gd2%100/10;
- gd23=gd2/100;
- gd1++;
- gd2--;
- }
- if(tt==20)
- {
- tt=0;
- sj1=sj%10 ;
- sj2=sj%100/10;
- sj3=sj/100;
- sj++ ;
- }
-
- }
复制代码
|