找回密码
 立即注册
查看: 1119|回复: 1

STC8g1k08A进不了定时中断

[复制链接]
  • 打卡等级:初来乍到
  • 打卡总天数:1
  • 最近打卡:2023-12-21 18:38:05

3

主题

0

回帖

39

积分

新手上路

积分
39
发表于 2023-12-21 18:47:06 | 显示全部楼层 |阅读模式
#include    "config.h"
#include    "GPIO.h"
#include    "main.h"
#include    "UART.h"
#include    "timer.h"
#include    "delay.h"
#include "store.h"
#include    "I2C.h"

/***************    功能说明    ****************

本例程基于STC8H8K64U为主控芯片的实验箱8进行编写测试,STC8G、STC8H系列芯片可通用参考.

程序使用P6口来演示跑马灯,输出低驱动。

下载时, 选择时钟 24MHz (可以在配置文件"config.h"中修改).

******************************************/


/*定时器0做16位自动重装, 中断频率为1KHZ*/
/************************ 定时器配置 ****************************/
void    Timer_config(void)
{
    TIM_InitTypeDef        TIM_InitStructure;                        //结构定义
    TIM_InitStructure.TIM_Mode      = TIM_16BitAutoReload;    //指定工作模式,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
    TIM_InitStructure.TIM_Priority    = Priority_3;            //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
    TIM_InitStructure.TIM_Interrupt = ENABLE;                    //中断是否允许,   ENABLE或DISABLE
    TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T;        //指定时钟源,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
    TIM_InitStructure.TIM_ClkOut    = DISABLE;                //是否输出高速脉冲, ENABLE或DISABLE
    TIM_InitStructure.TIM_Value     = 65536UL - (MAIN_Fosc / 1000);        //初值,
    TIM_InitStructure.TIM_Run       = ENABLE;                    //是否初始化后启动定时器, ENABLE或DISABLE
    Timer_Inilize(Timer0,&TIM_InitStructure);                    //初始化Timer0      Timer0,Timer1,Timer2,Timer3,Timer4
}


/******************** 主函数 **************************/
void main(void)
{
    u8 i=0;
    Timer_config();

    EA = 1;
    while(1);

}


/*************    功能说明    **************

本文件为STC8系列的定时器初始化和中断程序,用户可以在这个文件中修改自己需要的中断程序.

******************************************/

#include    "timer.h"
#include    "main.h"
/********************* Timer0中断函数************************/
void timer0_int (void) interrupt TIMER0_VECTOR
{
    P54=~P54;//DEBUG时进不了这里,请各位大佬帮忙分析下程序那里有问题。
}

/********************* Timer1中断函数************************/
void timer1_int (void) interrupt TIMER1_VECTOR
{

}

/********************* Timer2中断函数************************/
void timer2_int (void) interrupt TIMER2_VECTOR
{
}

/********************* Timer3中断函数************************/
void timer3_int (void) interrupt TIMER3_VECTOR
{
}

/********************* Timer4中断函数************************/
void timer4_int (void) interrupt TIMER4_VECTOR
{
}


//========================================================================
// 函数: u8    Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
// 描述: 定时器初始化程序.
// 参数: TIMx: 结构参数,请参考timer.h里的定义.
// 返回: 成功返回0, 空操作返回1,错误返回2.
// 版本: V1.0, 2012-10-22
//========================================================================
u8    Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
{
    if(TIM > Timer4)    return 1;    //空操作

    if(TIM == Timer0)
    {
        Timer0_Stop();        //停止计数
        if(TIMx->TIM_Interrupt == ENABLE)        Timer0_InterruptEnable();    //允许中断
        else        Timer0_InterruptDisable();    //禁止中断
        if(TIMx->TIM_Priority > Priority_3)    return 2;    //错误
        Timer0_Priority(TIMx->TIM_Priority);    //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3

        if(TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask)    return 2;    //错误
        TMOD = (TMOD & ~0x03) | TIMx->TIM_Mode;    //工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动重装, 3: 不可屏蔽16位自动重装
        if(TIMx->TIM_ClkSource == TIM_CLOCK_12T)    Timer0_12T();    //12T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_1T)        Timer0_1T();    //1T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext)    Timer0_AsCounter();    //对外计数或分频
        else        Timer0_AsTimer();    //定时
        if(TIMx->TIM_ClkOut == ENABLE)    Timer0_CLKO_Enable();    //输出时钟
        else        Timer0_CLKO_Disable();    //不输出时钟
        
        T0_Load(TIMx->TIM_Value);
        if(TIMx->TIM_Run == ENABLE)    Timer0_Run();    //开始运行
        return    0;        //成功
    }

    if(TIM == Timer1)
    {
        Timer1_Stop();        //停止计数
        if(TIMx->TIM_Interrupt == ENABLE)        Timer1_InterruptEnable();    //允许中断
        else        Timer1_InterruptDisable();    //禁止中断
        if(TIMx->TIM_Priority > Priority_3)    return 2;    //错误
        Timer1_Priority(TIMx->TIM_Priority);    //指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
        if(TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask)    return 2;    //错误
        TMOD = (TMOD & ~0x30) | (TIMx->TIM_Mode<<4);    //工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动重装, 3: 停止工作
        if(TIMx->TIM_ClkSource == TIM_CLOCK_12T)    Timer1_12T();    //12T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_1T)        Timer1_1T();    //1T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext)    Timer1_AsCounter();    //对外计数或分频
        else        Timer1_AsTimer();    //定时
        if(TIMx->TIM_ClkOut == ENABLE)    Timer1_CLKO_Enable();    //输出时钟
        else        Timer1_CLKO_Disable();    //不输出时钟
        
        T1_Load(TIMx->TIM_Value);
        if(TIMx->TIM_Run == ENABLE)    Timer1_Run();    //开始运行
        return    0;        //成功
    }

    if(TIM == Timer2)        //Timer2,固定为16位自动重装, 中断无优先级
    {
        Timer2_Stop();    //停止计数
        if(TIMx->TIM_Interrupt == ENABLE)        Timer2_InterruptEnable();    //允许中断
        else        Timer2_InterruptDisable();    //禁止中断
        if(TIMx->TIM_ClkSource >  TIM_CLOCK_Ext)    return 2;
        if(TIMx->TIM_ClkSource == TIM_CLOCK_12T)    Timer2_12T();    //12T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_1T)        Timer2_1T();    //1T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext)    Timer2_AsCounter();    //对外计数或分频
        else        Timer2_AsTimer();        //定时
        if(TIMx->TIM_ClkOut == ENABLE)    Timer2_CLKO_Enable();        //输出时钟
        else        Timer2_CLKO_Disable();    //不输出时钟

        T2_Load(TIMx->TIM_Value);
        if(TIMx->TIM_Run == ENABLE)    Timer2_Run();    //开始运行
        return    0;        //成功
    }

    if(TIM == Timer3)        //Timer3,固定为16位自动重装, 中断无优先级
    {
        Timer3_Stop();    //停止计数
        if(TIMx->TIM_Interrupt == ENABLE)        Timer3_InterruptEnable();    //允许中断
        else        Timer3_InterruptDisable();    //禁止中断
        if(TIMx->TIM_ClkSource >  TIM_CLOCK_Ext)    return 2;
        if(TIMx->TIM_ClkSource == TIM_CLOCK_12T)    Timer3_12T();    //12T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_1T)        Timer3_1T();    //1T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext)    Timer3_AsCounter();    //对外计数或分频
        else        Timer3_AsTimer();        //定时
        if(TIMx->TIM_ClkOut == ENABLE)    Timer3_CLKO_Enable();        //输出时钟
        else        Timer3_CLKO_Disable();    //不输出时钟

        T3_Load(TIMx->TIM_Value);
        if(TIMx->TIM_Run == ENABLE)    Timer3_Run();    //开始运行
        return    0;        //成功
    }

    if(TIM == Timer4)        //Timer3,固定为16位自动重装, 中断无优先级
    {
        Timer4_Stop();    //停止计数
        if(TIMx->TIM_Interrupt == ENABLE)        Timer4_InterruptEnable();    //允许中断
        else        Timer4_InterruptDisable();    //禁止中断
        if(TIMx->TIM_ClkSource >  TIM_CLOCK_Ext)    return 2;
        if(TIMx->TIM_ClkSource == TIM_CLOCK_12T)    Timer4_12T();    //12T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_1T)        Timer4_1T();    //1T
        if(TIMx->TIM_ClkSource == TIM_CLOCK_Ext)    Timer4_AsCounter();    //对外计数或分频
        else        Timer4_AsTimer();        //定时
        if(TIMx->TIM_ClkOut == ENABLE)    Timer4_CLKO_Enable();        //输出时钟
        else        Timer4_CLKO_Disable();    //不输出时钟

        T4_Load(TIMx->TIM_Value);
        if(TIMx->TIM_Run == ENABLE)    Timer4_Run();    //开始运行
        return    0;        //成功
    }
    return 2;    //错误
}






回复

使用道具 举报 送花

  • 打卡等级:偶尔看看III
  • 打卡总天数:56
  • 最近打卡:2025-05-03 10:05:34

718

主题

1万

回帖

1万

积分

管理员

积分
15646
发表于 2023-12-21 19:51:59 | 显示全部楼层
STC8G1K08A-36I-SOP8,  没有 T2/T3/T4, 有 T0/T1
回复 支持 反对

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2025-5-3 15:02 , Processed in 0.138912 second(s), 51 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表