?? timer1.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 "timer1.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)
{
RC1=TMR1ON; /*timer on/off -RED LED*/
LED3=TMR1L; /*display current value of TMR1L*/
T1CKPS1=RA1; /*update values of prescalar bits*/
T1CKPS0=RA0;
if (UPDATE_REQUIRED) /*display current settings for TIMER1*/
{
printf("\n TIMER1 STATUS\n----------------\n");
if (TMR1ON){ printf("TIMER1 is ON\n");}
else {printf("TIMER1 is OFF\n");}
if (TMR1CS){ printf("TIMER1 is in COUNTER mode\n");}
else {printf("TIMER1 is in TIMER mode\n");}
UPDATE_REQUIRED=0;
}
}
}
void init(void)
{
T1CON=0x80; /* we are testing TIMER1 */
T1CKPS1=RA1; /*update values of prescalar bits*/
T1CKPS0=RA0;
TMR1IF=0; /* Clear overflow flag*/
TMR1IE=1; /* Enable TIMER1 interrupts */
TMR1=0; /*Load initial value to TIMER1*/
T3CON=0x00; /*use TIMER3 as reference for comparison*/
TMR3IF=0;
TMR3IE=1;
TMR3=0; /*Load initial value to TIMER3*/
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=0x01; /* 1bit LED panel is connected to RC1 to indicate TIMER1 ON/OFF status*/
/* Push button connected to RC0 is used to test TIMER1 as a counter*/
TRISH=0x00; /* LED panel indicates frequency of TIMER3*/
TRISE=0x00; /* LED panel indicates current frequency of TIMER1 */
TRISF=0x00; /* LED panel shows current value of register TMR1L */
TRISD=0xFF; /* 8-bit DIP switch to set value of LED pannels */
LED1=PORTD; /*Load LEDs with current value of 8-bit DIP*/
LED2=PORTD;
LED3=TMR1L; /*Load LED3 with current value of TMR1L*/
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 + -