📄 eccpdemo.c
字号:
#include <pic18.h>
#include "eccpdemo.h"
#include <stdio.h>
void interrupt ISR(void);
void init(void);
void set_new_mode(void);
unsigned char MODE=0;
unsigned char OLD_MODE=1; /* to force an update immediately */
static volatile bit CAPTURE;
void main(void)
{
init();
while(1)
{
MODE=PORTC&0x0F; // read the DIP switch
if(MODE!=OLD_MODE) // if something has changed...
{
set_new_mode(); // ...update the SFRs
}
OLD_MODE=MODE;
{
}
if(CAPTURE) // if a capture event has happened...
{
CAPTURE=0; // ...display details
printf("\nCAPTURED at %X\r",TMR3);
}
if(PWM_MODE)
update_pwm();
}
}
void init(void)
{
GIE=1; // interrupts are enabled
PEIE=1; // peripheral interrupts are enabled
IPEN=0;
TXEN=1; /* enable serial port transmissions */
SPEN=1;
TXIE=0; /* not interrupt driven */
TRISC=0xBF;
TRISD=0xFF;
TRISA=0xFF;
RBIE=1; // pushbutton can trigger PORTB interrupt
ECCPAS=0b00000000; // no autoshutdown events
T3CON=0b10001101; //TMR3 used for CCP module
T2CON=0x04; // TMR2 used for PWM
ECCP1IE=1; // ECCP mode is interrupt enabled
ECCP1IF=0;
ECCPR1=0x0F000; // Compare events on TMR3 = 0xF000
CAPTURE=0;
}
void interrupt ISR(void)
{
if((RBIE)&&(RBIF)) // if pushbutton pressed...
{
switch(MODE&0x0F)
{
case 0b1000: // if LED set on compare, pushbutton clears LED
LED=OFF;
break;
case 0b1001: // if LED clear in compare, pushbuton sets LED
LED=ON;
break;
default:
break;
}
RBIF=0;
}
if (ECCP_INTERRUPT)
{
if(CAPTURE_MODE)
{
CAPTURE=1; // set this flag if a capture event has been detected
}
ECCP1IF=0;
}
}
void putch(unsigned char c)
{
TXREG=c; /* transmit a character to Serial IO */
while(!TXIF)continue;
TXIF=0;
}
void set_new_mode(void)
{
printf("\r");
/* output current mode selected, and set PORTD to
input (pushbutton) or output (LED/analyser) as the
mode requires */
switch(MODE)
{
case 2:
printf("function:COMPARE, TOGGLE ON MATCH \r");
TRISD4=0;
break;
case 4:
printf("function:CAPTURE, ON FALLING EDGE \r");
TRISD4=1;
break;
case 5:
printf("function:CAPTURE, ON RISING EDGE \r");
TRISD4=1;
break;
case 6:
printf("function:CAPTURE, ON 4th RISE \r");
TRISD4=1;
break;
case 7:
printf("function:CAPTURE, ON 16th RISE \r");
TRISD4=1;
break;
case 8:
printf("function:COMPARE, GO HIGH ON MATCH \r");
TRISD4=0;
break;
case 9:
printf("function:COMPARE, GO LOW ON MATCH \r");
TRISD4=0;
break;
case 0xA:
printf("function:COMPARE, NO OUTPUT ON MATCH \r");
TRISD4=0;
break;
case 0xB:
printf("function:COMPARE, DO SPECIAL ON MATCH \r");
TRISD4=0;
break;
case 0xC:
printf("PWM mode: A&C=Active hi,B&D=Active hi\r");
TRISD4=0;
break;
case 0xD:
printf("PWM mode: A&C=Active hi,B&D=Active lo\r");
TRISD4=0;
break;
case 0xE:
printf("PWM mode: A&C=Active lo,B&D=Active hi\r");
TRISD4=0;
break;
case 0xF:
printf("PWM mode: A&C=Active lo,B&D=Active lo\r");
TRISD4=0;
break;
default:
printf("function:OFF \r");
TRISD4=0;
break;
}
ECCP1CON=((ECCP1CON&0xF0)+MODE); // update mode on SFR
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -