時鐘顯示程序
使用MC9S08AC的Timer做一個時鐘,并在LCD1602上顯示
lcd1602.h
#ifndef _NICROSYSTEM_FREESCALE_S08_DEVKIT_LCD1602_H_
#define _NICROSYSTEM_FREESCALE_S08_DEVKIT_LCD1602_H_
unsigned char lcd_status(void);
void lcd_init(void);
void lcd_write_char(unsigned char x,unsigned char y, unsigned char ch);
void lcd_write_str(unsigned char x,unsigned char y,unsigned char *s);
void lcd_write_data(unsigned char data);
void lcd_write_cmd(unsigned char cmd);
#endif
lcd1602.c
#include "lcd1602.h"
#include "derivative.h"
#define LCD_DATA PTED
#define LCD_DATA_DIR PTEDD
#define LCD_DATA_DS PTEDS
#define LCDRS PTAD_PTAD0
#define LCDRW PTAD_PTAD1
#define LCDE PTBD_PTBD0
#define LCDRS_DIR PTADD_PTADD0
#define LCDRW_DIR PTADD_PTADD1
#define LCDE_DIR PTBDD_PTBDD0
#define LCDE_DS PTBDS_PTBDS0
#define LCDRS_DS PTADS_PTADS0
#define LCDRW_DS PTADS_PTADS1
void lcd_clear(void);
void lcd_write_cmd(unsigned char cmd);
void init_lcd() {
LCD_DATA_DIR=0xff;
LCDRS_DIR=1;
LCDRW_DIR=1;
LCDE_DIR=1;
LCDRS_DS=1;
LCDRW_DS=1;
LCDE_DS=1;
LCD_DATA_DS=0xff;
LCD_DATA=0;
LCDE=1;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
lcd_clear();
lcd_write_cmd(0x38);//設(shè)置lcd功能:8位總線,雙行顯示,5X7點陣字符
lcd_write_cmd(0x0f);//顯示開關(guān)控制:顯示ON,光標(biāo)ON,閃爍ON
lcd_write_cmd(0x06);//光標(biāo)輸入方式增量移位
lcd_write_cmd(0x80);
}
unsigned char lcd_status() {
byte lcdstatus;
LCD_DATA_DIR=0x00;
LCDRS=0;
LCDRW=1;
LCDE=0;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCDE=1;
lcdstatus=LCD_DATA;
LCD_DATA_DIR=0xff;
return lcdstatus;
}
void lcd_write_cmd(unsigned char cmd) {
byte status;
LCD_DATA=cmd;
LCDRS=0;
LCDRW=0;
LCDE=0;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
do{
status=lcd_status();
}
while(status&0x80);
LCDE=1;
}
void lcd_clear(void) {
lcd_write_cmd(0x01);
}
void lcd_write_data(unsigned char data) {
byte status;
LCD_DATA=data;
LCDRS=1;
LCDRW=0;
LCDE=0;
asm("nop");
asm("nop");
asm("nop");
asm("nop");
do{
status=lcd_status();
}
while(status&0x80);
LCDE=1;
}
void lcd_write_char(unsigned char x,unsigned char y,unsigned char ch) {
if(y)
lcd_write_cmd(0xc0+x);
else
lcd_write_cmd(0x80+x);
lcd_write_data(ch);
}
void lcd_write_str(unsigned char x,unsigned char y,char*s) {
if(y)
lcd_write_cmd(0xc0+x);
else
lcd_write_cmd(0x80+x);
while(*s){
lcd_write_data(*s);
s++;
}
}
main.c
#include /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "mc9s08ac16.h"
#include "lcd1602.h"
const unsigned char table[10]={‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’};
unsigned char hour=11,minute=20,second=0;
unsigned char con_result[9];
void init_system_clk(void)
{
ICGC1=0xf8;
ICGC2=0x89;
while(ICGS1_LOCK==0);
}
void convert() /*covert numeric data to char data,for instance,1 to ‘1’ */
{
unsigned char tens,unit;
tens=second/10;
unit=second%10;
con_result[7]=table[unit];
con_result[6]=table[tens];
tens=minute/10;
unit=minute%10;
con_result[4]=table[unit];
con_result[3]=table[tens];
tens=hour/10;
unit=hour%10;
con_result[1]=table[unit];
con_result[0]=table[tens];
con_result[8]=‘\0’;
con_result[2]=‘:’;
con_result[5]=‘:’;
}
interrupt VectorNumber_Vtpm1ovf void TMP1_OVF_ISR(void)
{
DisableInterrupts;
TPM1SC_TOF=0;
second++;
if(second>59)
{
minute++;
second=0;
}
if(minute>59)
{
hour++;
minute=0;
}
if(hour>23)
{
hour=0;
}
convert();
lcd_write_str(4,1,con_result);
lcd_write_cmd(0x0c);
EnableInterrupts;
}
void main(void)
{
EnableInterrupts; /* enable interrupts */
/* include your code here */
SOPT_COPE=0;
init_system_clk();
init_lcd();
lcd_write_str(2,0,"NicroSystem");
TPM1SC=0x4e;
TPM1MODH=0xf4;
TPM1MODL=0x24;
for(;;) {
//__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
評論
查看更多