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

📄 comparators.c

📁 picc_18v 8.35pl35 PIC18系列单片机开发C编译器
💻 C
字号:

/*A sample project file to be used with HI-TIDE, to demonstrate the functionality of the microcontroller's comparators module.*/
/*Refer to comparators.txt for additional information*/

#include <pic18.h>
#include "comparators.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)
	{
			RB0=C1OUT;																		/*C1OUT and C2OUT bits CMCON of are wired to RED LEDs*/
			RB1=C2OUT;
			if (UPDATE_REQUIRED)							  							/*Display current coparators' settings*/   
			{
				printf("\n COMPARATORS MODE\n----------------\n");
				switch (CMCON&07)			
				{
				case 0 :{	  printf("Comparators Reset \n");break;}
				case 1 :{  printf("One Independent Comparator with Output\n");break;}
				case 2 :{  printf("Two Independent Comparators \n");	break;}
				case 3 :{  printf("Two Independent Comparators with Outputs \n");break;}
				case 4 :{  printf("Two Common Reference Comparators\n");break;}
				case 5 :{  printf("Two Common Reference Comparators with Outputs\n");	break;}
				case 6 :{  printf("Four Inputs Multiplexed to Two Comparators\n");break;}
				case 7 :{  printf("Comparators Off \n");break;}
				default : printf("\n Illegal MODE \n");
				}
				if ((CMCON&07)==6)
				{
					printf("CIS: Comparator Input Switch Status\n----------------\n");
					if (CIS)
					{
						printf ("C1 VIN connects to RF5/AN10\n");
						printf ("C2 VIN connects to RF3/AN8\n");
					}
					else
					{
						printf ("C1 VIN connects to RF6/AN11\n");
						printf ("C2 VIN connects to RF4/AN9\n");
					}
				}
				printf("----------------\n");
				if (C2INV)	printf("C2 output inverted\n");
				if (C1INV)	printf("C1 output inverted\n");
				printf("----------------\n");					
				printf("C1 output is : %d\n",C1OUT);
				printf("C2 output is : %d\n",C2OUT);			
				printf("----------------\n");					
	
				if (CVREN)
				{
					printf("Voltage reference is : %d mV dc\n",((CVRCON&0x0F)*1000/24)*5);
				}
				UPDATE_REQUIRED=0;	
			}
	}
}

void init(void)
{

	CMCON=0;																	/*Initialise comparators*/
	CMIF=0;																	                                                                                 			
	CMIE=1;																                                                                                 			
	CVRCON=0;

	TRISE=0x0F;																/* 4-bit LED connected to PORTE reflects current mode of comparators*/
	TRISC=0x8F;																/*4-bit DIP connected to PORTC is used to set  the Voltage reference value*/
	TRISF=0x78;																/*Configure IO for comparators*/
	TRISD=0xFF;																/* 6-bit DIP switch connected to PORTD controls CMCON*/
	TRISB=0x10;																/* Push button is connected to   RB4 */

	TXEN=1;																		/* enable serial port transmissions */
	SPEN=1;				
	TXIE=0;																		/* not interrupt driven */		

	ADCON1=0x0F;															/*configure all ADC pins as digital*/

	RBIE=1;																		/* enable PORTB interrupts to*/
	GIEH=1;																		/* allow interrupts from PUSH BUTTONS */
	GIEL=1;	

	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 + -