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

📄 main.c.bak

📁 atmega16L输入捕获经典试验已经通过验证
💻 BAK
字号:

#include <avr/io.h> 
#include <avr/signal.h>
#include <avr/delay.h>
#include <math.h>
#define LCD_EN_PORT    PORTC 
#define LCD_RW_PORT    PORTC
#define LCD_RS_PORT    PORTC
#define LCD_CROL_DDR   DDRC

#define LCD_DATA_PORT  PORTD
#define LCD_DATA_DDR   DDRD
#define LCD_DATA_PIN   PIND

#define LCD_POWER_PORT  PORTC
#define LCD_POWER_DDR   DDRC 

#define LCD_EN         0B00001000   //portd3        out
#define LCD_RW         0B00000100   //portd2        out
#define LCD_RS         0B00000010   //portc1        out
#define LCD_DATA       0x0f   		//porta4/5/6/7   out
#define LCD_VCC        0B00000001   //portd0        out
#define LCD_GND        0B00000100   //portd2        out

/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init         (void);
void LCD_en_write     (void);
void LCD_write_char   (unsigned command,unsigned data);
void LCD_wait_busy   (void);
void LCD_set_xy       (unsigned char x, unsigned char y);
void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
//void _delay_us        (unsigned int n);
//void _delay_ms        (unsigned int n);
void LCD_init(void) 
{
  unsigned int i=0; 
  for (i=0;i<10;i++)
  _delay_ms(100); 
  for (i=0;i<20;i++) 
    { 
	LCD_write_char(0x28,0);  //4位显示
    _delay_ms(15); 
    } 
 LCD_write_char(0x0c,0);  //显示开
 _delay_ms(5); 
 LCD_write_char(0x01,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 );
	s ++;
      }
  }
  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_en_write(void)  
{
  LCD_wait_busy();
  LCD_EN_PORT|=LCD_EN;
  _delay_us(16);
  LCD_EN_PORT&=~LCD_EN;
}
void LCD_wait_busy   (void)
{
 LCD_CROL_DDR|=LCD_RW;
 LCD_CROL_DDR&=~LCD_RS;
 loop_until_bit_is_set(LCD_DATA_PORT,4);
 }
void LCD_write_char(unsigned command,unsigned data) 
{
 unsigned command_temp,data_temp,temp;
 command_temp=command;
 data_temp=data;
 _delay_us(16);
 if(command==0)
 {
  LCD_RS_PORT|=LCD_RS;  //RS=1
  temp=data_temp;
  data_temp=data_temp>>4;  
  LCD_DATA_PORT&=~LCD_DATA;
  LCD_DATA_PORT|=data_temp&LCD_DATA;  
  LCD_en_write();
  LCD_DATA_PORT&=~LCD_DATA;
  LCD_DATA_PORT|=temp&LCD_DATA;  //
  LCD_en_write();
 }
 else
 {
  LCD_RS_PORT&=~LCD_RS;   //RS=0
  temp=command_temp;
  command_temp=command_temp>>4;
  LCD_DATA_PORT&=~LCD_DATA;
  LCD_DATA_PORT|=command_temp&LCD_DATA;  
  LCD_en_write();
  LCD_DATA_PORT&=~LCD_DATA;
  LCD_DATA_PORT|=temp&LCD_DATA; 
  LCD_en_write();
  }
}
void init_timer1_icp(void)
{
	TCCR1B|=(1<<CS11)|(1<<CS10)|(1<<ICNC1);	//分频比64
	TIFR=1<<ICF1;	//清除没有执行的中断
	TIMSK=1<<TICIE1;
	DDRB=0xFF;
	DDRD&=~(1<<PD6);
	PORTD|=1<<PD6;
}
SIGNAL(SIG_INPUT_CAPTURE1)
{
	int temp;
	char i,j,k;
	TCNT1=0;
	temp=ICR1>>8;
	i=temp/100;
	temp=fmod(temp,100);
	j=temp/10;
	k=fmod(temp,10);
	LCD_set_xy(2,1);
	LCD_write_char(0,i+48);
	LCD_set_xy(3,1);
	LCD_write_char(0,j+48);
	LCD_set_xy(4,1);
	LCD_write_char(0,k+48);
}

int main(void)
{
    int i;	 
	LCD_POWER_PORT|=(LCD_VCC);
	LCD_POWER_PORT&=(~LCD_GND);
	LCD_POWER_DDR|=(LCD_VCC|LCD_GND);
	_delay_ms(15);
	LCD_DATA_DDR|=LCD_DATA;   
	LCD_CROL_DDR|=LCD_RS|LCD_EN|LCD_RW;  
    LCD_init();
	LCD_write_char(0x01,0);
	_delay_ms(2);
    LCD_write_string(0,0,"Wellcome");
    LCD_write_string(0,1,"www.OURAVR.com!");
	for(i=0;i<50;i++)
    _delay_ms(100);
    LCD_write_string(0,0,"happy new year!");
    LCD_write_string(0,1,"amork:everfriend");
	for(i=0;i<50;i++)
    _delay_ms(100);
    LCD_write_string(0,0,"The Rotate Is:  ");
    LCD_write_string(0,1,"X:   *2ms       ");
	SREG|=1<<SREG_I;
	init_timer1_icp();
    for(;;);   //for??"·
}

⌨️ 快捷键说明

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