0% found this document useful (0 votes)
8 views6 pages

PIC Microcontroller LCD Display Code

Uploaded by

tvu41209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

PIC Microcontroller LCD Display Code

Uploaded by

tvu41209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <BAI TAP LON VI XU LY.

h>

#include <stdio.h>

#include <stdlib.h>

#include <htc.h>

#include "lcd.h"

#pragma config FOSC = HS

#pragma config WDTE = OFF

#pragma config PWRTE = OFF

#pragma config NCLRE = ON

#pragma config CP = OFF

#pragma config CPD = OFF

#pragma config BOREN = OFF

#pragma config IESO = OFF

#pragma config FCMEN = OFF

#pragma config LVP = OFF

// CONFIG2

#pragma config BOR4V = BOR40V

#pragma config WRT = OFF

#define _XTAL_FREQ 2000000

#define sensor_cao RC0

#define sensor_thap RC1

#define sensor_vua RC2

DATA sp_cao = 0;

DATA sp_thap = 0;

DATA sp_vua = 0;

DATA tong = 0;
// Co hieu cho phep thap nhat lcd

DATA flag_cao = 0;

DATA flag_thap = 0;

DATA flag_vua = 0;

DATA flag_tong = 0;

// Tao 1 ki tu trai tim

const unsigned char trai_tim[8] = {0x00, 0x0A, 0x1F, 0x1F, 0x1F, 0x0E, 0x40, 0x00};

void delayTimer0() {

int t = 2700;

while (t--) {

TMR0 = 100;

INTCONbits.T0IF = 0; // Set timer interrupt flag to 0

while (!INTCONbits.T0IF);

void servo_xoay_P(void) {

PORTBbits.RB7 = 1;

__delay_us(1000);

PORTBbits.RB7 = 0;

__delay_us(19000);

void servo_xoay_G(void) {

PORTBbits.RB6 = 1; // Corrected from RBE to RB6

__delay_us(1800);
PORTBbits.RB6 = 0; // Corrected from RB7 to RB6

__delay_us(23000);

void servo_xoay_P2(void) {

PORTBbits.RB7 = 1;

__delay_us(1000);

PORTBbits.RB7 = 0;

__delay_us(19000);

void servo_xoay_G2(void) {

PORTBbits.RB6 = 1; // Corrected from RBE to RB6

__delay_us(1800);

PORTBbits.RB6 = 0; // Corrected from RB7 to RB6

__delay_us(23000);

void SP_CAO(void) {

if (sensor_cao) {

sp_cao++;

flag_cao = 1;

} else {

flag_cao = 0;

void SP_VUA(void) {

if (sensor_vua) {
sp_vua++;

flag_vua = 1;

} else {

flag_vua = 0;

void SP_THAP(void) {

if (sensor_thap) {

sp_thap++;

flag__tap = 1;

} else {

flag_thap = 0;

void PIC_Config(void) {

// Configure the PIC microcontroller settings

TRISC = 0xFF; // Set PORTC as input for sensors

TRISB = 0x00; // Set PORTB as output for servos

ANSEL = 0; // Set all pins as digital

OPTION_REGbits.PS0 = 0; // Set prescaler to 1/32

void main(void) {

PIC_Config();

lcd_init();

// Create a buffer to hold formatted strings


char buffer[16];

// Choose position for CGRAM, usually 0x40

lcd_put_byte(0, 0x40);

for (int i = 0; i < 8; i++) {

// Write each byte of the heart shape to CGRAM

lcd_put_byte(i, trai_tim[i]);

while (1) {

SP_VUA();

SP_CAO();

SP_THAP();

tong = sp_cao + sp_vua + sp_thap;

// Line 1, print product types

lcd_gotoxy(0, 0);

lcd_puts("CAO");

lcd_gotoxy(6, 1);

lcd_puts("[Link]");

lcd_gotoxy(15, 1);

lcd_puts("THAP");

// Line 3, print product values

lcd_gotoxy(1, 2);

sprintf(buffer, "%d", sp_cao);

lcd_puts(buffer);
lcd_gotoxy(7, 2);

sprintf(buffer, \"%d\", sp_vua);

lcd_puts(buffer);

lcd_gotoxy(16, 2);

sprintf(buffer, \"%d\", sp_thap);

lcd_puts(buffer);

// Line 4, print total

lcd_gotoxy(0, 3);

sprintf(buffer, \"TOTAL: %d\", tong);

lcd_puts(buffer);

// Add a small delay for readability

__delay_ms(500);

You might also like