sample.c

来自「pic单片机例程」· C语言 代码 · 共 78 行

C
78
字号
/*
 * Sample application using LCD interface (see lcd.c) and analog input
 * on a 16C71 (see adc.c)
 * 
 * 1) Read channel 0 of the ADC and write it to the LCD
 * 2) Read serial data and write it to line 2 of the LCD
 * 
 */

#ifndef _XTAL_FREQ
 // Unless specified elsewhere, 4MHz system frequency is assumed
 #define _XTAL_FREQ 4000000
#endif

#include	<htc.h>
#include	<stdio.h>
#include	"lcd.h"
#include	"adc.h"

main()
{
	unsigned int	i, last;
	unsigned char	diff;

	ADCON1 = 2;		// 16C71 requires Port A reconfiguration - RA2/3 are digital
				// I/O, 0 and 1 are analog
	TRISA = 0xF3;		// control lines are output
	TRISB = 0x00;		// Make all Port B data lines output

	PORTB = 0b01010000;	// Initialise interrupt-controlled lights (upper 4 bits).
	
	// Initialise & start timer0.

	T0CS = 0;		// Select timer mode.

	lcd_init();
	for(;;) {
		adc_read(0);		// read channel 0
		i = ADRES;
		adc_read(0);		// read channel 0 again
		i += ADRES;
		adc_read(0);		// read channel 0 again
		i += ADRES;
		adc_read(0);		// read channel 0 again
		i += ADRES;
		if(i > last)
			diff = i-last;
		else
			diff = last - i;
		if(diff > 3) {		// hysteresis
			diff = i/4;
			lcd_cursor(0);
			lcd_puts("Value = ");
			if(diff >= 100)
				lcd_putch(diff % 100);
			else
				lcd_putch(' ');
			if(diff >= 10)
				lcd_putch((diff/10) % 10);
			else
				lcd_putch(' ');
			lcd_putch(diff % 10);
			last = i;
		}
		__delay_ms(250);			// wait a while
	}
}

// This interrupt inverts the pattern on the upper
// 4 bits of the LEDs on Port B.
interrupt void
timer0_overflow_isr( void)
{
	PORTB = (PORTB&0x0f) | (~(PORTB&0xf0));
	
	T0IF = 0;		// Clear Timer 0 Interrupt Flag.
}

⌨️ 快捷键说明

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