autoshut.c

来自「picc_18v 8.35pl35 PIC18系列单片机开发C编译器」· C语言 代码 · 共 91 行

C
91
字号
#include <pic18.h>
#include <stdio.h>

void init(void);
void interrupt ISR(void);
void putch(unsigned char);

static volatile bit update;

void main(void)
{	
	init();
	
	while(1)
	{      
		if(INT1IF)	// pushbutton to turn ECCP module back on
		{
			ECCPASE=0;
			INT1IF=0;
			update=1;
		}
		
  		if(update)	// if the state of operation has changed
		{
			printf("ECCP module is ");	// report current state of ECCP module
			if(ECCPASE)
				printf("shutdown\r");
			else
				printf("active  \r");
			update=0;
		}
		
		RC2=!ECCPASE;	// GREEN LED
		RC0=ECCPASE;	// RED LED
	}
}

void init(void)
{
	GIE=1;
	PEIE=1;
	IPEN=0;
	
	CMIF=0;
	CMIE=1;
	
	INT0IF=0;		// INT0 is autoshutdown source
	INT0IE=1;
	INT1IE=0;	// INT1 re-activates ECCP module
	
	TXEN=1;		// enable serial transmissions
	SPEN=1;
	
	ECCPAS=0x70;	// autoshutdown triggered from 3 possible sources
	ECCP1CON=0x0C;	// ECCP module is in PWM mode.
	ECCPR1L=0x33;
	
	TMR2ON=1;		// TMR2 required for PWM mode

	TRISB=0xFF;
        TRISC=0x00;
	TRISD=0x0F;

	CMCON=0b00000110;	// comparator = CVREF module vs Pushbuttons
	CMIF=0;
	CMIE=1;
	
	CVRCON=0b10101000;	// set up CVREF module
	
	update=1;
}

void interrupt ISR(void)
{
	if((INT0IE)&&(INT0IF))
		INT0IF=0;
	
	if((CMIE)&&(CMIF))
		CMIF=0;
		
	update=1;
}

void putch(unsigned char c)
{
		TXREG=c;
		while(!TXIF)continue;TXIF=0;
}
		
		

⌨️ 快捷键说明

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