![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
I wrote a C program that sends a character to the lcd then jumps the cursor to the 2nd line. I changed my program to send a string and I'm finding these two functions are not doing the job. I'm using bitwise operators to assign the MSB of a char to D7, the next MSB to D6, etc. Can anyone see what's wrong? Code: void sendString(char *str)
{
int index = 0;
while (str[index] != 0)
{
sendChar(str[index]);
index++;
}
}
void sendChar(char c)
{
RS = 1;
D7 = (c >> 7) & 1;
D6 = (c >> 6) & 1;
D5 = (c >> 5) & 1;
D4 = (c >> 4) & 1;
enable();
D7 = (c >> 3) & 1;
D6 = (c >> 2) & 1;
D5 = (c >> 1) & 1;
D4 = (c >> 0) & 1;
enable();
delay_ms(200);
}
| |
| |
| | #2 | |
| Quote:
Code: void sendString(char *str){
unsigned char index = 0;
while (str[index] != '\0'){
sendChar(str[index]);
index++;
}
}
unsigned char mystr = "Hello\0";
void main(){
// ...
}
| ||
| |
| | #3 |
|
Did you initialize the display ? Were you able to send and display a single char?
__________________ Please post questions to the forums. PM's are for personal communication. BCHS/3v0's Tutorials Junebug USB PIC programmer kit., USB Bit Whacker, The 15 Minute Printed Circuit Board! (+drill time) | |
| |
| | #4 | |
| Quote:
__________________ Bits From Bytes - the best RepRap kit out there now featuring RepRap V3 with full electronics redesign based on PIC32MX440F256H (English) MySQL Blog (Serbian) Elco Blog (Serbian) Use forum to ask questions, do not use PP | ||
| |
| | #5 |
|
Yes i can comment out those functions and send a char by hard coding the ascii value in their place. I've used the debugger and the port value looks fine and the while loop ends when I expect. I've tried unsigned char, char, using '/0' instead of 0, initialising the string different ways. The cursor moves to the 2nd line alright but nothing displays. Code: #define D7 PORTB.F7
#define D6 PORTB.F6
#define D5 PORTB.F5
#define D4 PORTB.F4
#define E PORTB.F3
#define RS PORTB.F2
void functionSet(void);
void entryMode(void);
void displayOn(void);
void sendString(char *str);
void moveTo2nd(void);
void sendChar(char c);
void enable (void);
void main(void)
{
TRISB = 0;
delay_ms(200);
functionSet();
entryMode();
displayOn();
sendString("Hello");
moveTo2nd();
sendString("world");
while(1);
}
// 0010 - 4 bit mode
// 0010 1010 - 2 lines, 5 X 8.
void functionSet(void)
{
RS = 0;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 0;
enable();
delay_ms(200);
enable();
D7 = 1;
enable();
delay_ms(200);
}
void enable(void)
{
E = 1;
E = 0;
}
void entryMode(void) //0000 0110, increment with no shift
{
RS = 0;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
enable();
D6 = 1;
D5 = 1;
enable();
delay_ms(200);
}
void displayOn(void) //0000 1111. cursor on. blinking on.
{
RS = 0;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
enable();
D7 = 1;
D6 = 1;
D5 = 1;
D4 = 1;
enable();
delay_ms(200);
}
void sendString(char *str)
{
int index = 0;
while (str[index] != 0)
{
sendChar(str[index]);
index++;
}
}
void sendChar(char c)
{
RS = 1;
D7 = (c >> 7) & 1;
D6 = (c >> 6) & 1;
D5 = (c >> 5) & 1;
D4 = (c >> 4) & 1;
enable();
D7 = (c >> 3) & 1;
D6 = (c >> 2) & 1;
D5 = (c >> 1) & 1;
D4 = (c >> 0) & 1;
enable();
delay_ms(200);
}
void moveTo2nd(void) //1100 0000 - move cursor to 2nd line
{
RS = 0;
D7 = 1;
D6 = 1;
D5 = 0;
D4 = 0;
enable();
D7 = 0;
D6 = 0;
enable();
delay_ms(200);
}
Last edited by colin mac; 21st July 2008 at 05:24 PM. | |
| |
| | #6 |
|
first, change the enable() function Code: void enable(void)
{
E = 1;
Delay_us(2); // this delay is necessary according to datasheet
E = 0;
}
__________________ Bits From Bytes - the best RepRap kit out there now featuring RepRap V3 with full electronics redesign based on PIC32MX440F256H (English) MySQL Blog (Serbian) Elco Blog (Serbian) Use forum to ask questions, do not use PP | |
| |
| | #7 |
|
I don't think it's the delays, since I can send out a char by just sending an ascii value to the port and I tried just passing one char to sendChar() and it didn't work leaving me to believe it's a problem with sendChar().
| |
| |
| | #8 | |
| Quote:
Code: void main(void)
{
TRISB = 0;
delay_ms(200);
functionSet();
entryMode();
displayOn();
sendString("Hello\0");
moveTo2nd();
sendString("world\0");
while(1);
}
Last edited by eng1; 21st July 2008 at 07:24 PM. Reason: typo corrected | ||
| |
| | #9 | |
| Quote:
in particular this code works: Code: #define D7 PORTD.F7
#define D6 PORTD.F6
#define D5 PORTD.F5
#define D4 PORTD.F4
#define E PORTD.F3
#define RS PORTD.F2
void lcd_send_nibble( unsigned char n ) {
D7 = (n >> 3) & 1;
D6 = (n >> 2) & 1;
D5 = (n >> 1) & 1;
D4 = n & 1;
_asm nop
E=1;
delay_us(2);
E=0;
}
void lcd_send_byte( unsigned char address, unsigned char n ) {
RS = address;
_asm nop
E = 0;
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
Delay_ms(200);
}
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
unsigned char const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
void xlcd_init() {
unsigned char i;
RS = 0;
E = 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 main(){
ADCON1 = 0xff;
TRISD = 0;
PORTD = 0;
TRISC = 0;
PORTC = 0;
xlcd_init();
lcd_send_byte(0,1); //clear screen
lcd_send_byte(1,'h');
lcd_send_byte(1,'e');
lcd_send_byte(1,'l');
lcd_send_byte(1,'l');
lcd_send_byte(1,'o');
}
as for the "end of string" .. when you make string as you did "blah blah" mikroC automatically add '\0' to the end of it so you do not need to take care of that, also sprintf and other functions do the correct cstring termination ..
__________________ Bits From Bytes - the best RepRap kit out there now featuring RepRap V3 with full electronics redesign based on PIC32MX440F256H (English) MySQL Blog (Serbian) Elco Blog (Serbian) Use forum to ask questions, do not use PP Last edited by arhi; 21st July 2008 at 07:24 PM. | ||
| |
| | #10 |
|
That was an accident. '\0' and 0 are identical. If a string is passed or declared like "this" the null character is automatically added. edit - ok arhi. thanks Last edited by colin mac; 21st July 2008 at 07:23 PM. | |
| |
| | #11 |
|
@colin I use the "send_nibble" because of the init part .. but you can do it the way you are doing it .. the thing is in the timing ... note the _asm nop in 2 places .. for some reason - it is necessary - bit's me why but it helps, same thing work on 20 and on 8mhz .. so .. just one instruction delay is necessary . for whatever reason ...also, when I remove the 2us delay for enable bit, the simulation does not run .. hence - it is necessary .. (also noted in the datasheet)
__________________ Bits From Bytes - the best RepRap kit out there now featuring RepRap V3 with full electronics redesign based on PIC32MX440F256H (English) MySQL Blog (Serbian) Elco Blog (Serbian) Use forum to ask questions, do not use PP | |
| |
| | #12 |
|
well I couldn't figure out why it wouldn't work even with the delay, or why you code does. I was just guessing with delays. Turns out the code works in microchip's compiler though using their delay library. Code: #include <p18F2520.h>
#include <delays.h>
#pragma config OSC = RC
#pragma config LVP = OFF, PWRT = ON, WDT = OFF
#pragma config PBADEN = OFF
#pragma config DEBUG = OFF
#define D7 LATBbits.LATB7
#define D6 LATBbits.LATB6
#define D5 LATBbits.LATB5
#define D4 LATBbits.LATB4
#define E LATBbits.LATB3
#define RS LATBbits.LATB2
void functionSet(void);
void entryMode(void);
void displayOn(void);
void sendChar(char c);
void enable (void);
void sendString(far rom char *str);
void moveTo2nd(void);
void main(void)
{
TRISB = 0;
Delay10TCYx(80);
functionSet();
entryMode();
displayOn();
sendString("Hello");
moveTo2nd();
sendString("World");
while(1);
}
// 0010 - 4 bit mode
// 0010 1010 - 2 lines, 5 X 8.
void functionSet(void)
{
RS= 0;
D7 = 0;
D6 = 0;
D5 = 1;
D4 = 0;
enable();
Delay10TCYx(80);
enable();
D7 = 1;
enable();
Delay10TCYx(80);
}
void enable(void)
{
E = 1;
Delay10TCYx(1);
E = 0;
}
void entryMode(void) //0000 0110, increment with no shift
{
RS = 0;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
enable();
D6 = 1;
D5 = 1;
enable();
Delay10TCYx(80);
}
void displayOn(void) //0000 1111. cursor on. blinking on.
{
RS = 0;
D7 = 0;
D6 = 0;
D5 = 0;
D4 = 0;
enable();
D7 = 1;
D6 = 1;
D5 = 1;
D4 = 1;
enable();
Delay10TCYx(80);
}
void sendString(far rom char *str)
{
int index = 0;
while (str[index] != '\0')
{
sendChar(str[index]);
index++;
}
}
void sendChar(char c)
{
RS = 1;
D7 = (c >> 7) & 1;
D6 = (c >> 6) & 1;
D5 = (c >> 5) & 1;
D4 = (c >> 4) & 1;
enable();
D7 = (c >> 3) & 1;
D6 = (c >> 2) & 1;
D5 = (c >> 1) & 1;
D4 = (c & 1);
enable();
Delay10TCYx(80);
}
void moveTo2nd(void) //1100 0000 - move cursor to 2nd line
{
RS = 0;
D7 = 1;
D6 = 1;
D5 = 0;
D4 = 0;
enable();
D7 = 0;
D6 = 0;
enable();
Delay10TCYx(80);
}
Last edited by colin mac; 21st July 2008 at 10:26 PM. | |
| |
| | #13 | |
| Quote:
__________________ Bits From Bytes - the best RepRap kit out there now featuring RepRap V3 with full electronics redesign based on PIC32MX440F256H (English) MySQL Blog (Serbian) Elco Blog (Serbian) Use forum to ask questions, do not use PP | ||
| |
|
| Tags |
| mikroc, problem |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| mikroC lib troubble | Kryten | Micro Controllers | 17 | 28th May 2008 09:56 AM |
| mikroC demo version limits | ssylee | Micro Controllers | 8 | 19th October 2007 09:21 PM |
| Servo motor~~~~ problem~.....shaking problem!!!!! | Gundam82 | Robotics Chat | 10 | 4th October 2004 08:12 PM |