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

📄 mega8_0.c

📁 基于AVR平台的C语言开发
💻 C
字号:
//mega8_0.c : source file for the mega8设计 project
//

#include "mega8_0.h"
#include "inttypes.h"
#include "io.h"
#include "avr/signal.h"
#include "avr/interrupt.h"
#include <delay.h>

/////////////////////////////////////////////////////////////////////////////
//mega8_0
static unsigned char dis[16]={0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x08,0x09,0x0a,0x38,0x9c,0x68,0x1c,0x1e};
unsigned char key_get=0;
static void io_init(void)
{
	//{{WIZARD_MAP(General)
	//}}WIZARD_MAP(General)

	//{{WIZARD_MAP(I/O Ports)
	// PortB
	PORTB = 0x0;
	DDRB = 0x07;
	// PortC
	PORTC = 0x3f;
	DDRC = 0x30;
	// PortD
	PORTD = 0xc4;
	DDRD = 0xc0;
	//}}WIZARD_MAP(I/O Ports)

	//{{WIZARD_MAP(Watchdog)
	// Watchdog Enabled, Prescaler: OSC/16k
	wdt_disable();
	//}}WIZARD_MAP(Watchdog)

	//{{WIZARD_MAP(Analog Comparator)
	// Analog Comparator Disabled
	ACSR = 0x80;
	//}}WIZARD_MAP(Analog Comparator)
}


int main(void)
{
	//{{WIZARD_MAP(Initialization)
	io_init();
	//}}WIZARD_MAP(Initialization)
	// TODO: Add extra initialization here
     GICR=1<<INT0;
     MCUCR=((1<<ISC01)|(0<<ISC00));
     sei();                                      //中断使能
	//{{WIZARD_MAP(Global interrupt)
	//}}WIZARD_MAP(Global interrupt)
	while(1)
	{
		// TODO: Add your code here
/*
     led();
     led_red();
*/
	 get_k(); 
	 shuma();  
    } 
}

/**********************************************
 7.1  中断实验
 说明:INT0中断,下降沿触发中断
 *********************************************/
 SIGNAL(SIG_INTERRUPT0)
 {int i;
  for (i=0;i<2;i++)
  {
  cbi(PORTC,5);
  delay(500,8000);
  sbi(PORTC,5);
  cbi(PORTC,4);
  delay(500,8000);
  sbi(PORTC,4);
  cbi(PORTD,6);
  delay(500,8000);
  sbi(PORTD,6);
  cbi(PORTD,7);
  delay(500,8000);
  sbi(PORTD,7);
  }
}
/*****************************************************************
6.1  4个LED灯显示
硬件连接:PC5->D0  PC4->D1  PD6->D2  PD7->D3  
*****************************************************************/
/*
void led(void)
     {        
	     if (bit_is_clear(PINC,0)) 
	   	    cbi(PORTC,5);
		 else 
		  	sbi(PORTC,5);
	     if (bit_is_clear(PINC,1)) 
	     	cbi(PORTC,4);
         else 
	      	sbi(PORTC,4);
	     if (bit_is_clear(PINC,2)) 
	     	cbi(PORTD,6);
         else 
	      	sbi(PORTD,6);
	     if (bit_is_clear(PINC,3)) 
	     	cbi(PORTD,7);
	     else 
	      	sbi(PORTD,7);
      }
*/
/***********************************************
6.2  红绿灯控制
硬件:PC5->D0(红灯)  PC4->D1(绿灯)  
说明:红灯与绿灯交替闪烁,间隔1秒
************************************************/
/*
void led_red(void)
{   int i;
	for(i=0;i<5;i++)
	if((bit_is_clear(PINC,0))&&(bit_is_clear(PINC,1)))
	 {
	 cbi(PORTC,5);
	 delay(1000,8000);
	 sbi(PORTC,5);
	 cbi(PORTC,4);
	 delay(1000,8000);
	 sbi(PORTC,4);
     }
 }
 */
/*******************************************************
6.3  单个8段LED数码管显示
说明:推上开关S0,数码管显示1;推上S1,显示2;依次类推。
*********************************************************/
void shuma(void)
{        
	    if (key_get==1) 
	   	    {cbi(PORTC,5);
             led_out(dis[1]);}
		  else 
		  	{sbi(PORTC,5);
		     led_out(dis[0]);}
	     if (key_get==2) 
	     	{cbi(PORTC,4);
	         led_out(dis[2]);}
	      else 
	      	{sbi(PORTC,4);
	         led_out(dis[0]);}
	     if (key_get==3) 
	     	{cbi(PORTD,6);
	         led_out(dis[3]);}
	      else 
	      	{sbi(PORTD,6);
	         led_out(dis[0]);}
	     if (key_get==4) 
	     	{cbi(PORTD,7);
	         led_out(dis[4]);}
	      else 
	      	{sbi(PORTD,7);
	         led_out(dis[0]);}
}
/*******************************************************************************
                              595驱动LED数码管输出子程序
硬件连接:PB0->SER  PB1->RCLK  PB2->SRCLK
输    入:8Bits字符a
输    出:无
********************************************************************************/
void led_out(unsigned char a)
	{
	char i,d;
	 cbi(PORTB,1);//RCLK是锁存时钟信号,应独立控制,等8位数据全部移出后再送出一个上升沿,
	              //以防移位时LED闪烁;
	for(i=0;i<8;i++)
	{
		cbi(PORTB,2);
		d=a&0x80;//判断7位是否为1
		if(d)
			sbi(PORTB,0);//SER置1
		else
			cbi(PORTB,0);
		a<<=1;
		sbi(PORTB,2);//SRCLR接高电平,不进行清零输出
	}
      sbi(PORTB,1);	
}
/*****************************************
6.7  键盘扫描
作用:扫描键盘
输出:按键编码
******************************************/
unsigned char get_k(void)
{
		unsigned char a;
		a = PINC & 0x0f;
		//delay(3,8000);
		switch (a)
		{
			case 0x0e: key_get=1; break;
			case 0x0d: key_get=2; break;
			case 0x0b: key_get=3; break;
		 	case 0x07: key_get=4; break;
			default : key_get=0; break;
		}
		return key_get;
}

⌨️ 快捷键说明

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