中微 CMS8S5880 timer2

废话不多直接上代码

main.c

#include "uart.h"
#include "pwm.h"
#include "led.h"
#include "stdio.h"
#include "sys_time.h"
#include "timer.h"
#include "my_iic.h"



void main()
{
	UART0_INIT();								//initinal UART
	PWM_init();
	LED_INIT();
	Timer2Init();
	set_pwm5_Duty_cycle(30);
	IIC_GPIO_Init();
	printf("code start\r\n");
	EA = 1;//使能总中断
	calc_value_pressure();
	LED4_ON;
	LED5_ON;
	while(1)
	{
		if(get_sys_time_ms() % 100 == 0)
		{
			Put_press_and_temp_value();
			printf("Pressure %f\r\n",pressure);
			printf("TEMP %f\r\n",temp); 
		}
		clrwdt();	
	}

}

lib.h

#ifndef __LIB_H__
#define __LIB_H__

#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80



#endif

timer.c

#include <CMS/CMS8S5880.H>
#include "led.h"
#include "timer.h"
#include "lib.h"
#include "sys_time.h"
#include "my_iic.h"


/*Timer2初始化(溢出中断)*/
//2^16-63535/2000000 = 1000hz
void Timer2Init(void)
{
    xdata uint16_t temp = 0;
    xdata uint16_t hz = 10000;
    hz = 2000000 / hz;
    temp = 0xffff - hz;

    T2CON = 0x11;/*时钟来源=SysClk/12,溢出时自动重载*/
    /*定时时间=(2^16-{TH2,TL2})/f(Timer2时钟来源)*/
	RLDH = (temp >> 8) & 0xff;
	RLDL = temp & 0xff;
	//RLDH = 0xf8;
   // RLDL = 0x2f;
    
    TH2 = RLDH;
    TL2 = RLDL;
    
    /*清溢出标志*/
    T2IF &= 0x7F;
    
    /*使能Timer2溢出中断*/
    T2IE |= 0x80;//Timer2溢出中断
    ET2 = 1;//Timer2总中断
}


/*Timer2中断处理程序*/
void Timer2Int(void) interrupt 5
{  
	sys_time_count();
	delay_time_lose();
    T2IF &= 0x7F;//清溢出标志
}

timer.h

#ifndef __TIMER_H__
#define __TIMER_H__

void Timer2Init(void);


#endif

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值