a2demo.c

来自「Hitech microchip PIC C18 Compiler」· C语言 代码 · 共 79 行

C
79
字号
#include <pic18.h>#include "a2demo.h"#include <stdio.h>void init(void);void putch(unsigned char);volatile unsigned char CHANNEL = 0;	/* current analog input channel selected */volatile unsigned char LEDCHAN = 1;	/* show which LED to light, corresponds to channel */void main(void){	unsigned char volts;	unsigned char decivolts;	unsigned char last_value;	unsigned char last_channel=2;	// to force an update at startup		init();		while(1)	{		/* update the channel select LEDS */		PORTD=LEDCHAN;		/* update A2D configuration control */		/* result is left justified */		ADCON1=(PORTC&0x0F);		/* update the channel to the A2D converter */		CHS0=(CHANNEL&0b00000001);		CHS1=((CHANNEL&0b00000010)>>1);		CHS2=((CHANNEL&0b00000100)>>2);					/* request the A2D conversion */		GODONE=1;		while(GODONE)continue;		ADIF=0;		if((ADRESH!=last_value)||(CHANNEL!=last_channel))		{		/* if update required, output the results */		/* calculate the output voltage */			volts=0;			for(decivolts=(ADRESH*50/255);decivolts>=10;decivolts-=10)				volts++;			printf("%cChannel %d has %d.%d volts.  ",0x0D,(CHANNEL&0x07),volts,decivolts);		}		last_value=ADRESH;		last_channel=CHANNEL;	}}void init(void){	RBIE=1;	/* enable PORTB interrupts so that */	GIEH=1;	/* pushbuttons can be used to change */	GIEL=1;	/* A2D channel select */		TXEN=1;	/* enable serial port transmissions */	SPEN=1;		TXIE=0;	/* not interrupt driven */				TRISA=0x2F;	/* Some PORTA pins are analog input channels */	TRISB=0x30;	/* Pushbutton switches to change channel are on PORTB */	PORTB=0;	TRISD=0;	/* LED panel to indicate current channel is on PORTD */	TRISC=0x0F;/* switch panel to control A2D config is on PORTC */	TRISE=0x07;	/* Some PORTE inputs are analog input channels */		ADON=1;	/* enable A2D converter */	ADIE=0;	/* not interrupt driven */	ADCON1=0b00000000;}void putch(unsigned char c){	TXREG=c;	/* transmit a character to Serial IO */	while(!TXIF)continue;	TXIF=0;}

⌨️ 快捷键说明

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