📄 codelcd.txt
字号:
//ICC-AVR application builder : 5/4/2008 12:00:17 AM
// Target : M128
// Crystal: 8.0000Mhz
#include <iom128v.h>
#include <macros.h>
#define Setb_RS() PORTB |= 0x01
#define Clrb_RS() PORTB &= 0xFE
#define Setb_RW() PORTB |= 0x02
#define Clrb_RW() PORTB &= 0xFD
#define Setb_EN() PORTB |= 0x04
#define Clrb_EN() PORTB &= 0xFB
#define LCD_DATA PORTB
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
}
//TIMER0 initialize - prescale:32
// WGM: Normal
// desired value: 1KHz
// actual value: 1.000KHz (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT0 = 0x06; //set count
OCR0 = 0x00;
TCCR0 = 0x03; //start timer
}
void delay(unsigned int time)
{
unsigned char i;
for(;time>0;time--)
{
for(i=0;i<65;i++)
asm("NOP");
}
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
timer0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x01; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void LCD_DAT(unsigned char data)
{
LCD_DATA &= 0x0F;
LCD_DATA |= (data<<4);
}
void LCD_Init(void)
{
delay(10000); //Wait for more than 30mS
Clrb_RS();
Clrb_RW();
LCD_DAT(0x02);
Setb_EN();
delay(10);
Clrb_EN();
Clrb_RS();
Clrb_RW();
LCD_DAT(0x02);
Setb_EN();
delay(10);
Clrb_EN();
Clrb_RS();
Clrb_RW();
LCD_DAT(0x0F);
Setb_EN();
delay(10);
Clrb_EN();
delay(1000); //Wait for more than 39uS
Clrb_RS();
Clrb_RW();
LCD_DAT(0x00);
Setb_EN();
delay(10);
Clrb_EN();
Clrb_RS();
Clrb_RW();
LCD_DAT(0x0C); //0x0F
Setb_EN();
delay(10);
Clrb_EN();
delay(1000); //Wait for more than 39uS
Clrb_RS();
Clrb_RW();
LCD_DAT(0x00);
Setb_EN();
delay(10);
Clrb_EN();
Clrb_RS();
Clrb_RW();
LCD_DAT(0x01);
Setb_EN();
delay(10);
Clrb_EN();
Clrb_RS();
Clrb_RW();
LCD_DAT(0x00);
Setb_EN();
delay(10);
Clrb_EN();
Clrb_RS();
Clrb_RW();
LCD_DAT(0x06);
Setb_EN();
delay(10);
Clrb_EN();
delay(100);
}
void LCD_Write(unsigned char address, unsigned char data)
{
address |= 0x80;
Clrb_RS();
delay(1);
Clrb_RS();
delay(1);
Clrb_RW();
delay(1);
Clrb_EN();
LCD_DAT(address>>4);
delay(1);
Setb_EN();
delay(1);
Clrb_EN();
LCD_DAT(address); //LCD_DATA = address;
delay(1);
Setb_EN();
delay(1);
Clrb_EN();
Clrb_RS();
delay(1);
Setb_RS();
delay(1);
Clrb_RW();
delay(1);
Clrb_EN();
LCD_DAT(data>>4);
delay(1);
Setb_EN();
delay(1);
Clrb_EN();
LCD_DAT(data);
delay(1);
Setb_EN();
delay(1);
Clrb_EN();
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
static unsigned U=0;
static unsigned H=0;
static unsigned M=0;
static unsigned S=0;
U++;
if(U==1000)
{
U = 0;
PORTB = ~PORTB;
LCD_Write(64,(H%100)/10+48);
LCD_Write(65,(H%10)+48);
LCD_Write(66,':');
LCD_Write(67,(M%100)/10+48);
LCD_Write(68,(M%10)+48);
LCD_Write(69,':');
LCD_Write(70,(S%100)/10+48);
LCD_Write(71,(S%10)+48);
S++;
if(S==60)
{
S = 0;
M++;
if(M==60)
{
M = 0;
H++;
if(H==24)
H = 0;
}
}
}
}
void main(void)
{
init_devices();
LCD_Init();
LCD_Write(0,'R');
LCD_Write(1,'E');
LCD_Write(2,'A');
LCD_Write(3,'L');
LCD_Write(4,' ');
LCD_Write(5,'T');
LCD_Write(6,'I');
LCD_Write(7,'M');
LCD_Write(8,'E');
LCD_Write(9,' ');
LCD_Write(10,'8');
LCD_Write(11,'.');
LCD_Write(12,'0');
LCD_Write(13,'0');
LCD_Write(14,'M');
LCD_Write(15,'H');
/*
LCD_Write(64,'A');
LCD_Write(65,'T');
LCD_Write(66,'M');
LCD_Write(67,'E');
LCD_Write(68,'G');
LCD_Write(69,'A');
LCD_Write(70,'1');
LCD_Write(71,'2');
LCD_Write(72,'8');
LCD_Write(73,'-');
LCD_Write(74,'S');
LCD_Write(75,'D');
LCD_Write(76,'K');
LCD_Write(77,'V');
LCD_Write(78,'1');
LCD_Write(79,'0');
*/
while(1)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -