STC8H8k64U简单实验程序
#include "config.h"
#include "System_init.h"
#include "STC8G_H_Delay.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_Timer.h"
#include "STC8G_H_NVIC.h"
#include "STC8G_H_WDT.h"
u8 N = 6; // 系统启动指示灯闪烁次数
u16 S_0 = 0; // S_0 秒
extern u16 Timer0_Count; // Timer0 计数值
extern u8 Timer0_Flag; // Timer0 1s 标志位
/* 可调毫秒延时函数 22.1184MHz */
void MyDelay_ms(u16 ms)
{
unsigned char i, j;
while(ms--)
{
_nop_();
_nop_();
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
}
/* GPIO配置 */
void GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; // 定义结构体
GPIO_InitStructure.Pin = GPIO_Pin_0; // 指定要初始化的IO,运行灯
GPIO_InitStructure.Mode = GPIO_PullUp; // 指定IO的输入输出方式
GPIO_Inilize(GPIO_P0,&GPIO_InitStructure); // 初始化,P00用于运行灯
GPIO_InitStructure.Pin = GPIO_Pin_1; // 指定要初始化的IO,系统启动指示灯
GPIO_InitStructure.Mode = GPIO_PullUp; // 指定IO的输入输出方式
GPIO_Inilize(GPIO_P0,&GPIO_InitStructure); // 初始化,P00用于运行灯
}
/* 定时器配置 */
void Timer_Init(void)
{
TIM_InitTypeDef TIM_InitStructure; // 定义结构体
TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; // 指定工作模式,16位自动重装
TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; // 指定时钟源
TIM_InitStructure.TIM_ClkOut = DISABLE; // 输出高速脉冲
TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000UL); //初值
TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
Timer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4
NVIC_Timer0_Init(ENABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
}
/* 看门狗配置 */
void WDTRun_Init(void)
{
WDT_InitTypeDef WDT_InitStructure; //结构定义
WDT_InitStructure.WDT_Enable = ENABLE; //中断使能 ENABLE或DISABLE,打开看门狗,一旦开启,看门狗硬件不能软件关闭,只有复位才能关掉
WDT_InitStructure.WDT_IDLE_Mode = WDT_IDLE_STOP; //IDLE模式是否停止计数 WDT_IDLE_STOP,WDT_IDLE_RUN,CPU 休眠 (IDLE) 的时候,看门狗停止计数
WDT_InitStructure.WDT_PS = WDT_SCALE_256; //看门狗定时器时钟分频系数 WDT_SCALE_2,WDT_SCALE_4,WDT_SCALE_8,WDT_SCALE_16,WDT_SCALE_32,WDT_SCALE_64,WDT_SCALE_128,WDT_SCALE_256
WDT_Inilize(&WDT_InitStructure); //初始化
}
void main(void)
{
EAXSFR(); // 扩展寄存器访问使能
SYS_Init();
GPIO_Init(); // GPIO配置
while(N) // 控制启动指示灯闪烁次数
{
P01 = ~P01;
N--;
MyDelay_ms(100);
}
P01 = 1; // 关闭系统启动指示灯
WDTRun_Init(); // 防止系统死机,启动看门狗(4.5s)
while (1)
{
delay_ms(1); // 延时1ms
WDT_Clear(); // 喂狗
while(1)
{
WDT_Clear(); // 喂狗
if(Timer0_Flag == 1)
{
Timer0_Flag = 0;
S_0++;
if(S_0 >= 5)
{
P01 = ~P01;
S_0 = 0;
break;
}
}
}
}
}
#include "STC8G_H_Timer.h"
//#include "Task.h"
u16 Timer0_Run_Count = 0;
u16 Timer0_Count = 0;
u8 Timer0_Flag = 0;
//========================================================================
// 函数: Timer0_ISR_Handler
// 描述: Timer0中断函数.
// 参数: none.
// 返回: none.
// 版本: V1.0, 2020-09-23
//========================================================================
void Timer0_ISR_Handler (void) interrupt TMR0_VECTOR //进中断时已经清除标志
{
//Task_Marks_Handler_Callback(); //任务标记回调函数
Timer0_Count++;
Timer0_Run_Count++;
if(Timer0_Run_Count >= 500) // 系统运行灯 500ms 闪烁一次
{
Timer0_Run_Count = 0;
P00 = ~P00;
}
if(Timer0_Count == 1000)
{
Timer0_Flag = 1;
Timer0_Count = 0;
}
}
//========================================================================
// 函数: Timer1_ISR_Handler
// 描述: Timer1中断函数.
// 参数: none.
// 返回: none.
// 版本: V1.0, 2020-09-23
//========================================================================
void Timer1_ISR_Handler (void) interrupt TMR1_VECTOR //进中断时已经清除标志
{
// TODO: 在此处添加用户代码
P66 = ~P66;
}
//========================================================================
// 函数: Timer2_ISR_Handler
// 描述: Timer2中断函数.
// 参数: none.
// 返回: none.
// 版本: V1.0, 2020-09-23
//========================================================================
void Timer2_ISR_Handler (void) interrupt TMR2_VECTOR //进中断时已经清除标志
{
// TODO: 在此处添加用户代码
P65 = ~P65;
}
//========================================================================
// 函数: Timer3_ISR_Handler
// 描述: Timer3中断函数.
// 参数: none.
// 返回: none.
// 版本: V1.0, 2020-09-23
//========================================================================
void Timer3_ISR_Handler (void) interrupt TMR3_VECTOR //进中断时已经清除标志
{
// TODO: 在此处添加用户代码
P64 = ~P64;
}
//========================================================================
// 函数: Timer4_ISR_Handler
// 描述: Timer4中断函数.
// 参数: none.
// 返回: none.
// 版本: V1.0, 2020-09-23
//========================================================================
void Timer4_ISR_Handler (void) interrupt TMR4_VECTOR //进中断时已经清除标志
{
// TODO: 在此处添加用户代码
P63 = ~P63;
}