?? avr+315m433.92m+1602.c
字號:
//=======================================================================
//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++)
;
}
}
//========================================================
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -