📄 timer2.c
字号:
/*A sample project file to be used with HI-TIDE, to demonstrate the functionality of the microcontroller's TIMER1.*/
/*Refer to timer1.txt for additional information*/
#include <pic18.h>
#include "timer2.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)
{
RC0=TMR2ON; /*Timer on/off -RED LED*/
if (UPDATE_REQUIRED) /*Display current settings for TIMER2*/
{
TMR2IE=0; /*Disable TIMER2 interrupt while printing*/
printf("\n TIMER2 STATUS\n----------------\n");
if (TMR2ON)
{
printf("TIMER2 is ON\n----------------\n");
printf("PERIOD is %d\n----------------\n",PR2);
printf("PRESCALAR RATIO :");
switch (T2CON&0b00000011)
{
case 0b00000000 :{ printf("1:1\n");break;}
case 0b00000001 :{ printf("1:4 \n");break;}
case 0b00000010 :{ printf("1:16\n");break;}
case 0b00000011 :{ printf("1:16\n");break;}
default : printf("\n Illegal MODE \n");
}
printf("POSTSCALAR RATIO :");
switch (T2CON&0b01111000)
{
case 0b00000000 :{ printf("1:1 \n");break;}
case 0b00001000 :{ printf("1:2 \n");break;}
case 0b00010000 :{ printf("1:3 \n"); break;}
case 0b00011000 :{ printf("1:4 \n");break;}
case 0b00100000 :{ printf("1:5 \n");break;}
case 0b00101000 :{ printf("1:6 \n"); break;}
case 0b00110000 :{ printf("1:7 \n");break;}
case 0b00111000 :{ printf("1:8 \n");break;}
case 0b01000000 :{ printf("1:9\n");break;}
case 0b01001000 :{ printf("1:10\n");break;}
case 0b01010000 :{ printf("1:11\n");break;}
case 0b01011000 :{ printf("1:12\n");break;}
case 0b01100000 :{ printf("1:13\n");break;}
case 0b01101000 :{ printf("1:14\n");break;}
case 0b01110000 :{ printf("1:15\n");break;}
case 0b01111000 :{ printf("1:16\n");break;}
default : printf("\n Illegal MODE \n");
}
}
else
{
printf("TIMER2 is OFF\n");
}
UPDATE_REQUIRED=0;
TMR2IE=1; /*Reenable TIMER2 interrupt*/
}
}
}
void init(void)
{
T2CON=0x00; /* we are testing TIMER2 */
TMR2ON=0;
TMR2=0; /*Load initial value to TIMER2*/
PR2=0xFF;
TMR2IF=0; /* Clear overflow flag*/
TMR2IE=1; /* Enable TIMER2 interrupts */
T0CON=0x40; /*use TIMER0 as reference for comparison*/
TMR0IF=0;
TMR0IE=1;
TMR0=0; /*Load initial value to TIMER0*/
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=0x03; /* Set first 2 bits to input to read DIP switch values */
ADCON1=0x0F; /*configure pins of PORTA to be digital inputs*/
TRISB=0xF0; /* Push button is connected to RB4 to turn timers ON and OFF*/
TRISC=0x00; /* 1bit LED panel is connected to RC1 to indicate TIMER2 ON/OFF status*/
TRISH=0x00; /* LED panel shows current frequency of TIMER2*/
TRISE=0xFF;
TRISF=0x00; /* LED panel indicates current frequency of TIMER0 in 8bit mode*/
TRISD=0xFF; /* 8-bit DIP switch to set value of period register for TIMER2 (PR2)*/
LED1=1; /*Load LEDs with current value of 8-bit DIP*/
LED2=1;
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 + -