|
|
View previous topic :: View next topic |
Author |
Message |
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
help for LCD module control |
Posted: Sat Aug 04, 2007 9:02 am |
|
|
Dear all,
During my test program compiling in CCS Compiler I got one error message called "can not change the device type this far into the code"
The device I chose was PIC16F876 and the error cursor blinding in the header file of "16F876.h"
For my hardware requirement, I have changed some parameters in LCD1.c driver, and add some more functions named such as "lcd_read_byte(void)" which used for message reusing displaying on the LCD.
Please check my code shown below and give me some suggestions for futher codes promoting when you have time.
Thank you very much for your careness.
main code listing: (for LCD testing)
Code: | [code]#include "E:\Master material\Project files\project material\Project program\LCD Driver Code\P1.h"
#include <LCD1.C>
#include <16F876.h>
#device PIC16F876
#Fuse XT,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NODEBUG
#USE DELAY(clock=4000000)
#USE RS232(BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7)
#USE I2C(slave, sda=PIN_C4, scl=PIN_C3, address=0x00)
#USE standard_IO(A)
#USE standard_IO(B)
#USE standard_IO(C)
#define LCD_E PIN_RB0
#define LCD_RS PIN_RB1
#define LCD_RW PIN_RB2
#define LCD_DB4 PIN_RB3
#define LCD_DB5 PIN_RB4
#define LCD_DB6 PIN_RB5
#define LCD_DB7 PIN_RB6
#define LCD_SS PIN_RA5
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(SPI_SLAVE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
lcd_init();
delay_ms(100);
while(1)
{
lcd_putc('x');
lcd_putc('w');
lcd_putc('5');
lcd_putc('0');
lcd_putc('3');
delay_ms(1000);
lcd_putc('\f');
delay_ms(1000);
}
}
[i][b]LCD1.c code listing:[/b][/i]
// As defined in the following structure the pin connection is as follows:
// RB0 enable
// RB1 rs
// RB2 rw
// RB3 D4
// RB4 D5
// RB5 D6
// RB6 D7
// LCD pins D0-D3 are not used and PIC RB7 is not used.
// Un-comment the following define to use port B
#define use_portb_lcd TRUE
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
int data : 4;
BOOLEAN unused; // low order up. ENABLE will
}
struct lcd_pin_map lcd; // be pin B0.
#if defined(__PCH__)
#if defined use_portb_lcd
#byte lcd = 0xF81 // This puts the entire structure
#else
#byte lcd = 0xF83 // This puts the entire structure
#endif
#else
#if defined use_portb_lcd
#byte lcd = 6 // on to port B (at address 6)
#else
#byte lcd = 8 // on to port D (at address 8)
#endif
#endif
#if defined use_portb_lcd
#define set_tris_lcd(x) set_tris_b(x)
#else
#define set_tris_lcd(x) set_tris_d(x)
#endif
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the second line
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,15,0}; // For read mode data pins
are in
BYTE lcd_read_byte() {
BYTE low,high;
set_tris_lcd(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_lcd(LCD_WRITE);
return( (high<<4) | low);
}
void lcd_send_nibble(int8 nibble)
{
output_bit(LCD_DB4, !!(nibble & 1));
output_bit(LCD_DB5, !!(nibble & 2));
output_bit(LCD_DB6, !!(nibble & 4));
output_bit(LCD_DB7, !!(nibble & 8));
delay_cycles(1);
output_high(LCD_E);
delay_us(2);
output_low(LCD_E);
}
#define LCD_DB4 PIN_RB4
#define LCD_DB5 PIN_RB5
#define LCD_DB6 PIN_RB6
#define LCD_DB7 PIN_RB7
#define LCD_E PIN_RB0
#define LCD_RS PIN_RB1
#define LCD_RW PIN_RB2
#define USE_LCD_RW 1
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the 2nd line
int8 const LCD_INIT_STRING[4] =
{
0x20 | (lcd_type << 2), // Func set: 4-bit, 2 lines, 5x8 dots
0xc, // Display on
1, // Clear display
6 // Increment cursor
};
void lcd_send_nibble(int8 nibble)
{
output_bit(LCD_DB4, !!(nibble & 1));
output_bit(LCD_DB5, !!(nibble & 2));
output_bit(LCD_DB6, !!(nibble & 4));
output_bit(LCD_DB7, !!(nibble & 8));
delay_cycles(1);
output_high(LCD_E);
delay_us(2);
output_low(LCD_E);
}
#ifdef USE_LCD_RW
int8 lcd_read_nibble(void)
{
int8 retval;
#bit retval_0 = retval.0
#bit retval_1 = retval.1
#bit retval_2 = retval.2
#bit retval_3 = retval.3
retval = 0;
output_high(LCD_E);
delay_cycles(1);
retval_0 = input(LCD_DB4);
retval_1 = input(LCD_DB5);
retval_2 = input(LCD_DB6);
retval_3 = input(LCD_DB7);
output_low(LCD_E);
return(retval);
}
#endif
#ifdef USE_LCD_RW
int8 lcd_read_byte(void)
{
int8 low;
int8 high;
output_high(LCD_RW);
delay_cycles(1);
high = lcd_read_nibble();
low = lcd_read_nibble();
return( (high<<4) | low);
}
#endif
void lcd_send_byte( BYTE address, BYTE n ) {
lcd.rs = 0;
while ( !bit_test(lcd_read_byte(),6) ) ;
lcd.rs = address;
delay_cycles(1);
lcd.rw = 0;
delay_cycles(1);
lcd.enable = 0;
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0x0f);
}
void lcd_init(void)
{
int8 i;
output_low(LCD_RS);
#ifdef USE_LCD_RW
output_low(LCD_RW);
#endif
output_low(LCD_E);
delay_ms(15);
for(i=0 ;i < 3; i++)
{
lcd_send_nibble(0x03);
delay_ms(5);
}
lcd_send_nibble(0x02);
for(i=0; i < sizeof(LCD_INIT_STRING); i++)
{
lcd_send_byte(0, LCD_INIT_STRING[i]);
#ifndef USE_LCD_RW
delay_ms(5);
#endif
}
}
void lcd_gotoxy( BYTE x, BYTE y) {
BYTE address;
if(y!=1)
address=lcd_line_two;
else
address=0;
address+=x-1;
lcd_send_byte(0,0x80|address);
}
void lcd_putc( char c) {
switch (c) {
case '\f' : lcd_send_byte(0,1);
delay_ms(2); break;
case '\n' : lcd_gotoxy(1,2); break;
case '\b' : lcd_send_byte(0,0x10); break;
default : lcd_send_byte(1,c); break;
}
}
#ifdef USE_LCD_RW
char lcd_getc(int8 x, int8 y)
{
char value;
lcd_gotoxy(x,y);
while(bit_test(lcd_read_byte(),6));
output_high(LCD_RS);
value = lcd_read_byte();
output_low(lcd_RS);
return(value);
}
#endif[/code] |
By the way, I have not changed any code in <16F876.h> _________________ Enjoy our EEE |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Sat Aug 04, 2007 12:53 pm |
|
|
Hello,
I think its because you add the line
#device PIC16F876 after the
#include <16F876.h>
This is not necessary because #device is already declared in the 16F876.h
I propose you to declare #include <16F876.h> first and then your other #include as:
Code: | #include <16F876.h>
#include "E:\Master material\Project files\project material\Project program\LCD Driver Code\P1.h"
#include <LCD1>
...
|
dro. _________________ in médio virtus |
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
Thanks first but...... |
Posted: Sat Aug 04, 2007 4:58 pm |
|
|
Thanks for your reply but it still has the same problem, and the cursor was stopped at the header file and showing the error message, it seems like the process of header file compiling was wrong.
Do you have any more suggestions. cheers for your help.
The error code listing:
//////// Standard Header file for the PIC16F876 device ////////////////
#device PIC16F876
#nolist
//////// Program memory: 8192x14 Data RAM: 367 Stack: 8
//////// I/O: 22 Analog Pins: 5
//////// Data EEPROM: 256
//////// C Scratch area: 77 ID Location: 2000
//////// Fuses: LP,XT,HS,RC,NOWDT,WDT,NOPUT,PUT,PROTECT,PROTECT_5%
//////// Fuses: PROTECT_50%,NOPROTECT,NOBROWNOUT,BROWNOUT,LVP,NOLVP,CPD
//////// Fuses: NOCPD,WRT,NOWRT,DEBUG,NODEBUG
////////
////////////////////////////////////////////////////////////////// I/O
// Discrete I/O Functions: SET_TRIS_x(), OUTPUT_x(), INPUT_x(),
// PORT_B_PULLUPS(), INPUT(),
// OUTPUT_LOW(), OUTPUT_HIGH(),
// OUTPUT_FLOAT(), OUTPUT_BIT()
// Constants used to identify pins in the above are:
#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45
......
error message: can not change the device type this far into the code
the code shown above were part of <16F876.h> _________________ Enjoy our EEE |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Mon Aug 06, 2007 7:17 am |
|
|
Make sure you don't have a #device statement anywhere else in your code.
And this must be your first code line in your main file
Code: |
#include <16F876.h>
|
|
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
thanks second but... |
Posted: Mon Aug 06, 2007 10:16 am |
|
|
Thank you for your reply,
Can I have an idea about which kind of CCS C compiler you used for your C code compiling? Which I using was "PCWH compiler" IDE version 3.215;
PCB/PCM/PCH version 3.216. Is that a matter relevant to my problem.
Secondly, my main code including two files which were "16F876.h" and "LCD1.c". For the header file of PIC16F876,if you check this file in your CCS Compiler in devices folder, you can see "#device PIC16F876;
#nolist" were shown on first two line, and I can not change or delete them, the header file can not be compilied without "#device ",isn't it?
Give my your suggestions when you have time, I am sorry to bother you too much time on my problems, and thank you all of you again.
By the way, I tried to compile my program in CCS Compiler and MPLAB 7.60, but both of them would show me the same error messages.
*** Error 23 "C:\16F876.h" Line 2(8,9): Can not change device type this far into the code
*** Error 125 "C:\16F876.h" Line 19(9,15): Duplicate #define
*** Error 48 "C:\16F876.h" Line 19(17,23): Expecting a (
*** Error 125 "C:\16F876.h" Line 20(9,15): Duplicate #define
*** Error 43 "C:\16F876.h" Line 20(17,19): Expecting a declaration
*** Error 125 "C:\16F876.h" Line 21(9,15): Duplicate #define
*** Error 43 "C:\16F876.h" Line 21(17,19): Expecting a declaration
*** Error 125 "C:\16F876.h" Line 22(9,15): Duplicate #define
........ _________________ Enjoy our EEE |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Mon Aug 06, 2007 10:54 am |
|
|
Hello,
I'm not sure because my oldest PCH is 3.249. i can not test with 3.216.
Have you well chose the type of controleur in the tool bar pop up family : Microchip 12 bit, Microchip 14 bit or Microchip PIC 18 ?
dro. _________________ in médio virtus |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 06, 2007 11:28 am |
|
|
Quote: | #include "E:\Master material\Project files\project material\Project
program\LCD Driver Code\P1.h" |
What's in the P1.h file ? |
|
|
Ttelmah Guest
|
|
Posted: Mon Aug 06, 2007 11:31 am |
|
|
Look at the two files you are including at the start of the program originally shown. I'd suspect one of them already includes the processor.h file.
The rule is:
Just include the processor file once, at the start of the main program. Have no other processor define lines or files.
Best Wishes |
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
compiling successfully |
Posted: Mon Aug 06, 2007 11:37 am |
|
|
I have fixed my problem with deleting the header file, and define all necessary pins in my main file itself
I have got another problem which is that when i disconnected my ICD2 to my hardware, all selftesting would be fine just display no target available, but when I connect my target hardware to ICD2, it always show following messages :
ICD0019: Communications: Failed to open port: (Windows::GetLastError() = 0x2, ')
ICD0021: Unable to connect with MPLAB ICD 2
Can you give me some suggestions about that?
Thanks.
By the way I used RJ6 serial cable for programming my target chip and using ICD2 as power supplier for my target product.
power testing shown that there is 5.00V for the my target and target Vpp was 12.38V
ICD2 Vpp was 12.38V after pressing update button. _________________ Enjoy our EEE |
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
help |
Posted: Mon Aug 06, 2007 12:15 pm |
|
|
I changed my the power supplier of my target product, now it used its own power 5V.
But when I connected my ICD2 to this target, and running Programmer---Select programmer---MPLAB ICD2, there was error message shown below:
ICDWarn0020: Invalid target device id (expected=0x4F, read=0x0)selftesting was ok.
Hope anyone here can help me solve this problem or give me your suggestion.
cheers. _________________ Enjoy our EEE |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 06, 2007 12:44 pm |
|
|
What board are you using ? Did you buy it, or did you make it yourself ?
If you bought it, what's the name of the board, the manufacturer, and
the part number. |
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
sorry for my stupid brain |
Posted: Mon Aug 06, 2007 12:53 pm |
|
|
I have fixed this problem, coz I used the chip of PIC16F876A and I choose the device shown in ICD2 was PIC16F876, that's the problem.
sorry about that.
By the way, can you check my main code shown below:
#include <16F876A.h>
#include <LCD1.C>
#Fuses XT,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,NODEBUG
#USE DELAY(clock=4000000)
#USE RS232(BAUD=9600,XMIT=62,RCV=63)
#USE I2C(slave, sda=60, scl=59, address=0x00)
#USE standard_IO(A)
#USE standard_IO(B)
#USE standard_IO(C)
void main()
{
lcd_init();
delay_ms(100);
while(1)
{
lcd_putc('x');
lcd_putc('w');
lcd_putc('5');
lcd_putc('0');
lcd_putc('3');
delay_ms(1000);
lcd_putc('\f');
delay_ms(1000);
}
}
I have already compilied successfully and burning them into the chip successfully as well.
but nothing displaying on my LCD screen.
Is there any problem of my code, I am checking them now, too. _________________ Enjoy our EEE |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 06, 2007 12:59 pm |
|
|
Every time you ask a question, about a few minutes later, you post a
new message saying that you have already fixed the problem. Then
you ask another question in that post.
Why don't we sit back and let you fix it all. |
|
|
applecon2000
Joined: 29 Jul 2007 Posts: 31 Location: UK
|
hehe |
Posted: Mon Aug 06, 2007 1:06 pm |
|
|
you know, I am the one who was most familar with my design, I can not imagin whether I will fix my problem sooner or later.
When you thinking about my question, I am also thinking and trying every possibility of rectification.
With your careness and suggestion, I can have some more fresh and blinking ideas appearing in my brain.
So please think about my question if you do not mind.
I am here and present my sincere acknowledgement. _________________ Enjoy our EEE |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 06, 2007 1:12 pm |
|
|
You want someone to be a real-time helper and follow along you with
during the day, and read every idea that you think of every few minutes,
as you work on your project.
You want a technical chat forum.
Maybe other people want to do that with you, but I don't want to.
If you have a real problem that you cannot solve, and you have spent
a few hours trying, then post it. I don't want to be a personal lab TA.
It's taking advantage of the goodwill of the forum to expect this. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|