⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer0.c

📁 picc_18v 8.35pl35 PIC18系列单片机开发C编译器
💻 C
字号:

/*A sample project file to be used with HI-TIDE, to demonstrate the functionality of the microcontroller's TIMER0.*/
/*Refer to timer0.txt for additional information*/

#include <pic18.h>
#include "timer0.h"
#include <stdio.h>

void init(void);
void putch(unsigned char);
volatile bit UPDATE_REQUIRED ;		/* used to indicate when to refresh output data */

void main(void)
{
	init();

	while(1)
	{
					/*display current status of timer settings*/
			RE0=TMR0ON;	/*timer on/off -RED LED*/
			RE1=PSA;	/*prescalar off/on -GREEN LED*/	
			RE2=T0CS;	/*timer is in counter/timer mode -YELLOW LED*/
			RE3=T08BIT;	/*timer is in 8/16-bit mode -AMBER LED*/
			LED3=TMR0L;	/*Load LED3 with current value of TMR0L*/
			T0PS2=RA2;
			T0PS1=RA1;	/*update values of prescalar bits*/
			T0PS0=RA0;  

			if (UPDATE_REQUIRED)					
			{
				printf("\n TIMER0 STATUS\n----------------\n");
				if (TMR0ON){	printf("TIMER0 is ON\n");}
				else {printf("TIMER0 is OFF\n");}

				if (PSA){printf("PRESCALAR is OFF\n");}
				else{printf("PRESCALAR is ON\n");}

				if (T0CS){printf("TIMER0 is in COUNTER mode \n");}
				else{printf("TIMER0 is in TIMER mode \n");}

				if (T08BIT){printf("%cTIMER0 is in 8-bit mode \n",0x0D);	}
				else {printf("%cTIMER0 is in 16-bit mode \n",0x0D);}
				UPDATE_REQUIRED=0;	
			}
	}
}

void init(void)
{
	T0CON=0x4E;
	TMR0IF=0;			/* Clear overflow flag*/
	TMR0IE=1;			/* Enable TIMER0 interrupts */	
	
	T0PS2=RA2;
	T0PS1=RA1;			/*update values of prescalar bits*/
	T0PS0=RA0;  
	
	T1CON=0x80;			/*use TIMER1 as a reference for comparison*/
	TMR1IF=0;																	
	TMR1IE=1;																

	RBIE=1;				/* enable PORTB interrupts to */
	GIEH=1;				/* allow interrupts from PUSH BUTTONS */
	GIEL=1;	
	
	TXEN=1;				/* enable serial port transmissions */
	SPEN=1;				
	TXIE=0;				/* not interrupt driven */		

	TRISA=0x17;			/* Set first 3 bits to input to read DIP switch values and*/
					/*4th bit for counter, clock input */
	ADCON1=0x0F;			/*Configure all ADC pins  to be digital inputs*/

	TRISB=0xF0;			/* Pushbutton switches to change TIMER0 status are on PORTB */
	TRISC=0xBF;			/* LED panel to indicate oveflows of TIMER0*/
	TRISH=0x00;			/* LED panel to indicate oveflows  of TIMER1*/
	TRISD=0x00;			/* 8-bit DIP switch to set  value of LED pannels */
	TRISF=0x00;			/* LED panel to indicate current  value of TIMER0*/

	TRISE=0xC0;			/* LED panels to indicate current mode of the TIMER0*/

	TMR0=1;				/*Load initial value to TIMER0*/

	LED1=DIP1;			/*Load LEDs with current value of 8-bit DIP*/
	LED2=DIP1;
	LED3=TMR0L;			/*Load LED3 with current value of TMR0L*/

	UPDATE_REQUIRED=1;
}

void putch(unsigned char c)
{
	TXREG=c;			/* transmit a character to Serial IO */
	while(!TXIF)continue;
	TXIF=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -