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

📄 f_12_10_adc_ser.c

📁 * Use 10 MHz crystal frequency. * Use Timer0 for ten millisecond looptime. * Blink "Alive" LED e
💻 C
字号:

// For MCC18, this file uses the -Oi flag, which
// turns on integer promotions so that (ADRESH << 8) works.
#include        "config.h"#include        "delay.h"#include        "serial.c"#include        "serio.c"#include	"jwb.h"/* * program to read ADC on RA5 and display a bar graph on LCD module */void	init(void);void	char2hex(unsigned char c, unsigned char *s);void	int2hex(int c, unsigned char *s);void	Adc2Hex(int c, unsigned char *s);void	SelectAdcChannel(unsigned char c);unsigned char		ReadPot(void);int		ReadTemp(void);int		ReadAdcChannel(unsigned char c);extern void serial_init (char brg, char hi_speed);const unsigned char MESSAGE1[]	=	"turn pot";unsigned char str1[9];unsigned char str2[9];void main(void){	int					val0, val1;	char				i;	unsigned char		c;	double				dval0, dval1;	for (i=8; i<=0 ;i-- ) {		str1[i] = 0;		str2[i] = 0;	}	init();	printf("Hit any key to read ADCs...");  pcrlf();	while(1)	{		c = getch();		//TOGGLE(LED2);		if (c=='q')		{			for (i=0; i <5; i++)				printf("%d->%4d   ", i, ReadAdcChannel(i) );			pcrlf();		}		else		{			val0 = ReadAdcChannel(0);			val1 = ReadAdcChannel(1);			dval0 = 5.0 * val0 / 1024;		// convert AN0 value to float (assume Vdd=5V)			dval1 = 5.0 * val1 / 1024;		// convert AN1 value to float			printf("AN0=%4d (%1.3fV)     AN1=%4d (%1.3fV)", val0,dval0,val1,dval1);			pcrlf();		}	}}// initializevoid init(void){	PORTA = 0x00;	ADCON0 = BIT7|BIT6|BIT0;								// internal ADC oscillator + ADON	ADCON1 = BIT6|0x4;										// L.just, AN0+AN1+AN3, no ext REFs	SET_AS_INPUT(TRISA, BIT0 | BIT1);						// inputs for Two ADC channels	PORTB = 0x00;	SET_AS_OUTPUT(TRISB, BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5 );		// set RB0-RB5 to output	// recall that RB6 and RB7 are reserved for the HW programmer/debugger	PORTC = 0x00;	SET_AS_OUTPUT(TRISC, BIT0 | BIT1 | BIT2 | BIT3 | BIT4 | BIT5);		// RC0,RC3,RC5 drive DAC	// RC6 and RC7 are used in serial UART, so leave them alone	// initialize the serial port	serial_init(95, 1);			// setup 19200 baud serial IO w/ Fosc=29.4912MHz}int		ReadAdcChannel(unsigned char c){	unsigned char	temp;		temp = ADCON0 & (BIT7|BIT6|BIT0);	// clear bits except ADCS1:ASCS0 and ADON	ADCON0 = temp | ((c & 0x7)<<3);		// select the desired ADC channel and write to ADCON0	DelayUs(20);						// wait 20us for ADC to get ready	GODONE = 1;							// start ADC conversion	while(GODONE);						// then wait for ADC to finish	if ( ADFM )
		return ((ADRESH * 256) | ADRESL);	else		return ((ADRESH << 2) | (ADRESL>>6) );}void	SelectAdcChannel(unsigned char c){	unsigned char	temp;		temp = ADCON0 & (BIT7|BIT6|BIT0);	// clear bits except ADCS1:ASCS0 and ADON	ADCON0 = temp | ((c & 0x7)<<3);		// select the desired ADC channel and write to ADCON0}// ReadPot assumes that the ADCON1 has the RA5/AN4 channel configured as//    an ADC inputunsigned char ReadPot(void){	SelectAdcChannel(4);		// pot is connected to RA5/AN4	DelayUs(20);				// wait 20us for ADC to get ready	GODONE = 1;					// start ADC conversion	while(GODONE);				// wait for GODONE to low	return (ADRESH);			// just return the MSB}// ReadPot assumes that the ADCON1 has the RA0/AN0 channel configured as//    an ADC inputint ReadTemp(void){	SelectAdcChannel(0);		// temp sensor is located on RA0/AN0	DelayUs(20);				// wait 20us for ADC to get ready	GODONE = 1;					// start ADC conversion	while(GODONE);				// wait for GODONE to low	return ((ADRESH << 2) | (ADRESL>>6) );}void Adc2Hex(int c, unsigned char *s){	unsigned char tmp;	tmp = (c >> 8) & 0x0F;	// first hex digit	if (tmp >= 10)		tmp += 0x37;	else		tmp += 0x30;	*s = tmp;	// next two hex digits	tmp = c & 0xFF;	char2hex( tmp, s+1);}void int2hex(int c, unsigned char *s){	unsigned char tmp;	tmp = c >> 8;	char2hex( tmp, s);	tmp = c & 0xFF;	char2hex( tmp, s+2);}void char2hex(unsigned char c, unsigned char *s){	unsigned char tmp;	tmp = c >> 4;	// first hex digit	if (tmp >= 10)		tmp += 0x37;	else		tmp += 0x30;	*s = tmp;	s++;	// second hex digit	tmp = c & 0x0F;	if (tmp >= 10)		tmp += 0x37;	else		tmp += 0x30;	*s = tmp;}

⌨️ 快捷键说明

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