專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

1602 AVR單片機(jī)版 報(bào)警指示功能

作者:佚名   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2013年10月30日   【字體:
使用的是ATMEGA16芯片
功能說(shuō)明:正常狀態(tài)下,8個(gè)LED不停閃爍,1602顯示normal indication指示
                報(bào)警狀態(tài)下, 8個(gè)LED全發(fā)亮不閃爍,1602顯示alarm報(bào)警,只有按復(fù)位按鈕才能解除報(bào)警.
               alarm狀態(tài)時(shí),由于PB口的低3位和,1602時(shí)能,讀寫,數(shù)據(jù)指令寄存器選擇位復(fù)用,所以有點(diǎn)暗.


 

#include <iom16.h>
#include <intrinsics.h>
#define uchar unsigned char
#define uint unsigned int
#define RS1 PORTB_Bit0=1  //數(shù)據(jù)指令寄存器選擇
#define RS0 PORTB_Bit0=0
#define RW1 PORTB_Bit1=1  //讀寫選擇
#define RW0 PORTB_Bit1=0
#define EN1 PORTB_Bit2=1  //讀寫時(shí)能
#define EN0 PORTB_Bit2=0
#define DATAPORT PORTA    //1602數(shù)據(jù)口
#define busy 0x80         //繁忙標(biāo)志
#include "ku.h"           //調(diào)用函數(shù)庫(kù)
//-------------------------------------------
uchar alarm[]={"alarm "};   //報(bào)警字符串
uchar normal[]={"normal"};  //正常字符串
uchar indication[]={"indication"};
//-------------------------函數(shù)聲明--------------

 
void delay_1ms();           //延時(shí)1毫秒
void delay_nms(uint n);     //延時(shí)n毫秒
void wait();                //繁忙等待函數(shù)
void writedata(uchar w);    //寫數(shù)據(jù)
void writecmd(uchar cmd);   //寫指令
void init();                //1602初始化
void display(uchar x,uchar y,uchar *P); //顯示字符串函數(shù)

 

 
void delay(uint k)   //常用延遲函數(shù)
{
  uint i,j;
  for(i=0;i<k;i++)
    for(j=0;j<1140;j++);
}

 
void main()
{
  DDRB=0xff;   //設(shè)置PB口為輸出
  PORTB=0xff;
  DDRD=0x00;   //中斷源設(shè)置為輸入
  PORTD=0xff;
  MCUCR=0x02;  //中斷為下降沿
  GICR=0xc0;   //中斷為int0,int1
  SREG=0x80;   //中斷總開(kāi)關(guān)
  init();
  while(1)
  {
    PORTB=0xff;
    delay(300);
    PORTB=0x00;
    delay(300);
    display(5,0,normal);
    display(3,1,indication);
  }
}

 
#pragma vector = 0x04
__interrupt  void qq()        //中斷產(chǎn)生報(bào)警
  writecmd(0x01);            //清屏
  while(1)
  {
  display(6,0,alarm);
  PORTB=0x00;
  }
}
//-----------------------庫(kù)函數(shù)------------------------
void wait()
{
  uchar val;
  DATAPORT=0xff;
  RS0;
  RW1;
  __no_operation();
  EN1;
   __no_operation();      //注:一個(gè)__no_operation();延時(shí)130ns;
    __no_operation();
     DDRA=0x00;
     val=PINA;
     while(val&busy)val=PINA;
     EN0;
     DDRA=0xff;
}

 
void writecmd(uchar w)
{
  wait();
  RS0;
  RW0;
   __no_operation();
  DATAPORT=w;
   __no_operation();
   EN1;
    __no_operation();
     __no_operation();
     EN0;
}

 
void writedata(uchar data)
{
  wait();
  RS1;
  RW0;
   __no_operation();
   DATAPORT=data;
    __no_operation();
    EN1;
     __no_operation();
      __no_operation();
      EN0;
      
}

 

 
void delay_nms(uint k)
{
  uint i,j;
  for(i=0;i<k;i++)
    for(j=0;j<1140;j++);
}

 

 
void init()
{
 delay_nms(15);
writecmd(0x38);
delay_nms(5);
writecmd(0x38);
delay_nms(5);
writecmd(0x38);
writecmd(0x80);
writecmd(0x01);
writecmd(0x06);
writecmd(0x0c);
}

 
void display(uchar x,uchar y,uchar *p)
{

 
uchar add=0x80;                             //1602數(shù)據(jù)指針初值
y=y&0x01;
x=x&0x0f;
if(y)add=add+0x40;                           //顯示第二行加數(shù)據(jù)指針加0x40
writecmd(add+x);
while(*p!='\0')
{
  writedata(*p++);
}
}

 
關(guān)閉窗口

相關(guān)文章