/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.8c Professional
Automatic Program Generator
?Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project : M8
Version : 1.0
Date : 2006-5-5
Author :
Company :
Comments:AVR(ATMEGA8) 入门学习板
Chip type : ATmega8L
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
// 本章简介
/****************************************************
一,学习目的:AVR外接LCD1602液晶显示
二,基础知识: 使用CVAVR自带的库函数及其调用
LCD的各种指令,及设置相关函数:
0x01//清屏:显示消失,光标或闪烁位回到显示的左边界
0x02//归位将地址计数器AC清零,如果DDRAM移位,将显示返回到它的初始状态
设置输入方式:
0x04//画面不动光标左移
0x05//画面向右滚动
0x06//画面不动光标右移
0x07//画面向左滚动
0x08~0x0B//关显示
_lcd_ready();
_lcd_write_data( instruction );
void _lcd_ready(void)
//等待LCE是否准备接受数据,即_lcd_write_data(unsigned char data)
void _lcd_write_data(unsigned char data)
//写一字节的数据到LCD的命令寄存器
void lcd_write_byte(unsigned char addr, unsigned char data)
unsigned char lcd_read_byte(unsigned char addr)
unsigned char lcd_init(unsigned char lcd_columns)
void lcd_clear(void)
void lcd_gotoxy(unsigned char x, unsigned char y)
void lcd_putchar(char c)
void lcd_puts(char *str)
void lcd_putsf(char flash *str)
三,实验任务:熟悉CVAVR库函数
LCD的各种指令
在LCD显示字符及数值
四,注意事项:
CVAVR自带的LCD驱动函数用汇编语言所写,并且是4线驱动,介绍IO口及编译效率
将JP2上的PB1跳针短路,将PORTB.1连接到D2的阴极
****************************************************/
#include
#include //CVAVR 自带的延时函数
#asm
.equ __lcd_port=0x12 ;PORTD//液晶模块连接到D口(0X12)
#endasm
#include //包含使用LCD相关的函数库
/* 液晶和MCU之间的管脚连接示意图
[LCD] [AVR Port]
RS (pin4) ------ bit 0
RD (pin 5) ------ bit 1
EN (pin 6) ------ bit 2
DB4 (pin 11) --- bit 4
DB5 (pin 12) --- bit 5
DB6 (pin 13) --- bit 6
DB7 (pin 14) --- bit 7
*/
void main(void)
{
// Declare your local variables here
unsigned char i;
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);//初始化LCD ,该LCD是横向16个字符
lcd_gotoxy(0,0);
lcd_putsf("LCD WORK OK!");
delay_ms(1000);
while (1)
{
for(i=0;i<9;i++)
{
lcd_putchar(0x30+i);
_lcd_ready();
_lcd_write_data(0x10);
// Place your code here
delay_ms(500);
}
};
}