中微 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 
#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

 

 

你可能感兴趣的:(中微单片机开发,c语言,芯片,嵌入式)