www.openedv.com
您好,欢迎您!    会员注册 | 登入 
公告:欢迎访问www.openedv.com开源电子网,开源共享共同进步,祝您新年快乐,万事如意!
设为首页 | 加入收藏
论坛首页 » AVR单片机 前往: 
我的AVR资料[程序篇] 操作指定地址的eeprom(源码10)
发表人 内容
[Down] [Up]
[楼主位] 正点原子


等级:NO
注册时间:
2010/12/02 10:41
文章: 8089
来自: 湖南
在线

此代码包含了4*4矩阵键盘的扫描,然后通过键盘的按键输入要写入指定地址(这里固定为:eeprom的地址1)的值,并通过按键(14/15)执行写入和读出eeprom的操作.
这里的eeprom读写函数,使用的是cvavr自带的.我们只需要调用eeprom.h即可. 不过我不建议大家这么用,需要用eeprom的时候,可以通过eeprom关键字来定义,让编译器自动分配地址,免得每次自己去分配,比较麻烦,又容易出错.
代码如下:

 #include <mega16.h> 
#include <eeprom.h>
#define uchar unsigned char
#define uint unsigned int

uchar right=1;//for key_scan variable
void delay(unsigned char time)//delay function
{
    unsigned char b;
  for(;time>0;time--)
 for(b=50;b>0;b--);
}
//digital number display function, the first parameter is the number you want to show
//the second parameter is the position of the number
void display(unsigned char number,unsigned char position);
unsigned char key_scan();
void systeminit()//a simple system initialization function
{
    DDRA=0Xff;//set PORTA output
    PORTA=0XFF;//set PORTA output in high level
}
void main(void)
{
    unsigned char b1,b2;
 uchar key;
 unsigned char t=0;
 systeminit();
 while(1)
 {
   key=key_scan();
   if(key==15)eeprom_write(1,t);//write eeprom
   if(key==14)t=eeprom_read(1);//read eeprom
   if(key<14)t=key;
   b1=t%10;display(b1,8);delay(5);
   b2=(t/10)%10;display(b2,7);delay(5);
    }    
}        
//this function support icc and code vision avr
void display(unsigned char number,unsigned char position)//digtial show function
{  
    DDRA=0Xff;//set PORTA output
 PORTA=0XFF;//set PORTA output in high level
    switch(position)//choice the position
 {
  case 1:{PORTA&=0xf8;break;}
  case 2:{PORTA&=0xf9;break;}
  case 3:{PORTA&=0Xfa;break;}
  case 4:{PORTA&=0Xfb;break;}
  case 5:{PORTA&=0Xfc;break;}
  case 6:{PORTA&=0Xfd;break;}
  case 7:{PORTA&=0Xfe;break;}
  case 8:{PORTA&=0Xff;break;}
  } 
    switch(number)//show number
 {
  case 0:{PORTA&=0x87;break;}    //0
  case 1:{PORTA&=0x8f;break;}    //1
  case 2:{PORTA&=0x97;break;}    //2
  case 3:{PORTA&=0x9f;break;}    //3
  case 4:{PORTA&=0xa7;break;}    //4
     case 5:{PORTA&=0xaf;break;}    //5
  case 6:{PORTA&=0xb7;break;}    //6
  case 7:{PORTA&=0xbf;break;}    //7
  case 8:{PORTA&=0xc7;break;}    //8
     case 9:{PORTA&=0xcf;break;}    //9
  default:{PORTA=0xff;break;}    //null
 }       
}       
//this function support icc and code vision avr
unsigned char key_scan()     //check if key pressed

 uchar juder;
 uchar which;                   //scan which row
 uchar juder1=0;       //juder whether key-press undo
 DDRD=0X0F;//set low 4 output high 4 input
    PORTD=0XFF;//output high leve
    for(which=0;which<4;which++)
 { 
     juder=0;
     switch(which)//choice key row
  {
    case 0:{PORTD=0Xfe;break;}         //11111110
    case 1:{PORTD=0Xfd;break;}   //11111101
    case 2:{PORTD=0Xfb;break;}   //11111011
    case 3:{PORTD=0Xf7;break;}   //11110111
  }
  if(right)//get key number
  {
   while((PIND&0x80)==0)
   {
    juder++;
    if(juder>250){right=0;return(0+4*which);}
   }
   juder=0;
   while((PIND&0x40)==0)
   {
    juder++;
    if(juder>250){right=0;return(1+4*which);}
   }
   juder=0;
   while((PIND&0x20)==0)
   {
    juder++;
    if(juder>250){right=0;return(2+4*which);}
   }
   juder=0;
   while((PIND&0x10)==0)
   {
    juder++;
    if(juder>250){right=0;return(3+4*which);}
   }
  }
  else if((PIND&0x10)!=0&&(PIND&0x20)!=0&&(PIND&0x40)!=0&&(PIND&0x80)!=0)juder1++;
  else delay(1);//compensate
 }
 
 if(juder1==4)right=1;//make sure every time variable change only one time
 return 16;//if no key pull down return 16
}




我的淘宝小店:http://shop62103354.taobao.com
[加为好友] 回复 引用回复
 
前往: 

Powered by ALIENTEK工作室 © 粤ICP备12000418号-1