please dont rip this site

Microchip IO Dev Lcd Cmeter

// 
// CMeter.c
// This program displays the voltage at the input to the ADC
// in Volts as well as displays a bar graph at the bottom of
// the LCD terminal display using user defined characters.
//
// Ted Rossin
// 6-27-2003
// 6-30-2003
//

#include <pic.h>
#include <stdio.h>
#include "lcd.h"
#include "timer.h"
#include "serial.h"

unsigned char SampleADC(void);
void SetCursor(unsigned char x,unsigned char y);
void InitTerminal(void);
void DisplayBarGraph(unsigned char Count);

char bank1 Buf[21];  // Some extra memory from bank1

int main()
{
	unsigned int i;
    int voltage,whole,frac;
	unsigned char Raw;
	
	TRISB = 0x00;	// Porb B[7:0] is output
    TRISA = 0x01;   // Port A[0] is input for A/D
	ADCON1 = 0x0e;	// (left justified) 1 A/D channel
	ADCON0 = 0x81;  // FOSC/32, channel=0, ADON=1

	PORTB = 0xaa;	/* turn on some lights */

	InitRS232();
	InitTimer();
	InitTerminal();
	
	printf("%c",TERM_CLEAR);
	for(i=0;0<1;i++){
		SetCursor(0,1);
		Raw = SampleADC();
		voltage = ((200*(unsigned int)Raw)/255)*25;
		whole = voltage/1000;
		frac = (voltage - (whole*1000) + 5)/10;
    	sprintf(Buf,"ADC=%d.%2dV Raw=0x%2x\n",whole,frac,Raw);
		if(Buf[6]==' ') Buf[6] = '0';  // Fix leading zero bug
		if(Buf[16]==' ') Buf[16] = '0';  // Fix leading zero bug
		printf(Buf);
		printf("\tLoop=%u    \n",i);
		DisplayBarGraph(voltage/84);
		Waitms(10);
    	PORTB = i&0xff;
	}
}

void DisplayBarGraph(unsigned char Count)
{
    unsigned char i,NumWhole,NumFrac;

    NumWhole = Count/3;
    NumFrac = Count - (NumWhole*3);
	for(i=0;i<NumWhole;i++){
		putchar(0x83);
    }
	if(NumFrac){
	    putchar(0x80+NumFrac);
		i++;
	}
	for(;i<20;i++) putchar(' ');
}

void SetCursor(unsigned char x,unsigned char y)
{
    printf("%c%c%c",TERM_POS,x,y);
}

void InitTerminal(void)
{
	unsigned char i;

	putchar(TERM_ESC);  putchar(ESC_WRITE_CUSTOM+0);
	for(i=0;i<8;i++) putchar(0x1f);
	putchar(TERM_ESC);  putchar(ESC_WRITE_CUSTOM+1);
	for(i=0;i<8;i++) putchar(0x10);
	putchar(TERM_ESC);  putchar(ESC_WRITE_CUSTOM+2);
	for(i=0;i<8;i++) putchar(0x14);
	putchar(TERM_ESC);  putchar(ESC_WRITE_CUSTOM+3);
	for(i=0;i<8;i++) putchar(0x15);
}

unsigned char SampleADC(void)
{
//	call	Delay12us		; Wait for sample cap to charge
#asm
	bsf	_ADCON0,2	; bsf	ADCON0,GO (Start conversion)
WaitADC:
	btfsc	_ADCON0,2	; Wait for finish
	  goto	WaitADC
#endasm
	return(ADRESH);		// Fetch high 8 bits of result
}



file: /Techref/microchip/io/dev/lcd/CMeter.c, 2KB, , updated: 2003/9/10 12:39, local time: 2019/8/25 14:05,
TOP NEW HELP FIND: 
18.206.13.39:LOG IN

 ©2019 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE! / 

<A HREF="http://techref.massmind.org/techref/microchip/io/dev/lcd/CMeter.c"> microchip io dev lcd CMeter</A>

Did you find what you needed?

  LCD Front Panel Set: $8.99!
- 2x16 LCD HD44780 controller
- 4 push buttons - 3 LEDs - 2x12 header
 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .