0% found this document useful (0 votes)
17 views7 pages

Pic CSS C

The document contains code samples for interfacing with an LCD display and reading analog values from an ADC on a PIC microcontroller. It includes code to initialize the LCD, write text to the display, and read analog values from multiple channels of the ADC. It also contains pin definitions and constant strings for the text to display on the LCD.

Uploaded by

Tuan Han
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)
17 views7 pages

Pic CSS C

The document contains code samples for interfacing with an LCD display and reading analog values from an ADC on a PIC microcontroller. It includes code to initialize the LCD, write text to the display, and read analog values from multiple channels of the ADC. It also contains pin definitions and constant strings for the text to display on the LCD.

Uploaded by

Tuan Han
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/ 7

A Auto label net=xxx#

+ Rotate component

P Add component

Ctrl + Space Find command

LCD 16x2
Pin: VSS 0V

VDD 5v

VEE Contrast 0-5v

Code mẫu

//==================================================================================

#include <Test LCD.h>

#define LCD_ENABLE_PIN PIN_D0

#define LCD_RS_PIN PIN_D1

#define LCD_RW_PIN PIN_D2

#define LCD_DATA4 PIN_D4

#define LCD_DATA5 PIN_D5

#define LCD_DATA6 PIN_D6

#define LCD_DATA7 PIN_D7

#include <lcd.c>

unsigned int8 i;

const unsigned char HANG1[16]={"DH SP KY THUAT"};

const unsigned char HANG2[16]={" TP HO CHI MINH "};

//----------------------------------------------------------------------------------------------------------------------------------------
void main()

lcd_init ();

while (1)

For (i=0; i<16; i++) { lcd_putc(HANG1[i]); delay_ms(30);};

delay_ms(100);

lcd_putc("\n"); // Xuong dong

For (i=1; i<16; i++) { lcd_putc(HANG2[i]); delay_ms(30);};

delay_ms(100);

lcd_putc("\f"); // Clear display

//===================================================================================

#include <Test LCD.h>

#define LCD_ENABLE_PIN PIN_D0

#define LCD_RS_PIN PIN_D1

#define LCD_RW_PIN PIN_D2

#define LCD_DATA4 PIN_D4

#define LCD_DATA5 PIN_D5

#define LCD_DATA6 PIN_D6

#define LCD_DATA7 PIN_D7

#include <lcd.c>

unsigned int8 i,giay;

const unsigned char HANG1[16]={"DONG HO "};

const unsigned char HANG2[16]={"TP HO CHI MINH"};


//----------------------

void lcd_hienthi()

lcd_gotoxy(15,1);

lcd_putc((giay/10)+0x30);

lcd_putc((giay%10)+0x30);

void main()

lcd_init ();

lcd_gotoxy(1,1); delay_ms(10);

for (i=0;i<16;i++) { lcd_putc(hang1[i]); delay_ms(100);}

lcd_gotoxy(2,2); delay_ms(10);

for (i=0;i<16;i++) { lcd_putc(hang2[i]); delay_ms(100);}

while (1)

for(giay=0;giay<60;giay++)

lcd_hienthi();

delay_ms(500);

//===================================================================================
ADC có sẵn của PIC

Khởi tạo:

setup_adc(ADC_CLOCK_div_2); // Set CLOCK cho ADC

setup_adc_ports(ALL_ANALOG); //Mở ports cho chip

set_ADC_channel(0); //Khởi tạo kênh (0-7 ứng với AN0-AN7)

delay_ms(10); // Cần đợi để kênh khởi tạo

Ví dụ:

#include <ADC 8 chanel.h>

#INCLUDE <lcd.c>

#define LCD_ENABLE_PIN PIN_D0

#define LCD_RS_PIN PIN_D1

#define LCD_RW_PIN PIN_D2

#define LCD_DATA4 PIN_D4

#define LCD_DATA5 PIN_D5

#define LCD_DATA6 PIN_D6

#define LCD_DATA7 PIN_D7

#define TIEN input(pin_B0)

#define LUI input(pin_B1)

unsigned int16 adc;

unsigned long int i,a,b;

char chuoi[17];

//----------------------------------------------------------------------------

void lcd_puts(char *string) {

while (*string != '\0') {

lcd_putc(*string);

string++;

}
//----------------------------------------------------------------------------

void doc1()

adc=0;

set_ADC_channel(a);

delay_ms(10);

for (i=0;i<50;i++)

adc=adc+read_adc();

delay_ms(2);

adc=adc/50;

lcd_gotoxy(1,1);

sprintf(chuoi,"ADC AN%01ld = %04ld",a, adc);

lcd_puts(chuoi);

delay_ms(500);

//----------------------------------------------------------------------------

void doc2()

adc=0;

set_ADC_channel(a);

delay_ms(10);

for (i=0;i<50;i++)

adc=adc+read_adc();

delay_ms(2);

adc=adc/50;
lcd_gotoxy(1,2);

sprintf(chuoi,"ADC AN%01ld = %04ld",a, adc);

lcd_puts(chuoi);

delay_ms(500);

//----------------------------------------------------------------------------

void main()

lcd_init();

setup_adc(ADC_CLOCK_div_2);

setup_adc_ports(ALL_ANALOG);

a=0;

doc1(); a++; doc2();

While (true)

While (!TIEN)

if (a<7)

{a++; doc1(); a++; doc2(); delay_ms(10);}

else if (a=7) {a=0; doc1(); a++; doc2();delay_ms(10); }

While (!LUI)

if (a>2)

{ a=a-3; doc1(); a++; doc2(); delay_ms(10);}


else if (a=1) {a=6; doc1(); a++; doc2();delay_ms(10); }

You might also like