Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C port question

Status
Not open for further replies.
3v0 I'm with you kind of bad ideal but a lot of them compilers force you to buy there debugger
 
DAG!
Code:
#if ENDIAN = LOWER
    BF = LOWEND;
#else
    BF = UPPEND;        
#endif
[B][COLOR="Red"]
.....produces...
D:\Micro\PIC\LCD_LIB\lcd.c:6:Error [1029] malformed expression in '#if'[/COLOR][/B]

So i guess i cant :(

Hi Jason,

Sorry, my mistake, you need the double equals.

This works,
Code:
#define Upper 0
#define Lower 1

#define Endian Lower

    #if Endian==Lower
        //some code
    #else
        //some other code
    #endif

Mike.
 
I have got LCD interfacing code for PIC16F628A written on mikroC.
* Description:
This code demonstrates how to display test message on a LCD which
is connected to PIC16F628A through PORTB. D4-D7 pins of LCD are
connected to RB4-RB7, whereas RS and EN pins connected to RA0 and RA1
respectively.
MCU: PIC16F628A
Oscillator: XT, 4.0 MHz
*/
// LCD module connections
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
// Define Messages
char message1[] = "Testing LCD";
char message2[] = "using PIC16F628A";
char message3[] = "Test Successful!";
char message4[] = "2009/09/18";
void main() {
CMCON |= 7; // Disable Comparators
Lcd_Init(); // Initialize LCD
do {
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,message1); // Write message1 in 1st row
Lcd_Out(2,1,message2); // Write message1 in 2nd row
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,message3); // Write message3 in 1st row
Lcd_Out(2,1,message4);
Delay_ms(2000);
} while(1);
}

Raj
Experiments with PIC16F628A
 
I have a thing about compiler vendors that require you to buy their debugger. It is a stupid money grubbing thing to do. Sure you can use a simulator but why should you have to when you can get a decent ICD like the PICkit2 for $40 ?

They DON'T make you buy the debugger. :( At least according to their compiler download page the "fully functional free demo version" has only one limitation; "Note: in Demo version, hex output is limited to 2k of program words."

The think with MikroE is that they make the best development hardware in the business, which is USB plug and play, ICD etc seamlessly with their compilers. Like the EasyPIC6 (for $139);
**broken link removed**

This makes for REALLY fast development time as every single PIC pin has it's own button and LED, and every PIC pin is brought out to external connectors. I've been using their stuff for a couple of years and most things i get finished now without having to make any hardware at all until AFTER the project is done and debugged on the dev board. Check out the peripherals on the pcb above!

The MikroC PRO compiler is currently on special at $199.

Everyone is loyal to their favorite compiler and dev board but for speed/versatility and value for money the combination above is hard to beat.
 
They DON'T make you buy the debugger. At least according to their compiler download page the "fully functional free demo version" has only one limitation; "Note: in Demo version, hex output is limited to 2k of program words."
You have to buy there debugger
The mikroICD is a highly effective tool for a Real-Time debugging on hardware level. The mikroICD debugger enables you to execute the mikroBasic PRO for PIC 2009 program on a host PIC microcontroller and view variable values, Special Function Registers (SFR), RAM, CODE and EEPROM memory along with the mikroICD code execution on hardware.

If you have appropriate hardware and software for using mikroICD then you have to upon completion of writing your program to choose between Release build Type or ICD Debug build type.
 
I have LCD CODE i made already heh i trust me more :D

Code:
//THIS IS lcd.h

#ifndef __LCD_H
#define __LCD_H

#include <delays.h>
#include <p18cxxx.h>

#define ENDIAN   LOWER          //0 = 0:3 *** 1 = 4:7

#define LCD_PORT LATB               //LCD DATA PORT
#define LCD_TRIS TRISB              //LCD DATA DIRECTION PORT
#define LCD_DAT  PORTB              //LCD INPUT DATA VALUES

#define LCD_RS   LATBbits.LATB4     //LCD Command/Data Control
#define LCD_E    LATBbits.LATB5     //LCD Enable Line
//#define LCD_RW   LATBbits.LATB6     //LCD Read/Write Control

#define LCD_TRIS_RS TRISBbits.TRISB4
#define LCD_TRIS_E  TRISBbits.TRISB5
//#define LCD_TRIS_RW TRISBbits.TRISB4

#define CMD 0
#define TXT 1

#define LOWER 0
#define UPPER 1

#define LOWEND 0x08
#define UPPEND 0x80

#define LOWMSK 0x0F
#define UPPMSK 0xF0

#define DISPLAY 1
#define CURSOR  0

#define RIGHT 1
#define LEFT  0

#define MHz *1000000
#define MYFOSC 8MHz

void WaitLCDBusy(void);
void LCD_Init(void);
void LCD_DATA(unsigned char data,unsigned char type);
void LCD_NYB(unsigned char nyb);
void LCD_STR(unsigned rom char *text);
void LCD_LINE(char line);
void ShiftDisplay(unsigned char DC,unsigned char RL);
void DelayMS(unsigned int ms);

extern unsigned char BF;
#endif

Code:
//THIS IS lcd.c
#include "lcd.h"
unsigned char BF;
//--------------------------------------------------------------------------------//
void LCD_Init(void){
    #if ENDIAN==LOWER           //This will check if ENDIAN is LOW and if so
        BF = LOWEND;            //Set the BusyFlag MASK to check lower nibble
    #else                       //if not LOW, but HIGH(UPPER) then
        BF = UPPEND;            //Set BF to check upper nibble
    #endif

    #if ENDIAN==LOWER
        LCD_TRIS &= UPPMSK;       //ensure lower nibble is output
    #else
        LCD_TRIS &= LOWMSK;       //ensure upper nibble is output
    #endif

    LCD_E=0;                    //clear enable
    LCD_RS = 0;                 //going to write command
    LCD_TRIS_E=0;               //Set enable to output
    LCD_TRIS_RS=0;              //set RS to output
    #ifdef LCD_RW
        LCD_TRIS_RW=0;
        LCD_RW=0;
    #endif
    DelayMS(30);                //delay for LCD to initialise.
    LCD_NYB(0x30);              //Required for initialisation
    DelayMS(5);                 //required delay
    LCD_NYB(0x30);              //Required for initialisation
    DelayMS(1);                 //required delay
    LCD_DATA(0x02,0);           //set to 4 bit interface, 1 line and 5*7 font
    LCD_DATA(0x28,0);           //set to 4 bit interface, 2 line and 5*10 font
    LCD_DATA(0x0c,0);           //set to 4 bit interface, 2 line and 5*7 font
    LCD_DATA(0x01,0);           //clear display
    LCD_DATA(0x06,0);           //move cursor right after write

}
//--------------------------------------------------------------------------------//
void LCD_DATA(unsigned char data,unsigned char type){
    
    WaitLCDBusy();                  //TEST LCD FOR BUSY 

    if(type == CMD){
        LCD_RS = 0;                 //COMMAND MODE
    } else {
        LCD_RS = 1;                 //CHARACTER/DATA MODE
    }

    LCD_NYB(data>>4);               //WRITE THE UPPER NIBBLE
    LCD_NYB(data);                  //WRITE THE LOWER NIBBLE
}
//--------------------------------------------------------------------------------//
void WaitLCDBusy(void){
#ifdef LCD_RW                       //IF THE LCD_RW DEFINITION IS USED
    LCD_RS=0;                       //COMMAND MODE
    LCD_RW=1;                       //READ MODE

    #if ENDIAN==LOWER               //ENDIAN = 0? 
        LCD_TRIS |= LOWMSK;         //YES = SET LOWER NIBBLE INPUT
    #else                           //ELSE ENDIAN = 1
        LCD_TRIS |= UPPMSK;         //SET UPPER NIBBLE INPUT
    #endif

    LCD_E=1;                        //ENABLE LCD DATA LINES
    while(LCD_DAT & BF);            //TEST BUSY FLAG BIT
    LCD_E=0;                        //DISABLE LCD DATA LINES

    #if ENDIAN==LOWER               //ENDIAN = 0? 
        LCD_TRIS &= UPPMSK;         //YES = SET LOWER NIBBLE OUTPUT
    #else                           //ELSE ENDIAN = 1
        LCD_TRIS &= LOWMSK;         //SET UPPER NIBBLE OUTPUT
    #endif

    LCD_RW=0;
#else

    DelayMS(2);              //DELAY 1 MilliSeconds

#endif
}
//--------------------------------------------------------------------------------//
void LCD_NYB(unsigned char nyb){
    #if ENDIAN==LOWER                           //ENDIAN = 0? Yes Send the data LOWER
        LCD_PORT &= UPPMSK;                     //CLEAR LOWER PORT NIBBLE
        LCD_PORT |= (nyb & LOWMSK);             //SEND DATA LINE THE INFO 
    #else                                       //ENDIAN = 1 so send to HIGHER
        LCD_PORT &= LOWMSK;                     //CLEAR UPPER PORT NIBBLE
        LCD_PORT |= ((nyb << 4) & UPPMSK);      //SHIFT DATA OVER << 4, CLEAR LOWER NIBBLE
    #endif
    
    LCD_E = 1;          //ENABLE LCD DATA LINE
    Nop();              //SMALL DELAY
    LCD_E = 0;          //DISABLE LCD DATA LINE
}
//--------------------------------------------------------------------------------//
void LCD_STR(unsigned rom char *text){
    while(*text){
        LCD_DATA(*text++,1);
    }
}
//--------------------------------------------------------------------------------//
void LCD_LINE(char line){
    switch(line){
        case 0:
        case 1:
            LCD_DATA(0x80,0);
            break;
        case 2:
            LCD_DATA(0xC0,0);
            break;
    }
}
//--------------------------------------------------------------------------------//
void ShiftDisplay(unsigned char DC,unsigned char RL){
    unsigned char shift=0x10;
    if(DC == DISPLAY)
        shift |= 0b00001000;

    if(RL == RIGHT)
        shift |= 0b00000100;

    LCD_DATA(shift,0);
}
//--------------------------------------------------------------------------------//
void DelayMS(unsigned int ms){
    unsigned int x;
    for(x=0;x<ms;x++)
        Delay1KTCYx(2);
}
//--------------------------------------------------------------------------------//

Still not 100% yet but close to 95% heh

**broken link removed**
 
Last edited:
Mr RB said:
They DON'T make you buy the debugger....
Everyone is loyal to their favorite compiler and dev board but for speed/versatility and value for money the combination above is hard to beat.

When it comes to compiler vendors (and auto makers) I have as much loyality as a fence post (none).

I have been examining my attitude and comparing it to what I preferred when I was working.

When I was an engineer I wanted a tool chain that let me work in the same IDE regardless of what micro controller I was using. Development tool hardware and software cost were secondary to getting the project finished. In that environment it made sense to use the vendors IDE's and programmers if required.

As a hobby type and an educator I need to watch my pocket book. But I still want ICD. I see no reason to give it up ICD when a PICkit2 or Junebug can be had for less then $50. Given that all PIC ICD must communicate with (and is limited by) the PIC internal monitor I see no value in each vendor developing their own flavor of ICD.

In the end it depends on your perspective. There are people who care care less about ICD just as there are people who are perfectly happy to program in ASM.

From a hobby perspective whatever turns you crank is ok. So I should just close my trap.

3v0
 
I dont mind that i cant debug really. As long as i can program it. I dont like MikroC or any Mikro program because it uses shortcuts and does alot for you. I know a lot of people love that but its a turn off for me.

I like the satisfaction of creating something from almost scratch. Like pancakes :D i use the box and add the eggs and milk and bananas heh

it would suck to buy a already made mixture and pour and cook. Or already scrambled eggs or something like that. :D

Just my thoughts :) anyway. Im thinking of what else a LCD needs... like code i will add is:

Hex2Dec - ex: 0x2F = 0x04,0x07
Hex2Ascii - ex: 0x2F = 0x30, 0x78,0x32,0x64
Bin2Ascii - ex: 0b00001000 (0x08) = 0x30, 0x62,0x30,0x30,0x30,0x30,0x31,0x30,0x30,0x30
 
Last edited:
You have to buy there debugger

I'm still a bit stumped about that! Are you talking about you have to buy the hardware part (ie because ICD wont work with PICKit2 hardware)? The compiler has ICD available.

I use my EasyPIC4 dev board for commercial development and hobby stuff, I even made programming leads to plug into the PIC sockets so I can use it for in-circuit programming of other hardware. I'm sure it would do ICD on other hardware if I plugged the ICSP lead into that but I never use ICD, these days its rare for me to even turn the simulator on. But then I don't have to debug student's C code they wrote in txtspeak or show them what their code is doing to that poor PIC now... ;)

3v0- I get your point, my fondness for the MikroC compiler is tied to the fact that it works so well with its dev hardware, which I use. If I was a PICKit2 owner that teaches students the MikroC compiler might not have been my first choice, although it does handle single bits etc better than some C compilers.

I'm with Atomsoft on the compiler libraries too, I use very few of the MikroC libraries preferring to code my own. The dallas 1wire is the only library of thiers I can remember using, I wrote my own LCD stuff when i first got it. But that's a personality type thing, not a reflection on the compiler.
 
The mikroICD is a highly effective tool for a Real-Time debugging on hardware level. The mikroICD debugger enables you to execute the mikroBasic PRO for PIC 2009 program on a host PIC microcontroller and view variable values, Special Function Registers (SFR), RAM, CODE and EEPROM memory along with the mikroICD code execution on hardware.

If you have appropriate hardware and software for using mikroICD then you have to upon completion of writing your program to choose between Release build Type or ICD Debug build type.

What's hard about that my debugger don't work. Theirs will you have to buy some thing.

I don't need any EasyPIC4 dev board don't need a programmer or debugger I have all i need I like MiroC it the only C I use.

Atom you can code just like you do write it all yourself

Debugging a library call is not easy to do I don't like them
 
Last edited:
Just port pins are like PORTA.B0 was hard to get the hang of
Oh I have to run as admin on win 7 and vista
 
This is a sample you tell me LOL
Code:
void main(){
  char oldstate = 0;
  TRISB = 0xFF;          // set PORTB to be input
  TRISD = 0;             // set PORTD to be output
  PORTD = 0x0F;          // initialize PORTD
  
  do {
    if (Button(&PORTB, 1, 1, 1))                // detect logical one on RB1 pin
      oldstate = 1;
    if (oldstate && Button(&PORTB, 1, 1, 0)) {  // detect one-to-zero transition on RB1 pin
      PORTD = ~PORTD;                           // negates value on PORTD
      oldstate = 0;
    }
  } while(1);         // endless loop
}
 
Code:
if(button(&PORTB,1,1,1)) blah;
Adds 2 functions to the project;
button() = 60 PIC instructions and 2 ram
delay_500us = 18 PIC instructions

So just to test PORTB.F1 pin costs 78 rom and 2 ram!

You could have done;
Code:
if(PORTB.F1) blah;

which tests the same PORTB pin, and compiles to just one PIC instruction and uses no ram. Ok so the button() function does some debouncing, but your code that detects button HI then button \ edge is basically a debounce system anyway and could be coded using just a few rom, even in C.
 
The only reason I posted the code was it show how you do a function like
atom ask.

RB sure your code uses less it doesn't test anything
you for got the the test part
Code:
=1 
or 
=0
 
Last edited:
I'm guessing but I think even the mikroC compiler will still turn if(PORTB.F1==1) into one instruction.

Mike.
 
Unfortunately it doesn't. :( I talked with the MikroE guys about this many months ago and that is one of the things they fixed with the new compiler version.

My older version turns if(PORTB.F1==1) into about 4 instructions as all == operations are done by loading the rvalue into w (2 insts) then doing a SUBWF and test zero. But both if(blah.F?) and if(!blah.F?) both compile to a single BTFSx.

I can't blame MikroE for that, the compiler I am using is well obsolete and the new MikroC PRO has been out for ages and has major improvements in the compiler optimisations. I've just been slow to upgrade.

If you really want to know your compiler constantly check up on its ASM output... ;)
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top