#include <iom16v.h>
#include <macros.h>
#pragma interrupt_handler gs:9
#pragma interrupt_handler sd:10
#define LCM_RS_1 PORTB|=(1<<7)
#define LCM_RS_0 PORTB&=(~(1<<7))
#define LCM_RW_1 PORTB|=(1<<6)
#define LCM_RW_0 PORTB&=(~(1<<6))
#define LCM_E_1 PORTB|=(1<<5)
#define LCM_E_0 PORTB&=(~(1<<5))
#define uint unsigned int
#define uchar unsigned char
uint num=5;/*用以计个数*/
const unsigned char LCM_Dis_String1[]="number:";
const unsigned char LCM_Dis_String2[]="speed:";
const unsigned char table[]="0123456789";
void delay_1(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<1141;j++);
}
void delay_2(uint ns)
{
uint k,h;
for(k=0;k<ns;k++)
for(h=0;h<8;h++);
}
unsigned char LCM_Re_BAC()
{
unsigned char status;
DDRA=0X00;
LCM_RS_0;
LCM_RW_1;
LCM_E_1;
delay_2(1000);
status=PINA;
LCM_E_0;
return status;
}
void LCM_Wr_CMD(uchar cmd_dat)
{
while(LCM_Re_BAC()>=0X80);
DDRA=0XFF;
LCM_RS_0;
LCM_RW_0;
LCM_E_1;
PORTA=cmd_dat;
delay_2(1000);
LCM_E_0;
}
void LCM_Wr_DAT(uchar dis_dat)
{
while(LCM_Re_BAC()>=0X80);
DDRA=0XFF;
LCM_RS_1;
LCM_RW_0;
LCM_E_1;
PORTA=dis_dat;
delay_2(1000);
LCM_E_0;
delay_2(400);
}
void LCM_1602_Init(void)
{
LCM_Wr_CMD(0X38);
delay_1(5);
LCM_Wr_CMD(0X0c);
delay_1(5);
LCM_Wr_CMD(0X06);
delay_1(5);
LCM_Wr_CMD(0X01);
delay_1(5);
}
void gs(void) /*定时器1用以计算脉冲个数*/
{
uint a=0,b=0,c=0,d=0,e=0,f=0,g=0;
TCCR1B=0X00;
SREG|=BIT(7);
TIMSK&=0XFB;
if (num<999)
{
LCM_Wr_CMD(0X80+0X0D);
LCM_Wr_DAT('g');
a=num/100;
d=num%100;
b=d/10;
c=d%10;
LCM_Wr_CMD(0X80+0X0A);
delay_2(10);
LCM_Wr_DAT(table[a]);
delay_2(10);
LCM_Wr_DAT(table[b]);
delay_2(10);
LCM_Wr_DAT(table[c]);
delay_2(10);
}
else
{
LCM_Wr_CMD(0X80+0X0D);
LCM_Wr_DAT('k');
LCM_Wr_DAT('g');
a=num/100000;
d=num%100000;
b=d/10000;
e=d%10000;
c=e/1000;
f=e%1000;
g=f/100;
LCM_Wr_CMD(0X80+0X0A);
if((!a)&(!b))
{
LCM_Wr_DAT(table[c]);
delay_2(10);
LCM_Wr_DAT('.');
delay_2(10);
LCM_Wr_DAT(table[g]);
delay_2(10);
}
else
{
LCM_Wr_DAT(table[c]);
LCM_Wr_DAT('.');
LCM_Wr_DAT(table[g]);
}
}
TCNT1H=0XF0;
TCNT1L=0XBE;
TIMSK|=BIT(2);
TCCR1B=0X04;
}
void sd(void) /*定时器0用以就算传输速度,假定定时是1s*/
{
uint a=num;
TCCR0=0;
TIMSK=0XFE;
LCM_Wr_CMD(0XC0+0X08);
LCM_Wr_DAT(table[a]);
TCNT0=0X01;
TIMSK|=BIT(0);
TCCR0=0X05;
}
void main(void)
{
unsigned char temp1,temp2;
uchar x1=0;
DDRB=0XFF;
DDRA=0xff;
DDRD&=0XF8;
LCM_1602_Init();
temp1=0;
temp2=0;
LCM_Wr_CMD(0X80);
while(LCM_Dis_String1[temp1]!='\0')
{
LCM_Wr_DAT(LCM_Dis_String1[temp1++]);
delay_2(100);
}
LCM_Wr_CMD(0xc0);
while(LCM_Dis_String2[temp2]!='\0')
{
LCM_Wr_DAT(LCM_Dis_String2[temp2++]);
delay_2(100);
}
TCCR0=0X05;
TCNT0=0X01;
TCCR1B=0X04;
TCNT1H=0XF0;
TCNT1L=0XBE;
TIMSK|=BIT(2);
TIMSK|=BIT(0);
SREG|=BIT(7);
LCM_Wr_CMD(0XC0+0X0B);
LCM_Wr_DAT('g');
LCM_Wr_DAT('/');
LCM_Wr_DAT('s');
while(1) /*读脉冲*/
{
x1=PIND&0X01;
if(x1=0) num++;
}
}
新手,所以上述这个程序花了两天多时间,还是不行,感觉一直在执行中断,不知道该怎么改,真心菜啊。求大神给支个招啊。
|