CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

LCD 16x2- C code in 16f877a

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mizusuki



Joined: 12 Oct 2015
Posts: 1

View user's profile Send private message

LCD 16x2- C code in 16f877a
PostPosted: Mon Oct 12, 2015 7:19 am     Reply with quote

Hi everyone, i'm new in C programming. I wrote a simple code in c for HD44780, using 4 bit mode, but it don't work!

Adress.h have some resgister's adresses to use.
Delay.h only have some delays.
I am not using RW, in the schematics this pin is connected to ground.
The code compiles, but nothing occurs on Proteus.
Can anyone help me?
Code:

#include<16f877a.h>
#include"Adress.h"
#include"Delays.h"
#fuses HS,NOWDT,NOLVP,PUT,NOBROWNOUT

#bit    RS      =   PORTB.5
#bit    E      =   PORTB.4
#bit   RW      =   PORTB.6
#bit    D4      =   PORTB.0
#bit    D5      =   PORTB.1
#bit    D6      =   PORTB.2
#bit    D7      =   PORTB.3


void Pulse_E();
void Send_CMD(byte cmd);
void Init();
void Send_CHR(byte chr);

void main(void){
   
   TRISB   =   0x00;
   PORTB   =   0x00;
   OPTION_REG = (0x7F&OPTION_REG);
   RW=0;
   Init();
   Send_CHR('O');
   while(1){
   }
}

void Pulse_E(){
      
   E=1;
   delay_1ms();
   E=0;

}

void Send_CMD(byte cmd){
   
   delay_1ms();
   output_B(cmd&0xF0);
   swap(PORTB);
   RS=0;
      
   Pulse_E();
   
   output_B(cmd&0x0F);
   RS=0;
   Pulse_E();
   
   delay_1ms();
   
}

void Send_CHR(byte chr){
   
   swap(PORTB);
   output_B(chr&0x0F);
   RS=1;
      
   Pulse_E();
   
   output_B(chr&0x0F);
   RS=1;
   Pulse_E();
   
   delay_1ms();
   
}

void Init(){
      
   Send_CMD(0x20);
   Send_CMD(0x28);
   Send_CMD(0x01);
   delay_1ms();
   Send_CMD(0x06);
   Send_CMD(0x0D);
   delay_1ms();
}

Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Mon Oct 12, 2015 8:06 am     Reply with quote

Well done for actually having a try!.

There is a supplied LCD driver with the compiler, and also the flex one in the code library which should both be studied.

However the instruction the _will_ be causing you grief, is using swap directly on the port.

Swap directly swaps the nibbles of a register. Quickly and efficiently. However 'PORTB' is _not_ a simple register. It's actually two registers in one. When you write to PORTB, the value is latched and fed out the pins, When you read from PORTB, you directly read the logic levels on the pins. This is why you can get the 'RMW' problem on the PIC (Read Modify Write). Where comes _back_ from a read, is not what you actually output.

Using Swap like this, the data you want to put out on the port, will will also result in RS, E, and RW being operated.....
Part of the data will also be lost, since what you 'read back', may not be the bit patterns you have output... :(

Separately, Your fuses and clock statement, _must_ be before you load any code. You don't show a clock statement, but have your fuses below loading Adress & Delays.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Oct 14, 2015 5:26 am     Reply with quote

couple of comments..
1) rename Init() to perhaps LCD_Module_Init(). Although it's obvious in this program Init() is for an LCD, in more complex programs 'Init()' could be fore any number of peripherals. Doing this allow your code to be 'selfcommenting' so you(and others) _know_ what is happening. Also I'd rename any function that the LCD uses to 'LCD_purpose', again to make it easy to see what the function is for.

2) Put a delay_ms(500) before your program runs LCD_Module_Init(). Every LCD Module requires a 'long' hardware setup time, 2-300 ms is typical. It needs this time to internally configure itself to it's defaults. The PIC is super fast in comparison, so without the delay the PIC will try to access the LCD when it's not properly setup.

3) Be aware that Proteus is full of bugs, errors and faulty RDCs! The only real 'simulation' that will show what's happening is the Real World. It's always best to try code in a real PIC even in a simple white 'breadboard'. Heck if it works on one of them it WILL work in a PCB !

Jay
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: LCD 16x2- C code in 16f877a
PostPosted: Wed Oct 14, 2015 7:41 am     Reply with quote

mizusuki wrote:

Code:

#include<16f877a.h>
#include"Adress.h"
#include"Delays.h"
#fuses HS,NOWDT,NOLVP,PUT,NOBROWNOUT

void main(void){
   
   TRISB   =   0x00;
   PORTB   =   0x00;
   OPTION_REG = (0x7F&OPTION_REG);

...

}

void Pulse_E(){
      
   E=1;
   delay_1ms();
   E=0;

}


Is this even CCS C? Some bits look like CCS C, such as the fuses, but other parts, such as delay_1ms() aren't CCS C. It doesn't seem to be likely that this was written as CCS C. Is this "translated"/ported from another version of C and expected to run as is?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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