📄 combo1.c
字号:
//Include required header files here.
#include "io16f877.h"
#define LCD_RS RB4 // Register select
#define LCD_EN RB5 // Enable
#define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))
//This function is called to cause a delay of count Us.
void DelayUs(int count)
{
int i;
int j;
for(i=0;i<count;i++)
{
for(j=0;j<5;j++);
//This for loop has 5 NOPs & wastes 1 uS for our PIC clock frequency of 20MHz.
}
}
//This function is called to cause a delay of count Ms.
void DelayMs(int count)
{
int i;
int j;
for(i=0;i<count;i++)
{
for(j=0;j<5000;j++);
//This for loop has 5 NOPs & wastes 1 mS for our PIC clock frequency of 20MHz.
}
}
// This function initialize the IO ports
void initialize_IO_ports(void)
{TRISD=0xF0; //-- Port D bit 0 to 3 as output (output to LCD)
TRISB = 0x07; //set port B as input
PORTB = 0X00;
}
//This function sets up ADC & initialises it for use.
void initialize_ADC(void)
{
TRISA = 0xFF ; //portA as input as we need the analog channel.
//Configure portA as analog, VDD & VSS as VREFs;
ADCON1 = 0x82 ; // ADC result to be right-justified.
/************************************************************
Set AD conversion clock(TAD)to 32 times main clock freq as it should be
at least 1.6 uS. System clock period for 20MHz crystal is 0.2uS.
************************************************************/
ADCS1 = 1 ;
ADCS0 = 0 ;
//turn ON the ADC.
ADON = 1 ;
}
//This function takes analog input from channel_number & returns the value.
int read_ADC(int channel_number)
{
int value;
switch( channel_number)
{case 0:
CHS2=0;CHS1=0;CHS0=0;
break;
case 1:
CHS2=0;CHS1=0;CHS0=1;
break;
case 2:
CHS2=0;CHS1=1;CHS0=0;
break;
case 3:
CHS2=0;CHS1=1;CHS0=1;
break;
default:
CHS2=0;CHS1=0;CHS0=0;
}//end switch case.
DelayUs(100);
// start AD conversion.
GO=1;
//Then, wait for conversion to finish.
while(GO) {};
//Finally, read the values in the A/D result register pair.
value=(ADRESH<<8)+ADRESL;
//wait for at least 2TAD.
DelayUs(4);
return(value);
}
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
PORTD = (PORTD & 0xF0) | (c >> 4);
LCD_STROBE;
PORTD = (PORTD & 0xF0) | (c & 0x0F);
LCD_STROBE;
DelayUs(40);
}
/* Clear and home the LCD */
void
lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x01);
DelayMs(2);
}
/* write a string of chars to the LCD */
void
lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* Go to the specified position */
void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
LCD_RS = 0; // write control bytes
DelayMs(15); // power on delay
PORTD = 0x3; // attention!
LCD_STROBE;
DelayUs(250);
LCD_STROBE;
DelayMs(5);
LCD_STROBE;
DelayMs(5);
PORTD = 0x2; // set 4 bit mode
LCD_STROBE;
DelayUs(40);
lcd_write(0x28); // 4 bit, 2 line , 5x7 font
lcd_write(0x08); // display off
lcd_write(0x0F); // display on, blink curson on
lcd_write(0x06); // entry mode
}
int main ()
{
// declaration of the variable for the main fuction
// unsigned char tempc;
int analog_value0;
int analog_value1;
int analog_value2;
int analog_value3;
OPTION=0x00;
GIE=0;
DelayMs(100);
// calling the initialization function.
initialize_IO_ports(); // initialize IO ports
initialize_ADC(); // initialize AD convertor
lcd_init(); // initialize LCD
lcd_clear();
DelayMs(100);
lcd_goto(40);
while(1)
{if(RB0==1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -