PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT PIC,...)

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 20-08-2013, 05:22 PM   #1
ttlg59
Nhập môn đệ tử
 
Tham gia ngày: Sep 2012
Bài gửi: 3
:
[Help] Giúp mình xem lỗi CCS với

Mình mới tập viết code cho pic được 1 tuần, hiện đang thử viết một số code đơn giản. nhưng những thư viện mình dùng khi build thì toàn thông báo lỗi thế này:
Code:
The target "C:\Users\Luong\Documents\VXL\main.o" is already up to date.
Executing: "C:\Program files\Picc\CCSC.exe" +FM "lcd_lib_4bit.c" +EXPORT  #__DEBUG=1 +ICD +DF +LN +T +A +M +Z +Y=9 +EA #__16F877A=TRUE
*** Error 128 "lcd_lib_4bit.c" Line 18(2,10): A #DEVICE required before this line
C:\Users\Luong\Documents\VXL\lcd_lib_4bit.o ===>  1 Errors,  0 Warnings.
Khó hiếu nhất là cái thông baó: A #DEVICE required before this line. Khi chỉ vào nó nhỉ vào cái dòng #separate ấy

File lcd_lib_4bit.c
Code:
/*
Driver Lcd 16x2 che do 4 bit

*/
#include <stddef.h>
#define LCD_RS PIN_B2
#define LCD_EN PIN_B3
#define LCD_D4 PIN_B4
#define LCD_D5 PIN_B5
#define LCD_D6 PIN_B6
#define LCD_D7 PIN_B7
// misc display defines-
#define LINE_1 0x80
#define LINE_2 0xC0
#define CLR_SCR 0x01 // Ma xoa man hinh
#use delay(clock=20000000)
// prototype statements
#separate void lcd_init (void);                              // ham khoi tao LCD
#separate void lcd_position ( unsigned int x );              //Thiet lap vi tri con tro
#separate void lcd_gotoxy(char x, char y);                   // Lap vi tri con tro theo toa do hang x cot y
#separate void lcd_putchar ( unsigned int _ch );			 // Ham viet 1 ky tu/1 chuoi len LCD
#separate void lcd_cmd ( unsigned int _cmd); 				 // Ham gui lenh len LCD
#separate void lcd_send_data ( unsigned int _cmd ); 		 // Ham gui data
#separate void lcd_pulse ( void );							 // Xung kich hoat
#separate void lcd_setdata ( unsigned int _data );    		 // Ham xac dinh gui 4 bit
#separate void lcd_bcd(int8 y);								 // Ham in ma bcd

//khoi tao LCD**********************************************
#separate void lcd_init (void)
{
lcd_setdata (0x00);
delay_ms(200); /* wait enough time after Vdd rise >> 15ms */
output_low (LCD_RS);// che do gui lenh
lcd_setdata (0x03);
lcd_pulse();
lcd_pulse();
lcd_pulse(); 
lcd_setdata (0x02); //* set 4-bit interface */
lcd_cmd (0x28); /* send dual nibbles hereafter, MSN first */
lcd_cmd ( 0x2C ); /* function set (all lines, 5x7 characters) */
lcd_cmd ( 0x0C ); /* display ON, cursor off, no blink */
lcd_cmd ( 0x06 ); /* entry mode set, increment & scroll left */
lcd_cmd ( 0x01 ); /* clear display */
}
#separate void lcd_position ( unsigned int y ) 
{
if(y>0) y=y-1;
else y=1;
output_low(LCD_EN);
output_low(LCD_RS);
if (y<=15){
lcd_setdata (y|0x80); 
lcd_pulse();
lcd_setdata (swap(y)); // Dao vi tri bien x=0xab ve 0xba, gui di 4 bit thap
lcd_pulse();
}
if(y>15){
lcd_setdata (y|0xC0); // Dao nguoc vi tri bien x=0xab thanh 0xba, gui di 4 bit cao.
lcd_pulse();
lcd_setdata (swap(y)); // Tiep tuc dao vi tri bien x=0xba ve 0xab, gui di 4 bit thap
lcd_pulse();
}
}
#separate void lcd_gotoxy(char x, char y)
{
char address;
switch(x) {
case 1 : address=0x80;break;
case 2 : address=0xc0;break;
case 3 : address=0x94;break;
case 4 : address=0xd4;break;
}
address=address+(y-1);
lcd_cmd(0x80|address);
}

#separate void lcd_putc ( unsigned int _ch )
{
/* this subroutine works specifically for 4-bit Port A */
output_low(LCD_EN);
output_high(LCD_RS);
lcd_send_data(_ch);
output_low(LCD_RS);
}
#separate void lcd_cmd ( unsigned int _cmd )
{
output_low(LCD_RS); /* this subroutine works specifically for 4-bit Port A */
output_low(LCD_EN);
lcd_setdata (swap(_cmd)); /* gui 4 bit cao */
lcd_pulse();
lcd_setdata (swap(_cmd)); /* gui 4 bit thap */
lcd_pulse();
}
#separate void lcd_send_data ( unsigned int _cmd )
{
 /* this subroutine works specifically for 4-bit Port A */
lcd_setdata (swap(_cmd)); /* gui 4 bit cao */
lcd_pulse();
lcd_setdata (swap(_cmd)); /* gui 4 bit thap */
lcd_pulse();
}
#separate void lcd_pulse (void)
{
output_high(LCD_EN);
delay_us(10); // was 10
output_low(LCD_EN);
delay_ms(10); // was 5
}
#separate void lcd_setdata ( unsigned int _data ) // Ham gui data
{
output_bit ( LCD_D4, _data & 0x01 ); // Gui di bit cao D4-D7
output_bit ( LCD_D5, _data & 0x02 );
output_bit ( LCD_D6, _data & 0x04 );
output_bit ( LCD_D7, _data & 0x08 );
}
#separate void lcd_bcd(int8 y) // ham in ma bcd
{
lcd_putc(y+0x30);
}
Cái file lcd_lib_4bit gốc vốn ko có dòng "#use delay(clock=20000000)", nhưng ko chạy dc delay_us và delay_ms nên lại thêm vào.

File main.c
Code:
#include <16F877A.h>
#include "lcd_lib_4bit.c"
#include "ds1307.c"

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection

#use delay(clock=20000000)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)

#define EN PIN_B3
#define RS PIN_B2
#define D4 PIN_B4
#define D5 PIN_B5
#define D6 PIN_B6
#define D7 PIN_B7

#define SET PIN_C0 // Nut menu-set
#define UP PIN_C1 // Nut tang
#define DOWN PIN_C2 // Nut giam
#define MODE PIN_C6
#define ZERO PIN_C7
#define SPEAKER PIN_B0

//#int_lcd

//========================= Khai bao bien ===============================
int secon,minute,hour,date,wday,month,year;
int sech,secl,minh,minl,hrh,hrl,dayh,dayl,monh,monl,yrh,yrl;

//=========================== Khai bao ham ==========================
void ds1307_read();
void display_hr();
unsigned char bcd2upperchr(unsigned char bcd);
unsigned char bcd2lowerchr(unsigned char bcd);
//========================== Ham Main ================================

void main()
{  delay_ms(100);
   lcd_init();
   lcd_cmd(0x01);
   delay_ms(10);
   while(TRUE)
   {
      	ds1307_read();
      	display_hr();
		delay_ms(50);
   }

}

//************************************ Dinh nghia ham **********************************************

//================== Ham doc lay thong tin tu ds1307 =====================
void ds1307_read()
{
secon=read_ds1307(0);
minute=read_ds1307(1);
hour=read_ds1307(2);
wday=read_ds1307(3);
date=read_ds1307(4);
month=read_ds1307(5);
year=read_ds1307(6);
sech=bcd2upperchr(secon);
secl=bcd2lowerchr(secon);
minh=bcd2upperchr(minute);
minl=bcd2lowerchr(minute);
hrh=bcd2upperchr(hour);
hrl=bcd2lowerchr(hour);
dayh=bcd2upperchr(date);
dayl=bcd2lowerchr(date);
monh=bcd2upperchr(month);
monl=bcd2lowerchr(month);
yrh=bcd2upperchr(year);
yrl=bcd2lowerchr(year);
}

//==================== Ham hien thi ======================
void display_hr()
{
lcd_gotoxy(1,2);
switch (wday){
   case 2: {printf(lcd_putc,"Mon ");}
   break;
   case 3: {printf(lcd_putc,"Tue ");}
   break;
   case 4: {printf(lcd_putc,"Wed ");}
   break;
   case 5: {printf(lcd_putc,"Thu ");}
   break;
   case 6: {printf(lcd_putc,"Fri ");}
   break;
   case 7: {printf(lcd_putc,"Sat ");}
   break;
   case 1: {printf(lcd_putc,"Sun ");}
   break;
}
printf(lcd_bcd,"%u%u/%u%u/20%u%u",dayh,dayl,monh,monl,yrh,yrl);
lcd_gotoxy(5,2);
printf(lcd_bcd,"%u%u:%u%u:%u%u",hrh,hrl,minh,minl,sech,secl);
}

//================== Ham tach bit cao thap ======================
unsigned char bcd2upperchr(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}

unsigned char bcd2lowerchr(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}
File ds1307.c

Code:
#define RTC_SDA PIN_C4
#define RTC_SCL PIN_C3
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bin2bcd(BYTE datain);
BYTE bcd2bin(BYTE datain);

//==========================
void init_ds1307()
{
output_float(RTC_SCL);
output_float(RTC_SDA);
}

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

BYTE bin2bcd(BYTE datain) //Convert data to BCD
{
   int nibh;
   int nibl;

   nibh=datain/10;
   nibl=datain-(nibh*10);

   return((nibh<<4)|nibl);
}
BYTE bcd2bin(BYTE datain) //Convert data to int
{
   int i;
   i=datain;
   datain=(i>>4)*10;
   datain=datain+(i<<4>>4);
   return datain;
}

//=================== Ghi 1 byte vao ds1307 ========================
void write_ds1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
i2c_start();
status=i2c_write(0xd0);
}
}

//========== Doc 1 byte tu ds1307 ================
BYTE read_ds1307(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}
Mong mọi người giúp đỡ.

thay đổi nội dung bởi: ttlg59, 20-08-2013 lúc 05:40 PM.
ttlg59 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 07:24 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam