|
//=======================================================================
//ATmega8+315M或433.92M的超再生接收头+1602六线控制的1602显示解到的码
//作者:朱海峰
//邮箱:ntzhf100@163.com QQ:543376422
//
//========================================================================
#include <iom8v.h>
#include <macros.h>
#include "LCD_1602_4wires.h"
#define Beep_on() PORTB |= 0X04
#define Beep_off() PORTB &= ~0X04
void System_init(void);
void Receive_ID_Number(void);
void Show_ID_Number(uint32_t n);
void Print_hex(uint32_t n);
void Print_num(uint8_t num);
const uint8_t g_num[] = "0123456789ABCDEF";
/****************************************/
void main(void)
{
System_init();
Delay_nms(10);
LCD_init();
Beep_off();
LCD_write_string(1,1,"Receive RF ID");
SREG = 0x80; // SEI
while(1)
{
Receive_ID_Number();
}
}
/***************************************/
void System_init(void)
{
/*********************I/O口初始化**************************/
DDRC |= LCD_DATA;
DDRB |= 0XC6;
PORTB &= ~0X80;//我的RW引脚未接地,通过输出低电平开使它
//处在低电平状态
/*********************************************************/
}
/***************************************/
#define get_port_val() ((PINC & 0x20) >> 5)
static uint32_t g_key_cmd = 0x00;
void Receive_ID_Number(void)
{
static uint16_t cyc_cnt = 0, low_cnt = 0, high_cnt = 0;
static uint8_t st = 0;
static uint32_t last_cmd = 0;
cyc_cnt++; //查询计数自加1
if (st == get_port_val()) //如果当前端口读到的电平和上次的一样则退出
return;
/* st must euqal 0x00 or 0x01 */
st = !st; //如果不一样则保存的电平状态取反
if (st) //如果ST==1则说明当前是由高电平向低电平变化
{
low_cnt = cyc_cnt; //将当前的查询计数值给低电平计数变量
}
else //否则说明当前是由低电平向高电平变化
{
high_cnt = cyc_cnt; //将当前的查询计数值给低电平计数变量
}
cyc_cnt = 0; //查询计数变量赋0
if (!st) //如果当前是由低电平向高电平的变化则退出
return;
if ((low_cnt >> 4) > high_cnt)//如果一直低电平则进入
{
g_key_cmd &= 0x00FFFFFF; //取其后24位
//如果当前的码等于上次的码并且上次的码不为0则显示接收到的码
if ((g_key_cmd == last_cmd) && (last_cmd != 0))
{
Show_ID_Number(g_key_cmd);//调用显示函数
}
last_cmd = g_key_cmd;//保存当前的码,以便下次比较用
}
g_key_cmd = g_key_cmd << 1;//解到0
if (low_cnt < high_cnt)
{
g_key_cmd |= 0x01; //解到1
}
}
/***************************************/
void Show_ID_Number(uint32_t n)
{
//LCD_write_string(1,2,"==>[");
Beep_on();
LCD_set_xy(5,2);
Print_hex(n);
//LCD_write_string(13,2,"]<==");
Beep_off();
Delay_nms(10);
}
/*************************************/
void Print_num(uint8_t num)
{
LCD_write_byte(0,g_num[num >> 4]);
LCD_write_byte(0,g_num[num & 0x0F]);
}
/************************************/
void Print_hex(uint32_t n)
{
Print_num((n & 0xFF000000) >> 24);
Print_num((n & 0x00FF0000) >> 16);
Print_num((n & 0x0000FF00) >> 8);
Print_num((n & 0x000000FF));
}
/************************************/
//=================================第二个液晶6线控制的C头文件========================
/*============================================================
工程:字符型液晶通用六线驱动
作者:朱海峰
日期:2007.02.28
E_mail:ntzhf100@163.com
说明:谢谢www.ouravr.com论坛上的朋友们的提供的宝贵经验
液晶指令说明:
0x08==============>关闭显示
0x0c==============>开显示
0x01==============>清除LCD的显示内容
0x06==============>移动光标
0x0c==============>显示开,光标关
0x28==============>16*2显示,5*7点阵,4位数据
0x1c==============>字符右移一格
0x18==============>字符左移一格
0x10==============>光标右移一格
0x14==============>光标左移一格
显示地址:
===============================================================
=0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 ................ 0x27=
=0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 ................ 0x67=
===============================================================
每行可以显示40个字符,可以看到的只有16个字符,可以通过指令使字符
整体移动来显示所有字符。
LCD_write_byte函数功能:当cmd=0时,向LCD写入数据,否则向LCD写
入命令
四线控制的方式:先送字节的高四位,在送低四位。
值得注意的是当使用的I/O口为高四位时数据先给另一个变量,变量再将
数据高四位送到I/O口,接着是将变量左移四位,再送到I/O口上去。
当使用的I/O口为低四位时数据先给另一个变量,变量右移四位后送到I/O
口上去,接着将数据给变量直接送入I/O口。
使用时注意一下。
============================================================*/
#include <iom8v.h>
#include <macros.h>
//定义MCU与LCD的接口
#define LCD_DATA_PORT PORTC
#define LCD_CONTROL_PORT PORTB
#define LCD_DATA 0x0F
#define SET_RS() LCD_CONTROL_PORT|=0x40
#define CLR_RS() LCD_CONTROL_PORT&=~0x40
#define SET_EN() LCD_CONTROL_PORT|=0x02
#define CLR_EN() LCD_CONTROL_PORT&=~0x02
#define xtal 8 //晶振调整 MHz
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned long uint32_t;
/*-----------------------------------------------------
Public function prototypes
-------------------------------------------------------*/
void LCD_init (void);
void LCD_write_byte (uint8_t cmd,uint8_t data);
void LCD_Write_half_byte (void);
void LCD_set_xy (uint8_t x, uint8_t y);
void LCD_write_string (uint8_t X,uint8_t Y,uint8_t *s);
void Move (uint8_t step,uint8_t dirction,uint16_t time);
void Flash_lcd (uint16_t delay_t,uint8_t times);
void Delay_nms (uint16_t ms);
uint8_t data_temp;
//==================================================
void LCD_init(void)
{
Delay_nms(50);
Delay_nms(1);
LCD_write_byte(1,0x28); //4bit test 显示模式设置(不检测忙信号)
Delay_nms(1);
LCD_write_byte(1,0x08); // 显示关闭
Delay_nms(1);
LCD_write_byte(1,0x01); // 显示清屏
Delay_nms(1);
LCD_write_byte(1,0x06); // 显示光标移动设置
Delay_nms(1);
LCD_write_byte(1,0x0C); // 显示开及光标设置
Delay_nms(10);
}
/*--------------------------------------------------
LCD_write_byte : 英文字符显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置,X:1-16,Y:1-2
---------------------------------------------------*/
void LCD_write_byte(uint8_t cmd,uint8_t data)
{
if (cmd == 1)
{
CLR_RS();
}
if (cmd == 0)
{
SET_RS();
}
data_temp = data;
data_temp = data_temp >> 4;
LCD_Write_half_byte();
data_temp = data;
LCD_Write_half_byte();
Delay_nms(1);
}
/*----------------写4bit到LCD------------------------*/
void LCD_Write_half_byte(void)
{
LCD_DATA_PORT &= 0xf0; //
LCD_DATA_PORT = data_temp;//send 4bit
SET_EN();
NOP();
CLR_EN();
NOP();
}
/*----------------------------------------------------
LCD_set_xy : 设置LCD显示的起始位置
输入参数:x、y : 显示字符串的位置,X:1-16,Y:1-2
-----------------------------------------------------*/
void LCD_set_xy( uint8_t x, uint8_t y )
{
unsigned char address;
if (y == 1)
{
address = 0x80 - 1 + x;
}
else
{
address = 0xc0 - 1 + x;
}
LCD_write_byte(1,address);
}
/*---------------------------------------------------
LCD_write_string : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置
---------------------------------------------------*/
void LCD_write_string(uint8_t X,uint8_t Y,uint8_t *s)
{
LCD_set_xy( X, Y );
while (*s)
{
LCD_write_byte(0,*s);
s++;
}
}
//=======================================================
void Move(uint8_t step,uint8_t dirction,uint16_t time)
{
uint8_t i;
for(i=0; i<step-1; i++)
{
LCD_write_byte(1,dirction); //字符移动方向
Delay_nms(time); //控制移动时间
}
}
//=========================================================
void Flash_lcd(uint16_t delay_t,uint8_t times)
{
uint8_t j;
for(j=0; j<times; j++)
{
LCD_write_byte(1,0x08);
Delay_nms(delay_t);
LCD_write_byte(1,0x0c);
Delay_nms(delay_t);
}
}
//========================================================
void Delay_nms(uint16_t ms)
{
uint16_t i;
while(ms--)
{
for(i=1; i<(uint16_t)(xtal*143-2); i++)
;
}
}
//======================================================== |
|