Kumara
Joined: 16 Oct 2013 Posts: 7 Location: Sri Lanka
|
Counter with lcd |
Posted: Thu Oct 17, 2013 10:49 am |
|
|
This will help for beginners. Lcd.c driver is available in Picc\Drivers (CCS C Compiler).
Code: |
// Program function: Digital counter with LCD.
// Target processor 16F877A (20 MHZ)
// Written by Kumara
#include <16F877A.h> // Pic Microcontroller (MCU)
#DEVICE ADC=10 // 8 Bit analog resolution
#fuses NOWDT,HS, NOPUT, NOPROTECT // Fuses
#fuses NOBROWNOUT, NOLVP // Fuses
#use delay(clock=20000000) // Crystal Oscillator Frequency ( 20 MHZ)
#include <lcd.c> // Driver for LCD Module ( PICC\DRIVERS )
void main() // Main block
{
INT16 PRE_COUNT=0; // Declare variable
INT16 SET_COUNT; // Declare variable
INT1 SW1; // Declare variable
BOOLEAN ISPRESSED=FALSE; // Boolean logic=0;
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
lcd_init();
while (TRUE) { // Endless loop
SW1=INPUT(PIN_C0); // Positive inputs
IF(PRE_COUNT==0)
{
lcd_putc("\f"); // LCD clear
lcd_gotoxy(1,1); // Select Raw 1 & start digit 1 position
printf(lcd_putc," PRE_COUNT:%04LU ",PRE_COUNT);
lcd_gotoxy(1,2); // Select Raw 2 & start digit 1 position
printf(lcd_putc," SET_COUNT:%04LU ",SET_COUNT);
DELAY_MS(200);
}
////////////////////////////////////////////////////////
SET_ADC_CHANNEL(0); // INPUT AN0
DELAY_US(100);
SET_COUNT=READ_ADC(); // SET VALUE= RA1 ( 10K V/R )
SET_COUNT=(SET_COUNT); // FOR 4 DIGITS UP TO 1023
////////////////////////////////////////////////////////
IF (SW1 && !ISPRESSED)
{
DELAY_MS(10);
SW1=INPUT(PIN_A1);
IF(SW1)
{PRE_COUNT++;
ISPRESSED=TRUE;
lcd_putc("\f"); // LCD clear
lcd_gotoxy(1,1); // Select Raw 1 & start digit 1 position
printf(lcd_putc," PRE_COUNT:%04LU ",PRE_COUNT);
lcd_gotoxy(1,2); // Select Raw 2 & start digit 1 position
printf(lcd_putc," SET_COUNT:%04LU ",SET_COUNT);
DELAY_MS(100);
IF(PRE_COUNT>=SET_COUNT)
{
OUTPUT_HIGH(PIN_B0);
}
ELSE
{
OUTPUT_LOW(PIN_B0);
}
}
}
IF(!SW1) // This line must be here
{
ISPRESSED=FALSE; // This line must be here
}
}
}
|
|
|