⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 AVR基础试验程序
💻 TXT
字号:
#include <iom16v.h> 
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
char dis_buff[6]; // 显示缓冲区,存放要显示的8个字符的段码值
uchar Data1[4]={10,0,5,0};      //定义初始方波占空比:50%
uchar Data2[4]={10,0,5,0}; 
uchar Key1_Flag,Key2_Flag,q,s=0,u,t=0;
char time_1ms_ok,display_ok=0;
unsigned  int freq_time,time0_old,time0_new,r,p=0;
unsigned long int freq;
///////////////////////////////////////////

void DelayMs(uint i )           //Ms级延时,参数i为延时时间
{uint j;
 for(;i!=0;i--)
  {for(j=1142;j!=0;j--) {;}}     //原来8000,改为1142//
}
/////////////////////////////////////////////////
#define LCD_EN_PORT    PORTA
#define LCD_RW_PORT    PORTA
#define LCD_RS_PORT    PORTA
#define LCD_DATA_PORT  PORTA
#define LCD_DATA_DDR   DDRA
#define LCD_DATA_PIN   PINA
#define LCD_EN         (1<<PA2)   //portc5         out
#define LCD_RW        (1<<PA1)    //portd3         out/in
#define LCD_RS         (1<<PA0)  //portc4         out
#define LCD_DATA        ((1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7))   //portd4/5/6/7   out

void delay_1us()
  {
   asm("nop");
  }
/////////////////////////
void delay_nus(unsigned int n)       //N us延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1us();
  }
 ////////////////////////// 
void delay_1ms(void)                 //1ms延时函数
  {
   unsigned int i;
   for (i=0;i<1140;i++);
  }
 ////////////////////////////////// 
void delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  }
////////////////////////////////////////


void LCD_en_write(void)        //EN端产生一个高电平脉冲,写LCD
  {
    LCD_EN_PORT |= LCD_EN;
    delay_nus(1);
    LCD_EN_PORT &= ~LCD_EN;
  }
/////////////////////////////////////
 void LCD_wait_Ready(void)                  //等待LCD空闲
   {
    LCD_DATA_DDR &= ~0x80;                 //PD7 I/O口方向设置为输入   	
    LCD_RW_PORT|=LCD_RW;                 //RW=1    	
    LCD_RS_PORT&=~LCD_RS;                //RS=0    	
    LCD_EN_PORT |= LCD_EN;                 //EN=1    	
    while (!( LCD_DATA_PIN&0x80 ) == 0);   //RW=1,读PD7,为0表示空闲? 	
    LCD_EN_PORT &= ~LCD_EN;                //EN=0    	
    LCD_DATA_DDR |= 0xf0;  	              //设置为输出porta4/5/6/7	 
  }
////////////////////////////////////
void LCD_write_char(unsigned char command,unsigned char data)
  {    unsigned char command_temp,data_temp;        
       command_temp = command;
      data_temp = data;
       LCD_wait_Ready();
       LCD_RW_PORT &= ~LCD_RW; 	                   //RW=0	   
       if (command == 0)      
	   {LCD_RS_PORT |= LCD_RS;             //RS=1 	
   	 LCD_DATA_PORT &= 0X0F;	 
	 LCD_DATA_PORT |= data_temp&0xf0; 	   //send high 4bit	 
	
      }
  else 
     {
	 LCD_RS_PORT &= ~LCD_RS;            //RS=0
	LCD_DATA_PORT &= 0X0F;
     LCD_DATA_PORT |= command_temp&0xf0;//send high 4bit
     }
    LCD_en_write();
       command_temp=command_temp<< 4;         //send low 4bit
    data_temp=data_temp<<4;
       LCD_DATA_PORT&=0X0F;
    if (command==0)LCD_DATA_PORT|=data_temp&0xf0;
	   
    else
     LCD_DATA_PORT|=command_temp&0xf0;   		
    LCD_en_write();
       LCD_RW_PORT|=LCD_RW;	    
      LCD_RS_PORT^=LCD_RS;    
	  delay_nus(1);
  }
