⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autoshut.c

📁 picc_18v 8.35pl35 PIC18系列单片机开发C编译器
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -