设计一个单片机里程表用的中断0外部检测信号,定时器1做1秒更新:具体如下
中断0函数
{
count++;
}
定时器1函数
{
//50ms初值
a++;
if(a==20) //1秒时间
{
a=0;
num=count; //count的值给 num, num送到显示屏
_nop_;
_nop_;
count=0; //count数清零
}
}
问题是,count清零时,num也跟着变了,在显示屏上的反应就是屏上的数值1S跳动一次,可是被测信号一直是没有变的,
看了此论坛好多相似的程序,都是这么操作的,他们为什么可以,我的为什么不行呢,
搞 了几天了,知道的方法都试了就是不行
完整代码如下:
- #include"STC15F2K60S2.H"
- //#include <reg52.H>
- #include <intrins.h>
- #include <chinese_code.h>
- //sbit key=P3^2;
- sbit cs1=P1^4;
- sbit reset=P1^5;
- sbit rs=P1^3;
- sbit sclk=P1^0;
- sbit sid=P1^1;
- sbit led=P0^0;
- //sbit cruise =P1^0; // 开关
- sbit plus_A =P3^7; // 速度上升
- //sbit cool =P1^3; //
- sbit plus_B =P3^6; // 温度下降
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- uint count;
- static ulong speed;
- uint a;
- uint speed_set, temp_set;
- bit flag;
- /*延时:1毫秒的i倍*/
- void delay(int i)
- {
- int j,k;
- for(j=0;j<i;j++)
- for(k=0;k<110;k++);
- }
- /*延时:1us的i倍*/
- void delay_us(int i)
- {
- int j,k;
- for(j=0;j<i;j++)
- for(k=0;k<1;k++);
- }
- //=============transfer command to LCM===============
- void transfer_command(int data1)
- {
- char i;
- cs1=0;
- rs=0;
- for(i=0;i<8;i++)
- {
- sclk=0;
- if(data1&0x80) sid=1;
- else sid=0;
- sclk=1;
- data1=data1<<=1;
- }
- }
- //-----------transfer data to LCM---------------
- void transfer_data(int data1)
- {
- char i;
- cs1=0;
- rs=1;
- for(i=0;i<8;i++)
- {
- sclk=0;
- if(data1&0x80) sid=1;
- else sid=0;
- sclk=1;
- data1=data1<<=1;
- }
- }
- void initial_lcd()
- {
- reset=0;
- delay(100);
- reset=1;
- delay(100);
- transfer_command(0x30); //EXT=0
- transfer_command(0x94); //Sleep out
- transfer_command(0x31); //EXT=1
- transfer_command(0xD7); //Autoread disable
- transfer_data(0X9F); //
- transfer_command(0x32); //Analog SET
- transfer_data(0x00); //OSC Frequency adjustment
- transfer_data(0x01); //Frequency on booster capacitors->6KHz
- transfer_data(0x04); //Bias=1/11
- transfer_command(0x20); // Gray Level
- transfer_data(0x01);
- transfer_data(0x03);
- transfer_data(0x05);
- transfer_data(0x07);
- transfer_data(0x09);
- transfer_data(0x0b);
- transfer_data(0x0d);
- transfer_data(0x10);
- transfer_data(0x11);
- transfer_data(0x13);
- transfer_data(0x15);
- transfer_data(0x17);
- transfer_data(0x19);
- transfer_data(0x1b);
- transfer_data(0x1d);
- transfer_data(0x1f);
- transfer_command(0x30); // EXT1=0,EXT0=0,表示选择了“扩展指令表1”
- transfer_command(0x75); // 页地址设置
- transfer_data(0X00); // 起始页地址:YS=0X00
- transfer_data(0X14); // 结束页地址:YE=0x1F 每4行为一页,第0~3行为第0页,第124~127行为第31页(31=0x1f)
- transfer_command(0x15); // 列地址设置
- transfer_data(0X00); // 起始列地址:XS=0
- transfer_data(0Xff); // 结束列地址:XE=256(0xff)
- transfer_command(0xBC); //Data scan direction
- transfer_data(0x00); //MX.MY=Normal
- transfer_data(0xA6);
-
- transfer_command(0x0c); //数据格式选择,0x0C是低位在前D0-D7,0x08是高位在前D7-D0
- transfer_command(0xCA); //显示控制
- transfer_data(0X00); // 设置CL驱动频率:CLD=0
- transfer_data(0X4F); //占空比:Duty=128
- transfer_data(0X20); //N行反显:Nline=off
- transfer_command(0xF0); //显示模式
- transfer_data(0X10); //如果设为0x11:表示选择4灰度级模式,如果设为0x10:表示选择黑白模式
- transfer_command(0x81); //设置对比度,“0x81”不可改动,紧跟着的2个数据是可改的,但“先微调后粗调”这个顺序别乱了
- transfer_data(0x2f); //对比度微调,可调范围0x00~0x3f,共64级 //0x2f
- transfer_data(0x02); //对比度粗调,可调范围0x00~0x07,共8级 //0x02
- transfer_command(0x20); //Power control
- transfer_data(0x0B); //D0=regulator ; D1=follower ; D3=booste, on:1 off:0
- delay_us(100);
- transfer_command(0xAF); //打开显示
- }
- /*写LCD 行列地址:X为起始的列地址,Y为起始的行地址,x_total,y_total分别为列地址及行地址的起点到终点的差值 */
- void lcd_address(int x,int y,x_total,y_total)
- {
- x=x-1;
- y=y-1;//+9
-
- transfer_command(0x15); //Set Column Address
- transfer_data(x);
- transfer_data(x+x_total-1);
- transfer_command(0x75); //Set Page Address
- transfer_data(y);
- transfer_data(y+y_total-1);
- transfer_command(0x30);
- transfer_command(0x5c);
- }
- /*清屏*/
- void clear_screen()
- {
- int i,j;
- lcd_address(0,0,161,11);
- for(i=0;i<20;i++)
- {
- for(j=0;j<161;j++)
- {
- transfer_data(0x00);
- }
- }
- }
- /*显示160*80点阵的图像*/
- void disp_160x80(int x,int y,char *dp)
- {
- int i,j;
- lcd_address(x,y,160,10);
- for(i=0;i<10;i++)
- {
- for(j=0;j<160;j++)
- {
- transfer_data(*dp);
- dp++;
- }
- }
- }
- /*显示160*8点阵的图像*/
- /*void disp_160x8(int x,int y,char *dp)
- {
- int i,j;
- lcd_address(x,y,160,1);
- for(j=0;j<160;j++)
- {
- transfer_data(*dp);
- dp++;
- }
-
- } */
- /*显示8*16点阵的汉字或等同于8*16点阵的图像*/
- void disp_8x16(int x,int y,uchar *dp)
- {
- int i,j;
- lcd_address(x,y,8,2);
- for(i=0;i<2;i++)
- {
- for(j=0;j<8;j++)
- {
- transfer_data(*dp);
- dp++;
- }
- }
- }
- /*显示8*48点阵的汉字或等同于8*48点阵的图像*/
- void disp_8x48(int x,int y,uchar *dp)
- {
- int i,j;
- lcd_address(x,y,8,6);
- for(i=0;i<6;i++)
- {
- for(j=0;j<8;j++)
- {
- transfer_data(*dp);
- dp++;
- }
- }
- }
- /*显示16*32点阵的汉字或等同于16*32点阵的图像*/
- void disp_16x32(int x,int y,uchar *cc)
- {
- int i,j;
- lcd_address(x,y,16,4);
- for(i=0;i<4;i++)
- {
- for(j=0;j<16;j++)
- {
- transfer_data(*cc);
- cc++;
- }
- }
- }
- void disp_cruise()
- {
- disp_8x16(8*1,5, dac);
- disp_8x16(8*2,5, dar);
- disp_8x16(8*3,5, dau);
- disp_8x16(8*4,5, dai);
- disp_8x16(8*5,5, das);
- disp_8x16(8*6,5, dae);
- disp_8x16(8*10,5, dav);
- disp_8x16(8*11,5, dac);
- disp_8x16(8*12,5, dac);
- disp_8x16(8*15,5, dac);
- disp_8x16(8*16,5, dao);
- disp_8x16(8*17,5, dao);
- disp_8x16(8*18,5, dal); //第一行显示到此
- disp_8x16(8*9,7, x1);
- disp_8x16(8*10,7, x3);
- disp_8x16(8*11,7, dian);
- disp_8x16(8*12,7, x5);
- disp_8x16(8*13,7, xv); //第二行显示
- disp_8x16(8*3,9, dao);
- disp_8x16(8*4,9, dan);
- disp_8x16(8*9,9, da3);
- disp_8x16(8*10,9, da5);
- disp_8x16(8*11,9, dadian);
- disp_8x16(8*12,9, da7);
- disp_8x16(8*13,9, da1c);
- disp_8x16(8*16,9, dao);
- disp_8x16(8*17,9, daf);
- disp_8x16(8*18,9, daf);
- disp_8x48(8*8,5,line1); //显示两条竖线
- disp_8x48(8*14,5,line1); //显示两条竖线
- }
- /****************************************
- **********速度显示函数*************/
- void disp_speed()
- {
- uchar hundred,ten,ge;
- /* if(flag==1)
- {
- flag=0;
- speed=count*6696/1000;
- count=0;
- a=0;
- } */
- hundred=speed/100;
- ten=speed%100/10;
- ge=speed%10;
- disp_8x16(1,2, das); //小S显示
-
- switch (hundred)
- {
- case 0: disp_16x32(16*1,1,c0 );
- break;
- case 1: disp_16x32(16*1,1,c1 );
- break;
- case 2: disp_16x32(16*1,1,c2 );
- break;
- case 3: disp_16x32(16*1,1,c3 );
- break;
- case 4: disp_16x32(16*1,1,c4 );
- break;
- case 5 : disp_16x32(16*1,1,c5 );
-
- break;
- default:
- break;
- }
- switch (ten)
- {
- case 0: disp_16x32(16*2,1,c0);
- break;
- case 1: disp_16x32(16*2,1,c1 );
- break;
- case 2: disp_16x32(16*2,1,c2 );
- break;
- case 3: disp_16x32(16*2,1,c3 );
- break;
- case 4: disp_16x32(16*2,1,c4 );
- break;
- case 5 : disp_16x32(16*2,1,c5 );
- break;
- case 6: disp_16x32(16*2,1,c6 );
- break;
- case 7: disp_16x32(16*2,1,c7 );
- break;
- case 8: disp_16x32(16*2,1,c8 );
- break;
- case 9: disp_16x32(16*2,1,c9 );
- break;
- }
- switch (ge)
- {
- case 0: disp_16x32(16*3,1, c0);
- break;
- case 1: disp_16x32(16*3,1, c1);
- break;
- case 2: disp_16x32(16*3,1, c2);
- break;
- case 3: disp_16x32(16*3,1, c3);
- break;
- case 4: disp_16x32(16*3,1, c4);
- break;
- case 5 : disp_16x32(16*3,1, c5);
- break;
- case 6: disp_16x32(16*3,1, c6);
- break;
- case 7: disp_16x32(16*3,1, c7);
- break;
- case 8: disp_16x32(16*3,1, c8);
- break;
- case 9: disp_16x32(16*3,1, c9);
- break;
- }
- disp_16x32(16*5,1, ck);
- disp_16x32(16*6,1, cm);
- disp_16x32(16*7,1, c11);
- disp_16x32(16*8,1, ch); //此行和上面3行为 KM/h 显示
- }
- /*********速度设定显示函数***************/
- void dispspeed_set()
- {
- uchar hundred,ten,one;
- hundred=speed_set/100;
- ten=speed_set%100/10;
- one=speed_set%10;
- switch (hundred)
- {
- case 0: disp_8x16(8*1,7, x0);
- break;
- case 1: disp_8x16(8*1,7, x1);
- break;
- case 2: disp_8x16(8*1,7, x2);
- break;
- case 3: disp_8x16(8*1,7, x3);
- break;
- case 4: disp_8x16(8*1,7, x4);
- break;
- case 5: disp_8x16(8*1,7, x5);
- break;
- case 6: disp_8x16(8*1,7, x6);
- break;
- case 7: disp_8x16(8*1,7, x7);
- break;
- case 8: disp_8x16(8*1,7, x8);
- break;
- case 9: disp_8x16(8*1,7, x9);
- break;
- default:
- break;
- }
- switch (ten)
- {
- case 0: disp_8x16(8*2,7, x0);
- break;
- case 1: disp_8x16(8*2,7, x1);
- break;
- case 2: disp_8x16(8*2,7, x2);
- break;
- case 3: disp_8x16(8*2,7, x3);
- break;
- case 4: disp_8x16(8*2,7, x4);
- break;
- case 5: disp_8x16(8*2,7, x5);
- break;
- case 6: disp_8x16(8*2,7, x6);
- break;
- case 7: disp_8x16(8*2,7, x7);
- break;
- case 8: disp_8x16(8*2,7, x8);
- break;
- case 9: disp_8x16(8*2,7, x9);
- break;
- default:
- break;
- }
- switch (one)
- {
- case 0: disp_8x16(8*3,7, x0);
- break;
- case 1: disp_8x16(8*3,7, x1);
- break;
- case 2: disp_8x16(8*3,7, x2);
- break;
- case 3: disp_8x16(8*3,7, x3);
- break;
- case 4: disp_8x16(8*3,7, x4);
- break;
- case 5: disp_8x16(8*3,7, x5);
- break;
- case 6: disp_8x16(8*3,7, x6);
- break;
- case 7: disp_8x16(8*3,7, x7);
- break;
- case 8: disp_8x16(8*3,7, x8);
- break;
- case 9: disp_8x16(8*3,7, x9);
- break;
- default:
- break;
- }
-
- disp_8x16(8*4,7, xk);
- disp_8x16(8*5,7, xm);
- disp_8x16(8*6,7, gang);
- disp_8x16(8*7,7, xh);
- }
- /*********温度设定显示函数***************/
- void disptemp_set()
- {
- uchar set1,hundred1,ten1,one1;
- set1=temp_set;
- hundred1=set1/100;
- ten1=set1%100/10;
- one1=set1%10;
- switch (hundred1)
- {
- case 0: disp_8x16(8*15,7, x0);
- break;
- case 1: disp_8x16(8*15,7, x1);
- break;
- case 2: disp_8x16(8*15,7, x2);
- break;
- case 3: disp_8x16(8*15,7, x3);
- break;
- case 4: disp_8x16(8*15,7, x4);
- break;
- case 5: disp_8x16(8*15,7, x5);
- break;
- case 6: disp_8x16(8*15,7, x6);
- break;
- case 7: disp_8x16(8*15,7, x7);
- break;
- case 8: disp_8x16(8*15,7, x8);
- break;
- case 9: disp_8x16(8*15,7, x9);
- break;
- default:
- break;
- }
- switch (ten1)
- {
- case 0: disp_8x16(8*16,7, x0);
- break;
- case 1: disp_8x16(8*16,7, x1);
- break;
- case 2: disp_8x16(8*16,7, x2);
- break;
- case 3: disp_8x16(8*16,7, x3);
- break;
- case 4: disp_8x16(8*16,7, x4);
- break;
- case 5: disp_8x16(8*16,7, x5);
- break;
- case 6: disp_8x16(8*16,7, x6);
- break;
- case 7: disp_8x16(8*16,7, x7);
- break;
- case 8: disp_8x16(8*16,7, x8);
- break;
- case 9: disp_8x16(8*16,7, x9);
- break;
- default:
- break;
- }
- switch (one1)
- {
- case 0: disp_8x16(8*18,7, x0);
- break;
- case 1: disp_8x16(8*18,7, x1);
- break;
- case 2: disp_8x16(8*18,7, x2);
- break;
- case 3: disp_8x16(8*18,7, x3);
- break;
- case 4: disp_8x16(8*18,7, x4);
- break;
- case 5: disp_8x16(8*18,7, x5);
- break;
- case 6: disp_8x16(8*18,7, x6);
- break;
- case 7: disp_8x16(8*18,7, x7);
- break;
- case 8: disp_8x16(8*18,7, x8);
- break;
- case 9: disp_8x16(8*18,7, x9);
- break;
- default:
- break;
- }
- disp_8x16(8*17,7, dian);
- disp_8x16(8*19,7, x1c);
- }
- /**********定时器初始化**********/
- void Init_time0(void)
- {
- TMOD |= 0x02; //定时器0工作方式2,8位自动装入计数
- TH0=0X06; //2.5MS定时时间
- TL0=0X06;
- EA=1;
- IT0=0; //外部中断1工作方式(下降沿有效)
- EX0=1; //打开外部中断0
- EX1=1; //打开外部中断1
- IT1=1;
- ET0=1; // 定时器0中断允许
- TR0=1; // 定时器打开
- }
-
- /*********外部中断0****************/
- void exter0() interrupt 0
- {
- count++; //脉冲数取样
- }
- /*********外部中断1****************/
- void exter1() interrupt 2
- {
- if(plus_A==1)
- {
- speed_set++;
- if(speed_set>=120)
- {
- speed_set=120;
- }
- }
- else
- {
- speed_set--;
- if(speed_set<=1)
- {
- speed_set=1;
- }
- }
- /* if(plus_B==1)
- {
- temp_set++;
- if(temp_set>=32)
- {
- temp_set=32;
- }
- }
- else
- {
- temp_set--;
- if(temp_set<=16)
- {
- temp_set=16;
- }
- } */
- }
- /*******定时器0,8位自动重装*****/
- void time0() interrupt 1
- {
- TH0=0X06; //2.5MS定时时间
- TL0=0X06;
- a++;
- if(a==4000) //1S时间刷新一次
- {
- // EX0=0;
- speed= count*6696/1000; //6696=1.86*3600
- _nop_;
- _nop_;
- _nop_;
- count=0;
- flag=1;
- a=0;
- led=~led;
- // EX0=1;
- }
- }
- void init_io()
- {
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- P6M0 = 0x00;
- P6M1 = 0x00;
- P7M0 = 0x00;
- P7M1 = 0x00;
- }
- //-----------------------------
- void main ()
- {
- init_io();
- flag=0;
- Init_time0();
- // Init_time1();
- initial_lcd(); //对液晶模块进行初始化设置
- // clear_screen();
- // disp_160x80(1,1,buick); //logo disp
- // delay(3000);
- clear_screen();
- while(1)
- {
- disp_speed();
- disp_cruise();
- disptemp_set();
- dispspeed_set();
- }
- }
复制代码
|