这是NSS08Kit-R1开发板上的看门狗演示程序。NSS08Kit-R1是我们开发的一款低成本飞思卡尔S08开发板,支持MC9S08AC16、MC9S08QD4、MC9S08SH8、MC9S08QE8等多款单片机,可以说一个开发板可以用于学习和开发多种型号的单片机应用。
//filename:delay.h
#ifndef _NICROSYSTEM_FREESCALE_S08_DELAY_H_
#define _NICROSYSTEM_FREESCALE_S08_DELAY_H_
void delay_us(unsigned char cnt);
void delay_ms(unsigned char cnt);
void delay_1s(unsigned char cnt);
#endif
//filename:delay.c
/********************
@ Nicrosystem Freescale HCS08/RS08 Develop Kit
@ delay function
@ author:bluehacker<nicrosystem@gmail.com>
@ Date:2009-04-04
*******************************/
/***************
Note:
这里的延时并不准
*/
#include "delay.h"
#include "derivative.h"
#ifndef BUS_CLK
#define BUS_CLK 4000000L
#endif
void delay_us(unsigned char cnt) {
unsigned char i;
for(i=0;i<cnt;i++){
__RESET_WATCHDOG();
}
}
void delay_ms(unsigned char cnt){
unsigned char i;
for(i=0;i<cnt;i++){
__RESET_WATCHDOG();
delay_us(250);
delay_us(250);
delay_us(250);
delay_us(250);
}
}
void delay_1s(unsigned char cnt){
unsigned char i;
for(i=0;i<cnt;i++){
delay_ms(250);
delay_ms(250);
delay_ms(250);
delay_ms(250);
}
}
//filename:mcuinit.h
#ifndef _NICROSYSTEM_FREESCALE_S08_DEVKIT_MCUINIT_H_
#define _NICROSYSTEM_FREESCALE_S08_DEVKIT_MCUINIT_H_
void init_system_clock();
#endif
//filename:mcuinit.c
/*****************************
@ Nicrosystem Freescale S08 DevKit(NSS08Kit-R1)
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-04-07
******************************/
#include "mcuinit.h"
#include "derivative.h"
//设置系统时钟模式,为FEE,8MHZ的系统主频,BUS CLK为4MHZ
void init_system_clock()
{
ICGC1=0xf8;
ICGC2=0x89;
while(ICGS1_LOCK==0);
}
filename: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_set_mode(unsigned char cursor, unsigned char text);
void lcd_reset_cursor(void);
void lcd_write_char(unsigned char x,unsigned char y, unsigned char d);
void lcd_write_str(unsigned char x,unsigned char y,unsigned char *s);
void lcd_write_data(unsigned char dat);
void lcd_write_cmd(unsigned char cmd);
#endif
//filename:lcd1602.c
/*****************************
@ Nicrosystem Freescale S08 DevKit(NSS08Kit-R1)
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-04-07
******************************/
/********************************************************
**本开发板支持1602 字符型LCD
**这种LCD的接口资料可以用google找到很多
**引脚定义:
* 1---GND
* 2---VDD
* 3---VLCD:对比度调节
* 4---RS:寄存器选择,高电平时选择数据寄存器、低电平时选择指令寄存器
* 5---R/W:读写信号线,高电平时进行读操作,低电平时进行写操作。
当RS和RW共同为低电平时可以写入指令或者显示地址,当RS为低电平
RW为高电平时可以读忙信号,当RS为高电平RW为低电平时可以写入数据。
* 6---E:使能端,当E端由高电平跳变成低电平时,液晶模块执行命令。
* 7~14---DB0~DB7数据线、
* 15---A:背光引脚,"A"接正
* 16---K:背光引脚,"K"接负
*********************************************************/
#include "lcd1602.h"
#include "delay.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
/*向LCD写入命令*/
void lcd_write_cmd(unsigned char cmd)
{
unsigned char status;
__RESET_WATCHDOG();
LCD_DATA=cmd;
LCDRS=0;
LCDRW=0;
LCDE=0;
delay_us(5);
do{
status=lcd_status();
}while(status&0x80);
LCDE=1;
}
void lcd_write_data(unsigned char dat)
{
unsigned char status;
__RESET_WATCHDOG();
LCD_DATA=dat;
LCDRS=1;
LCDRW=0;
LCDE=0;
delay_us(5);
do{
status=lcd_status();
}while(status&0x80);
LCDE=1;
}
void lcd_clear(void)
{
lcd_write_cmd(0x01);
}
/*显示屏字符串写入函数*/
void lcd_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0) {
lcd_write_cmd(0x80 + x);
}
else {
lcd_write_cmd(0xC0 + x);
}
while (*s) {
lcd_write_data( *s);
s ++;
}
}
void lcd_write_char(unsigned char x,unsigned char y, unsigned char d)
{
if(y==0)
{
lcd_write_cmd(0x80+x);
}
else
{
lcd_write_cmd(0xc0+x);
}
lcd_write_data(d);
}
//光标复位
void lcd_reset_cursor(void)
{
lcd_write_cmd(0x02);
}
//设置显示模式
void lcd_set_mode(unsigned char cursor, unsigned char text)
{
unsigned char mode="0x04";
if(cursor){//光标右移
mode|=0x02;
}
else{//光标左移
mode|=0x00;
}
if(text){//文字移动
mode|=0x01;
}
else{//文字不移动
mode|=0;
}
lcd_write_cmd(mode);
}
void lcd_init(void)
{
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;
__RESET_WATCHDOG();
LCD_DATA=0;
LCDE=1;
delay_ms(250);
lcd_clear();
lcd_write_cmd(0x38);//设置lcd功能:8位总线,双行显示,5X7点阵字符
lcd_write_cmd(0x0f);//显示开关控制:显示ON,光标ON,闪烁ON
lcd_write_cmd(0x06);//光标输入方式增量移位
lcd_write_cmd(0x80);
}
//读取状态,是否忙
unsigned char lcd_status(void)
{
unsigned char tmp="0";
LCD_DATA=0xff;
LCDRS=0;
LCDRW=1;
LCDE=0;
__RESET_WATCHDOG();
delay_us(1);
LCDE=1;
LCD_DATA_DIR=0x00;
tmp=LCD_DATA;
LCD_DATA_DIR=0xff;
return tmp;
}
//filename:main.c
/*****************************
@ Nicrosystem Freescale S08 DevKit(NSS08Kit-R1)
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-04-11
******************************/
/*****************************
@说明:
本程序测试MC9S08AC16的看门狗COP,
我们先在LCD1602上显示,当看门狗
复位时,可以看到LCD灭掉又重新显示
******************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "lcd1602.h"
#include "mcuinit.h"
#include "delay.h"
void main(void) {
EnableInterrupts; /* enable interrupts */
/* include your code here */
__RESET_WATCHDOG();
init_system_clock();
SOPT2_COPCLKS=0;//select internal 1 KHZ clock
SOPT_COPT=1;//256ms overflow
SOPT_COPE=1;//enable cop
__RESET_WATCHDOG();
lcd_init();
__RESET_WATCHDOG();
lcd_write_str(1,0,"Nicrosystem");
__RESET_WATCHDOG();
lcd_write_str(0,1,"FreeScale COP");
__RESET_WATCHDOG();
delay_1s(1);
lcd_clear();
__RESET_WATCHDOG();
delay_1s(1);
for(;;) {
//__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
文章评论(0条评论)
登录后参与讨论