0% found this document useful (0 votes)
77 views8 pages

Họ và tên: Hoàng Ngọc Bình - Nguyễn Thanh Tùng Lớp: Tự động hóa -K58 Bài 1

The document contains code for an Arduino project that reads an analog voltage from a pin, converts it to a temperature value, and displays the temperature on an LCD screen while also controlling LEDs based on the temperature. It initializes the LCD, defines functions for controlling the LCD, initializes ADC to read the analog pin, converts the ADC value to a voltage and temperature, and displays the temperature on the LCD in a continuous loop.

Uploaded by

Lê Minh Hiếu
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)
77 views8 pages

Họ và tên: Hoàng Ngọc Bình - Nguyễn Thanh Tùng Lớp: Tự động hóa -K58 Bài 1

The document contains code for an Arduino project that reads an analog voltage from a pin, converts it to a temperature value, and displays the temperature on an LCD screen while also controlling LEDs based on the temperature. It initializes the LCD, defines functions for controlling the LCD, initializes ADC to read the analog pin, converts the ADC value to a voltage and temperature, and displays the temperature on the LCD in a continuous loop.

Uploaded by

Lê Minh Hiếu
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
You are on page 1/ 8

Họ và tên: Hoàng Ngọc Bình – Nguyễn Thanh Tùng

Lớp: Tự động hóa -K58

Bài 1

/*
* File: adc.c
* Author: admin
*
* Created on January 30, 2020, 3:46 PM
*/

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements


// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)

#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)

#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is
digital I/O, HV on MCLR must be used for programming)

#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)

#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program
memory may be written to by EECON control)

#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.

// Use project enums instead of #define for ON and OFF.

#include <stdlib.h>

#include <pic.h>

#include <pic16f877a.h>

#include <stdio.h>

#include <conio.h>

#include <xc.h>

#define _XTAL_FREQ 8000000

#define RS RD2

#define EN RD3

#define D4 RD4

#define D5 RD5

#define D6 RD6

#define D7 RD7

////////////////////////////////////

void Lcd_Port(char a)

if(a&1)
D4=1;

else

D4=0;

if(a&2)

D5=1;

else

D5=0;

if(a&4)

D6=1;

else

D6=0;

if(a&8)

D7=1;

else

D7=0;

/////////////////////////////////////////

void Lcd_Cmd(char a)

RS=0;

Lcd_Port(a);

EN=1;

__delay_ms(4);

EN=0;

////////////////////////////////////////

void Lcd_Init()

Lcd_Port(0x00);

__delay_ms(20);
Lcd_Cmd(0x03);

__delay_ms(5);

Lcd_Cmd(0x03);

__delay_ms(11);

Lcd_Cmd(0x03);

Lcd_Cmd(0x02);

Lcd_Cmd(0x02);

Lcd_Cmd(0x08);

Lcd_Cmd(0x00);

Lcd_Cmd(0x0C);

Lcd_Cmd(0x00);

Lcd_Cmd(0x06);

///////////////////////////////////////

void Lcd_Clear()

Lcd_Cmd(0);

Lcd_Cmd(1);

void Lcd_Set_Crusor(char a,char b)

char temp,z,y;

if(a==1)

temp=0x80+b-1;

y=temp&0x0f;

z=temp>>4;

Lcd_Cmd(z);

Lcd_Cmd(y);

}
else if(a==2)

temp=0XC0+b-1;

z=temp>>4;

y=temp&0x0f;

Lcd_Cmd(z);

Lcd_Cmd(y);

//////////////////////////////////////////

void Lcd_Write_Char(char a)

char temp,y;

temp=a&0x0f;

y=a&0xF0;

RS=1;

Lcd_Port(y>>4);

EN=1;

__delay_ms(40);

EN=0;

Lcd_Port(temp);

EN=1;

__delay_ms(40);

EN=0;

void Lcd_Write_String(char*a)

int i;

for(i=0;a[i]!='\0';i++)

Lcd_Write_Char(a[i]);
}

////////////////////////////////////////

void Lcd_Shift_Right()

Lcd_Cmd(0x01);

Lcd_Cmd(0x0c);

void Lcd_Shift_Left()

Lcd_Cmd(0x01);

Lcd_Cmd(0x08);

////////////////////////////////////////

void ADC_init()

ADCON0=0X81;

ADCON1=0X80;

unsigned int ADC_Read(unsigned char channel)

if(channel>7)

return 0;

if(channel<=7)

ADCON0 &=0XC5;

ADCON0 |=channel<<3;

__delay_ms(2);

GO_nDONE=1;

while(GO_nDONE);

return((ADRESH<<8)+ADRESL);

}
}

void main(void)

unsigned int aa;

unsigned int voltage,f;

TRISA= 0XFF;

TRISB= 0X00;

TRISC= 0X00;

TRISD= 0X00;

TRISE2=1;

char s[20];

Lcd_Init();

ADC_init();

while(1)

aa=ADC_Read(7);

voltage=5000.0f/1023*aa;

f=voltage/10;

Lcd_Clear();

sprintf(s,"value=%d",f);

Lcd_Set_Crusor(1,1);

Lcd_Write_String(s);

Lcd_Write_Char(223);

Lcd_Write_Char('C');

if(f<=26)

RB6=0;

RB7=0;

if(f>26 && f<=30)

{
RB6=1;

RB7=0;

if(f >30)

RB6=1;

RB7=1;

Lcd_Set_Crusor(2,1);

__delay_ms(2000);

return;

You might also like