View Single Post
Old 03-10-2010, 07:35 PM   #2
xuan_68
Nhập môn đệ tử
 
Tham gia ngày: Nov 2007
Bài gửi: 1
:
Chạy chữ trên LCD

Trích:
Nguyên văn bởi MrAnhKieu View Post
Mình khai báo như thế này

char const dong6[16]={'L','C','D',' ','V','A',' ','P','I','C','1','6','F','8','7','7'};

giờ mình muốn xuất nó ra LCD mà chạy từ phải sang trái thì phải viết như thế nào??
Mới học nên muốn biết thêm>
Chào bạn, mình xin gửi tới bạn giải thuật như sau. Cái này đã chạy thử ngon lành. Trước hết đây là chuỗi ký tự và ta sẽ dùng phép tráo đổi ký tự. Tóm tắt giải thuật như sau:
1. lưu biến dong6[0] vào 1 biến tạm x.
2. dùng 1 vòng for tráo đổi vị trí ký tự (dong6[i]=dong6[i+1])
3. sau khi kết thúc vòng for thì ta thay ký tự cuối cùng là x.
4. xuất dòng ký tự lên màn hình.
5. delay và lặp lại chương trình

Để cụ thể hóa mình sẽ gửi tới bạn 1 code sau: chạy dòng chữ "Dai thang mua xuan 1975" làm ví dụ nhé. Mình dùng CCS
//------------------------------------------------------------------------------
#include "C:\Documents and Settings\QUACH CUONG\My Documents\test PIC\pic18f4680_chuchay.h"
#include <LCD_PIC18F4620_420_D.C> //dirver LCD cho pic18 (LCD su dung 4bit)
#include <string.h>
char chuoi[];
//------------------Chuong trinh con chay chu----------------------------------
void chay_chu(void)
{
int i;
char x;
x=chuoi[0];
for(i=0;i<(strlen(chuoi)-1);i++) // strlen() la ham lay chieu dai chuoi
{
chuoi[i]=chuoi[i+1];
}
chuoi[strlen(chuoi)-1]=x;
lcd_gotoxy(1,1);
//ss=mid(chuoi, 0, 15)
printf(lcd_putc,chuoi);

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


void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
lcd_init();
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

// TODO: USER CODE!!
strcpy(chuoi,"Dai thang mua xuan 1975. "); // phia sau dong chu nen de 1 khoang trang de phan biet dong chu khi chay.

while(1)
{
chay_chu();
delay_ms(400);
}

}


//================================================== ===================================
Đây là driver cho LCD 4 bit ghép ở cổng D

////////////////////////////////////////////////////////////////////////////
//// LCD420.C ////
//// Driver for common 4x20 LCD modules ////
//// ////
//// lcd_init() Must be called before any other function. ////
//// ////
//// lcd_putc(c) Will display c on the next position of the LCD. ////
//// The following have special meaning: ////
//// \f Clear display ////
//// \n Go to start of second line ////
//// \b Move back one position ////
//// ////
//// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) ////
//// ////
//// lcd_getc(x,y) Returns character at position x,y on LCD ////
//// ////
////////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,1997 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS C ////
//// compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, reproduction ////
//// or distribution is permitted without written permission. ////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
////////////////////////////////////////////////////////////////////////////

// As defined in the following structure the pin connection is as follows:
// D0 enable
// D1 rs
// D2 rw
// D4 D4
// D5 D5
// D6 D6
// D7 D7
//
// LCD pins D0-D3 are not used and PIC B3 is not used.

struct lcd_pin_map { // This structure is overlayed
BOOLEAN enable; // on to an I/O port to gain
BOOLEAN rs; // access to the LCD pins.
BOOLEAN rw; // The bits are allocated from
BOOLEAN unused; // low order up. ENABLE will
int data : 4; // be pin B0.
} lcd;

#byte lcd = 0xF83 // This puts the entire structure
// on to port B (at address 6)

#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines


BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
// These bytes need to be sent to the LCD
// to start it up.


// The following are used for setting
// the I/O port direction register.

struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in


BYTE lcdline;

BYTE lcd_read_byte() {
BYTE low,high;

set_tris_d(LCD_READ);
lcd.rw = 1;
delay_cycles(1);
lcd.enable = 1;
delay_cycles(1);
high = lcd.data;
lcd.enable = 0;
delay_cycles(1);
lcd.enable = 1;
delay_us(1);
low = lcd.data;
lcd.enable = 0;
set_tris_d(LCD_WRITE);
return( (high<<4) | low);
}


void lcd_send_nibble( BYTE n ) {
lcd.data = n;
delay_cycles(1);
lcd.enable = 1;
delay_us(2);
lcd.enable = 0;
}


void lcd_send_byte( BYTE address, BYTE n ) {

lcd.rs = 0;
while ( bit_test(lcd_read_byte(),7) ) ;
lcd.rs = address;
delay_cycles(1);
lcd.rw = 0;
delay_cycles(1);
lcd.enable = 0;
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}


void lcd_init() {
BYTE i;

set_tris_d(LCD_WRITE);
lcd.rs = 0;
lcd.rw = 0;
lcd.enable = 0;
delay_ms(15);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(5);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0, LCD_INIT_STRING[i]);
}


void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;

switch(y) {
case 1 : address=0x80;break;
case 2 : address=0xc0;break;
case 3 : address=0x94;break;
case 4 : address=0xd4;break;
}
address+=x-1;
lcd_send_byte(0,address);
}

void lcd_putc( char c) {
switch (c) {
case '\f' : lcd_send_byte(0,1);
lcdline=1;
delay_ms(2);
break;
case '\n' : lcd_gotoxy(1,++lcdline); break;
case '\b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}

char lcd_getc( BYTE x, BYTE y) {
char value;

lcd_gotoxy(x,y);
lcd.rs=1;
value = lcd_read_byte();
lcd.rs=0;
return(value);
}

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

// CHÚC VUI HA.
xuan_68 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn