简单介绍下,我的初衷是控制步进电机旋转方向和速度,并以12864显示
设计中有四个按钮开关,从上到下依次是:控制旋转的开关;控制方向的开关;增加速度的按钮;减少速度的按钮。我遇到的问题是:1、点击“增速按钮"和”减速按钮“时,12864显示屏都会出现故障。但如果把sp(speed)从中断程序中放到main函数里,就不会出现显示故障。 请问sp(speed)放到中断程序中,哪里出了问题?怎么改? (注:sp(int i)作用是当按下加减速按钮时,12864的显示不同的速度等级)
2、
proteus仿真遇到这个问题,怎么解决?
我发现第2个问题和main函数一开始的这一段有关: LCDMcs=1; //右屏开显示 LCDScs=0; hz_LCDDisp16(4,0,hz_ji); LCDMcs=0; //左屏开显示 LCDScs=1; hz_LCDDisp16(0,0,hz_zhuang); hz_LCDDisp16(0,16,hz_tai); hz_LCDDisp16(0,32,hz_maohao); hz_LCDDisp16(2,0,hz_fang); hz_LCDDisp16(2,16,hz_xiang); hz_LCDDisp16(2,32,hz_maohao); hz_LCDDisp16(4,0,hz_su); hz_LCDDisp16(4,16,hz_du); hz_LCDDisp16(4,32,hz_maohao); hz_LCDDisp16(4,48,s1);
============================================================ 以下是代码 和附件============================================================
新建文件夹.rar
(79.16 KB, 下载次数: 60)
============================================================#include <reg52.h>#define uchar unsigned char#define uint unsigned int#define LCDLCDDisp_Off 0x3e #define LCDLCDDisp_On 0x3f#define Page_Add 0xb8//页地址#define LCDCol_Add 0x40//列地址#define Start_Line 0xC0//行地址/*****液晶显示器的端口定义*****/#define data_ora P0 /*液晶数据总线*/sbit LCDMcs=P2^1 ; /*片选1*/sbit LCDScs=P2^2 ; /*片选2*/sbit RESET=P2^3 ; /*复位信号*/sbit LCDDi=P2^4 ; /*数据/指令 选择*/sbit LCDRW=P2^5 ; /*读/写 选择*/sbit LCDEnable=P2^6 ; /*读/写 使能*//*数字1~6*/uchar code s1[]= {0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uchar code s2[]={0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uchar code s3[]={0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uchar code s4[]={0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uchar code s5[]={0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uchar code s6[]={0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uchar code s7[]={0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};uchar code s8[]={0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
/*汉字*/uchar code hz_zhuang[]={0x00,0x08,0x30,0x00,0xFF,0x20,0x20,0x20,0x20,0xFF,0x20,0x20,0x22,0x2C,0x20,0x00,0x04,0x04,0x02,0x01,0xFF,0x80,0x40,0x30,0x0E,0x01,0x06,0x18,0x20,0x40,0x80,0x00};uchar code hz_tai[]={0x00,0x04,0x84,0x84,0x44,0x24,0x54,0x8F,0x14,0x24,0x44,0x84,0x84,0x04,0x00,0x00,0x41,0x39,0x00,0x00,0x3C,0x40,0x40,0x42,0x4C,0x40,0x40,0x70,0x04,0x09,0x31,0x00};uchar code hz_fang[]={0x08,0x08,0x08,0x08,0x08,0xF8,0x89,0x8E,0x88,0x88,0x88,0x88,0x08,0x08,0x08,0x00,0x00,0x80,0x40,0x20,0x18,0x07,0x00,0x00,0x40,0x80,0x40,0x3F,0x00,0x00,0x00,0x00};uchar code hz_xiang[]={0x00,0xF8,0x08,0x08,0x0C,0xCA,0x49,0x48,0x48,0xC8,0x08,0x08,0x08,0xF8,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x1F,0x08,0x08,0x08,0x1F,0x00,0x40,0x80,0x7F,0x00,0x00};uchar code hz_maohao[]={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};uchar code hz_kai[]={0x80,0x82,0x82,0x82,0xFE,0x82,0x82,0x82,0x82,0x82,0xFE,0x82,0x82,0x82,0x80,0x00,0x00,0x80,0x40,0x30,0x0F,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00};uchar code hz_guan[]={0x00,0x00,0x10,0x11,0x16,0x10,0x10,0xF0,0x10,0x10,0x14,0x13,0x10,0x00,0x00,0x00,0x81,0x81,0x41,0x41,0x21,0x11,0x0D,0x03,0x0D,0x11,0x21,0x41,0x41,0x81,0x81,0x00};uchar code hz_zheng[]={0x00,0x02,0x02,0xC2,0x02,0x02,0x02,0xFE,0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x00,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00};uchar code hz_fan[]={0x00,0x00,0xFC,0x24,0x24,0xE4,0x24,0x24,0x22,0x22,0x22,0xA3,0x62,0x00,0x00,0x00,0x40,0x30,0x8F,0x80,0x40,0x40,0x23,0x14,0x08,0x14,0x22,0x41,0x40,0x80,0x80,0x00};uchar code hz_su[]={0x40,0x40,0x42,0xCC,0x00,0x04,0xF4,0x94,0x94,0xFF,0x94,0x94,0xF4,0x04,0x00,0x00,0x00,0x40,0x20,0x1F,0x20,0x48,0x44,0x42,0x41,0x5F,0x41,0x42,0x44,0x48,0x40,0x00};uchar code hz_du[]={0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00};uchar code hz_ji[]={0x20,0x30,0xAC,0x63,0x30,0x00,0x02,0x02,0xFE,0x02,0x02,0x62,0x5A,0xC6,0x00,0x00,0x22,0x67,0x22,0x12,0x12,0x40,0x30,0x8F,0x80,0x43,0x2C,0x10,0x2C,0x43,0x80,0x00};
/****************************************************************************函数功能:LCD延时程序入口参数:t出口参数:****************************************************************************/void LCDdelay(unsigned int t){ unsigned int i,j; for(i=0;i<t;i++); for(j=0;j<10;j++);}/****************************************************************************状态检查,LCD是否忙
*****************************************************************************/void CheckState() { unsigned char dat,DATA;//状态信息(判断是否忙) LCDDi=0; // 数据指令选择,D/I(RS)="L" ,表示 DB7∽DB0 为显示指令数据 LCDRW=1; //R/W="H" ,E="H"数据被读到DB7∽DB0 do { DATA=0x00; LCDEnable=1; //EN下降源 LCDdelay(2);//延时 dat=DATA; LCDEnable=0; dat=0x80 & dat; //仅当第7位为0时才可操作(判别busy信号) } while(!(dat==0x00));}/*****************************************************************************函数功能:写命令到LCD程序,RS(DI)=L,RW=L,EN=H,即来一个脉冲写一次入口参数:cmdcode出口参数:*****************************************************************************/void write_com(unsigned char cmdcode){ CheckState();//检测LCD是否忙 LCDDi=0; LCDRW=0; P0=cmdcode; LCDdelay(2); LCDEnable=1; LCDdelay(2); LCDEnable=0;}/*****************************************************************************函数功能:LCD初始化程序入口参数:出口参数:*****************************************************************************/void init_lcd(){ LCDdelay(100); LCDMcs=1;//刚开始关闭两屏 LCDScs=1; LCDdelay(100); write_com(LCDLCDDisp_Off); //写初始化命令 write_com(Page_Add+0); write_com(Start_Line+0); write_com(LCDCol_Add+0); write_com(LCDLCDDisp_On);}/*****************************************************************************函数功能:写数据到LCD程序,RS(DI)=H,RW=L,EN=H,即来一个脉冲写一次入口参数:LCDDispdata出口参数:*****************************************************************************/void write_data(unsigned char LCDDispdata){ CheckState();//检测LCD是否忙 LCDDi=1; LCDRW=0; P0=LCDDispdata; LCDdelay(2); LCDEnable=1; LCDdelay(2); LCDEnable=0;}/*****************************************************************************函数功能:清除LCD内存程序入口参数:pag,col,hzk出口参数:*****************************************************************************/void Clr_Scr(){ unsigned char j,k; LCDMcs=0; //左、右屏均开显示 LCDScs=0; write_com(Page_Add+0); write_com(LCDCol_Add+0); for(k=0;k<8;k++)//控制页数0-7,共8页 { write_com(Page_Add+k); //每页每页进行写 for(j=0;j<64;j++) //每页最多可写32个中文文字或64个ASCII字符 { write_com(LCDCol_Add+j); write_data(0x00);//控制列数0-63,共64列,写点内容,列地址自动加1 } }}/*****************************************************************************函数功能:指定位置显示汉字16*16程序入口参数:page,column,hzk出口参数:*****************************************************************************/void hz_LCDDisp16(unsigned char page,unsigned char column, unsigned char code *hzk){ unsigned char j=0,i=0; for(j=0;j<2;j++) { write_com(Page_Add+page+j); write_com(LCDCol_Add+column); for(i=0;i<16;i++) write_data(hzk[16*j+i]); }}
#include "stdlib.h"#include "reg52.h"#include "CONFIG.H"sbit SW1=P3^0;sbit SW2=P3^1;sbit KEY1=P3^2;sbit KEY2=P3^3;unsigned char hh=6;int ang=900; //旋转角度int n=0;int z=1; //判断正转到位int f=0; //判断负转到位int i=0;int speed=1;code unsigned char runf[8]={0x03,0x01,0x09,0x08,0x0c,0x04,0x06,0x02}; //反转(逆时针)code unsigned char runz[8]={0x02,0x06,0x04,0x0c,0x08,0x09,0x01,0x03}; //正转(顺时针)void DELAY();void in
tial();
void sp(int i){switch(i){case 1: hz_LCDDisp16(4,48,s1); break; case 2: hz_LCDDisp16(4,48,s2); break; case 3: hz_LCDDisp16(4,48,s3); break; case 4: hz_LCDDisp16(4,48,s4); break; case 5: hz_LCDDisp16(4,48,s5); break; case 6: hz_LCDDisp16(4,48,s6); break; case 7: hz_LCDDisp16(4,48,s7); break; case 8: hz_LCDDisp16(4,48,s8); break; default: break;}}main(){ intial(); Clr_Scr(); LCDMcs=1; //右屏开显示 LCDScs=0; hz_LCDDisp16(4,0,hz_ji); LCDMcs=0; //左屏开显示 LCDScs=1; hz_LCDDisp16(0,0,hz_zhuang); hz_LCDDisp16(0,16,hz_tai); hz_LCDDisp16(0,32,hz_maohao); hz_LCDDisp16(2,0,hz_fang); hz_LCDDisp16(2,16,hz_xiang); hz_LCDDisp16(2,32,hz_maohao); hz_LCDDisp16(4,0,hz_su); hz_LCDDisp16(4,16,hz_du); hz_LCDDisp16(4,32,hz_maohao); hz_LCDDisp16(4,48,s1);
while(1){ if(SW1) //判断电机是否运行 { hz_LCDDisp16(0,48,hz_kai); if(SW2) //判断是否反转 { hz_LCDDisp16(2,48,hz_fan); if(ang==0) { for(i=0;i<8;i++) { P1=runf
; DELAY(); } } else { if(f) { n=ang/45; while(n>0) { for(i=8-((i-1)%8);n>0;n--) { P1=runf[i%8]; i++; DELAY(); } } f=0;z=1; } } } else { hz_LCDDisp16(2,48,hz_zheng); if(ang==0) { for(i=0;i<8;i++) { P1=runz; DELAY(); } } else {if(z) {n=ang/45; while(n>0) { for(i=0;n>0;n--) { P1=runz[i%8]; i++; DELAY(); } } z=0;f=1; } } } } else { hz_LCDDisp16(0,48,hz_guan); if(SW2) {hz_LCDDisp16(2,48,hz_fan);} else {hz_LCDDisp16(2,48,hz_zheng);} } }}void intial(){TMOD=0x10;TH1=0x3c;//定时50msTL1=0xb0;TR1=1;IE=0x8d;
}
void DELAY(){unsigned char i,j; i=(50-hh)*10; for(;i>0;i--) { j=(50-hh)*10; for(;j>0;j--);}}void time_1()interrupt 3 using 3{TH1=0x3c;TL1=0xb0;}
void int_0() interrupt 0 using 0{unsigned int i;hh=hh+2;speed=hh/2-2; sp(speed); for(i=10000000;i>0;i--) {if(hh>=20) { hh=20;speed=8;} }}
void int_1() interrupt 2 using 2{unsigned int i;hh=hh-2;speed=hh/2-2;sp(speed); for(i=10000000;i>0;i--) {if(hh<=6) { hh=6;speed=1;} }}
可是我这个proteus仿真都不正常额
我 用的是430系列
一周热门 更多>