1602 AVR单片机版 报警指示功能
功能说明:正常状态下,8个LED不停闪烁,1602显示normal indication指示
报警状态下, 8个LED全发亮不闪烁,1602显示alarm报警,只有按复位按钮才能解除报警.
alarm状态时,由于PB口的低3位和,1602时能,读写,数据指令寄存器选择位复用,所以有点暗.
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define RS1 PORTB_Bit0=1 //数据指令寄存器选择
#define RS0 PORTB_Bit0=0
#define RW1 PORTB_Bit1=1 //读写选择
#define RW0 PORTB_Bit1=0
#define EN1 PORTB_Bit2=1 //读写时能
#define EN0 PORTB_Bit2=0
#define DATAPORT PORTA //1602数据口
#define busy 0x80 //繁忙标志
#include "ku.h" //调用函数库
//-------------------------------------------
uchar alarm[]={"alarm "}; //报警字符串
uchar normal[]={"normal"}; //正常字符串
uchar indication[]={"indication"};
//-------------------------函数声明--------------
void delay_1ms(); //延时1毫秒
void delay_nms(uint n); //延时n毫秒
void wait(); //繁忙等待函数
void writedata(uchar w); //写数据
void writecmd(uchar cmd); //写指令
void init(); //1602初始化
void display(uchar x,uchar y,uchar *P); //显示字符串函数
void delay(uint k) //常用延迟函数
{
uint i,j;
for(i=0;i for(j=0;j<1140;j++); } void main() { DDRB=0xff; //设置PB口为输出 PORTB=0xff; DDRD=0x00; //中断源设置为输入 PORTD=0xff; MCUCR=0x02; //中断为下降沿 GICR=0xc0; //中断为int0,int1 SREG=0x80; //中断总开关 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() //中断产生报警 { writecmd(0x01); //清屏 while(1) { display(6,0,alarm); PORTB=0x00; } } //-----------------------库函数------------------------ void wait() { uchar val; DATAPORT=0xff; RS0; RW1; __no_operation(); EN1; __no_operation(); //注:一个__no_operation();延时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 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数据指针初值 y=y&0x01; x=x&0x0f; if(y)add=add+0x40; //显示第二行加数据指针加0x40 writecmd(add+x); while(*p!=) { writedata(*p++); } }
1602AVR单片机报警指 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)