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

📄 lcd2demo_8490_r0.c

📁 是关于PIC单片机(型号是PIC16F8490)实现液晶显示的一个DEMO
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************
 *
 * PICDEM LCD2 Demo Program for 8490 family devices.
 *
 ***********************************************************************
 * FileName:            LCD2Demo_8490_R0.C
 * Dependencies:		LCD_8490.h
 * Processor:           PIC18F8490
 * Assembler:
 * Linker:
 * Compiler:			C18
 * Company:             Microchip Technology, Inc.
 *
 * Microchip Technology Incorporated ("Microchip") retains all ownership
 * and intellectual property rights in the code accompanying this message
 * and in all derivatives hereto.  You may use this code, and any
 * derivatives created by any person or entity by or on your behalf,
 * exclusively with Microchip抯 proprietary products.  Your acceptance
 * and/or use of this code constitutes agreement to the terms and
 * conditions of this notice.
 *
 * CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS".
 * NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING,
 * BUT NOT LIMITED TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS CODE,
 * ITS INTERACTION WITH MICROCHIP扴 PRODUCTS, COMBINATION WITH ANY OTHER
 * PRODUCTS, OR USE IN ANY APPLICATION.
 *
 * YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE
 * LIABLE, WHETHER IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR
 * BREACH OF STATUTORY DUTY), STRICT LIABILITY, INDEMNITY, CONTRIBUTION,
 * OR OTHERWISE, FOR ANY INDIRECT, SPECIAL, PUNITIVE, EXEMPLARY, INCIDENTAL
 * OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF ANY KIND WHATSOEVER
 * RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED
 * OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO
 * MICROCHIP SPECIFICALLY TO HAVE THIS CODE DEVELOPED.
 *
 * You agree that you are solely responsible for testing the code and
 * determining its suitability.  Microchip has no obligation to modify,
 * test, certify, or support the code.
 *
 * Refer to the PICDEM LCD 2 Users Guide for additional information
 **********************************************************************/
#include "p18f8490.h"
#include "LCD_8490.h"

//***********************************************************************
// Configuration bits
//***********************************************************************
#pragma config OSC  =INTIO67
#pragma config FCMEN=OFF
#pragma config IESO =OFF
#pragma config PWRT =OFF
#pragma config BOREN=OFF
#pragma config WDT  =OFF
#pragma config DEBUG=ON
#pragma config XINST=OFF

//**********************************************************************
// Structure Definitions
//**********************************************************************
typedef union
 {
  struct
  {
  unsigned b0:1;
  unsigned b1:1;
  unsigned b2:1;
  unsigned b3:1;
  unsigned b4:1;
  unsigned b5:1;
  unsigned b6:1;
  unsigned b7:1;
  }location;
  unsigned char ch;
 }Bytebits;

struct INT_nibble {
				unsigned a: 4;
				unsigned b: 4;
				unsigned c: 4;
				unsigned d: 4;
};

struct S_LONG_nibble {
				unsigned a: 4;
				unsigned b: 4;
				unsigned c: 4;
				unsigned d: 4;
				unsigned e: 4;
				unsigned f: 4;
};

union integer {
				struct INT_nibble Nib;
				unsigned char Char;
				unsigned int  Int;
};

union SL {
				  struct S_LONG_nibble Nib;
				  unsigned short long S_Long;
};
//**********************************************************************
// Function Prototypes
//**********************************************************************
void LCD_CONFIG_R (void);
void DisplayOn (void);
void DisplayOff (void);
void BarGraph (unsigned char);
void CirGraph (unsigned char);
void S1Num (unsigned char);
void S2Num (unsigned char);
void S3Num (unsigned char);
void S4Num (unsigned char);
void S5Num (unsigned char);
void S6Num (unsigned char);
void S7Num (unsigned char);
void S8Num (unsigned char);
void LLine (unsigned int);
void ULine (unsigned short long);

void Voltmeter (void);
void VoltM_Display (unsigned short long i);
void BttryIndic (unsigned char i);

void Thermometer (void);
void Temp_Display_C (unsigned short long i);
void Temp_Display_F (unsigned short long i);
void TempIndic (unsigned char i);

void RTC_Display(void);
void InitialDemo(void);

unsigned short long HEX2BIN (unsigned int);
unsigned short long AdjustBCD (unsigned short long);

void InterruptHandlerHigh (void);

void Delay3 (void);
void Delay_LP (void);
void Delay_LP1 (void);
//**********************************************************************
// Defines
//**********************************************************************
#define A 10
#define C 12
#define E 14
#define F 15
#define H 17
#define L 21
#define P 25



#define F_1 1
#define F_2 2
#define F_3 3
#define F_4 4
#define EnableAll 0xFF
#define No_Disp 255

#define Pressed 0
#define Default 1
#define SW3 	PORTAbits.RA6
#define SW4 	PORTAbits.RA7

//**********************************************************************
// Variable declaration
//**********************************************************************

unsigned char i, cnt, Temp;
unsigned int j;
unsigned short long k;

struct INT_nibble Vn;
union integer In;

struct S_LONG_nibble ST_Ln;
union SL BCD;

unsigned int an0voltage, an2voltage;
unsigned int Reading;

unsigned char BD_VLTG_x10_InHex;

unsigned char SEC, MIN, HR;

unsigned char BGCNT, SEC_FLAG, SEC_FG, TMR3FG, TMP_CNT;

unsigned char DemoCode, S_2, S_1, BiasBits;

unsigned char i0,i1,i2;
Bytebits Port_bits, Temp_bits;
//**********************************************************************
// Main Program
//**********************************************************************

void main (void)
{
    OSCCON  = 0b01000000;  //1 MHZ
    OSCTUNE = 0b01100000;

	BD_VLTG_x10_InHex = 0x32; // 0x21 for 3.3 V  0x33 for 5.1V.
	BGCNT = 0;
	SEC_FLAG = 0;

	LCD_CONFIG_R(); 		// Configure LCD module to use internal Charge Pump.

	// Hardware Set UP

	TRISBbits.TRISB0 = 0; 	// Control Power for RS-232, Thermistor, Voltmeter & EEPROM

	INTCONbits.RBIE = 1;
	INTCONbits.GIE  = 1;
	INTCONbits.PEIE = 1;

	LATBbits.LATB0 = 0;   	// Power OFF for AUX.
	DisplayOn();
	Delay3();
	DisplayOff();
	MCHPON; VaritronixON; 	// Display MCHP & Varitronix Symbol.

	Sleep(); 				// Low Power Mode.

	S_1 = Default;
	S_2 = Default;
	while (S_2 == Default)
	{
	   S1Num(H);S2Num(E);S3Num(L);S4Num(L);S5Num(0);
 	   DemoCode = F_1;
	}

	DisplayOff();

	//Timer1 Initialization
	TMR1H = 0x80;
	T1CONbits.T1RUN = 1;
	T1CONbits.T1CKPS0 = 0;
	T1CONbits.T1OSCEN = 1;
	T1CONbits.T1SYNC = 1;
	T1CONbits.TMR1CS = 1;
	T1CONbits.TMR1ON = 1;
	PIE1bits.TMR1IE = 0;

	while(1)
	{

		while (DemoCode == F_1)
		{
			ADCON1 = 0x0D;			//RA0-RA2 Analog inputs.
			S_1 = Default;
			S_2 = Default;
		 	F1ON; MCHPON; VaritronixON;
			LATBbits.LATB0 = 1;   	// Power ON, for jumper on I/O & RB0.
			TRISAbits.TRISA0 = 1; 	// AN0
			ADCON0bits.CHS3 = 0;  	// channel 0
			ADCON0bits.CHS2 = 0;
			ADCON0bits.CHS1 = 0;
			ADCON0bits.CHS0 = 0;
			ADCON2 = 0xBF;		  	// right justify, A/D RC Osc
			ADCON0bits.ADON = 1;  	// ADC on


			while (S_2 == Default)
			{
				PIR1bits.ADIF = 0;
				PIE1bits.ADIE = 1;
				ADCON0bits.GO_DONE = 1;
				Sleep();
				Reading = an0voltage;
				Voltmeter();
			}

			DemoCode = F_2;
			DisplayOff();
			ADCON0bits.ADON = 0;  	// ADC Off
		}
		while (DemoCode == F_2)
		{
			S_1 = Default;
			S_2 = Default;
			F2ON; MCHPON; VaritronixON;
			TRISAbits.TRISA1 = 1; 	//AN2
			ADCON0bits.CHS3 = 0;  	// channel 2
			ADCON0bits.CHS2 = 0;
			ADCON0bits.CHS1 = 0;
			ADCON0bits.CHS0 = 1;
			ADCON2 = 0xBF;			// right justify, A/D RC Osc
			ADCON0bits.ADON = 1;	// ADC on
			cnt = 4; //n samples
			SEC_FG = 1;
			TMP_CNT = 2;

			while (S_2 == Default)
			{
				Reading = 0;
				for ( i=0; i<cnt; i++)
				{
					PIR1bits.ADIF = 0;
					PIE1bits.ADIE = 1;
					ADCON0bits.GO_DONE = 1;
					Sleep();
					Reading += an2voltage;
				}
				Reading /= cnt;
				if (SEC_FG == 1) { SEC_FG = 0;TMP_CNT++;}
				if (TMP_CNT == 3)
				{
					TMP_CNT = 0;
					Thermometer();
				}
			}
			DemoCode = F_3;
			DisplayOff();
			ADCON0bits.ADON = 0;  // ADC Off
			LATBbits.LATB0 = 0;   // Power Off for AUX

		}
		while (DemoCode == F_3)
		{
			S_1 = Default;
			S_2 = Default;

			F3ON; MCHPON; VaritronixON;
			PIE1bits.TMR1IE = 1;
			MIN = 0; HR = 0; SEC = 0;

			while (S_2 == Default)
			{
				RTC_Display();
				Sleep();
				if (SW4 == Pressed)
				{
				  while(SW4 == Pressed)
				  {
					  Delay_LP1();	//Delay for Key press
					  if (SW3 == Pressed) MIN++;
					  else if (SW4 == Pressed) HR++;
					  RTC_Display();
				   }
				}
			}
			DemoCode = F_1;
			DisplayOff();
			PIE1bits.TMR1IE = 0;
		}
	}

}//END Main

//**********************************************************************
// End of Main Program
//**********************************************************************
//----------------------------------------------------------------------------
// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08
void
InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void
InterruptHandlerHigh ()
{

    if (PIR1bits.TMR1IF == 1)
    {
		PIR1bits.TMR1IF = 0;
		SEC++;
		if (SEC > 59){ SEC = 0; MIN++;}
		if (MIN > 59){ MIN = 0; HR++;}
		if (HR > 23 ) { HR = 0;}

		TMR1H = 0x80;
    	SEC_FLAG = ~SEC_FLAG;
		SEC_FG = 1;
	}

	if (PIR1bits.ADIF == 1)
	{
		PIR1bits.ADIF = 0;
		Temp = ADCON0 & 0x3C;
		if ( Temp == 0){an0voltage = ((unsigned int)(ADRESH) << 8) + ADRESL;}
		if ( Temp == 4){an2voltage = ((unsigned int)(ADRESH) << 8) + ADRESL;}
	}

	if (INTCONbits.RBIF == 1)
	{
		INTCONbits.RBIF = 0;
		Port_bits.ch = PORTB;
		if (Port_bits.location.b7 == 1){S_2 = Pressed;}
		if (Port_bits.location.b6 == 1){S_1 = Pressed;}
	}

	if (PIR2bits.TMR3IF == 1)
    {
		PIR2bits.TMR3IF = 0;
		TMR3FG = 1;
    }

}
//**********************************************************************
void Thermometer (void)
{
	if (SEC_FLAG == 0)
	{
	j= 2*(Reading - 392);
	k= HEX2BIN (j); //to display in C
	Temp_Display_C (k);
	}
	else
	{
	j = 36*(Reading - 303);
	k = HEX2BIN (j); //to display in F
	Temp_Display_F (k);
	}
}
//**********************************************************************
void Temp_Display_C(unsigned short long i)
{
  BCD.S_Long = i;
  S5Num (No_Disp);
  S6Num (BCD.Nib.c);
  S7Num (BCD.Nib.b);
  S8Num (BCD.Nib.a);
  DP2ON;X25ON;X26OFF;
  TempIndic (BCD.Nib.c);
}
//**********************************************************************
void Temp_Display_F(unsigned short long i)
{
  BCD.S_Long = i;
  if (BCD.Nib.e == 0) S5Num(No_Disp);
  else S5Num (BCD.Nib.e);
  S6Num (BCD.Nib.d);
  S7Num (BCD.Nib.c);
  S8Num (BCD.Nib.b);
  DP2ON;X26ON;X25OFF;
}
//**********************************************************************
void Voltmeter (void)
{
	j= an0voltage * BD_VLTG_x10_InHex;
	k= HEX2BIN (j);
	VoltM_Display (k);
	BarGraph(an0voltage/50);
}
//**********************************************************************
void VoltM_Display (unsigned short long i)
{
  BCD.S_Long = i;
  S4Num (BCD.Nib.e);
  S5Num (BCD.Nib.d);
  //S6Num (BCD.Nib.c);  //Optional for LSBs
  //S7Num (BCD.Nib.b);
  //S8Num (BCD.Nib.a);
  DP1ON;
  //BttryIndic (BCD.Nib.e);
  //Unused
}
//**********************************************************************
unsigned short long HEX2BIN (unsigned int Binary)
{
unsigned short long Result ;

⌨️ 快捷键说明

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