/////////////////////////////////////////////////////
void LCD_set_xy( unsigned char x, unsigned char y )
  {
    unsigned char address;
    if (y == 0) address = 0x80 + x;
    else 
       address = 0xc0 + x;
    LCD_write_char( address, 0 );
  }
//////////////////////////////////////
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
  {
    LCD_set_xy( X, Y );    
    while (*s)      {
        LCD_write_char( 0, *s);
		//delay_nus(70);
	s ++;
     }
}
/////////////////////////////////
void LCD_write_string1(unsigned char X,unsigned char Y,unsigned char dat)
  {
     LCD_set_xy( X, Y );       
        LCD_write_char( 0, dat);	
		 
}
/////////////////////////////////////////
void freq_to_disbuff() // 频率值转化为BCD码送显示缓冲区
{
char i,j=5;
//freq=freq*10;
for (i=0;i<=5;i++)
{
dis_buff[j-i]=freq%10;
freq=freq/10;
}
dis_buff[0]=0x30|dis_buff[0];
dis_buff[1]=0x30|dis_buff[1];
dis_buff[2]=0x30|dis_buff[2];
dis_buff[3]=0x30|dis_buff[3];
dis_buff[4]=0x30|dis_buff[4];
dis_buff[5]=0x30|dis_buff[5];
}
///////////////////////////////////////////////////
void LCD_init(void)
  {
    delay_nms(15);
    LCD_write_char(0x28,0);      //4bit test
    LCD_write_char(0x0c,0);      //显示开
    LCD_write_char(0x01,0);      //显示清屏
    LCD_write_char(0x06,0);      //显示光标移动设置
  }

////////////////////////////////////
#pragma interrupt_handler Timer2:4
void Timer2(void)
{
time0_new=TCNT0;//
time_1ms_ok=1;
display_ok=~display_ok;
//if(display_ok)

}
////////////////////////////////////////////////////////////


void Add_Process(uchar *p)//
{if(p[1]!=1)
  {p[2]=p[2]+1;
   if(p[2]==10) {p[2]=0;p[1]++;}
  }
}

//////////////////////////////////////////

void Sub_Process(uchar *p)
{uchar temp;
 temp=p[1]*10+p[2];
 if(temp!=0)
  {temp=temp-1;
   p[1]=temp/10;
   p[2]=temp-p[1]*10;
  }
 
}
 void Set_Process(uchar *p)
{uint i,j;
 i=p[1]*10+p[2];
 i=155*i/10;
 j=i;
 OCR1AH=i>>8;
 OCR1AL=i&0x00ff;
 OCR1BH=j>>8;
 OCR1BL=j&0x00ff;
}
void change()
{
Data2[0]=0x30|Data1[0];
Data2[1]=0x30|Data1[1];
Data2[2]=0x30|Data1[2];
Data2[3]=0x30|Data1[3];
}
void Key_Process()
{while((PINC&0x10)==0) {Key1_Flag=1;}  
 while((PINC&0x20)==0) {Key2_Flag=1;}
 while((PINC&0x40)==0)  {q=1;} 
  while((PINC&0x80)==0)  {t=1;} 
 if(Key1_Flag==1)
    { Key1_Flag=0;
	Add_Process(Data1);	     	 
	}
 if(Key2_Flag==1)
     {Sub_Process(Data1);		
	  Key2_Flag=0;}
if(q==1)
{s=1;
q=0;
u=0;
} 	
if(t==1)
{u=1;
t=0; 
s=0; 
}
}	    


void pwm_init(void)
{
 DDRD=(1<<PD4)|(1<<PD5);  
 PORTD=(1<<PD4)|(1<<PD5);
 TIMSK = 0x00;  
 TCCR1A=(1<<COM1A1)|(1<<WGM11)|(1<<COM1B1); 
 TCCR1B=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10);
 ICR1=155;
 OCR1A=77;
  OCR1B=77;
 }
 void for_ward()
 {TCCR1A=(1<<WGM11); 
 PORTD&=~(1<<PD4);
 PORTD&=~(1<<PD5);
  TCCR1A=(1<<COM1A1)|(1<<WGM11); 
 PORTD&=~(1<<PD4);
 PORTD&=~(1<<PD5);
 OCR1A=77;
 
 }
void back_ward()
 { TCCR1A=(1<<WGM11); 
 PORTD&=~(1<<PD4);
 PORTD&=~(1<<PD5);
 TCCR1A=(1<<COM1B1)|(1<<WGM11); 
  PORTD&=~(1<<PD4);
 PORTD&=~(1<<PD5);
 OCR1B=77;
 }
 //////////////////////////////////////////////////////////////////
void main(void)
{
char i;
DDRB=0x00;                   
PORTB=0x01;
DDRD=0xff;                    //设置D口为推挽1输出
 PORTD=0xff;  
DDRC=0x00;                    //设置C口为三态输入
PORTC=0x00;
DDRA|=LCD_DATA|LCD_RW;
DDRA|=LCD_RS|LCD_EN;     
LCD_init(); 
LCD_write_char(0x01,0);      //显示清屏
LCD_write_string(0,0,"Freq:      Hz");
LCD_write_string(0,1,"zkb:   %");
asm("nop");
 LCD_write_string1(5,0,0x30);
asm("nop");
LCD_write_string1( 6,0,0x30);
asm("nop");
LCD_write_string1( 7,0,0x30);
asm("nop");
LCD_write_string1( 8,0,0x30);
LCD_write_string1( 9,0,0x30);
LCD_write_string1( 0x0A,0,0x30);
 LCD_write_string1(4,1,0x30);
LCD_write_string1(5,1,0x35);
 LCD_write_string1(6,1,0x30);
 asm("nop");
// T/C0初始化,外部计数方式
for ( i=0;i<=5;i++)dis_buff[i]= 0;               
pwm_init();                  //熄灭所有的数码管  
time0_old=0;
TCCR0=0x06; // 外部T0脚下降沿触发计数,普通模式
TCNT0=0x00;
OCR0=0x00;
TCCR2=0x0C; // 内部时钟,32分频(8M/64=125KHz),CTC模式
TCNT2=0x00;
OCR2=0x7C; // OCR2 = 0x7C(124),(124+1)/125=1ms
TIMSK=0x80; // 允许T/C2比较匹配中断
SEI(); // 开放全局中断
while (1)
{
 Set_Process(Data1);
 if(++p>=2500)
 { 
Key_Process();
change();         //设置PWM占空比
 LCD_write_string1(4,1,Data2[1]);
 LCD_write_string1(5,1,Data2[2]);
 LCD_write_string1(6,1,Data2[3]);
p=0;
}
 
if(s==1)
 {for_ward();
 }
  if(u==1)
 {back_ward();
} 	  
if (time_1ms_ok)
{ // 累计T/C0的计数值

if (time0_new>=time0_old)freq=freq+(time0_new-time0_old);
else freq=freq+(256-time0_old+time0_new);
time0_old=time0_new;
if(++freq_time>=1000)
{
freq_time=0; // 100ms到,
freq_to_disbuff(); // 将100ms内的脉冲计数值送显示
freq=0;
LCD_write_string1(5,0,dis_buff[0]);
LCD_write_string1( 6,0,dis_buff[1]);
LCD_write_string1( 7,0,dis_buff[2]);
LCD_write_string1( 8,0,dis_buff[3]);
LCD_write_string1(0x9,0,dis_buff[4]);
LCD_write_string1( 0x0A,0,dis_buff[5]);
}
time_1ms_ok=0;
};
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -