估计是个学生,要参加电子竟赛的选拔吧。 送你一程,以下为参考 1s 一次状态改变:
//========================================================================
// 头文件
//========================================================================
#include "type_def.h"
#include "STC8G.H"
#include <intrins.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//========================================================================
// 主时钟定义
//========================================================================
#define MAIN_Fosc 24000000L //定义主时钟
//定时器0做16位自动重装, 中断频率为1000HZ, 1ms
#define T0_1mS (65536UL - (MAIN_Fosc / 1000UL)) //初值
u8 ucT0_1MS=0; // 10ms计时
//========================================================================
// 函数: Timer0_ISR_Handler
// 描述: Timer0中断函数.
// 参数: none.
// 返回: none.
//========================================================================
void Timer0_ISR_Handler (void) interrupt 1 //进中断时已经清除标志
{ // 1ms
// TODO: 在此处添加用户代码
ucT0_1MS++; // 10ms计时
}
void main(void)
{
u8 uc1s=0;
// IO
P3_MODE_IO_PU(GPIO_Pin_1|GPIO_Pin_2); //P3.1,P3.2 设置为准双向口
// 定时器
AUXR=0xC0; // 设置为 1T模式
//定时器0做16位自动重装, 中断频率为1000HZ, 1ms
TMOD=0x00;
TL0=T0_1mS; // 初值
TH0=(T0_1mS)>>8; // 初值
TR0=1;
ET0=1;
EA = 1;
while(1)
{
if(ucT0_1MS>=10)
{
ucT0_1MS=0; // 10ms定时
uc1s++; // 1s 计时
}
if(uc1s>=100) // 1s
{
uc1s=0;
P31 =~P31; // 测试点
P32 = ~P32; // 此处为1s/次,请根据自己的需求修改
}
}
}
|