📄 ccp.c
字号:
/*A sample project file to be used with HI-TIDE, to demonstrate the functionality of the microcontroller's CCPx.*/
/*Refer to ccp.txt for additional information*/
#include <pic18.h>
#include "ccp.h"
#include <stdio.h>
void init(void);
void putch(unsigned char);
volatile bit UPDATE_REQUIRED; /*used to indicate when to refresh output data*/
volatile char CURRENT_MODE1=0; /*used to indicate change CCP1CON register*/
volatile char CURRENT_MODE2=0; /*used to indicate change CCP2CON register*/
volatile bit SPECIAL_EVENT1=0; /*indicates trigger event for CCP1*/
volatile bit SPECIAL_EVENT2=0; /*indicates trigger event for CCP2*/
volatile bit CCP1INT=0; /*indicates that interrupt for CCP1 has occured*/
volatile bit CCP2INT=0; /*indicates that interrupt for CCP2 has occured*/
void main(void)
{
init();
while(1)
{
CCPR1L=DIP1;
CCPR2L=DIP2;
if (CCP1INT)
{
CCP1IE=0;
printf("CCP1 interrupt occured\n"); /*report interrupt on CCP1*/
CCP1INT=0;
if (SPECIAL_EVENT1)
{
printf("CCP1 special event occured, TMR1 has been reset \n"); /*report special event on CCP1*/
SPECIAL_EVENT1=0;
}
CCP1IE=1;
}
if (CCP2INT)
{
CCP2IE=0;
printf("CCP2 interrupt occured\n"); /*report interrupt on CCP2*/
CCP2INT=0;
if (SPECIAL_EVENT2)
{
printf("CCP2 special event occured, TMR3 has been reset GODONE value is: %d \n",GODONE); /*report special event on CCP2*/
SPECIAL_EVENT2=0;
}
CCP2IE=1;
}
if (UPDATE_REQUIRED)
{
LED1=CURRENT_MODE1+CURRENT_MODE2*16; /*update LEDs that are used to display current status of CCPs*/
CCP1CON=CURRENT_MODE1; /*update CCP1CON with new value*/
CCP2CON=CURRENT_MODE2; /*update CCP2CON with new value*/
printf("\n CCP1 STATUS\n----------------\n"); /*display status of CCP1*/
switch (CURRENT_MODE1)
{
case 0 :{ printf("Capture/Compare/PWM off \n");break;}
case 2 :{ printf("Compare mode, toggle output on match,\n current value of TRISC is %x \n",TRISC); break;}
case 3 :{ printf("Virtual CAN component is not implemented \n");break;}
case 4 :{ printf("Capture mode, every falling edge\n");break;}
case 5 :{ printf("Capture mode, every rising edge\n"); break;}
case 6 :{ printf("Capture mode, every 4th rising edge\n");break;}
case 7 :{ printf(" Capture mode, every 16th rising edge\n");break;}
case 8 :{ printf("Compare mode,Initialize CCP pin Low, \non compare match force CCP pin High \n"); break;}
case 9 :{ printf("Compare mode,Initialize CCP pin High, \non compare match force CCP pin Low\n");break;}
case 10 :{printf("Compare mode,Generate software \ninterrupt on compare match. CCP pin is unaffected\n");break;}
case 11 :{ printf("Compare mode,Trigger special \nevent (reset TMR1 or TMR3)\n"); break;}
default : printf("\n unimplemented MODE \n");
}
printf("----------------");
printf("\n CCP2 STATUS\n----------------\n"); /*display status of CCP2*/
switch (CURRENT_MODE2)
{
case 0 :{ printf("Capture/Compare/PWM off \n");break;}
case 2 :{ printf("Compare mode, toggle output on match \n"); break;}
case 3 :{ printf("Virtual CAN component is not implemented \n");break;}
case 4 :{ printf("Capture mode, every falling edge\n");break;}
case 5 :{ printf("Capture mode, every rising edge\n"); break;}
case 6 :{ printf("Capture mode, every 4th rising edge\n");break;}
case 7 :{ printf(" Capture mode, every 16th rising edge\n");break;}
case 8 :{ printf("Compare mode,Initialize CCP pin Low, \non compare match force CCP pin High \n"); break;}
case 9 :{ printf("Compare mode,Initialize CCP pin High, \non compare match force CCP pin Low\n");break;}
case 10 :{printf("Compare mode,Generate software \ninterrupt on compare match. CCP pin is unaffected\n");break;}
case 11 :{ printf("Compare mode,Trigger special \nevent (reset TMR1 or TMR3)\n"); break;}
default : printf("\n unimplemented MODE \n");
}
UPDATE_REQUIRED=0;
}
}
}
void init(void)
{
T3CON=0xA1; /*T3CON controls TIMER3 AND selects which timer is used for each CCP*/
TMR3=0; /*Load initial value to TIMER3*/
TMR3ON=1;
T1CON=0x81; /*use TIMER1 */
TMR1=0; /*Load initial value to TIMER1*/
TMR1ON=1;
TRISB=0x30; /* Push button is connected to RB4 to turn timers ON and OFF*/
TRISD=0xFF; /* 8-bit DIP switch to set value of CCP1*/
TRISH=0xFF; /* 8-bit DIP switch to set value of CCP2*/
TRISF=0x00; /* LED panels are connected to PORTF */
TRISE=0x80; /* RE7 is IO pin for CCP2*/
TRISC=0x04; /* RC2 is IO pin for CCP1*/
ADCON1=0x0F; /*configure pins of PORTF to be digital outputs*/
PSPMODE=0;
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 */
CCP1CON=0; /*initialise CCP1*/
CCPR1=0;
CCP1IF=0;
CCP1IE=1;
CCP2CON=0; /*initialise CCP2*/
CCPR2=0;
CCP2IF=0;
CCP2IE=1;
CURRENT_MODE1=0;
CURRENT_MODE2=0;
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 + -